/[thuban]/trunk/thuban/Thuban/UI/classifier.py
ViewVC logotype

Diff of /trunk/thuban/Thuban/UI/classifier.py

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

revision 576 by jonathan, Mon Mar 31 18:31:17 2003 UTC revision 606 by jonathan, Fri Apr 4 12:16:39 2003 UTC
# Line 27  from Thuban.Model.color import Color Line 27  from Thuban.Model.color import Color
27    
28  from Thuban.Model.layer import Layer, SHAPETYPE_ARC, SHAPETYPE_POLYGON, SHAPETYPE_POINT  from Thuban.Model.layer import Layer, SHAPETYPE_ARC, SHAPETYPE_POLYGON, SHAPETYPE_POINT
29    
30    from Thuban.UI.classgen import ClassGenDialog, ClassGenerator
31    
32  from dialogs import NonModalDialog  from dialogs import NonModalDialog
33    
34  # widget id's  # widget id's
# Line 36  ID_CLASS_TABLE = 40011 Line 38  ID_CLASS_TABLE = 40011
38  ID_CLASSIFY_OK = 4001  ID_CLASSIFY_OK = 4001
39  ID_CLASSIFY_CANCEL = 4002  ID_CLASSIFY_CANCEL = 4002
40  ID_CLASSIFY_ADD = 4003  ID_CLASSIFY_ADD = 4003
41  ID_CLASSIFY_GENRANGE = 4004  ID_CLASSIFY_GENCLASS = 4004
42  ID_CLASSIFY_REMOVE = 4005  ID_CLASSIFY_REMOVE = 4005
43  ID_CLASSIFY_MOVEUP = 4006  ID_CLASSIFY_MOVEUP = 4006
44  ID_CLASSIFY_MOVEDOWN = 4007  ID_CLASSIFY_MOVEDOWN = 4007
45  ID_CLASSIFY_APPLY = 4008  ID_CLASSIFY_APPLY = 4008
46    ID_CLASSIFY_EDITPROPS = 4009
47    
48  # table columns  # table columns
49  COL_SYMBOL = 0  COL_SYMBOL = 0
# Line 60  import weakref Line 63  import weakref
63  class ClassGrid(wxGrid):  class ClassGrid(wxGrid):
64    
65    
66      def __init__(self, parent):      def __init__(self, parent, classifier):
67          """Constructor.          """Constructor.
68    
69          parent -- the parent window          parent -- the parent window
# Line 73  class ClassGrid(wxGrid): Line 76  class ClassGrid(wxGrid):
76          wxGrid.__init__(self, parent, ID_CLASS_TABLE)          wxGrid.__init__(self, parent, ID_CLASS_TABLE)
77          #self.SetTable(ClassTable(fieldData, layer.ShapeType(), self), True)          #self.SetTable(ClassTable(fieldData, layer.ShapeType(), self), True)
78    
79            self.classifier = classifier
80    
81          self.currentSelection = []          self.currentSelection = []
82    
83          EVT_GRID_CELL_LEFT_DCLICK(self, self._OnCellDClick)          EVT_GRID_CELL_LEFT_DCLICK(self, self._OnCellDClick)
84          EVT_GRID_RANGE_SELECT(self, self._OnSelectedRange)          EVT_GRID_RANGE_SELECT(self, self._OnSelectedRange)
85          EVT_GRID_SELECT_CELL(self, self._OnSelectedCell)          EVT_GRID_SELECT_CELL(self, self._OnSelectedCell)
86            EVT_GRID_COL_SIZE(self, self._OnCellResize)
87            EVT_GRID_ROW_SIZE(self, self._OnCellResize)
88    
89          #print "123123123: ", ('Show' in dir(self))          #print "123123123: ", ('Show' in dir(self))
90    
# Line 91  class ClassGrid(wxGrid): Line 98  class ClassGrid(wxGrid):
98    
99      def CreateTable(self, clazz, shapeType, group = None):      def CreateTable(self, clazz, shapeType, group = None):
100    
101          assert(isinstance(clazz, Classification))          assert isinstance(clazz, Classification)
102    
         self.shapeType = shapeType  
103          table = self.GetTable()          table = self.GetTable()
104          if table is None:          if table is None:
105              w = self.GetDefaultColSize() * 3 + self.GetDefaultRowLabelSize()              w = self.GetDefaultColSize() * 3 + self.GetDefaultRowLabelSize()
# Line 108  class ClassGrid(wxGrid): Line 114  class ClassGrid(wxGrid):
114          self.ClearSelection()          self.ClearSelection()
115    
116          #print "8------------------"          #print "8------------------"
117          table.Reset(clazz, self.shapeType, group)          table.Reset(clazz, shapeType, group)
118          #print "9------------------"          #print "9------------------"
119    
120  #   def Show(self, show = True):  #   def Show(self, show = True):
# Line 203  class ClassGrid(wxGrid): Line 209  class ClassGrid(wxGrid):
209      def SelectGroup(self, group, makeVisible = True):      def SelectGroup(self, group, makeVisible = True):
210          if group is None: return          if group is None: return
211    
212          assert(isinstance(group, ClassGroup))          assert isinstance(group, ClassGroup)
213    
214          table = self.GetTable()          table = self.GetTable()
215    
216          assert(table is not None)          assert table is not None
217    
218    
219          #print "-- ", group          #print "-- ", group
# Line 241  class ClassGrid(wxGrid): Line 247  class ClassGrid(wxGrid):
247          r = event.GetRow()          r = event.GetRow()
248          c = event.GetCol()          c = event.GetCol()
249          if c == COL_SYMBOL:          if c == COL_SYMBOL:
250              prop = self.GetTable().GetValueAsCustom(r, c, None)              self.classifier.EditGroupProperties(r)
             #prop = group.GetProperties()  
251    
             # get a new ClassGroupProperties object and copy the  
             # values over to our current object  
             propDlg = SelectPropertiesDialog(NULL, prop, self.shapeType)  
             if propDlg.ShowModal() == wxID_OK:  
                 new_prop = propDlg.GetClassGroupProperties()  
                 #prop.SetProperties(new_prop)  
                 self.GetTable().SetValueAsCustom(r, c, None, new_prop)  
             propDlg.Destroy()  
252    
253      #      #
254      # _OnSelectedRange() and _OnSelectedCell() were borrowed      # _OnSelectedRange() and _OnSelectedCell() were borrowed
# Line 282  class ClassGrid(wxGrid): Line 279  class ClassGrid(wxGrid):
279          #self.ConfigureForSelection()          #self.ConfigureForSelection()
280          event.Skip()          event.Skip()
281    
282        def _OnCellResize(self, event):
283            self.FitInside()
284    
285  class ClassTable(wxPyGridTableBase):  class ClassTable(wxPyGridTableBase):
286      """Represents the underlying data structure for the grid."""      """Represents the underlying data structure for the grid."""
287    
# Line 320  class ClassTable(wxPyGridTableBase): Line 320  class ClassTable(wxPyGridTableBase):
320          shapeType -- the type of shape that the layer uses          shapeType -- the type of shape that the layer uses
321          """          """
322    
323          assert(isinstance(clazz, Classification))          assert isinstance(clazz, Classification)
324    
325          self.GetView().BeginBatch()          self.GetView().BeginBatch()
326    
327          self.fieldType = clazz.GetFieldType()          self.fieldType = clazz.GetFieldType()
328          self.shapeType = shapeType          self.shapeType = shapeType
329    
330          old_len = len(self.tdata)          self.SetClassification(clazz, group)
331            self.__Modified(-1)
332            #print "11------------------"
333    
334            self.GetView().EndBatch()
335            self.GetView().FitInside()
336    
337        def SetClassification(self, clazz, group = None):
338    
339            self.GetView().BeginBatch()
340    
341            old_len = self.GetNumberRows()
342          self.tdata = []          self.tdata = []
343    
344          #print "9------------------"          #print "9------------------"
# Line 339  class ClassTable(wxPyGridTableBase): Line 349  class ClassTable(wxPyGridTableBase):
349          row = -1          row = -1
350          for g in clazz:          for g in clazz:
351              ng = copy.deepcopy(g)              ng = copy.deepcopy(g)
352              self.__SetRow(-1, ng)              self.__SetRow(None, ng)
353              if g is group:              if g is group:
354                  row = self.GetNumberRows() - 1                  row = self.GetNumberRows() - 1
355                  #print "selecting row..."                  #print "selecting row..."
356    
357          #print "10------------------"          #print "10------------------"
358    
359          self.__Modified(-1)          self.__NotifyRowChanges(old_len, self.GetNumberRows())
   
         self.__NotifyRowChanges(old_len, len(self.tdata))  
         #print "11------------------"  
360    
361          if row > -1:          if row > -1:
362              self.GetView().ClearSelection()              self.GetView().ClearSelection()
363              self.GetView().SelectRow(row)              self.GetView().SelectRow(row)
364              self.GetView().MakeCellVisible(row, 0)              self.GetView().MakeCellVisible(row, 0)
365                
366          self.GetView().EndBatch()          self.GetView().EndBatch()
367            self.GetView().FitInside()
368    
369      def __NotifyRowChanges(self, curRows, newRows):      def __NotifyRowChanges(self, curRows, newRows):
370          #          #
# Line 372  class ClassTable(wxPyGridTableBase): Line 380  class ClassTable(wxPyGridTableBase):
380          elif newRows < curRows:          elif newRows < curRows:
381              msg = wxGridTableMessage(self,              msg = wxGridTableMessage(self,
382                          wxGRIDTABLE_NOTIFY_ROWS_DELETED,                          wxGRIDTABLE_NOTIFY_ROWS_DELETED,
383                          curRows - newRows,    # position                          curRows,              # position
384                          curRows - newRows)    # how many                          curRows - newRows)    # how many
385              self.GetView().ProcessTableMessage(msg)              self.GetView().ProcessTableMessage(msg)
386              self.GetView().FitInside()              self.GetView().FitInside()
# Line 382  class ClassTable(wxPyGridTableBase): Line 390  class ClassTable(wxPyGridTableBase):
390    
391          The table is considered modified after this operation.          The table is considered modified after this operation.
392    
393          row -- if row is -1 or greater than the current number of rows          row -- if row is < 0 'group' is inserted at the top of the table
394                 then group is appended to the end.                 if row is >= GetNumberRows() or None 'group' is append to
395                        the end of the table.
396                   otherwise 'group' replaces row 'row'
397          """          """
398    
399          # either append or replace          # either append or replace
400          if row == -1 or row >= self.GetNumberRows():          if row is None or row >= self.GetNumberRows():
401              self.tdata.append(group)              self.tdata.append(group)
402            elif row < 0:
403                self.tdata.insert(0, group)
404          else:          else:
405              self.tdata[row] = group              self.tdata[row] = group
406    
# Line 407  class ClassTable(wxPyGridTableBase): Line 419  class ClassTable(wxPyGridTableBase):
419          if isinstance(group, ClassGroupRange):     return _("Range")          if isinstance(group, ClassGroupRange):     return _("Range")
420          if isinstance(group, ClassGroupMap):       return _("Map")          if isinstance(group, ClassGroupMap):       return _("Map")
421    
422          assert(False) # shouldn't get here          assert False # shouldn't get here
423          return _("")          return _("")
424    
425      def GetNumberRows(self):      def GetNumberRows(self):
# Line 452  class ClassTable(wxPyGridTableBase): Line 464  class ClassTable(wxPyGridTableBase):
464              return group.GetLabel()              return group.GetLabel()
465    
466          # col must be COL_VALUE          # col must be COL_VALUE
467          assert(col == COL_VALUE)          assert col == COL_VALUE
468    
469          if isinstance(group, ClassGroupDefault):          if isinstance(group, ClassGroupDefault):
470              return _("DEFAULT")              return _("DEFAULT")
# Line 461  class ClassTable(wxPyGridTableBase): Line 473  class ClassTable(wxPyGridTableBase):
473          elif isinstance(group, ClassGroupRange):          elif isinstance(group, ClassGroupRange):
474              return _("%s - %s") % (group.GetMin(), group.GetMax())              return _("%s - %s") % (group.GetMin(), group.GetMax())
475    
476          assert(False) # shouldn't get here          assert False  # shouldn't get here
477          return None          return None
478    
479      def __ParseInput(self, value):      def __ParseInput(self, value):
# Line 501  class ClassTable(wxPyGridTableBase): Line 513  class ClassTable(wxPyGridTableBase):
513    
514                  return (conv(Str2Num(value[:i])), conv(Str2Num(value[i+1:])))                  return (conv(Str2Num(value[:i])), conv(Str2Num(value[i+1:])))
515    
516          assert(False) # shouldn't get here          assert False  # shouldn't get here
517          return (0,)          return (0,)
518                            
519    
# Line 517  class ClassTable(wxPyGridTableBase): Line 529  class ClassTable(wxPyGridTableBase):
529          typeName -- unused, but needed to overload wxPyGridTableBase          typeName -- unused, but needed to overload wxPyGridTableBase
530          """          """
531    
532          assert(col >= 0 and col < self.GetNumberCols())          assert col >= 0 and col < self.GetNumberCols()
533          assert(row >= 0 and row < self.GetNumberRows())          assert row >= 0 and row < self.GetNumberRows()
534    
535          group = self.tdata[row]          group = self.tdata[row]
536    
# Line 563  class ClassTable(wxPyGridTableBase): Line 575  class ClassTable(wxPyGridTableBase):
575                              changed = True                              changed = True
576                          ngroup.SetRange(dataInfo[0], dataInfo[1])                          ngroup.SetRange(dataInfo[0], dataInfo[1])
577                      else:                      else:
578                          assert(False)                          assert False
579                          pass                          pass
580    
581                      if changed:                      if changed:
582                          ngroup.SetLabel(group.GetLabel())                          ngroup.SetLabel(group.GetLabel())
583                          self.SetClassGroup(row, ngroup)                          self.SetClassGroup(row, ngroup)
584          else:          else:
585              assert(False) # shouldn't be here              assert False # shouldn't be here
586              pass              pass
587    
588          if mod:          if mod:
# Line 625  class ClassTable(wxPyGridTableBase): Line 637  class ClassTable(wxPyGridTableBase):
637          The table is considered modified if any rows are removed.          The table is considered modified if any rows are removed.
638          """          """
639    
640          assert(pos >= 0)          assert pos >= 0
641          old_len = len(self.tdata)          old_len = len(self.tdata)
642          for row in range(pos, pos - numRows, -1):          for row in range(pos, pos - numRows, -1):
643              group = self.GetClassGroup(row)              group = self.GetClassGroup(row)
# Line 645  class ClassTable(wxPyGridTableBase): Line 657  class ClassTable(wxPyGridTableBase):
657          old_len = len(self.tdata)          old_len = len(self.tdata)
658          for i in range(numRows):          for i in range(numRows):
659              np = ClassGroupSingleton()              np = ClassGroupSingleton()
660              self.__SetRow(-1, np)              self.__SetRow(None, np)
661    
662          if self.IsModified():          if self.IsModified():
663              self.__NotifyRowChanges(old_len, len(self.tdata))              self.__NotifyRowChanges(old_len, len(self.tdata))
# Line 733  class Classifier(NonModalDialog): Line 745  class Classifier(NonModalDialog):
745          controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)          controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
746          self.controlButtons.append(button)          self.controlButtons.append(button)
747    
748          button = wxButton(panel, ID_CLASSIFY_GENRANGE, _("Generate Ranges"))          button = wxButton(panel, ID_CLASSIFY_EDITPROPS, _("Edit Properties"))
749            controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
750            self.controlButtons.append(button)
751    
752            button = wxButton(panel, ID_CLASSIFY_GENCLASS, _("Generate Class"))
753          controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)          controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
754          self.controlButtons.append(button)          self.controlButtons.append(button)
755    
# Line 757  class Classifier(NonModalDialog): Line 773  class Classifier(NonModalDialog):
773          # Classification data table          # Classification data table
774          #          #
775    
776          self.classGrid = ClassGrid(panel)          self.classGrid = ClassGrid(panel, self)
777          #self.__SetGridTable(self.__cur_field, group)          #self.__SetGridTable(self.__cur_field, group)
778          #self.fields.SetSelection(self.__cur_field)          #self.fields.SetSelection(self.__cur_field)
779    
# Line 776  class Classifier(NonModalDialog): Line 792  class Classifier(NonModalDialog):
792          panelBox.Add(controlBox, 1, wxGROW, 10)          panelBox.Add(controlBox, 1, wxGROW, 10)
793    
794          EVT_BUTTON(self, ID_CLASSIFY_ADD, self._OnAdd)          EVT_BUTTON(self, ID_CLASSIFY_ADD, self._OnAdd)
795            EVT_BUTTON(self, ID_CLASSIFY_EDITPROPS, self._OnEditGroupProperties)
796          EVT_BUTTON(self, ID_CLASSIFY_REMOVE, self._OnRemove)          EVT_BUTTON(self, ID_CLASSIFY_REMOVE, self._OnRemove)
797          EVT_BUTTON(self, ID_CLASSIFY_GENRANGE, self._OnGenRange)          EVT_BUTTON(self, ID_CLASSIFY_GENCLASS, self._OnGenClass)
798          EVT_BUTTON(self, ID_CLASSIFY_MOVEUP, self._OnMoveUp)          EVT_BUTTON(self, ID_CLASSIFY_MOVEUP, self._OnMoveUp)
799          EVT_BUTTON(self, ID_CLASSIFY_MOVEDOWN, self._OnMoveDown)          EVT_BUTTON(self, ID_CLASSIFY_MOVEDOWN, self._OnMoveDown)
800    
# Line 805  class Classifier(NonModalDialog): Line 822  class Classifier(NonModalDialog):
822          panel.SetSizer(panelBox)          panel.SetSizer(panelBox)
823          panelBox.SetSizeHints(panel)          panelBox.SetSizeHints(panel)
824    
825          topBox.Add(panel, 1, wxGROW, 0)          topBox.Add(panel, 1, wxGROW, 0)
826          panelBox.SetSizeHints(self)          panelBox.SetSizeHints(self)
827          self.SetAutoLayout(True)          self.SetAutoLayout(True)
828          self.SetSizer(topBox)          self.SetSizer(topBox)
# Line 816  class Classifier(NonModalDialog): Line 833  class Classifier(NonModalDialog):
833    
834          self.haveApplied = False          self.haveApplied = False
835    
836        def EditGroupProperties(self, row):
837            table = self.classGrid.GetTable()
838            prop = table.GetValueAsCustom(row, COL_SYMBOL, None)
839    
840            # get a new ClassGroupProperties object and copy the
841            # values over to our current object
842            propDlg = SelectPropertiesDialog(NULL, prop, self.layer.ShapeType())
843            if propDlg.ShowModal() == wxID_OK:
844                new_prop = propDlg.GetClassGroupProperties()
845                table.SetValueAsCustom(row, COL_SYMBOL, None, new_prop)
846            propDlg.Destroy()
847            
848    
849      def __BuildClassification(self, fieldIndex):      def __BuildClassification(self, fieldIndex):
850    
851          numRows = self.classGrid.GetNumberRows()          numRows = self.classGrid.GetNumberRows()
852          assert(numRows > 0) # there should always be a default row          assert numRows > 0  # there should always be a default row
853    
854          clazz = Classification()          clazz = Classification()
855          if fieldIndex == 0:          if fieldIndex == 0:
# Line 871  class Classifier(NonModalDialog): Line 901  class Classifier(NonModalDialog):
901          fieldName = self.fields.GetString(fieldIndex)          fieldName = self.fields.GetString(fieldIndex)
902          fieldType = self.layer.GetFieldType(fieldName)          fieldType = self.layer.GetFieldType(fieldName)
903    
904          assert(Classifier.type2string.has_key(fieldType))          assert Classifier.type2string.has_key(fieldType)
905    
906          text = Classifier.type2string[fieldType]          text = Classifier.type2string[fieldType]
907    
# Line 881  class Classifier(NonModalDialog): Line 911  class Classifier(NonModalDialog):
911    
912          #print "3------------------"          #print "3------------------"
913    
914          assert(oldIndex >= -1)          assert oldIndex >= -1
915    
916          self.fields.SetSelection(newIndex)          self.fields.SetSelection(newIndex)
917    
# Line 901  class Classifier(NonModalDialog): Line 931  class Classifier(NonModalDialog):
931          self.__SetFieldTypeText(newIndex)          self.__SetFieldTypeText(newIndex)
932    
933    
934        def _OnEditGroupProperties(self, event):
935            sel = self.classGrid.GetCurrentSelection()
936    
937            if len(sel) == 1:
938                self.EditGroupProperties(sel[0])
939    
940      def _OnFieldSelect(self, event):      def _OnFieldSelect(self, event):
941          index = self.fields.GetSelection()          index = self.fields.GetSelection()
942          self.__SelectField(index, self.__cur_field)          self.__SelectField(index, self.__cur_field)
# Line 941  class Classifier(NonModalDialog): Line 977  class Classifier(NonModalDialog):
977      def _OnRemove(self, event):      def _OnRemove(self, event):
978          self.classGrid.DeleteSelectedRows()          self.classGrid.DeleteSelectedRows()
979    
980      def _OnGenRange(self, event):      def _OnGenClass(self, event):
981          print "Classifier._OnGenRange()"  
982            genDlg = ClassGenDialog(self,
983                                    self.layer.table,
984                                    self.fields.GetString(self.__cur_field))
985    
986            if genDlg.ShowModal() == wxID_OK:
987                clazz = genDlg.GetClassification()
988                self.fields.SetClientData(self.__cur_field, clazz)
989                self.classGrid.GetTable().SetClassification(clazz)
990            genDlg.Destroy()
991    
992      def _OnMoveUp(self, event):      def _OnMoveUp(self, event):
993          sel = self.classGrid.GetCurrentSelection()          sel = self.classGrid.GetCurrentSelection()
# Line 1086  class SelectPropertiesDialog(wxDialog): Line 1131  class SelectPropertiesDialog(wxDialog):
1131    
1132      def __GetColor(self, cur):      def __GetColor(self, cur):
1133          dialog = wxColourDialog(self)          dialog = wxColourDialog(self)
1134          dialog.GetColourData().SetColour(Color2wxColour(cur))          if cur is not Color.None:
1135                dialog.GetColourData().SetColour(Color2wxColour(cur))
1136    
1137          ret = None          ret = None
1138          if dialog.ShowModal() == wxID_OK:          if dialog.ShowModal() == wxID_OK:
1139              ret = wxColour2Color(dialog.GetColourData().GetColour())              ret = wxColour2Color(dialog.GetColourData().GetColour())
# Line 1151  class ClassDataPreviewer: Line 1198  class ClassDataPreviewer:
1198    
1199      def Draw(self, dc, rect, prop, shapeType):      def Draw(self, dc, rect, prop, shapeType):
1200    
1201          assert(dc is not None)          assert dc is not None
1202          assert(isinstance(prop, ClassGroupProperties))          assert isinstance(prop, ClassGroupProperties)
1203    
1204          if rect is None:          if rect is None:
1205              x = 0              x = 0

Legend:
Removed from v.576  
changed lines
  Added in v.606

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26