/[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 629 by jonathan, Wed Apr 9 10:09:44 2003 UTC revision 1100 by jonathan, Fri May 30 06:28:21 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
   
 import classifier  
20    
21  from Thuban.common import Str2Num  import classifier, resource
22    
23  ID_CLASSGEN_GEN = 4001  from Thuban.Model.classgen import \
24  ID_CLASSGEN_CLOSE = 4002      GenUniformDistribution, GenSingletonsFromList, GenQuantiles, \
25  ID_CLASSGEN_COMBO = 4007      CustomRamp, GreyRamp, RedRamp, GreenRamp, BlueRamp, GreenToRedRamp, \
26        HotToColdRamp
27    
28    USEALL_BMP  = "group_use_all"
29    USE_BMP     = "group_use"
30    USENOT_BMP  = "group_use_not"
31    USENONE_BMP = "group_use_none"
32    
33    GENCOMBOSTR_UNIFORM = _("Uniform Distribution")
34    GENCOMBOSTR_UNIQUE = _("Unique Values")
35    GENCOMBOSTR_QUANTILES = _("Quantiles from Table")
36    
37    PROPCOMBOSTR_CUSTOM     = _("Custom Ramp")
38    PROPCOMBOSTR_GREY       = _("Grey Ramp")
39    PROPCOMBOSTR_RED        = _("Red Ramp")
40    PROPCOMBOSTR_GREEN      = _("Green Ramp")
41    PROPCOMBOSTR_BLUE       = _("Blue Ramp")
42    PROPCOMBOSTR_GREEN2RED  = _("Green-to-Red Ramp")
43    PROPCOMBOSTR_HOT2COLD   = _("Hot-to-Cold Ramp")
44    
45  COMBOSTR_UNIFORM = _("Uniform Distribution")  ID_CLASSGEN_GENCOMBO = 4007
46  COMBOSTR_UNIQUE = _("Unique Values")  ID_CLASSGEN_PROPCOMBO = 4008
47    
48  class ClassGenDialog(wxDialog):  class ClassGenDialog(wxDialog):
49                                                                                    
50      def __init__(self, parent, layer, fieldName):      def __init__(self, parent, layer, fieldName):
51          """Inialize the class generating dialog.          """Inialize the class generating dialog.
52    
# Line 38  class ClassGenDialog(wxDialog): Line 55  class ClassGenDialog(wxDialog):
55    
56          wxDialog.__init__(self, parent, -1, _("Generate Classification"),          wxDialog.__init__(self, parent, -1, _("Generate Classification"),
57                            style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)                            style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
58                                                                                    
59          self.parent = parent          self.parent = parent
60            self.layer = layer
61          self.clazz = None          self.clazz = None
62    
63          self.type, name, width, prec = layer.table.field_info_by_name(fieldName)          col = layer.table.Column(fieldName)
64            self.type = col.type
65    
66            self.fieldName = fieldName
67            self.fieldType = self.type
68    
69            self.curGenPanel = None
70    
71            self.genpanels = []
72    
73          #############          #############
74          # we need to create genButton first because when we create the          # we need to create genButton first because when we create the
75          # panels they will call AllowGenerate() which uses genButton.          # panels they will call AllowGenerate() which uses genButton.
76          #          #
77          buttonSizer = wxBoxSizer(wxHORIZONTAL)          self.genButton = wxButton(self, wxID_OK, _("Generate"))
78          self.genButton = wxButton(self, ID_CLASSGEN_GEN, _("Generate"))          self.genButton.SetDefault()
79            self.cancelButton = wxButton(self, wxID_CANCEL, _("Close"))
80    
81            self.genChoice = wxChoice(self, ID_CLASSGEN_GENCOMBO)
82    
83            self.genpanels.append((GENCOMBOSTR_UNIQUE, GenUniquePanel))
84            if self.type in (FIELDTYPE_INT, FIELDTYPE_DOUBLE):
85                self.genpanels.append((GENCOMBOSTR_UNIFORM, GenUniformPanel))
86                self.genpanels.append((GENCOMBOSTR_QUANTILES, GenQuantilesPanel))
87    
88            for name, clazz in self.genpanels:
89                self.genChoice.Append(name, [clazz, None])
90    
91            self.genChoice.SetSelection(0)
92    
93            for i in range(self.genChoice.GetCount()):
94                clazz, obj = self.genChoice.GetClientData(i)
95    
96                if obj is None:
97                    obj = clazz(self, self.layer, self.fieldName, self.fieldType)
98                    obj.Hide()
99                    self.genChoice.SetClientData(i, [clazz, obj])
100    
         buttonSizer.Add(self.genButton, 0, wxALL, 4)  
         buttonSizer.Add(60, 20, 0, wxALL, 4)  
         buttonSizer.Add(wxButton(self, ID_CLASSGEN_CLOSE, _("Close")),  
                         0, wxALL, 4)  
101    
102          #############          #############
103    
# Line 64  class ClassGenDialog(wxDialog): Line 107  class ClassGenDialog(wxDialog):
107                    0, wxALL, 4)                    0, wxALL, 4)
108          sizer.Add(wxStaticText(          sizer.Add(wxStaticText(
109              self, -1,              self, -1,
110              _("Field Type: %s") % classifier.Classifier.type2string[self.type]),              _("Data Type: %s") % classifier.Classifier.type2string[self.type]),
111              0, wxALL, 4)              0, wxALL, 4)
112    
113          psizer = wxBoxSizer(wxHORIZONTAL)          psizer = wxBoxSizer(wxHORIZONTAL)
114          psizer.Add(wxStaticText(          psizer.Add(wxStaticText(self, -1, _("Generate:")),
             self, -1,  
             _("Generate:")),  
115              0, wxALIGN_CENTER_VERTICAL, 0)              0, wxALIGN_CENTER_VERTICAL, 0)
116            psizer.Add(self.genChoice, 1, wxALL | wxGROW, 4)
         self.genCombo = wxComboBox(self,  
                                 ID_CLASSGEN_COMBO,  
                                 "", style = wxCB_READONLY)  
         psizer.Add(self.genCombo, 1, wxALL | wxGROW, 4)  
         EVT_COMBOBOX(self, ID_CLASSGEN_COMBO, self._OnGenTypeSelect)  
117    
118          sizer.Add(psizer, 0, wxALL | wxGROW, 4)          sizer.Add(psizer, 0, wxALL | wxGROW, 4)
119    
120          #############          self.sizer_genPanel = wxBoxSizer(wxVERTICAL)
121            sizer.Add(self.sizer_genPanel, 1, wxGROW | wxALL, 4)
         self.genPanel = None  
122    
123          panel = GenUniquePanel(self, layer, fieldName, self.type)          psizer = wxBoxSizer(wxHORIZONTAL)
124          self.genCombo.Append(COMBOSTR_UNIQUE)          psizer.Add(wxStaticText(self, -1, _("Color Scheme:")),
125          self.genCombo.SetClientData(self.genCombo.GetCount() - 1, panel)              0, wxALIGN_CENTER_VERTICAL, 0)
         sizer.Add(panel, 1, wxGROW | wxALL, 4)  
   
         self.genPanel = panel  
   
         if self.type in (FIELDTYPE_INT, FIELDTYPE_DOUBLE):  
             panel = GenUniformPanel(self, layer, fieldName, self.type)  
             self.genCombo.Append(COMBOSTR_UNIFORM)  
             self.genCombo.SetClientData(self.genCombo.GetCount() - 1, panel)  
             sizer.Add(panel, 0, wxGROW | wxALL, 4)  
             sizer.Show(panel, False)  
126    
127          #############          # Properties (Ramp) ComboBox
128            self.propCombo = wxChoice(self, ID_CLASSGEN_PROPCOMBO)
129    
130          self.propPanel = None          self.propPanel = None
131            custom_ramp_panel = CustomRampPanel(self, layer.ShapeType())
132    
133          panel = CustomRampPanel(self, layer.ShapeType())          self.propCombo.Append(PROPCOMBOSTR_GREY,  GreyRamp())
134            self.propCombo.Append(PROPCOMBOSTR_RED,   RedRamp())
135            self.propCombo.Append(PROPCOMBOSTR_GREEN, GreenRamp())
136            self.propCombo.Append(PROPCOMBOSTR_BLUE,  BlueRamp())
137            self.propCombo.Append(PROPCOMBOSTR_GREEN2RED, GreenToRedRamp())
138            self.propCombo.Append(PROPCOMBOSTR_HOT2COLD,  HotToColdRamp())
139            self.propCombo.Append(PROPCOMBOSTR_CUSTOM, custom_ramp_panel)
140    
141          self.propPanel = panel          self.propCombo.SetSelection(0)
142    
143          sizer.Add(panel, 1, wxALL | wxGROW, 4)          psizer.Add(self.propCombo, 1, wxALL | wxGROW, 4)
144            sizer.Add(psizer, 0, wxALL | wxGROW, 4)
         #############  
145    
146          sizer.Add(buttonSizer, 0,          sizer.Add(custom_ramp_panel, 1, wxGROW | wxALL, 4)
147                    wxALL | wxALIGN_BOTTOM | wxALIGN_CENTER_HORIZONTAL, 4)          sizer.Show(custom_ramp_panel, False)
148    
149            # Finally place the main buttons
150            buttonSizer = wxBoxSizer(wxHORIZONTAL)
151            buttonSizer.Add(self.genButton, 0, wxRIGHT|wxEXPAND, 10)
152            buttonSizer.Add(self.cancelButton, 0, wxRIGHT|wxEXPAND, 10)
153            sizer.Add(buttonSizer, 0, wxALIGN_RIGHT|wxBOTTOM|wxTOP, 10)
154    
155          self.SetSizer(sizer)          self.SetSizer(sizer)
156          self.SetAutoLayout(True)          self.SetAutoLayout(True)
157          sizer.SetSizeHints(self)          sizer.SetSizeHints(self)
158    
159          self.sizer = sizer          self.topBox = sizer
160    
161            self.__DoOnGenTypeSelect()
162    
163            EVT_CHOICE(self, ID_CLASSGEN_GENCOMBO, self._OnGenTypeSelect)
164            EVT_CHOICE(self, ID_CLASSGEN_PROPCOMBO, self._OnPropTypeSelect)
165            EVT_BUTTON(self, wxID_OK, self.OnOK)
166            EVT_BUTTON(self, wxID_CANCEL, self.OnCancel)
167    
168          EVT_BUTTON(self, ID_CLASSGEN_GEN, self._OnGenerate)          self.__DoOnGenTypeSelect()
169          EVT_BUTTON(self, ID_CLASSGEN_CLOSE, self._OnCloseBtn)  
170            self.genChoice.SetFocus()
171    
172      def GetClassification(self):      def GetClassification(self):
173          return self.clazz          return self.clazz
# Line 130  class ClassGenDialog(wxDialog): Line 175  class ClassGenDialog(wxDialog):
175      def AllowGenerate(self, on):      def AllowGenerate(self, on):
176          pass #self.genButton.Enable(on)          pass #self.genButton.Enable(on)
177    
178      def _OnGenerate(self, event):      def OnOK(self, event):
179            """This is really the generate button, but we want to override
180            the wxDialog class.
181            """
182    
183            index = self.genChoice.GetSelection()
184    
185          selIndex = self.genCombo.GetSelection()          assert index != -1, "button should be disabled!"
186    
187            genSel = self.genChoice.GetString(index)
188            clazz, genPanel = self.genChoice.GetClientData(index)
189    
         sel = self.genCombo.GetString(selIndex)  
         genPanel = self.genCombo.GetClientData(selIndex)  
190          propPanel = self.propPanel          propPanel = self.propPanel
191    
192          if sel == COMBOSTR_UNIFORM:          if genSel in (GENCOMBOSTR_UNIFORM,          \
193                          GENCOMBOSTR_UNIQUE,           \
194                          GENCOMBOSTR_QUANTILES):
195    
             min = genPanel.GetMin()  
             max = genPanel.GetMax()  
196              numGroups = genPanel.GetNumGroups()              numGroups = genPanel.GetNumGroups()
             sp = propPanel.GetStartProperties()  
             ep = propPanel.GetEndProperties()  
197    
198              if min is not None \              index = self.propCombo.GetSelection()
                 and max is not None \  
                 and numGroups is not None:  
199    
200                  self.clazz = ClassGenerator().GenUnifromDistribution(              propSel = self.propCombo.GetString(index)
201                               min, max, numGroups, sp, ep,              propPanel = self.propCombo.GetClientData(index)
                              self.type == FIELDTYPE_INT)  
202    
203                  self.parent._SetClassification(self.clazz)              ramp = propPanel.GetRamp()
204    
205          elif sel == COMBOSTR_UNIQUE:              if genSel == GENCOMBOSTR_UNIFORM:
206    
207              list = genPanel.GetValueList()                  min = genPanel.GetMin()
208              numGroups = genPanel.GetNumGroups()                  max = genPanel.GetMax()
             sp = propPanel.GetStartProperties()  
             ep = propPanel.GetEndProperties()  
209    
210              if len(list) > 0 \                  if min is not None \
211                  and numGroups is not None:                      and max is not None \
212                        and numGroups is not None:
213    
214                  self.clazz = ClassGenerator().GenSingletonsFromList(                      self.clazz = GenUniformDistribution(
215                                  list, numGroups, sp, ep)                                  min, max, numGroups, ramp,
216                                    self.type == FIELDTYPE_INT)
217    
218                  self.parent._SetClassification(self.clazz)                      self.parent._SetClassification(self.clazz)
219    
220          else:              elif genSel == GENCOMBOSTR_UNIQUE:
221              pass  
222                    list = genPanel.GetValueList()
223    
224                    if len(list) > 0 \
225                        and numGroups is not None:
226    
227                        self.clazz = GenSingletonsFromList(
228                                        list, numGroups, ramp)
229    
230                        self.parent._SetClassification(self.clazz)
231    
232                elif genSel == GENCOMBOSTR_QUANTILES:
233    
234                    _range = genPanel.GetRange()
235                    _list = genPanel.GetList()
236                    _list.sort()
237    
238                    delta = 1 / float(numGroups)
239                    percents = [delta * i for i in range(1, numGroups + 1)]
240                    adjusted, self.clazz = \
241                        GenQuantiles(_list, percents, ramp, _range)
242    
243                    if adjusted:
244                        dlg = wxMessageDialog(self,
245                            _("Based on the data from the table and the input\n" +
246                              "values, the exact quantiles could not be generated.\n\n" +
247                              "Accept a close estimate?"),
248                            _("Problem with Quantiles"),
249    
250      def _OnCloseBtn(self, event):                          wxYES_NO|wxYES_DEFAULT|wxICON_QUESTION)
251                        if dlg.ShowModal() == wxID_YES:
252                            self.parent._SetClassification(self.clazz)
253                    else:
254                        self.parent._SetClassification(self.clazz)
255    
256        def OnCancel(self, event):
257          self.Close()          self.Close()
258    
259      def _OnGenTypeSelect(self, event):      def _OnGenTypeSelect(self, event):
260            self.__DoOnGenTypeSelect()
261            return
262    
263          combo = event.GetEventObject()          combo = event.GetEventObject()
264    
265          selIndex = combo.GetSelection()          selIndex = combo.GetSelection()
266    
267          if self.genPanel is not None:          if self.genPanel is not None:
268              self.sizer.Show(self.genPanel, False)              self.topBox.Show(self.genPanel, False)
269    
270          self.genPanel = combo.GetClientData(selIndex)          self.genPanel = combo.GetClientData(selIndex)
271          if self.genPanel is not None:          if self.genPanel is not None:
272              self.sizer.Show(self.genPanel, True)              self.topBox.Show(self.genPanel, True)
273    
274            self.topBox.SetSizeHints(self)
275            self.topBox.Layout()
276    
277        def _OnPropTypeSelect(self, event):
278            combo = event.GetEventObject()
279    
280            selIndex = combo.GetSelection()
281            sel = combo.GetString(selIndex)
282    
283            if isinstance(self.propPanel, wxPanel):
284                self.topBox.Show(self.propPanel, False)
285    
286            self.propPanel = combo.GetClientData(selIndex)
287    
288            if isinstance(self.propPanel, wxPanel):
289                self.topBox.Show(self.propPanel, True)
290    
291            self.topBox.SetSizeHints(self)
292            self.topBox.Layout()
293    
294        def __DoOnGenTypeSelect(self):
295            choice = self.genChoice
296    
297            sel = choice.GetSelection()
298            if sel == -1: return
299    
300          self.sizer.SetSizeHints(self)          clazz, obj = choice.GetClientData(sel)
         self.sizer.Layout()  
301    
302            if self.curGenPanel is not None:
303                self.curGenPanel.Hide()
304                self.sizer_genPanel.Remove(self.curGenPanel)
305    
306            self.curGenPanel = obj
307            self.curGenPanel.Show()
308    
309            self.sizer_genPanel.Add(self.curGenPanel, 1,
310                wxALL|wxEXPAND|wxADJUST_MINSIZE, 3)
311            self.sizer_genPanel.Layout()
312            self.Layout()
313            self.topBox.SetSizeHints(self)
314    
315  ID_UNIFORM_MIN = 4001  ID_UNIFORM_MIN = 4001
316  ID_UNIFORM_MAX = 4002  ID_UNIFORM_MAX = 4002
# Line 238  class GenUniformPanel(wxPanel): Line 356  class GenUniformPanel(wxPanel):
356          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
357    
358          sizer.Add(wxStaticText(self, -1, _("Number of Groups:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Number of Groups:")), 0, wxALL, 4)
359          self.numGroupsCtrl = wxSpinCtrl(self, ID_UNIFORM_NGROUPS, style=wxTE_RIGHT)          self.numGroupsCtrl = wxSpinCtrl(self, ID_UNIFORM_NGROUPS,
360                                            style=wxTE_RIGHT)
361          EVT_TEXT(self, ID_UNIFORM_NGROUPS, self._OnNumGroupsChanged)          EVT_TEXT(self, ID_UNIFORM_NGROUPS, self._OnNumGroupsChanged)
362          EVT_SPINCTRL(self, ID_UNIFORM_NGROUPS, self._OnNumGroupsChanged)          EVT_SPINCTRL(self, ID_UNIFORM_NGROUPS, self._OnNumGroupsChanged)
363          sizer.Add(self.numGroupsCtrl, 1, wxALL, 4)          sizer.Add(self.numGroupsCtrl, 1, wxALL, 4)
# Line 259  class GenUniformPanel(wxPanel): Line 378  class GenUniformPanel(wxPanel):
378          self.numGroupsChanging = False          self.numGroupsChanging = False
379          self.steppingChanging = False          self.steppingChanging = False
380    
381          self.numGroupsCtrl.SetRange(1, 100)          self.numGroupsCtrl.SetRange(1, sys.maxint)
382    
383          self.numGroupsCtrl.SetValue(1)          self.numGroupsCtrl.SetValue(1)
384          self.stepCtrl.SetValue("1")          self.stepCtrl.SetValue("1")
# Line 309  class GenUniformPanel(wxPanel): Line 428  class GenUniformPanel(wxPanel):
428          self.numGroupsCtrl.Enable(on)          self.numGroupsCtrl.Enable(on)
429          self.stepCtrl.Enable(on)          self.stepCtrl.Enable(on)
430    
         if on:  
             self.numGroupsCtrl.SetRange(1, abs(max - min) / 0.001)  
   
431          ngroups = self.GetNumGroups()          ngroups = self.GetNumGroups()
432    
433          if ngroups is not None  \          if ngroups is not None  \
# Line 342  class GenUniformPanel(wxPanel): Line 458  class GenUniformPanel(wxPanel):
458          min = self.GetMin()          min = self.GetMin()
459          max = self.GetMax()          max = self.GetMax()
460    
         if ngroups >= self.numGroupsCtrl.GetMax():  
             self.numGroupsCtrl.SetRange(1, ngroups + 1)  
   
461          if ngroups is not None  \          if ngroups is not None  \
462              and min is not None \              and min is not None \
463              and max is not None \              and max is not None \
# Line 393  class GenUniformPanel(wxPanel): Line 506  class GenUniformPanel(wxPanel):
506      def _OnRetrieve(self, event):      def _OnRetrieve(self, event):
507    
508          if self.layer.table is not None:          if self.layer.table is not None:
509              range = self.layer.table.field_range(self.fieldName)              wxBeginBusyCursor()
510              self.minCtrl.SetValue(str(range[0][0]))              try:
511              self.maxCtrl.SetValue(str(range[1][0]))                  min, max = self.layer.table.ValueRange(self.fieldName)
512                    self.minCtrl.SetValue(str(min))
513                    self.maxCtrl.SetValue(str(max))
514                finally:
515                    wxEndBusyCursor()
516    
517      def __GetValidatedTypeEntry(self, win, value, type, badValue = None):      def __GetValidatedTypeEntry(self, win, value, type, badValue = None):
518    
# Line 437  class GenUniformPanel(wxPanel): Line 554  class GenUniformPanel(wxPanel):
554          return valid          return valid
555    
556      def __CalcStepping(self, min, max, ngroups):      def __CalcStepping(self, min, max, ngroups):
557          step = Str2Num(str((max - min) / float(ngroups)))          step = (max - min) / float(ngroups)
558          if self.fieldType == FIELDTYPE_INT:          if self.fieldType == FIELDTYPE_INT:
559              step = int(step)              step = int(step)
560    
# Line 461  ID_UNIQUE_DONTUSE = 4004 Line 578  ID_UNIQUE_DONTUSE = 4004
578  ID_UNIQUE_USENONE = 4005  ID_UNIQUE_USENONE = 4005
579  ID_UNIQUE_SORTAVAIL = 4006  ID_UNIQUE_SORTAVAIL = 4006
580  ID_UNIQUE_SORTUSE = 4007  ID_UNIQUE_SORTUSE = 4007
581    ID_UNIQUE_REVAVAIL = 4008
582    ID_UNIQUE_REVUSE = 4009
583    
584  class GenUniquePanel(wxPanel):  class GenUniquePanel(wxPanel):
585    
# Line 476  class GenUniquePanel(wxPanel): Line 595  class GenUniquePanel(wxPanel):
595                                      wxVERTICAL)                                      wxVERTICAL)
596    
597    
598            #bsizer = wxBoxSizer(wxVERTICAL)
599            topSizer.Add(wxButton(self, ID_UNIQUE_RETRIEVE,
600                                _("Retrieve From Table")),
601                       0, wxALL | wxALIGN_RIGHT, 4)
602    
603            EVT_BUTTON(self, ID_UNIQUE_RETRIEVE, self._OnRetrieve)
604    
605            #topSizer.Add(bsizer, 0, wxALL, 4)
606    
607          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
608    
609          self.dataList = []          self.dataList = []
# Line 487  class GenUniquePanel(wxPanel): Line 615  class GenUniquePanel(wxPanel):
615          self.list_avail_data = []          self.list_avail_data = []
616          psizer.Add(self.list_avail, 1, wxGROW, 0)          psizer.Add(self.list_avail, 1, wxGROW, 0)
617    
618          psizer.Add(wxButton(self, ID_UNIQUE_SORTAVAIL, _("Sort")))          bsizer = wxBoxSizer(wxHORIZONTAL)
619            bsizer.Add(wxButton(self, ID_UNIQUE_SORTAVAIL, _("Sort")))
620            EVT_BUTTON(self, ID_UNIQUE_SORTAVAIL, self._OnSortList)
621    
622          EVT_BUTTON(self, ID_UNIQUE_SORTAVAIL, self._OnSortAvailList)          bsizer.Add(wxButton(self, ID_UNIQUE_REVAVAIL, _("Reverse")))
623            EVT_BUTTON(self, ID_UNIQUE_REVAVAIL, self._OnReverseList)
624    
625            psizer.Add(bsizer, 0, wxGROW, 0)
626          sizer.Add(psizer, 1, wxGROW, 0)          sizer.Add(psizer, 1, wxGROW, 0)
627    
628                    
629          bsizer = wxBoxSizer(wxVERTICAL)          bsizer = wxBoxSizer(wxVERTICAL)
630          bsizer.Add(wxButton(self, ID_UNIQUE_USEALL, _("Use All")),  
631            bmp = resource.GetBitmapResource(USEALL_BMP, wxBITMAP_TYPE_XPM)
632            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_USEALL, bmp),
633                     0, wxGROW | wxALL, 4)                     0, wxGROW | wxALL, 4)
634          bsizer.Add(wxButton(self, ID_UNIQUE_USE, _("Use >>")),          bmp = resource.GetBitmapResource(USE_BMP, wxBITMAP_TYPE_XPM)
635            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_USE, bmp),
636                     0, wxGROW | wxALL, 4)                     0, wxGROW | wxALL, 4)
637          bsizer.Add(wxButton(self, ID_UNIQUE_DONTUSE, _("<< Don't Use")),          bmp = resource.GetBitmapResource(USENOT_BMP, wxBITMAP_TYPE_XPM)
638            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_DONTUSE, bmp),
639                     0, wxGROW | wxALL, 4)                     0, wxGROW | wxALL, 4)
640          bsizer.Add(wxButton(self, ID_UNIQUE_USENONE, _("Use None")),          bmp = resource.GetBitmapResource(USENONE_BMP, wxBITMAP_TYPE_XPM)
641            bsizer.Add(wxBitmapButton(self, ID_UNIQUE_USENONE, bmp),
642                     0, wxGROW | wxALL, 4)                     0, wxGROW | wxALL, 4)
643    
644          EVT_BUTTON(self, ID_UNIQUE_USEALL, self._OnUseAll)          EVT_BUTTON(self, ID_UNIQUE_USEALL, self._OnUseAll)
# Line 518  class GenUniquePanel(wxPanel): Line 655  class GenUniquePanel(wxPanel):
655          self.list_use_data = []          self.list_use_data = []
656          psizer.Add(self.list_use, 1, wxGROW, 0)          psizer.Add(self.list_use, 1, wxGROW, 0)
657    
658          psizer.Add(wxButton(self, ID_UNIQUE_SORTUSE, _("Sort")))          bsizer = wxBoxSizer(wxHORIZONTAL)
659            bsizer.Add(wxButton(self, ID_UNIQUE_SORTUSE, _("Sort")))
660            EVT_BUTTON(self, ID_UNIQUE_SORTUSE, self._OnSortList)
661    
662          EVT_BUTTON(self, ID_UNIQUE_SORTUSE, self._OnSortUseList)          bsizer.Add(wxButton(self, ID_UNIQUE_REVUSE, _("Reverse")))
663            EVT_BUTTON(self, ID_UNIQUE_REVUSE, self._OnReverseList)
664    
665          sizer.Add(psizer, 1, wxGROW, 0)          psizer.Add(bsizer, 0, wxGROW, 0)
666    
667          bsizer = wxBoxSizer(wxVERTICAL)          sizer.Add(psizer, 1, wxGROW, 0)
         bsizer.Add(wxButton(self, ID_UNIQUE_RETRIEVE,  
                             _("Retrieve From Table")),  
                    0, wxGROW | wxALL, 4)  
   
         EVT_BUTTON(self, ID_UNIQUE_RETRIEVE, self._OnRetrieve)  
668    
         sizer.Add(bsizer, 0, wxALL, 4)  
669    
670          topSizer.Add(sizer, 1, wxGROW, 0)          topSizer.Add(sizer, 1, wxGROW, 0)
671    
# Line 539  class GenUniquePanel(wxPanel): Line 673  class GenUniquePanel(wxPanel):
673          self.SetAutoLayout(True)          self.SetAutoLayout(True)
674          topSizer.SetSizeHints(self)          topSizer.SetSizeHints(self)
675    
676          self.parent.AllowGenerate(False)          width, height = self.list_avail.GetSizeTuple()
677            self.list_avail.SetColumnWidth(0,width)
678            width, height = self.list_use.GetSizeTuple()
679            self.list_use.SetColumnWidth(0,width)
680    
681          self.mylist = ["Hallo", "Welt!", "Wie", "geht", "es", "dir", "?"]          self.parent.AllowGenerate(False)
682    
683      def GetNumGroups(self):      def GetNumGroups(self):
684          return self.list_use.GetItemCount()          return self.list_use.GetItemCount()
# Line 552  class GenUniquePanel(wxPanel): Line 689  class GenUniquePanel(wxPanel):
689              list.append(self.dataList[self.list_use.GetItemData(i)])              list.append(self.dataList[self.list_use.GetItemData(i)])
690          return list          return list
691    
692      def _OnSortAvailList(self, event):      def _OnSortList(self, event):
693          self.list_avail.SortItems(lambda i1, i2:          id = event.GetId()
694                                      cmp(self.dataList[i1],  
695                                          self.dataList[i2]))          if id == ID_UNIQUE_SORTUSE:
696                list = self.list_use
697      def _OnSortUseList(self, event):          else:
698          self.list_use.SortItems(lambda i1, i2:              list = self.list_avail
699                                      cmp(self.dataList[i1],  
700                                          self.dataList[i2]))          list.SortItems(lambda i1, i2: cmp(self.dataList[i1],
701                                              self.dataList[i2]))
702    
703        def _OnReverseList(self, event):
704            id = event.GetId()
705    
706            if id == ID_UNIQUE_REVUSE:
707                list = self.list_use
708            else:
709                list = self.list_avail
710    
711            #
712            # always returning 1 reverses the list
713            #
714            list.SortItems(lambda i1, i2: 1)
715    
716      def _OnRetrieve(self, event):      def _OnRetrieve(self, event):
717          self.list_use.DeleteAllItems()          self.list_use.DeleteAllItems()
# Line 568  class GenUniquePanel(wxPanel): Line 719  class GenUniquePanel(wxPanel):
719          self.list_avail.DeleteAllItems()          self.list_avail.DeleteAllItems()
720          self.list_avail_data = []          self.list_avail_data = []
721    
722          list = self.layer.table.GetUniqueValues(self.fieldName)          list = self.layer.table.UniqueValues(self.fieldName)
723          index = 0          index = 0
724          for v in list:          for v in list:
725              self.dataList.append(v)              self.dataList.append(v)
# Line 583  class GenUniquePanel(wxPanel): Line 734  class GenUniquePanel(wxPanel):
734              self.__MoveListItem(0, self.list_avail, self.list_use)              self.__MoveListItem(0, self.list_avail, self.list_use)
735    
736      def _OnUse(self, event):      def _OnUse(self, event):
         print "_OnUse"  
737          self.__MoveSelectedItems(self.list_avail, self.list_use)          self.__MoveSelectedItems(self.list_avail, self.list_use)
738    
739      def _OnDontUse(self, event):      def _OnDontUse(self, event):
         print "_OnDontUse"  
740          self.__MoveSelectedItems(self.list_use, self.list_avail)          self.__MoveSelectedItems(self.list_use, self.list_avail)
741    
742      def _OnUseNone(self, event):      def _OnUseNone(self, event):
         print "_OnUseNone"  
743    
744          for i in range(self.list_use.GetItemCount()):          for i in range(self.list_use.GetItemCount()):
745              self.__MoveListItem(0, self.list_use, self.list_avail)              self.__MoveListItem(0, self.list_use, self.list_avail)
# Line 626  class GenUniquePanel(wxPanel): Line 774  class GenUniquePanel(wxPanel):
774  #       list.SetColumnWidth(0, event.GetSize().GetWidth())  #       list.SetColumnWidth(0, event.GetSize().GetWidth())
775  #        #      
776    
777    ID_QUANTILES_RANGE = 4001
778    ID_QUANTILES_RETRIEVE = 4002
779    
780    class GenQuantilesPanel(wxPanel):
781    
782        def __init__(self, parent, layer, fieldName, fieldType):
783            wxPanel.__init__(self, parent, -1)
784    
785            self.parent = parent
786            self.layer = layer
787            self.fieldName = fieldName
788            self.fieldType = fieldType
789    
790            topBox = wxStaticBoxSizer(wxStaticBox(self, -1, ""),
791                                        wxVERTICAL)
792    
793            self.text_range = wxTextCtrl(self, ID_QUANTILES_RANGE, "")
794            self.button_retrieve = wxButton(self, ID_QUANTILES_RETRIEVE,
795                                            _("Retrieve from Table"))
796    
797            self.spin_numClasses = wxSpinCtrl(self, -1, style=wxTE_RIGHT)
798            self.spin_numClasses.SetRange(1, sys.maxint)
799            self.spin_numClasses.SetValue(1)
800    
801    
802            sizer = wxBoxSizer(wxHORIZONTAL)
803            sizer.Add(wxStaticText(self, -1, _("Apply to Range")), 0, wxALL, 4)
804            sizer.Add(self.text_range, 1, wxALL, 4)
805            sizer.Add(self.button_retrieve, 0, wxALL, 4)
806    
807            topBox.Add(sizer, 0, wxEXPAND, 0)
808    
809            sizer = wxBoxSizer(wxHORIZONTAL)
810            sizer.Add(wxStaticText(self, -1, _("Number of Classes:")), 0, wxALL, 4)
811            sizer.Add(self.spin_numClasses, 1, wxALL, 4)
812    
813            topBox.Add(sizer, 0, wxEXPAND, 0)
814    
815            self.SetSizer(topBox)
816            self.SetAutoLayout(True)
817            topBox.Fit(self)
818            topBox.SetSizeHints(self)
819    
820            EVT_TEXT(self, ID_QUANTILES_RANGE, self.OnRangeText)
821            EVT_BUTTON(self, ID_QUANTILES_RETRIEVE, self.OnRetrieve)
822    
823            self.__range = None
824    
825        def GetNumGroups(self):
826            return self.spin_numClasses.GetValue()
827    
828        def GetRange(self):
829            assert self.__range is not None
830    
831            return self.__range
832    
833        def GetList(self):
834    
835            _list = []
836    
837            if self.layer.table is not None:
838                try:
839                    wxBeginBusyCursor()
840    
841                    #
842                    # FIXME: Replace with a call to table when the method
843                    # has been written to get all the values
844                    #
845                    table = self.layer.table
846                    for i in range(table.NumRows()):
847                        _list.append(table.ReadValue(i, self.fieldName))
848                finally:
849                    wxEndBusyCursor()
850    
851            return _list
852    
853        def OnRangeText(self, event):
854    
855            try:
856                self.__range = Range(self.text_range.GetValue())
857            except ValueError:
858                self.__range = None
859    
860            if self.__range is not None:
861                self.text_range.SetForegroundColour(wxBLACK)
862            else:
863                self.text_range.SetForegroundColour(wxRED)
864    
865        def OnRetrieve(self, event):
866    
867            if self.layer.table is not None:
868                wxBeginBusyCursor()
869                try:
870                    min, max = self.layer.table.ValueRange(self.fieldName)
871                    self.text_range.SetValue("[" + str(min) + ";" + str(max) + "]")
872                finally:
873                    wxEndBusyCursor()
874    
875  ID_CUSTOMRAMP_COPYSTART = 4001  ID_CUSTOMRAMP_COPYSTART = 4001
876  ID_CUSTOMRAMP_COPYEND = 4002  ID_CUSTOMRAMP_COPYEND = 4002
877  ID_CUSTOMRAMP_EDITSTART = 4003  ID_CUSTOMRAMP_EDITSTART = 4003
# Line 657  class CustomRampPanel(wxPanel): Line 903  class CustomRampPanel(wxPanel):
903                        | wxALIGN_CENTER_VERTICAL, \                        | wxALIGN_CENTER_VERTICAL, \
904                     4)                     4)
905    
906            bmp = resource.GetBitmapResource(USE_BMP, wxBITMAP_TYPE_XPM)
907          bsizer = wxBoxSizer(wxVERTICAL)          bsizer = wxBoxSizer(wxVERTICAL)
908          bsizer.Add(wxButton(self, ID_CUSTOMRAMP_COPYSTART, _("Copy >>")),          bsizer.Add(wxBitmapButton(self, ID_CUSTOMRAMP_COPYSTART, bmp),
909                     0, wxGROW | wxALL, 4)                     0, wxGROW | wxALL, 4)
910          bsizer.Add(wxButton(self, ID_CUSTOMRAMP_COPYEND, _("<< Copy")),          bmp = resource.GetBitmapResource(USENOT_BMP, wxBITMAP_TYPE_XPM)
911            bsizer.Add(wxBitmapButton(self, ID_CUSTOMRAMP_COPYEND, bmp),
912                     0, wxGROW | wxALL, 4)                     0, wxGROW | wxALL, 4)
913    
914          topSizer.Add(bsizer,          topSizer.Add(bsizer,
# Line 696  class CustomRampPanel(wxPanel): Line 944  class CustomRampPanel(wxPanel):
944          self.SetAutoLayout(True)          self.SetAutoLayout(True)
945          topSizer.SetSizeHints(self)          topSizer.SetSizeHints(self)
946    
947      def GetStartProperties(self):      def GetRamp(self):
948          return self.startPropCtrl.GetProperties()          return CustomRamp(self.startPropCtrl.GetProperties(),
949                              self.endPropCtrl.GetProperties())
     def GetEndProperties(self):  
         return self.endPropCtrl.GetProperties()  
950    
951      def _OnCopyStart(self, event):      def _OnCopyStart(self, event):
952          self.endPropCtrl.SetProperties(self.startPropCtrl.GetProperties())          self.endPropCtrl.SetProperties(self.startPropCtrl.GetProperties())
# Line 714  class CustomRampPanel(wxPanel): Line 960  class CustomRampPanel(wxPanel):
960      def _OnEditEnd(self, event):      def _OnEditEnd(self, event):
961          self.endPropCtrl.DoEdit()          self.endPropCtrl.DoEdit()
962    
963  class ClassGenerator:    
   
     def GenSingletonsFromList(self, list, numGroups, prop1, prop2):  
         """Generate a new classification consisting solely of singletons.  
   
         The resulting classification will consist of at most 'numGroups'  
         groups whose group properties ramp between 'prop1' and 'prop2'. There  
         could be fewer groups if 'list' contains fewer that 'numGroups' items.  
   
         list -- any object that implements the iterator interface  
   
         numGroups -- how many groups to generate. This can not be  
                      determined while the classification is being  
                      generated because the stepping values must  
                      be precalculated to ramp between prop1 and prop2.  
   
         prop1 -- initial group property values  
   
         prop2 -- final group property values  
         """  
   
         clazz = Classification()  
         if numGroups == 0: return clazz  
   
         for value, prop in zip(list, CustomRamp(numGroups, prop1, prop2)):  
             clazz.AppendGroup(ClassGroupSingleton(value, prop))  
   
         return clazz  
   
     def GenSingletons(self, min, max, numGroups, prop1, prop2):  
   
         clazz = Classification()  
   
         #step = int((max - min) / float(numGroups))  
         step = int(Str2Num(str((max - min + 1) / float(numGroups))))  
   
         if numGroups > 0:  
             cur_value = min  
   
             for prop in CustomRamp(numGroups, prop1, prop2):  
                 clazz.AppendGroup(  
                     ClassGroupSingleton(  
                         Str2Num(str(cur_value)),  
                         prop))  
                 cur_value += step  
   
         return clazz  
   
     def GenUnifromDistribution(self, min, max, numGroups,  
                                prop1, prop2, intStep = False):  
         """Generate a classification with numGroups range groups  
         each with the same interval.  
   
         intStep -- force the calculated stepping to an integer.  
                    Useful if the values are integers but the  
                    number of groups specified doesn't evenly  
                    divide (max - min).  
         """  
   
         clazz = Classification()  
         if numGroups == 0: return clazz  
   
         step = Str2Num(str((max - min) / float(numGroups)))  
   
         if intStep:  
             step = int(step)  
   
         cur_min = min  
         cur_max = cur_min + step  
   
         i = 0  
         for prop in CustomRamp(numGroups, prop1, prop2):  
   
             if i == (numGroups - 1):  
                 cur_max = max  
   
             # this check guards against rounding issues  
             if cur_min != cur_max:  
                 clazz.AppendGroup(  
                     ClassGroupRange(  
                         Str2Num(str(cur_min)),  
                         Str2Num(str(cur_max)),  
                         prop))  
   
             cur_min = cur_max  
             cur_max += step  
             i += 1  
   
         return clazz  
   
 CLR  = 0  
 STEP = 1  
 class CustomRamp:  
   
     def __init__(self, num, prop1, prop2):  
   
         self.count = int(num)  
         num = float(num)  
   
         clr = prop1.GetLineColor()  
         lineColor2 = prop2.GetLineColor()  
           
         self.noLine = clr is not Color.Transparent \  
                         and lineColor2 is not Color.Transparent  
   
   
         self.lineInfo = self.__GetColorInfo(prop1.GetLineColor(),  
                                             prop2.GetLineColor(),  
                                             num)  
   
         self.fillInfo = self.__GetColorInfo(prop1.GetFill(),  
                                             prop2.GetFill(),  
                                             num)  
   
         self.lineWidth = prop1.GetLineWidth()  
         self.lineWidthStep = (prop2.GetLineWidth() - self.lineWidth) / num  
   
     def __iter__(self):  
         return self  
   
     def next(self):  
         if self.count == 0:  
             raise StopIteration  
   
         prop = ClassGroupProperties()  
   
         if self.lineInfo is None:  
             prop.SetLineColor(Color.Transparent)  
         else:  
             prop.SetLineColor(Color(self.lineInfo[CLR][0] / 255,  
                                     self.lineInfo[CLR][1] / 255,  
                                     self.lineInfo[CLR][2] / 255))  
   
             self.lineInfo[CLR][0] += self.lineInfo[STEP][0]  
             self.lineInfo[CLR][1] += self.lineInfo[STEP][1]  
             self.lineInfo[CLR][2] += self.lineInfo[STEP][2]  
   
         if self.fillInfo is None:  
             prop.SetFill(Color.Transparent)  
         else:  
             prop.SetFill(Color(self.fillInfo[CLR][0] / 255,  
                             self.fillInfo[CLR][1] / 255,  
                             self.fillInfo[CLR][2] / 255))  
   
             self.fillInfo[CLR][0] += self.fillInfo[STEP][0]  
             self.fillInfo[CLR][1] += self.fillInfo[STEP][1]  
             self.fillInfo[CLR][2] += self.fillInfo[STEP][2]  
   
   
         prop.SetLineWidth(int(self.lineWidth))  
         self.lineWidth        += self.lineWidthStep  
   
         self.count -= 1  
   
         return prop  
   
     def __GetColorInfo(self, color1, color2, numGroups):  
   
         if color1 is Color.Transparent and color2 is Color.Transparent:  
             #  
             # returning early  
             #  
             return None  
         elif color1 is not Color.Transparent and color2 is Color.Transparent:  
             color = [color1.red   * 255,  
                      color1.green * 255,  
                      color1.blue  * 255]  
             step = (0, 0, 0)  
         elif color1 is Color.Transparent and color2 is not Color.Transparent:  
             color = [color2.red   * 255,  
                      color2.green * 255,  
                      color2.blue  * 255]  
             step = (0, 0, 0)  
         else:  
             color = [color1.red   * 255,  
                      color1.green * 255,  
                      color1.blue  * 255]  
             step = ((color2.red   * 255 - color1.red   * 255)   / numGroups,  
                     (color2.green * 255 - color1.green * 255) / numGroups,  
                     (color2.blue  * 255 - color1.blue  * 255)  / numGroups)  
   
   
         return (color, step)  

Legend:
Removed from v.629  
changed lines
  Added in v.1100

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26