ColorDialog patch (winning HSV)

Jan-Oliver Wagner jan at intevation.de
Sat May 1 00:03:22 CEST 2004


Hi Thuban developers,

I've prepared a patch to make it possible to
make use of different color selection dialogs.
Especially, the pyColourChooser which offers HSV
in contrast to the default wxColourDialog of wxWindows.
Also, the custom colors feature works.

attached is a patch to Thuban/UI/classifier.py
in order to use a new abstract ColorDialog and
attached is the new file Thuban/UI/colordialog.py.

Let me know whether you think this abstract ColorDialog
class is a good or bad idea.

	Jan

-- 
Jan-Oliver Wagner               http://intevation.de/~jan/

Intevation GmbH	              	     http://intevation.de/
FreeGIS	                               http://freegis.org/
-------------- next part --------------
Index: classifier.py
===================================================================
RCS file: /thubanrepository/thuban/Thuban/UI/classifier.py,v
retrieving revision 1.64
diff -u -3 -p -r1.64 classifier.py
--- classifier.py	1 Aug 2003 14:27:57 -0000	1.64
+++ classifier.py	30 Apr 2004 22:00:49 -0000
@@ -33,6 +33,7 @@ from Thuban.Model.layer import Layer, Ra
 from Thuban.Model.data import SHAPETYPE_ARC, SHAPETYPE_POLYGON, SHAPETYPE_POINT
 
 from Thuban.UI.classgen import ClassGenDialog
+from Thuban.UI.colordialog import ColorDialog
 
 from dialogs import NonModalNonParentDialog
 from messages import MAP_REPLACED
@@ -1285,13 +1286,12 @@ class SelectPropertiesDialog(wxDialog):
         self.previewWin.Refresh()
 
     def __GetColor(self, cur):
-        dialog = wxColourDialog(self)
-        if cur is not Transparent:
-            dialog.GetColourData().SetColour(Color2wxColour(cur))
+        dialog = ColorDialog(self)
+        dialog.SetColor(cur)
 
         ret = None
         if dialog.ShowModal() == wxID_OK:
-            ret = wxColour2Color(dialog.GetColourData().GetColour())
+            ret = dialog.GetColor()
 
         dialog.Destroy()
 
-------------- next part --------------
# Copyright (c) 2004 by Intevation GmbH
# Authors:
# Jan-Oliver Wagnber <jan at intevation.de>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with Thuban for details.

__version__ = "$Revision$"

from wxPython.wx import wxDialog, wxColourDialog, wxID_OK, wxID_CANCEL, \
                        wxBoxSizer, wxVERTICAL, wxHORIZONTAL, wxALL, \
                        wxALIGN_CENTER_HORIZONTAL, wxButton, EVT_BUTTON, \
                        wxInitAllImageHandlers

from Thuban import _

from Thuban.UI.common import Color2wxColour, wxColour2Color

from Thuban.Model.color import Transparent


# determine whether the pyColourChooserDialog is available
# It was not available with the very first versions of wxWindows 2.4
# (I don't know the exact version though)
try:
    from wxPython.lib.colourchooser import wxPyColourChooser
    _wxPyColourChooser = True
    wxInitAllImageHandlers() # should be somewhere at Thuban startup?
except:
    _wxPyColourChooser = False


class PyColorChooserDialog(wxDialog):
    """
    A Dialog that uses the wxPyColourChooser Frame and simply
    adds OK and Cancel button to form a modal color selection dialog.
    """

    def __init__(self, parent):
        wxDialog.__init__(self, parent, -1, _("Select Color"))

        self.parent = parent
        self.dialog_layout()

    def dialog_layout(self):
        top_box = wxBoxSizer(wxVERTICAL)

        self.chooser = wxPyColourChooser(self, -1)

        top_box.Add(self.chooser, 1, wxALL | wxALIGN_CENTER_HORIZONTAL, 5)

        box = wxBoxSizer(wxHORIZONTAL)
        box.Add(wxButton(self, wxID_OK, _("OK")), 0, wxALL, 4) 
        box.Add(wxButton(self, wxID_CANCEL, _("Cancel")), 0, wxALL, 4) 
        top_box.Add(box, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 10)

        EVT_BUTTON(self, wxID_OK, self.OnOK)
        EVT_BUTTON(self, wxID_CANCEL, self.OnCancel)

        self.SetAutoLayout(True)
        self.SetSizer(top_box)
        top_box.Fit(self)
        top_box.SetSizeHints(self)

    def GetValue(self):
        return self.chooser.GetValue()

    def SetValue(self, color):
        return self.chooser.SetValue(color)

    def OnOK(self, event):
        self.EndModal(wxID_OK)

    def OnCancel(self, event):
        self.EndModal(wxID_CANCEL)


class ColorDialog:
    """
    The color dialog raises one of the available color selection
    dialogs. ColorDialog is no derived from any GUI class though.

    Furthermore, wxColour class is mapped to the Thuban
    Color class already through this class.

    Eventually it should be configurable globally for Thuban
    which color dialog to use since they might differ in
    functionality, translation or stability.
    """

    def __init__(self, parent):
        if _wxPyColourChooser:
            self.dlg = PyColorChooserDialog(parent)
        else:
            self.dlg = wxColourDialog(parent)

    def GetColor(self):
        if _wxPyColourChooser:
            return wxColour2Color(self.dlg.GetValue())
        else:
            return wxColour2Color(self.dlg.GetColourData().GetColour())

    def SetColor(self, color):
        if color is not Transparent:
            if _wxPyColourChooser:
                self.dlg.SetValue(Color2wxColour(color))
            else:
                self.dlg.GetColourData().SetColour(Color2wxColour(color))
        
    def ShowModal(self):
        return self.dlg.ShowModal()

    def Destroy(self):
        return self.dlg.Destroy()


if __name__ == "__main__":
    # Test routine to run the dialog without Thuban

    from wxPython.wx import wxApp, NULL

    wxInitAllImageHandlers()

    class _TestApp(wxApp):
        def OnInit(self):
            dialog = ColorDialog(NULL)

            if dialog.ShowModal() == wxID_OK:
                print "Selected color:", dialog.GetColor()
            else:
                print "No color selected"

            dialog.Destroy()
            return True

    app = _TestApp()
    app.MainLoop()


More information about the Thuban-devel mailing list

This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)