/[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 415 by jonathan, Wed Feb 19 16:52:51 2003 UTC revision 441 by jonathan, Thu Feb 27 15:55:00 2003 UTC
# Line 16  from wxPython.grid import * Line 16  from wxPython.grid import *
16    
17  from Thuban import _  from Thuban import _
18  from Thuban.common import *  from Thuban.common import *
19    from Thuban.UI.common import *
20    
21  from Thuban.Model.classification import * #Classification, ClassData  from Thuban.Model.classification import * #Classification, ClassData
22    
# Line 65  class ClassTable(wxPyGridTableBase): Line 66  class ClassTable(wxPyGridTableBase):
66    
67      __col_labels = [_("Visual"), _("Value"), _("Label")]      __col_labels = [_("Visual"), _("Value"), _("Label")]
68    
     # this is tied to the values of classification.ClassData  
     __row_labels = [_("Default"), _("Point"), _("Range"), _("Map")]  
   
69      def __init__(self, clazz, shapeType, view = None):      def __init__(self, clazz, shapeType, view = None):
70          wxPyGridTableBase.__init__(self)          wxPyGridTableBase.__init__(self)
71          self.SetView(view)          self.SetView(view)
# Line 89  class ClassTable(wxPyGridTableBase): Line 87  class ClassTable(wxPyGridTableBase):
87          if clazz is None:          if clazz is None:
88              clazz = Classification()              clazz = Classification()
89    
90          p = clazz.GetDefaultData()  #       p = clazz.GetDefaultGroup()
91          np = ClassDataPoint(classData = p)  #       np = ClassDataDefault(classData = p)
92          self.tdata.append([np, 'DEFAULT', np.GetLabel()])  #       self.tdata.append([np, 'DEFAULT', np.GetLabel()])
93    
94          for p in clazz.points.values():  #       for p in clazz.points.values():
95              np = ClassDataPoint(p.GetValue(), classData = p)  #           np = ClassDataPoint(p.GetValue(), classData = p)
96              self.tdata.append([np, np.GetValue(), np.GetLabel()])  #           self.tdata.append([np, np.GetValue(), np.GetLabel()])
97    
98          for p in clazz.ranges:  #       for p in clazz.ranges:
99              np = ClassDataRange(p.GetMin(), p.GetMax(), classData = p)  #           np = ClassDataRange(p.GetMin(), p.GetMax(), classData = p)
100              self.tdata.append([np,  #           self.tdata.append([np,
101                                 '%s - %s' % (np.GetMin(), np.GetMax()),  #                              '%s - %s' % (np.GetMin(), np.GetMax()),
102                                 np.GetLabel()])  #                              np.GetLabel()])
103    
104            i = 0
105            for p in clazz:
106                np = copy.copy(p)
107                self.__SetRow(i, np)
108                i += 1
109    
110    
111          self.modified = 0          self.modified = 0
112    
113          #          #
# Line 125  class ClassTable(wxPyGridTableBase): Line 130  class ClassTable(wxPyGridTableBase):
130    
131          self.GetView().EndBatch()          self.GetView().EndBatch()
132    
133        def __SetRow(self, row, group):
134    
135            if isinstance(group, ClassGroupDefault):
136                data = [group, 'DEFAULT', group.GetLabel()]
137            elif isinstance(group, ClassGroupSingleton):
138                data = [group, group.GetValue(), group.GetLabel()]
139            elif isinstance(group, ClassGroupRange):
140                data = [group,
141                        '%s - %s' % (group.GetMin(), group.GetMax()),
142                        group.GetLabel()]
143    
144            if row >= len(self.tdata):
145                self.tdata.append(data)
146            else:
147                self.tdata[row] = data
148    
149      def GetColLabelValue(self, col):      def GetColLabelValue(self, col):
150          return self.__col_labels[col]          return self.__col_labels[col]
151    
152      def GetRowLabelValue(self, row):      def GetRowLabelValue(self, row):
153          data = self.tdata[row][COL_VISUAL]          data = self.tdata[row][COL_VISUAL]
154          type = data.GetType()          if isinstance(data, ClassGroupDefault): return _("Default")
155          return self.__row_labels[type]          if isinstance(data, ClassGroupSingleton): return _("Singleton")
156            if isinstance(data, ClassGroupRange): return _("Range")
157            if isinstance(data, ClassGroupMap): return _("Map")
158    
159      def GetNumberRows(self):      def GetNumberRows(self):
160          return len(self.tdata)          return len(self.tdata)
# Line 167  class ClassTable(wxPyGridTableBase): Line 190  class ClassTable(wxPyGridTableBase):
190          # function.          # function.
191          #          #
192          try:          try:
193              return (ClassData.POINT, Str2Num(value))              return (Str2Num(value))
194          except:          except:
195              i = value.find('-')              i = value.find('-')
196              if i == 0:              if i == 0:
197                  i = value.find('-', 1)                  i = value.find('-', 1)
198    
199              return (ClassData.RANGE,              return (Str2Num(value[:i]), Str2Num(value[i+1:]))
                     Str2Num(value[:i]),  
                     Str2Num(value[i+1:]))  
200                            
201    
202      def SetValueAsCustom(self, row, col, typeName, value):      def SetValueAsCustom(self, row, col, typeName, value):
# Line 185  class ClassTable(wxPyGridTableBase): Line 206  class ClassTable(wxPyGridTableBase):
206              self.tdata[row][COL_VISUAL] = value              self.tdata[row][COL_VISUAL] = value
207          elif col == COL_VALUE:          elif col == COL_VALUE:
208              if row != 0: # DefaultData row              if row != 0: # DefaultData row
                 type = data.GetType()  
209    
210                  if type == ClassData.MAP:                  if isinstance(data, ClassGroupMap):
211                      # something special                      # something special
212                      pass                      pass
213                  else: # POINT, RANGE                  else: # POINT, RANGE
# Line 197  class ClassTable(wxPyGridTableBase): Line 217  class ClassTable(wxPyGridTableBase):
217                          # bad input, ignore the request                          # bad input, ignore the request
218                      else:                      else:
219    
220                          if dataInfo[0] == ClassData.POINT:                          if len(dataInfo) == 1:
221                              if type != ClassData.POINT:                              if not isinstance(data, ClassGroupSingleton):
222                                  data = ClassDataPoint(classData = data)                                  ndata = ClassGroupSingleton(prop = data)
223                              data.SetValue(dataInfo[1])                              ndata.SetValue(dataInfo[1])
224                              self.tdata[row][COL_VALUE] = data.GetValue()                          elif len(dataInfo) == 2:
225                          elif dataInfo[0] == ClassData.RANGE:                              if not isinstance(data, ClassGroupRange):
                             if type != ClassData.RANGE:  
226                                  data = ClassDataRange(classData = data)                                  data = ClassDataRange(classData = data)
227                              data.SetRange(dataInfo[1], dataInfo[2])                              data.SetRange(dataInfo[1], dataInfo[2])
                             self.tdata[row][COL_VALUE] = \  
                                 "%s - %s" % (data.GetMin(), data.GetMax())  
228    
229                          self.tdata[row][COL_VISUAL] = data                          ndata.SetLabel(data.GetLabel())
230                            self.__SetRow(row, ndata)
231    
232                            #self.tdata[row][COL_VISUAL] = data
233    
234                          # i just want it to redraw!                          self.GetView().Refresh()
                         self.GetView().BeginBatch()  
                         self.GetView().EndBatch()  
235    
236          elif col == COL_LABEL:          elif col == COL_LABEL:
237              data.SetLabel(value)              data.SetLabel(value)
# Line 233  class ClassTable(wxPyGridTableBase): Line 251  class ClassTable(wxPyGridTableBase):
251    
252          return attr          return attr
253    
254      def GetClassData(self, row):      def GetClassGroup(self, row):
255          return self.tdata[row][COL_VISUAL]          return self.tdata[row][COL_VISUAL]
256    
257      def __Modified(self):      def __Modified(self):
# Line 247  class ClassTable(wxPyGridTableBase): Line 265  class ClassTable(wxPyGridTableBase):
265          self.tdata.append([np, np.GetValue(), np.GetLabel()])          self.tdata.append([np, np.GetValue(), np.GetLabel()])
266          msg = wxGridTableMessage(self, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1)          msg = wxGridTableMessage(self, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1)
267          self.GetView().ProcessTableMessage(msg)          self.GetView().ProcessTableMessage(msg)
268          self.GetView().BeginBatch()          self.GetView().Refresh()
         self.GetView().EndBatch()  
269    
270  class Classifier(wxDialog):  class Classifier(wxDialog):
271            
# Line 273  class Classifier(wxDialog): Line 290  class Classifier(wxDialog):
290                                       style = wxCB_READONLY)                                       style = wxCB_READONLY)
291    
292          self.num_cols = layer.table.field_count()          self.num_cols = layer.table.field_count()
293          self.__cur_prop = -1          # just assume the first field in case one hasn't been
294            # specified in the file.
295            self.__cur_prop = 0
296          field = layer.GetClassification().GetField()          field = layer.GetClassification().GetField()
297          for i in range(self.num_cols):          for i in range(self.num_cols):
298              type, name, len, decc = layer.table.field_info(i)              type, name, len, decc = layer.table.field_info(i)
# Line 337  class Classifier(wxDialog): Line 356  class Classifier(wxDialog):
356    
357          if numRows > 0:          if numRows > 0:
358              table = self.classGrid.GetTable()              table = self.classGrid.GetTable()
359              clazz.SetDefaultData(table.GetClassData(0))              clazz.SetDefaultGroup(table.GetClassGroup(0))
360    
361              for i in range(1, numRows):              for i in range(1, numRows):
362                  clazz.AddClassData(table.GetClassData(i))                  clazz.AddGroup(table.GetClassGroup(i))
363    
364          return clazz          return clazz
365    
# Line 390  class Classifier(wxDialog): Line 409  class Classifier(wxDialog):
409          r = event.GetRow()          r = event.GetRow()
410          c = event.GetCol()          c = event.GetCol()
411          if c == COL_VISUAL:          if c == COL_VISUAL:
412              prop = self.classGrid.GetTable().GetValueAsCustom(r, c, None)              # XXX: getting the properties is only possible with non-Maps!!!
413                group = self.classGrid.GetTable().GetValueAsCustom(r, c, None)
414                prop = group.GetProperties()
415              propDlg = SelectPropertiesDialog(NULL, prop, self.layer.ShapeType())              propDlg = SelectPropertiesDialog(NULL, prop, self.layer.ShapeType())
416              if propDlg.ShowModal() == wxID_OK:              if propDlg.ShowModal() == wxID_OK:
417                  new_prop = propDlg.GetClassData()                  new_prop = propDlg.GetClassGroupProperties()
418                  prop.SetStroke(new_prop.GetStroke())                  prop.SetStroke(new_prop.GetStroke())
419                  prop.SetStrokeWidth(new_prop.GetStrokeWidth())                  prop.SetStrokeWidth(new_prop.GetStrokeWidth())
420                  prop.SetFill(new_prop.GetFill())                  prop.SetFill(new_prop.GetFill())
421                  self.classGrid.BeginBatch()                  self.classGrid.Refresh()
                 self.classGrid.EndBatch()  
422              propDlg.Destroy()              propDlg.Destroy()
423    
424    
425  ID_SELPROP_OK = 4001  ID_SELPROP_OK = 4001
426  ID_SELPROP_CANCEL = 4002  ID_SELPROP_CANCEL = 4002
427  ID_SELPROP_SPINCTRL = 4002  ID_SELPROP_SPINCTRL = 4002
428    ID_SELPROP_PREVIEW = 4003
429    ID_SELPROP_STROKECLR = 4004
430    ID_SELPROP_FILLCLR = 4005
431    
432  class SelectPropertiesDialog(wxDialog):  class SelectPropertiesDialog(wxDialog):
433    
# Line 412  class SelectPropertiesDialog(wxDialog): Line 435  class SelectPropertiesDialog(wxDialog):
435          wxDialog.__init__(self, parent, -1, _("Select Properties"),          wxDialog.__init__(self, parent, -1, _("Select Properties"),
436                            style = wxRESIZE_BORDER)                            style = wxRESIZE_BORDER)
437    
438            self.prop = ClassGroupProperties(prop)
439    
440          topBox = wxBoxSizer(wxVERTICAL)          topBox = wxBoxSizer(wxVERTICAL)
   
         self.prop = ClassData(classData = prop)  
441    
442          topBox.Add(wxStaticText(self, -1, _("Stroke Color: ")),          itemBox = wxBoxSizer(wxHORIZONTAL)
443              0, wxALIGN_LEFT | wxALL, 4)  
444            # preview box
445            previewBox = wxBoxSizer(wxVERTICAL)
446            previewBox.Add(wxStaticText(self, -1, _("Preview:")),
447                0, wxALIGN_LEFT | wxALL, 4)
448            self.previewer = ClassDataPreviewer(None, self.prop, shapeType,
449                                                self, ID_SELPROP_PREVIEW, (40, 40))
450            previewBox.Add(self.previewer, 1, wxGROW, 15)
451    
452            itemBox.Add(previewBox, 1, wxALIGN_LEFT | wxALL | wxGROW, 0)
453    
454            # control box
455            ctrlBox = wxBoxSizer(wxVERTICAL)
456            ctrlBox.Add(
457                wxButton(self, ID_SELPROP_STROKECLR, "Change Stroke Color"),
458                0, wxALIGN_CENTER_HORIZONTAL | wxALL | wxGROW, 4)
459            EVT_BUTTON(self, ID_SELPROP_STROKECLR, self.OnChangeStrokeColor)
460    
461            if shapeType != SHAPETYPE_ARC:
462                ctrlBox.Add(
463                    wxButton(self, ID_SELPROP_FILLCLR, "Change Fill Color"),
464                    0, wxALIGN_LEFT | wxALL | wxGROW, 4)
465                EVT_BUTTON(self, ID_SELPROP_FILLCLR, self.OnChangeFillColor)
466    
467          spinBox = wxBoxSizer(wxHORIZONTAL)          spinBox = wxBoxSizer(wxHORIZONTAL)
468          spinBox.Add(wxStaticText(self, -1, _("Stroke Width: ")),          spinBox.Add(wxStaticText(self, -1, _("Stroke Width: ")),
469                  0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxBOTTOM, 4)                  0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 4)
470          self.spinCtrl = wxSpinCtrl(self, ID_SELPROP_SPINCTRL,          self.spinCtrl = wxSpinCtrl(self, ID_SELPROP_SPINCTRL,
471                                     min=1, max=10,                                     min=1, max=10,
472                                     value=str(prop.GetStrokeWidth()),                                     value=str(prop.GetStrokeWidth()),
# Line 431  class SelectPropertiesDialog(wxDialog): Line 476  class SelectPropertiesDialog(wxDialog):
476    
477          spinBox.Add(self.spinCtrl, 0, wxALIGN_LEFT | wxALL, 4)          spinBox.Add(self.spinCtrl, 0, wxALIGN_LEFT | wxALL, 4)
478    
479          topBox.Add(spinBox, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_BOTTOM, 10)          ctrlBox.Add(spinBox, 0, wxALIGN_RIGHT | wxALL, 0)
480            itemBox.Add(ctrlBox, 0, wxALIGN_RIGHT | wxALL | wxGROW, 0)
481            topBox.Add(itemBox, 1, wxALIGN_LEFT | wxALL | wxGROW, 0)
482    
         if shapeType != SHAPETYPE_ARC:  
             topBox.Add(wxStaticText(self, -1, _("Fill Color: ")),  
                 0, wxALIGN_LEFT | wxBOTTOM, 4)  
483    
484          #          #
485          # Control buttons:          # Control buttons:
# Line 463  class SelectPropertiesDialog(wxDialog): Line 507  class SelectPropertiesDialog(wxDialog):
507    
508      def OnSpin(self, event):      def OnSpin(self, event):
509          self.prop.SetStrokeWidth(self.spinCtrl.GetValue())          self.prop.SetStrokeWidth(self.spinCtrl.GetValue())
510            self.previewer.Refresh()
511    
512      def GetClassData(self):      def __GetColor(self, cur):
513            dialog = wxColourDialog(self)
514            dialog.GetColourData().SetColour(Color2wxColour(cur))
515            ret = None
516            if dialog.ShowModal() == wxID_OK:
517                ret = wxColour2Color(dialog.GetColourData().GetColour())
518    
519            dialog.Destroy()
520    
521            return ret
522            
523        def OnChangeStrokeColor(self, event):
524            clr = self.__GetColor(self.prop.GetStroke())
525            if clr is not None:
526                self.prop.SetStroke(clr)
527            self.previewer.Refresh() # XXX: work around, see ClassDataPreviewer
528    
529        def OnChangeFillColor(self, event):
530            clr = self.__GetColor(self.prop.GetFill())
531            if clr is not None:
532                self.prop.SetFill(clr)
533            self.previewer.Refresh() # XXX: work around, see ClassDataPreviewer
534    
535        def GetClassGroupProperties(self):
536          return self.prop          return self.prop
537    
538    
539  class ClassDataPreviewer:  class ClassDataPreviewer(wxWindow):
540    
541        def __init__(self, rect, prop, shapeType,
542                           parent = None, id = -1, size = wxDefaultSize):
543            if parent is not None:
544                wxWindow.__init__(self, parent, id, size=size)
545                EVT_PAINT(self, self.OnPaint)
546    
547            self.rect = rect
548            self.prop = prop
549            self.shapeType = shapeType
550    
551        def OnPaint(self, event):
552            dc = wxPaintDC(self)
553    
554      def Draw(self, dc, rect, data, shapeType):          # XXX: this doesn't seem to be having an effect:
555            dc.DestroyClippingRegion()
556    
557          stroke = data.GetStroke()          self.Draw(dc, None)
558    
559        def Draw(self, dc, rect, prop = None, shapeType = None):
560    
561            if prop is None: prop = self.prop
562            if shapeType is None: shapeType = self.shapeType
563    
564            if rect is None:
565                x = y = 0
566                w, h = self.GetClientSizeTuple()
567            else:
568                x = rect.GetX()
569                y = rect.GetY()
570                w = rect.GetWidth()
571                h = rect.GetHeight()
572    
573            stroke = prop.GetStroke()
574          if stroke is Color.None:          if stroke is Color.None:
575              pen = wxTRANSPARENT_PEN              pen = wxTRANSPARENT_PEN
576          else:          else:
577              pen = wxPen(wxColour(stroke.red * 255,              pen = wxPen(Color2wxColour(stroke),
578                                   stroke.green * 255,                          prop.GetStrokeWidth(),
                                  stroke.blue * 255),  
                         data.GetStrokeWidth(),  
579                          wxSOLID)                          wxSOLID)
580    
581          stroke = data.GetFill()          stroke = prop.GetFill()
582          if stroke is Color.None:          if stroke is Color.None:
583              brush = wxTRANSPARENT_BRUSH              brush = wxTRANSPARENT_BRUSH
584          else:          else:
585              brush = wxBrush(wxColour(stroke.red * 255,              brush = wxBrush(Color2wxColour(stroke), wxSOLID)
                                      stroke.green * 255,  
                                      stroke.blue * 255), wxSOLID)  
586    
587          dc.SetPen(pen)          dc.SetPen(pen)
588          dc.SetBrush(brush)          dc.SetBrush(brush)
589    
590          if shapeType == SHAPETYPE_ARC:          if shapeType == SHAPETYPE_ARC:
591              dc.DrawSpline([wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight()),              dc.DrawSpline([wxPoint(x, y + h),
592                             wxPoint(rect.GetX() + rect.GetWidth()/2,                             wxPoint(x + w/2, y + h/4),
593                                     rect.GetY() + rect.GetHeight()/4),                             wxPoint(x + w/2, y + h/4*3),
594                             wxPoint(rect.GetX() + rect.GetWidth()/2,                             wxPoint(x + w, y)])
                                    rect.GetY() + rect.GetHeight()/4*3),  
                            wxPoint(rect.GetX() + rect.GetWidth(), rect.GetY())])  
595    
596          elif shapeType == SHAPETYPE_POINT or \          elif shapeType == SHAPETYPE_POINT or \
597               shapeType == SHAPETYPE_POLYGON:               shapeType == SHAPETYPE_POLYGON:
598    
599              dc.DrawCircle(rect.GetX() + rect.GetWidth()/2,              dc.DrawCircle(x + w/2, y + h/2,
600                            rect.GetY() + rect.GetHeight()/2,                            (min(w, h) - prop.GetStrokeWidth())/2)
                           (min(rect.GetWidth(), rect.GetHeight())  
                            - data.GetStrokeWidth())/2)  
601    
602  class ClassRenderer(wxPyGridCellRenderer):  class ClassRenderer(wxPyGridCellRenderer):
603    
604      def __init__(self, shapeType):      def __init__(self, shapeType):
605          wxPyGridCellRenderer.__init__(self)          wxPyGridCellRenderer.__init__(self)
606          self.shapeType = shapeType          self.previewer = ClassDataPreviewer(None, None, shapeType)
607    
608      def Draw(self, grid, attr, dc, rect, row, col, isSelected):      def Draw(self, grid, attr, dc, rect, row, col, isSelected):
609          data = grid.GetTable().GetValueAsCustom(row, col, "")          data = grid.GetTable().GetValueAsCustom(row, col, "")
# Line 526  class ClassRenderer(wxPyGridCellRenderer Line 616  class ClassRenderer(wxPyGridCellRenderer
616          dc.DrawRectangle(rect.GetX(), rect.GetY(),          dc.DrawRectangle(rect.GetX(), rect.GetY(),
617                           rect.GetWidth(), rect.GetHeight())                           rect.GetWidth(), rect.GetHeight())
618    
619          ClassDataPreviewer().Draw(dc, rect, data, self.shapeType)          if not isinstance(data, ClassGroupMap):
620                self.previewer.Draw(dc, rect, data.GetProperties())
621    
622          if isSelected:          if isSelected:
623              dc.SetPen(wxPen(wxColour(0 * 255, 0 * 255, 0 * 255),              dc.SetPen(wxPen(wxColour(0 * 255, 0 * 255, 0 * 255),

Legend:
Removed from v.415  
changed lines
  Added in v.441

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26