/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/classgen.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Thuban/UI/classgen.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 614 by jonathan, Mon Apr 7 08:56:38 2003 UTC revision 1341 by jonathan, Tue Jul 1 16:10:42 2003 UTC
# Line 5  Line 5 
5  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
6  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
7    
8    import sys
9    
10  from Thuban import _  from Thuban import _
11    
12  from wxPython.wx import *  from wxPython.wx import *
13    
14  from Thuban.Model.classification import Classification, ClassGroupRange, \  from Thuban.Model.classification import ClassGroupProperties
     ClassGroupSingleton, ClassGroupProperties  
15    
16  from Thuban.Model.table import Table, FIELDTYPE_INT, FIELDTYPE_DOUBLE, \  from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \
17       FIELDTYPE_STRING       FIELDTYPE_STRING
18    
19  from Thuban.Model.color import Color  from Thuban.Model.range import Range
20    from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor
21    
22    import classifier, resource
23    
24    from Thuban.Model.classgen import \
25        generate_uniform_distribution, generate_singletons, generate_quantiles, \
26        CustomRamp, GreyRamp, RedRamp, GreenRamp, BlueRamp, GreenToRedRamp, \
27        HotToColdRamp
28    
29    USEALL_BMP  = "group_use_all"
30    USE_BMP     = "group_use"
31    USENOT_BMP  = "group_use_not"
32    USENONE_BMP = "group_use_none"
33    
34    GENCOMBOSTR_UNIFORM = _("Uniform Distribution")
35    GENCOMBOSTR_UNIQUE = _("Unique Values")
36    GENCOMBOSTR_QUANTILES = _("Quantiles from Table")
37    
38    PROPCOMBOSTR_CUSTOM     = _("Custom Ramp")
39    PROPCOMBOSTR_GREY       = _("Grey Ramp")
40    PROPCOMBOSTR_RED        = _("Red Ramp")
41    PROPCOMBOSTR_GREEN      = _("Green Ramp")
42    PROPCOMBOSTR_BLUE       = _("Blue Ramp")
43    PROPCOMBOSTR_GREEN2RED  = _("Green-to-Red Ramp")
44    PROPCOMBOSTR_HOT2COLD   = _("Hot-to-Cold Ramp")
45    
46  ID_CLASSGEN_GEN = 4001  ID_CLASSGEN_GENCOMBO = 4007
47  ID_CLASSGEN_CANCEL = 4002  ID_CLASSGEN_PROPCOMBO = 4008
48    
49  class ClassGenDialog(wxDialog):  class ClassGenDialog(wxDialog):
50                                                                                    
51      def __init__(self, parent, table, fieldName):      def __init__(self, parent, layer, fieldName):
52            """Inialize the class generating dialog.
53    
54            parent -- this must be an instance of the Classifier class
55            """
56    
57          wxDialog.__init__(self, parent, -1, _("Generate Classification"),          wxDialog.__init__(self, parent, -1, _("Generate Classification"),
58                            style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)                            style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
59                                                                                    
60            self.parent = parent
61            self.layer = layer
62          self.clazz = None          self.clazz = None
63    
64          type, name, width, prec = table.field_info_by_name(fieldName)          col = layer.ShapeStore().Table().Column(fieldName)
65            self.type = col.type
66    
67            self.fieldName = fieldName
68            self.fieldType = self.type
69    
70            self.curGenPanel = None
71    
72            self.genpanels = []
73    
74            #############
75            # we need to create genButton first because when we create the
76            # panels they will call AllowGenerate() which uses genButton.
77            #
78            self.genButton = wxButton(self, wxID_OK, _("Generate"))
79            self.cancelButton = wxButton(self, wxID_CANCEL, _("Close"))
80            self.genButton.SetDefault()
81    
82            self.genChoice = wxChoice(self, ID_CLASSGEN_GENCOMBO)
83    
84            self.genpanels.append((GENCOMBOSTR_UNIQUE, GenUniquePanel))
85            if self.type in (FIELDTYPE_INT, FIELDTYPE_DOUBLE):
86                self.genpanels.append((GENCOMBOSTR_UNIFORM, GenUniformPanel))
87                self.genpanels.append((GENCOMBOSTR_QUANTILES, GenQuantilesPanel))
88    
89            for name, clazz in self.genpanels:
90                self.genChoice.Append(name, [clazz, None])
91    
92            self.genChoice.SetSelection(0)
93    
94            for i in range(self.genChoice.GetCount()):
95                clazz, obj = self.genChoice.GetClientData(i)
96    
97                if obj is None:
98                    obj = clazz(self, self.layer, self.fieldName, self.fieldType)
99                    obj.Hide()
100                    self.genChoice.SetClientData(i, [clazz, obj])
101    
102    
103            #############
104    
105          sizer = wxBoxSizer(wxVERTICAL)          sizer = wxBoxSizer(wxVERTICAL)
106    
107          buttonSizer = wxBoxSizer(wxHORIZONTAL)          sizer.Add(wxStaticText(self, -1, _("Field: %s") % fieldName),
108          self.genButton = wxButton(self, ID_CLASSGEN_GEN, _("Generate"))                    0, wxALL, 4)
109          buttonSizer.Add(self.genButton, 0, wxALL, 4)          sizer.Add(wxStaticText(
110          buttonSizer.Add(60, 20, 0, wxALL, 4)              self, -1,
111          buttonSizer.Add(wxButton(self, ID_CLASSGEN_CANCEL, _("Cancel")),              _("Data Type: %s") % classifier.Classifier.type2string[self.type]),
112                          0, wxALL, 4)              0, wxALL, 4)
113    
114          if type in (FIELDTYPE_INT, FIELDTYPE_DOUBLE):          psizer = wxBoxSizer(wxHORIZONTAL)
115              self.panel = GenRangePanel(self, table, fieldName, type)          psizer.Add(wxStaticText(self, -1, _("Generate:")),
116          elif type == FIELDTYPE_STRING:              0, wxALIGN_CENTER_VERTICAL, 0)
117              print "Select a field that is an int/decimal"          psizer.Add(self.genChoice, 1, wxALL | wxGROW, 4)
118              #panel = GenSingletonPanel(self, table, fieldName)  
119          else:          sizer.Add(psizer, 0, wxALL | wxGROW, 4)
120              assert False, "Shouldn't be here."  
121              pass          self.sizer_genPanel = wxBoxSizer(wxVERTICAL)
122            sizer.Add(self.sizer_genPanel, 1, wxGROW | wxALL, 4)
123    
124            psizer = wxBoxSizer(wxHORIZONTAL)
125            psizer.Add(wxStaticText(self, -1, _("Color Scheme:")),
126                0, wxALIGN_CENTER_VERTICAL, 0)
127    
128            # Properties (Ramp) ComboBox
129            self.propCombo = wxChoice(self, ID_CLASSGEN_PROPCOMBO)
130    
131            self.propPanel = None
132            custom_ramp_panel = CustomRampPanel(self, layer.ShapeType())
133    
134            self.propCombo.Append(PROPCOMBOSTR_GREY,  GreyRamp)
135            self.propCombo.Append(PROPCOMBOSTR_RED,   RedRamp)
136            self.propCombo.Append(PROPCOMBOSTR_GREEN, GreenRamp)
137            self.propCombo.Append(PROPCOMBOSTR_BLUE,  BlueRamp)
138            self.propCombo.Append(PROPCOMBOSTR_GREEN2RED, GreenToRedRamp)
139            self.propCombo.Append(PROPCOMBOSTR_HOT2COLD,  HotToColdRamp())
140            self.propCombo.Append(PROPCOMBOSTR_CUSTOM, custom_ramp_panel)
141    
142            self.propCombo.SetSelection(0)
143    
144            psizer.Add(self.propCombo, 1, wxALL | wxGROW, 4)
145            sizer.Add(psizer, 0, wxALL | wxGROW, 4)
146    
147          sizer.Add(self.panel, 1, wxGROW | wxALL, 4)          sizer.Add(custom_ramp_panel, 1, wxGROW | wxALL, 4)
148          sizer.Add(buttonSizer, 0,          sizer.Show(custom_ramp_panel, False)
149                    wxALL | wxALIGN_BOTTOM | wxALIGN_CENTER_HORIZONTAL, 4)  
150            # Finally place the main buttons
151            buttonSizer = wxBoxSizer(wxHORIZONTAL)
152            buttonSizer.Add(self.genButton, 0, wxRIGHT|wxEXPAND, 10)
153            buttonSizer.Add(self.cancelButton, 0, wxRIGHT|wxEXPAND, 10)
154            sizer.Add(buttonSizer, 0, wxALIGN_RIGHT|wxBOTTOM|wxTOP, 10)
155    
156          self.SetSizer(sizer)          self.SetSizer(sizer)
157          self.SetAutoLayout(True)          self.SetAutoLayout(True)
158          sizer.SetSizeHints(self)          sizer.SetSizeHints(self)
159    
160          EVT_BUTTON(self, ID_CLASSGEN_GEN, self._OnGenerate)          self.topBox = sizer
161          EVT_BUTTON(self, ID_CLASSGEN_CANCEL, self._OnCancel)  
162            self.__DoOnGenTypeSelect()
163    
164            EVT_CHOICE(self, ID_CLASSGEN_GENCOMBO, self._OnGenTypeSelect)
165            EVT_CHOICE(self, ID_CLASSGEN_PROPCOMBO, self._OnPropTypeSelect)
166            EVT_BUTTON(self, wxID_OK, self.OnOK)
167            EVT_BUTTON(self, wxID_CANCEL, self.OnCancel)
168    
169            self.__DoOnGenTypeSelect()
170    
171            self.genChoice.SetFocus()
172    
173      def GetClassification(self):      def GetClassification(self):
174          return self.clazz          return self.clazz
175    
176      def AllowGenerate(self, on):      def AllowGenerate(self, on):
177          self.genButton.Enable(on)          pass #self.genButton.Enable(on)
178    
179      def _OnGenerate(self, event):      def OnOK(self, event):
180          min = self.panel.GetMin()          """This is really the generate button, but we want to override
181          max = self.panel.GetMax()          the wxDialog class.
182          numGroups = self.panel.GetNumGroups()          """
         cgp = ClassGroupProperties()  
         cgp2 = ClassGroupProperties()  
                                                                                   
         cgp.SetLineColor(Color(.5, 0, 0))  
         cgp2.SetLineColor(Color(1, 0, 1))  
         cgp2.SetLineWidth(10)  
183    
184          if min is not None \          index = self.genChoice.GetSelection()
185              and max is not None \  
186              and numGroups is not None:          assert index != -1, "button should be disabled!"
187              self.clazz = ClassGenerator().GenerateRanges(min, max,  
188                                                           numGroups, cgp, cgp2)          genSel = self.genChoice.GetString(index)
189          else:            clazz, genPanel = self.genChoice.GetClientData(index)
190              self.clazz = None # for now  
191            propPanel = self.propPanel
192          self.EndModal(wxID_OK)  
193                                                                                            if genSel in (GENCOMBOSTR_UNIFORM,          \
194      def _OnCancel(self, event):                        GENCOMBOSTR_UNIQUE,           \
195          self.EndModal(wxID_CANCEL)                        GENCOMBOSTR_QUANTILES):
196    
197  ID_RANGE_MIN = 4001              numGroups = genPanel.GetNumGroups()
198  ID_RANGE_MAX = 4002  
199  ID_RANGE_NGROUPS = 4003              index = self.propCombo.GetSelection()
200  ID_RANGE_STEP = 4004  
201                propSel = self.propCombo.GetString(index)
202                propPanel = self.propCombo.GetClientData(index)
203    
204                ramp = propPanel.GetRamp()
205    
206                if genSel == GENCOMBOSTR_UNIFORM:
207    
208                    min = genPanel.GetMin()
209                    max = genPanel.GetMax()
210    
211  class GenRangePanel(wxPanel):                  if min is not None \
212                        and max is not None \
213                        and numGroups is not None:
214    
215      def __init__(self, parent, table, fieldName, fieldType):                      self.clazz = generate_uniform_distribution(
216                                    min, max, numGroups, ramp,
217                                    self.type == FIELDTYPE_INT)
218    
219                        self.parent._SetClassification(self.clazz)
220    
221                elif genSel == GENCOMBOSTR_UNIQUE:
222    
223                    list = genPanel.GetValueList()
224    
225                    if len(list) > 0:
226                        self.clazz = generate_singletons(list, ramp)
227                        self.parent._SetClassification(self.clazz)
228    
229                elif genSel == GENCOMBOSTR_QUANTILES:
230    
231                    _range = genPanel.GetRange()
232                    _list = genPanel.GetList()
233                    _list.sort()
234    
235                    delta = 1 / float(numGroups)
236                    percents = [delta * i for i in range(1, numGroups + 1)]
237                    adjusted, self.clazz = \
238                        generate_quantiles(_list, percents, ramp, _range)
239    
240                    if adjusted:
241                        dlg = wxMessageDialog(self,
242                            _("Based on the data from the table and the input\n" +
243                              "values, the exact quantiles could not be generated.\n\n" +
244                              "Accept a close estimate?"),
245                            _("Problem with Quantiles"),
246    
247                            wxYES_NO|wxYES_DEFAULT|wxICON_QUESTION)
248                        if dlg.ShowModal() == wxID_YES:
249                            self.parent._SetClassification(self.clazz)
250                    else:
251                        self.parent._SetClassification(self.clazz)
252    
253        def OnCancel(self, event):
254            self.Close()
255    
256        def _OnGenTypeSelect(self, event):
257            self.__DoOnGenTypeSelect()
258            return
259    
260            combo = event.GetEventObject()
261    
262            selIndex = combo.GetSelection()
263    
264            if self.genPanel is not None:
265                self.topBox.Show(self.genPanel, False)
266    
267            self.genPanel = combo.GetClientData(selIndex)
268            if self.genPanel is not None:
269                self.topBox.Show(self.genPanel, True)
270    
271            self.topBox.SetSizeHints(self)
272            self.topBox.Layout()
273    
274        def _OnPropTypeSelect(self, event):
275            combo = event.GetEventObject()
276    
277            selIndex = combo.GetSelection()
278            sel = combo.GetString(selIndex)
279    
280            if isinstance(self.propPanel, wxPanel):
281                self.topBox.Show(self.propPanel, False)
282    
283            self.propPanel = combo.GetClientData(selIndex)
284    
285            if isinstance(self.propPanel, wxPanel):
286                self.topBox.Show(self.propPanel, True)
287    
288            self.topBox.SetSizeHints(self)
289            self.topBox.Layout()
290    
291        def __DoOnGenTypeSelect(self):
292            choice = self.genChoice
293    
294            sel = choice.GetSelection()
295            if sel == -1: return
296    
297            clazz, obj = choice.GetClientData(sel)
298    
299            if self.curGenPanel is not None:
300                self.curGenPanel.Hide()
301                self.sizer_genPanel.Remove(self.curGenPanel)
302    
303            self.curGenPanel = obj
304            self.curGenPanel.Show()
305    
306            self.sizer_genPanel.Add(self.curGenPanel, 1,
307                wxALL|wxEXPAND|wxADJUST_MINSIZE, 3)
308            self.sizer_genPanel.Layout()
309            self.Layout()
310            self.topBox.SetSizeHints(self)
311    
312    ID_UNIFORM_MIN = 4001
313    ID_UNIFORM_MAX = 4002
314    ID_UNIFORM_NGROUPS = 4003
315    ID_UNIFORM_STEP = 4004
316    ID_UNIFORM_RETRIEVE = 4005
317    
318    class GenUniformPanel(wxPanel):
319    
320        def __init__(self, parent, layer, fieldName, fieldType):
321          wxPanel.__init__(self, parent, -1)          wxPanel.__init__(self, parent, -1)
322    
323          self.parent = parent          self.parent = parent
324            self.layer = layer
325            self.fieldName = fieldName
326          self.fieldType = fieldType          self.fieldType = fieldType
327    
328          topSizer = wxStaticBoxSizer(wxStaticBox(self, -1, "Ranges"),          topSizer = wxStaticBoxSizer(wxStaticBox(self, -1, ""),
329                                      wxVERTICAL)                                      wxVERTICAL)
330    
331            #############
332    
333          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
334    
335          sizer.Add(wxStaticText(self, -1, _("Min:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Min:")), 0, wxALL, 4)
336          self.minCtrl = wxTextCtrl(self, ID_RANGE_MIN, style=wxTE_RIGHT)          self.minCtrl = wxTextCtrl(self, ID_UNIFORM_MIN, style=wxTE_RIGHT)
337          sizer.Add(self.minCtrl, 0, wxALL, 4)          sizer.Add(self.minCtrl, 1, wxALL, 4)
338          EVT_TEXT(self, ID_RANGE_MIN, self._OnRangeChanged)          EVT_TEXT(self, ID_UNIFORM_MIN, self._OnRangeChanged)
         self.goodTextColour = self.minCtrl.GetForegroundColour()  
         self.badTextColour = wxRED  
339    
340          sizer.Add(wxStaticText(self, -1, _("Max:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Max:")), 0, wxALL, 4)
341          self.maxCtrl = wxTextCtrl(self, ID_RANGE_MAX, style=wxTE_RIGHT)          self.maxCtrl = wxTextCtrl(self, ID_UNIFORM_MAX, style=wxTE_RIGHT)
342          sizer.Add(self.maxCtrl, 0, wxALL, 4)          sizer.Add(self.maxCtrl, 1, wxALL, 4)
343          EVT_TEXT(self, ID_RANGE_MAX, self._OnRangeChanged)          EVT_TEXT(self, ID_UNIFORM_MAX, self._OnRangeChanged)
344          topSizer.Add(sizer, 0, wxALL, 4)  
345            sizer.Add(wxButton(self, ID_UNIFORM_RETRIEVE, _("Retrieve From Table")),
346                0, wxALL, 4)
347            EVT_BUTTON(self, ID_UNIFORM_RETRIEVE, self._OnRetrieve)
348    
349            topSizer.Add(sizer, 1, wxGROW, 0)
350    
351            #############
352    
353          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
354    
355          sizer.Add(wxStaticText(self, -1, _("Number of Groups:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Number of Groups:")), 0, wxALL, 4)
356          self.numGroupsCtrl = wxSpinCtrl(self, ID_RANGE_NGROUPS, style=wxTE_RIGHT)          self.numGroupsCtrl = wxSpinCtrl(self, ID_UNIFORM_NGROUPS,
357          EVT_TEXT(self, ID_RANGE_NGROUPS, self._OnNumGroupsChanged)                                          style=wxTE_RIGHT)
358          EVT_SPINCTRL(self, ID_RANGE_NGROUPS, self._OnNumGroupsChanged)          EVT_TEXT(self, ID_UNIFORM_NGROUPS, self._OnNumGroupsChanged)
359          sizer.Add(self.numGroupsCtrl, 0, wxALL, 4)          EVT_SPINCTRL(self, ID_UNIFORM_NGROUPS, self._OnNumGroupsChanged)
360            sizer.Add(self.numGroupsCtrl, 1, wxALL, 4)
361    
362          sizer.Add(wxStaticText(self, -1, _("Stepping:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Stepping:")), 0, wxALL, 4)
363          self.stepCtrl = wxTextCtrl(self, ID_RANGE_STEP, style=wxTE_RIGHT)          self.stepCtrl = wxTextCtrl(self, ID_UNIFORM_STEP, style=wxTE_RIGHT)
364          EVT_TEXT(self, ID_RANGE_STEP, self._OnSteppingChanged)          EVT_TEXT(self, ID_UNIFORM_STEP, self._OnSteppingChanged)
365          sizer.Add(self.stepCtrl , 0, wxALL, 4)          sizer.Add(self.stepCtrl , 1, wxALL, 4)
366          topSizer.Add(sizer, 0, wxALL, 4)  
367            topSizer.Add(sizer, 1, wxGROW, 0)
368    
369            #############
370    
371          self.SetSizer(topSizer)          self.SetSizer(topSizer)
372          self.SetAutoLayout(True)          self.SetAutoLayout(True)
# Line 141  class GenRangePanel(wxPanel): Line 375  class GenRangePanel(wxPanel):
375          self.numGroupsChanging = False          self.numGroupsChanging = False
376          self.steppingChanging = False          self.steppingChanging = False
377    
378          self.numGroupsCtrl.SetRange(1, 100)          self.numGroupsCtrl.SetRange(1, sys.maxint)
379    
380          self.numGroupsCtrl.SetValue(1)          self.numGroupsCtrl.SetValue(1)
381          self.stepCtrl.SetValue("1")          self.stepCtrl.SetValue("1")
# Line 156  class GenRangePanel(wxPanel): Line 390  class GenRangePanel(wxPanel):
390                                              FIELDTYPE_INT,                                              FIELDTYPE_INT,
391                                              None)                                              None)
392    
         ##if self.__ValidateEntry(self.numGroupsCtrl, value, int):  
             #return value  
   
         #return None  
   
393      def GetStepping(self):      def GetStepping(self):
394          step = self.stepCtrl.GetValue()          step = self.stepCtrl.GetValue()
395          return self.__GetValidatedTypeEntry(self.stepCtrl,          return self.__GetValidatedTypeEntry(self.stepCtrl,
# Line 196  class GenRangePanel(wxPanel): Line 425  class GenRangePanel(wxPanel):
425          self.numGroupsCtrl.Enable(on)          self.numGroupsCtrl.Enable(on)
426          self.stepCtrl.Enable(on)          self.stepCtrl.Enable(on)
427    
         if on:  
             self.numGroupsCtrl.SetRange(1, abs(max - min) / 0.001)  
   
428          ngroups = self.GetNumGroups()          ngroups = self.GetNumGroups()
429    
430          if ngroups is not None  \          if ngroups is not None  \
# Line 206  class GenRangePanel(wxPanel): Line 432  class GenRangePanel(wxPanel):
432              and max is not None \              and max is not None \
433              and ngroups != 0:              and ngroups != 0:
434    
435              self.stepCtrl.SetValue(str((max - min) / ngroups))              #self.stepCtrl.SetValue(str((max - min) / ngroups))
436                self.stepCtrl.SetValue(str(self.__CalcStepping(min, max, ngroups)))
437                #self.numGroupsCtrl.SetValue(ngroups)
438    
439              self.parent.AllowGenerate(self.GetStepping() is not None)              self.parent.AllowGenerate(self.GetStepping() is not None)
440          else:          else:
# Line 227  class GenRangePanel(wxPanel): Line 455  class GenRangePanel(wxPanel):
455          min = self.GetMin()          min = self.GetMin()
456          max = self.GetMax()          max = self.GetMax()
457    
         if ngroups >= self.numGroupsCtrl.GetMax():  
             self.numGroupsCtrl.SetRange(1, ngroups + 1)  
   
458          if ngroups is not None  \          if ngroups is not None  \
459              and min is not None \              and min is not None \
460              and max is not None \              and max is not None \
# Line 243  class GenRangePanel(wxPanel): Line 468  class GenRangePanel(wxPanel):
468              # called steppingChanging tries to prevent the recursion.              # called steppingChanging tries to prevent the recursion.
469              #              #
470              self.numGroupsChanging = True              self.numGroupsChanging = True
471              self.stepCtrl.SetValue(str((max - min) / ngroups))  
472                self.stepCtrl.SetValue(str(self.__CalcStepping(min, max, ngroups)))
473    
474              self.parent.AllowGenerate(self.GetStepping() is not None)              self.parent.AllowGenerate(self.GetStepping() is not None)
475          else:          else:
# Line 268  class GenRangePanel(wxPanel): Line 494  class GenRangePanel(wxPanel):
494              # see note in _OnNumGroupsChanged              # see note in _OnNumGroupsChanged
495              #              #
496              self.steppingChanging = True              self.steppingChanging = True
497              n = int((max - min) / step)              self.numGroupsCtrl.SetValue(self.__CalcNumGroups(min, max, step))
             if n == 0:  
                 n = 1  
   
             self.numGroupsCtrl.SetValue(n)  
498    
499              self.parent.AllowGenerate(self.GetNumGroups() is not None)              self.parent.AllowGenerate(self.GetNumGroups() is not None)
500          else:          else:
501              self.parent.AllowGenerate(False)              self.parent.AllowGenerate(False)
502    
503        def _OnRetrieve(self, event):
504            table = self.layer.ShapeStore().Table()
505            if table is not None:
506                ThubanBeginBusyCursor()
507                try:
508                    min, max = table.ValueRange(self.fieldName)
509                    self.minCtrl.SetValue(str(min))
510                    self.maxCtrl.SetValue(str(max))
511                finally:
512                    ThubanEndBusyCursor()
513    
514      def __GetValidatedTypeEntry(self, win, value, type, badValue = None):      def __GetValidatedTypeEntry(self, win, value, type, badValue = None):
515    
516          if type == FIELDTYPE_INT:          if type == FIELDTYPE_INT:
# Line 313  class GenRangePanel(wxPanel): Line 546  class GenRangePanel(wxPanel):
546          else:          else:
547              win.SetForegroundColour(wxRED)              win.SetForegroundColour(wxRED)
548    
549            win.Refresh()
550    
551          return valid          return valid
           
552    
553  class GenSingletonPanel(wxPanel):      def __CalcStepping(self, min, max, ngroups):
554            if self.fieldType == FIELDTYPE_INT:
555                step = int((max - min + 1) / float(ngroups))
556            else:
557                step = (max - min) / float(ngroups)
558    
559            return step
560    
561        def __CalcNumGroups(self, min, max, step):
562            n = int((max - min) / step)
563            if n == 0:
564                n = 1
565    
566            if self.fieldType == FIELDTYPE_INT and step == 1:
567                n += 1
568    
569            return n
570    
571      def __init__(self, parent, table, fieldName, fieldType):  
572    ID_UNIQUE_RETRIEVE = 4001
573    ID_UNIQUE_USEALL = 4002
574    ID_UNIQUE_USE = 4003
575    ID_UNIQUE_DONTUSE = 4004
576    ID_UNIQUE_USENONE = 4005
577    ID_UNIQUE_SORTAVAIL = 4006
578    ID_UNIQUE_SORTUSE = 4007
579    ID_UNIQUE_REVAVAIL = 4008
580    ID_UNIQUE_REVUSE = 4009
581    
582    class GenUniquePanel(wxPanel):
583    
584        def __init__(self, parent, layer, fieldName, fieldType):
585          wxPanel.__init__(self, parent, -1)          wxPanel.__init__(self, parent, -1)
586    
587            self.parent = parent
588            self.layer = layer
589            self.fieldName = fieldName
590            self.fieldType = fieldType
591    
592  class ClassGenerator:          topSizer = wxStaticBoxSizer(wxStaticBox(self, -1, ""),
593                                        wxVERTICAL)
594    
     def GenerateSingletons(self, list, numGroups, prop1, prop2):  
         """Generate a new classification consisting solely of singletons.  
595    
596          The resulting classification will consist of at most 'numGroups'          #bsizer = wxBoxSizer(wxVERTICAL)
597          groups whose group properties ramp between 'prop1' and 'prop2'. There          topSizer.Add(wxButton(self, ID_UNIQUE_RETRIEVE,
598          could be fewer groups if 'list' contains fewer that 'numGroups' items.                              _("Retrieve From Table")),
599                       0, wxALL | wxALIGN_RIGHT, 4)
600    
601          list -- any object that implements the iterator interface          EVT_BUTTON(self, ID_UNIQUE_RETRIEVE, self._OnRetrieve)
602    
603          numGroups -- how many groups to generate. This can not be          #topSizer.Add(bsizer, 0, wxALL, 4)
                      determined while the classification is being  
                      generated because the stepping values must  
                      be precalculated to ramp between prop1 and prop2.  
604    
605          prop1 -- initial group property values          sizer = wxBoxSizer(wxHORIZONTAL)
606    
607          prop2 -- final group property values          self.dataList = []
         """  
608    
609          clazz = Classification()          psizer = wxBoxSizer(wxVERTICAL)
610          if numGroups == 0: return clazz          self.list_avail = wxListCtrl(self, -1,
611                            style=wxLC_REPORT | wxLC_SINGLE_SEL)
612            self.list_avail.InsertColumn(0, "Available")
613            self.list_avail_data = []
614            psizer.Add(self.list_avail, 1, wxGROW, 0)
615    
616            bsizer = wxBoxSizer(wxHORIZONTAL)
617            bsizer.Add(wxButton(self, ID_UNIQUE_SORTAVAIL, _("Sort")))
618            EVT_BUTTON(self, ID_UNIQUE_SORTAVAIL, self._OnSortList)
619    
620          for value, prop in zip(list, PropertyRamp(numGroups, prop1, prop2)):          bsizer.Add(wxButton(self, ID_UNIQUE_REVAVAIL, _("Reverse")))
621              clazz.AppendGroup(ClassGroupSingleton(value, prop))          EVT_BUTTON(self, ID_UNIQUE_REVAVAIL, self._OnReverseList)
622    
623          return clazz          psizer.Add(bsizer, 0, wxGROW, 0)
624            sizer.Add(psizer, 1, wxGROW, 0)
625    
626      def GenerateRanges(self, min, max, numGroups, prop1, prop2):          
627            bsizer = wxBoxSizer(wxVERTICAL)
628    
629          clazz = Classification()          bmp = resource.GetBitmapResource(USEALL_BMP, wxBITMAP_TYPE_XPM)
630          if numGroups == 0: return clazz          bsizer.Add(wxBitmapButton(self, ID_UNIQUE_USEALL, bmp),
631                       0, wxGROW | wxALL, 4)
632            bmp = resource.GetBitmapResource(USE_BMP, wxBITMAP_TYPE_XPM)
633            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_USE, bmp),
634                       0, wxGROW | wxALL, 4)
635            bmp = resource.GetBitmapResource(USENOT_BMP, wxBITMAP_TYPE_XPM)
636            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_DONTUSE, bmp),
637                       0, wxGROW | wxALL, 4)
638            bmp = resource.GetBitmapResource(USENONE_BMP, wxBITMAP_TYPE_XPM)
639            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_USENONE, bmp),
640                       0, wxGROW | wxALL, 4)
641    
642            EVT_BUTTON(self, ID_UNIQUE_USEALL, self._OnUseAll)
643            EVT_BUTTON(self, ID_UNIQUE_USE, self._OnUse)
644            EVT_BUTTON(self, ID_UNIQUE_DONTUSE, self._OnDontUse)
645            EVT_BUTTON(self, ID_UNIQUE_USENONE, self._OnUseNone)
646    
647            sizer.Add(bsizer, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4)
648    
649            psizer = wxBoxSizer(wxVERTICAL)
650            self.list_use = wxListCtrl(self, -1,
651                            style=wxLC_REPORT | wxLC_SINGLE_SEL)
652            self.list_use.InsertColumn(0, "Use")
653            self.list_use_data = []
654            psizer.Add(self.list_use, 1, wxGROW, 0)
655    
656            bsizer = wxBoxSizer(wxHORIZONTAL)
657            bsizer.Add(wxButton(self, ID_UNIQUE_SORTUSE, _("Sort")))
658            EVT_BUTTON(self, ID_UNIQUE_SORTUSE, self._OnSortList)
659    
660            bsizer.Add(wxButton(self, ID_UNIQUE_REVUSE, _("Reverse")))
661            EVT_BUTTON(self, ID_UNIQUE_REVUSE, self._OnReverseList)
662    
663          step = (max - min) / float(numGroups)          psizer.Add(bsizer, 0, wxGROW, 0)
664    
665          cur_min = min          sizer.Add(psizer, 1, wxGROW, 0)
         cur_max = cur_min + step  
666    
         i = 0  
         for prop in PropertyRamp(numGroups, prop1, prop2):  
667    
668              if i == (numGroups - 1):          topSizer.Add(sizer, 1, wxGROW, 0)
                 cur_max = max  
669    
670              # this check guards against rounding issues          self.SetSizer(topSizer)
671              if cur_min != cur_max:          self.SetAutoLayout(True)
672                  clazz.AppendGroup(ClassGroupRange(cur_min, cur_max, prop))          topSizer.SetSizeHints(self)
673    
674              cur_min = cur_max          width, height = self.list_avail.GetSizeTuple()
675              cur_max += step          self.list_avail.SetColumnWidth(0,width)
676              i += 1          width, height = self.list_use.GetSizeTuple()
677            self.list_use.SetColumnWidth(0,width)
678    
679          return clazz          self.parent.AllowGenerate(False)
680    
681  class PropertyRamp:      def GetNumGroups(self):
682            return self.list_use.GetItemCount()
683    
684      def __init__(self, num, prop1, prop2):      def GetValueList(self):
685            list = []
686            for i in range(self.list_use.GetItemCount()):
687                list.append(self.dataList[self.list_use.GetItemData(i)])
688            return list
689    
690          self.count = int(num)      def _OnSortList(self, event):
691          num = float(num)          id = event.GetId()
692    
693            if id == ID_UNIQUE_SORTUSE:
694                list = self.list_use
695            else:
696                list = self.list_avail
697    
698          self.lineColor = prop1.GetLineColor()          list.SortItems(lambda i1, i2: cmp(self.dataList[i1],
699          self.fillColor = prop1.GetFill()                                            self.dataList[i2]))
         self.lineWidth = prop1.GetLineWidth()  
700    
701          lineColor2 = prop2.GetLineColor()      def _OnReverseList(self, event):
702          fillColor2 = prop2.GetFill()          id = event.GetId()
         lineWidth2 = prop2.GetLineWidth()  
703    
704          self.line_redStep   = (lineColor2.red   - self.lineColor.red)   / num          if id == ID_UNIQUE_REVUSE:
705          self.line_greenStep = (lineColor2.green - self.lineColor.green) / num              list = self.list_use
706          self.line_blueStep  = (lineColor2.blue  - self.lineColor.blue)  / num          else:
707                list = self.list_avail
708    
709          self.line_widthStep = (lineWidth2 - self.lineWidth) / num          #
710            # always returning 1 reverses the list
711            #
712            list.SortItems(lambda i1, i2: 1)
713    
714        def _OnRetrieve(self, event):
715            self.list_use.DeleteAllItems()
716            self.list_use_data = []
717            self.list_avail.DeleteAllItems()
718            self.list_avail_data = []
719    
720          self.fill_redStep   = (fillColor2.red   - self.fillColor.red)   / num          ThubanBeginBusyCursor()
721          self.fill_greenStep = (fillColor2.green - self.fillColor.green) / num          try:
722          self.fill_blueStep  = (fillColor2.blue  - self.fillColor.blue)  / num              list = self.layer.ShapeStore().Table().UniqueValues(self.fieldName)
723                index = 0
724                for v in list:
725                    self.dataList.append(v)
726                    i = self.list_avail.InsertStringItem(index, str(v))
727                    self.list_avail.SetItemData(index, i)
728        
729                    self.list_avail_data.append(v)
730                    index += 1
731            finally:
732                ThubanEndBusyCursor()
733    
734      def __iter__(self):      def _OnUseAll(self, event):
735          return self          for i in range(self.list_avail.GetItemCount()):
736                self.__MoveListItem(0, self.list_avail, self.list_use)
737    
738      def next(self):      def _OnUse(self, event):
739          if self.count == 0:          self.__MoveSelectedItems(self.list_avail, self.list_use)
             raise StopIteration  
740    
741          prop = ClassGroupProperties()      def _OnDontUse(self, event):
742          prop.SetFill(self.fillColor)          self.__MoveSelectedItems(self.list_use, self.list_avail)
743          prop.SetLineColor(self.lineColor)  
744          prop.SetLineWidth(int(self.lineWidth))      def _OnUseNone(self, event):
745            
746          self.lineColor.red   += self.line_redStep          for i in range(self.list_use.GetItemCount()):
747          self.lineColor.green += self.line_greenStep              self.__MoveListItem(0, self.list_use, self.list_avail)
748          self.lineColor.blue  += self.line_blueStep  
749        def __MoveSelectedItems(self, list_src, list_dest):
750            while True:
751                index = list_src.GetNextItem(-1,
752                                             wxLIST_NEXT_ALL,
753                                             wxLIST_STATE_SELECTED)
754    
755                if index == -1:
756                    break
757    
758                self.__MoveListItem(index, list_src, list_dest)
759    
760    
761        def __MoveListItem(self, index, list_src, list_dest):
762    
763            item = list_src.GetItem(index)
764    
765            x = list_dest.InsertStringItem(
766                    list_dest.GetItemCount(),
767                    str(self.dataList[item.GetData()]))
768    
769            list_dest.SetItemData(x, item.GetData())
770    
771            list_src.DeleteItem(index)
772    
773    #   def _OnListSize(self, event):
774    #       list = event.GetEventObject()
775    
776    #       list.SetColumnWidth(0, event.GetSize().GetWidth())
777    #      
778    
779    ID_QUANTILES_RANGE = 4001
780    ID_QUANTILES_RETRIEVE = 4002
781    
782    class GenQuantilesPanel(wxPanel):
783    
784        def __init__(self, parent, layer, fieldName, fieldType):
785            wxPanel.__init__(self, parent, -1)
786    
787            self.parent = parent
788            self.layer = layer
789            self.fieldName = fieldName
790            self.fieldType = fieldType
791    
792            topBox = wxStaticBoxSizer(wxStaticBox(self, -1, ""),
793                                        wxVERTICAL)
794    
795            self.text_range = wxTextCtrl(self, ID_QUANTILES_RANGE, "")
796            self.button_retrieve = wxButton(self, ID_QUANTILES_RETRIEVE,
797                                            _("Retrieve from Table"))
798    
799            self.spin_numClasses = wxSpinCtrl(self, -1, style=wxTE_RIGHT)
800            self.spin_numClasses.SetRange(1, sys.maxint)
801            self.spin_numClasses.SetValue(1)
802    
803    
804            sizer = wxBoxSizer(wxHORIZONTAL)
805            sizer.Add(wxStaticText(self, -1, _("Apply to Range")), 0, wxALL, 4)
806            sizer.Add(self.text_range, 1, wxALL, 4)
807            sizer.Add(self.button_retrieve, 0, wxALL, 4)
808    
809            topBox.Add(sizer, 0, wxEXPAND, 0)
810    
811            sizer = wxBoxSizer(wxHORIZONTAL)
812            sizer.Add(wxStaticText(self, -1, _("Number of Classes:")), 0, wxALL, 4)
813            sizer.Add(self.spin_numClasses, 1, wxALL, 4)
814    
815            topBox.Add(sizer, 0, wxEXPAND, 0)
816    
817            self.SetSizer(topBox)
818            self.SetAutoLayout(True)
819            topBox.Fit(self)
820            topBox.SetSizeHints(self)
821    
822            EVT_TEXT(self, ID_QUANTILES_RANGE, self.OnRangeText)
823            EVT_BUTTON(self, ID_QUANTILES_RETRIEVE, self.OnRetrieve)
824    
825            self.__range = None
826    
827        def GetNumGroups(self):
828            return self.spin_numClasses.GetValue()
829    
830        def GetRange(self):
831            assert self.__range is not None
832    
833            return self.__range
834    
835        def GetList(self):
836            _list = []
837            table = self.layer.ShapeStore().Table()
838            if table is not None:
839                ThubanBeginBusyCursor()
840                try:
841                    #
842                    # FIXME: Replace with a call to table when the method
843                    # has been written to get all the values
844                    #
845                    for i in range(table.NumRows()):
846                        _list.append(table.ReadValue(i, self.fieldName))
847                finally:
848                    ThubanEndBusyCursor()
849    
850            return _list
851    
852        def OnRangeText(self, event):
853    
854            try:
855                self.__range = Range(self.text_range.GetValue())
856            except ValueError:
857                self.__range = None
858    
859            if self.__range is not None:
860                self.text_range.SetForegroundColour(wxBLACK)
861            else:
862                self.text_range.SetForegroundColour(wxRED)
863    
864        def OnRetrieve(self, event):
865            table = self.layer.ShapeStore().Table()
866            if table is not None:
867                ThubanBeginBusyCursor()
868                try:
869                    min, max = table.ValueRange(self.fieldName)
870                    self.text_range.SetValue("[" + str(min) + ";" + str(max) + "]")
871                finally:
872                    ThubanEndBusyCursor()
873    
874    ID_CUSTOMRAMP_COPYSTART = 4001
875    ID_CUSTOMRAMP_COPYEND = 4002
876    ID_CUSTOMRAMP_EDITSTART = 4003
877    ID_CUSTOMRAMP_EDITEND = 4004
878    ID_CUSTOMRAMP_SPROP = 4005
879    ID_CUSTOMRAMP_EPROP = 4006
880    
881    class CustomRampPanel(wxPanel):
882    
883        def __init__(self, parent, shapeType):
884            wxPanel.__init__(self, parent, -1)
885    
886            topSizer = wxStaticBoxSizer(wxStaticBox(self, -1, ""), wxHORIZONTAL)
887    
888            bsizer = wxBoxSizer(wxVERTICAL)
889            bsizer.Add(wxStaticText(self, -1, _("Start:")), 0, wxALL | wxCENTER, 4)
890            self.startPropCtrl = classifier.ClassGroupPropertiesCtrl(
891                self, ID_CUSTOMRAMP_SPROP,
892                ClassGroupProperties(), shapeType,
893                style=wxSIMPLE_BORDER, size=(40, 20))
894            bsizer.Add(self.startPropCtrl, 1, wxGROW | wxALL | wxCENTER, 4)
895            bsizer.Add(wxButton(self, ID_CUSTOMRAMP_EDITSTART, _("Change")),
896                       0, wxGROW | wxALL | wxCENTER, 4)
897    
898            topSizer.Add(bsizer,
899                       1, wxALL \
900                          | wxSHAPED \
901                          | wxALIGN_CENTER_HORIZONTAL \
902                          | wxALIGN_CENTER_VERTICAL, \
903                       4)
904    
905            bmp = resource.GetBitmapResource(USE_BMP, wxBITMAP_TYPE_XPM)
906            bsizer = wxBoxSizer(wxVERTICAL)
907            bsizer.Add(wxBitmapButton(self, ID_CUSTOMRAMP_COPYSTART, bmp),
908                       0, wxGROW | wxALL, 4)
909            bmp = resource.GetBitmapResource(USENOT_BMP, wxBITMAP_TYPE_XPM)
910            bsizer.Add(wxBitmapButton(self, ID_CUSTOMRAMP_COPYEND, bmp),
911                       0, wxGROW | wxALL, 4)
912    
913            topSizer.Add(bsizer,
914                       0, wxALL \
915                          | wxALIGN_CENTER_HORIZONTAL \
916                          | wxALIGN_CENTER_VERTICAL,
917                       4)
918    
919            bsizer = wxBoxSizer(wxVERTICAL)
920            bsizer.Add(wxStaticText(self, -1, _("End:")), 0, wxALL | wxCENTER, 4)
921            self.endPropCtrl = classifier.ClassGroupPropertiesCtrl(
922                self, ID_CUSTOMRAMP_EPROP,
923                ClassGroupProperties(), shapeType,
924                style=wxSIMPLE_BORDER, size=(40, 20))
925            bsizer.Add(self.endPropCtrl, 1, wxGROW | wxALL | wxCENTER, 4)
926            bsizer.Add(wxButton(self, ID_CUSTOMRAMP_EDITEND, _("Change")),
927                       0, wxGROW | wxALL | wxCENTER, 4)
928    
929            topSizer.Add(bsizer,
930                       1, wxALL \
931                          | wxSHAPED \
932                          | wxALIGN_RIGHT \
933                          | wxALIGN_CENTER_HORIZONTAL \
934                          | wxALIGN_CENTER_VERTICAL,
935                       4)
936    
937            EVT_BUTTON(self, ID_CUSTOMRAMP_COPYSTART, self._OnCopyStart)
938            EVT_BUTTON(self, ID_CUSTOMRAMP_COPYEND, self._OnCopyEnd)
939            EVT_BUTTON(self, ID_CUSTOMRAMP_EDITSTART, self._OnEditStart)
940            EVT_BUTTON(self, ID_CUSTOMRAMP_EDITEND, self._OnEditEnd)
941    
942            self.SetSizer(topSizer)
943            self.SetAutoLayout(True)
944            topSizer.SetSizeHints(self)
945    
946        def GetRamp(self):
947            return CustomRamp(self.startPropCtrl.GetProperties(),
948                              self.endPropCtrl.GetProperties())
949    
950          self.fillColor.red   += self.fill_redStep      def _OnCopyStart(self, event):
951          self.fillColor.green += self.fill_greenStep          self.endPropCtrl.SetProperties(self.startPropCtrl.GetProperties())
         self.fillColor.blue  += self.fill_blueStep  
952    
953          self.lineWidth       += self.line_widthStep      def _OnCopyEnd(self, event):
954            self.startPropCtrl.SetProperties(self.endPropCtrl.GetProperties())
955    
956          self.count -= 1      def _OnEditStart(self, event):
957            self.startPropCtrl.DoEdit()
958    
959          return prop      def _OnEditEnd(self, event):
960            self.endPropCtrl.DoEdit()
961    
962      

Legend:
Removed from v.614  
changed lines
  Added in v.1341

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26