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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 650 - (hide annotations)
Fri Apr 11 14:27:41 2003 UTC (21 years, 10 months ago) by jonathan
File MIME type: text/x-python
File size: 44450 byte(s)
Refactored ID's from ID_CLASSIFY_* to ID_PROPERTY_*.
(Classifier.__init__): Minor changes to the layout.
(Classifier._OnTitleChanged): Listen for when the user edits the
        title and update the dialog's title and the layer's title.

1 bh 476 # Copyright (c) 2001, 2003 by Intevation GmbH
2 jonathan 372 # Authors:
3     # Jonathan Coles <[email protected]>
4     #
5     # This program is free software under the GPL (>=v2)
6     # Read the file COPYING coming with Thuban for details.
7    
8     """Dialog for classifying how layers are displayed"""
9    
10     __version__ = "$Revision$"
11    
12 jonathan 376 import copy
13    
14 jonathan 473 from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \
15     FIELDTYPE_STRING
16    
17 jonathan 372 from wxPython.wx import *
18     from wxPython.grid import *
19    
20 jan 374 from Thuban import _
21 jonathan 415 from Thuban.common import *
22 jonathan 441 from Thuban.UI.common import *
23 jan 374
24 jonathan 451 from Thuban.Model.classification import *
25 jonathan 392
26 jonathan 415 from Thuban.Model.color import Color
27    
28 jonathan 460 from Thuban.Model.layer import Layer, SHAPETYPE_ARC, SHAPETYPE_POLYGON, SHAPETYPE_POINT
29 jonathan 392
30 jonathan 606 from Thuban.UI.classgen import ClassGenDialog, ClassGenerator
31    
32 jonathan 485 from dialogs import NonModalDialog
33    
34 jonathan 372 ID_CLASS_TABLE = 40011
35    
36    
37 jonathan 460 # table columns
38 jonathan 638 COL_VISIBLE = 0
39     COL_SYMBOL = 1
40     COL_VALUE = 2
41     COL_LABEL = 3
42     NUM_COLS = 4
43 jonathan 415
44 jonathan 460 # indices into the client data lists in Classifier.fields
45 jonathan 451 FIELD_CLASS = 0
46     FIELD_TYPE = 1
47     FIELD_NAME = 2
48    
49 jonathan 415 #
50     # this is a silly work around to ensure that the table that is
51     # passed into SetTable is the same that is returned by GetTable
52     #
53     import weakref
54     class ClassGrid(wxGrid):
55    
56 jonathan 570
57 jonathan 606 def __init__(self, parent, classifier):
58 jonathan 460 """Constructor.
59    
60     parent -- the parent window
61    
62     clazz -- the working classification that this grid should
63     use for display.
64     """
65    
66 jonathan 485 wxGrid.__init__(self, parent, ID_CLASS_TABLE)
67 jonathan 415
68 jonathan 606 self.classifier = classifier
69    
70 jonathan 570 self.currentSelection = []
71    
72 jonathan 460 EVT_GRID_CELL_LEFT_DCLICK(self, self._OnCellDClick)
73 jonathan 451 EVT_GRID_RANGE_SELECT(self, self._OnSelectedRange)
74     EVT_GRID_SELECT_CELL(self, self._OnSelectedCell)
75 jonathan 606 EVT_GRID_COL_SIZE(self, self._OnCellResize)
76     EVT_GRID_ROW_SIZE(self, self._OnCellResize)
77 jonathan 451
78 jonathan 638 #def GetCellAttr(self, row, col):
79     #print "GetCellAttr ", row, col
80     #wxGrid.GetCellAttr(self, row, col)
81    
82 jonathan 570 def CreateTable(self, clazz, shapeType, group = None):
83    
84 jonathan 606 assert isinstance(clazz, Classification)
85 jonathan 460
86     table = self.GetTable()
87     if table is None:
88 jonathan 650 w = self.GetDefaultColSize() * NUM_COLS \
89     + self.GetDefaultRowLabelSize()
90     h = self.GetDefaultRowSize() * 4 \
91     + self.GetDefaultColLabelSize()
92    
93 jonathan 500 self.SetDimensions(-1, -1, w, h)
94     self.SetSizeHints(w, h, -1, -1)
95 jonathan 570 table = ClassTable(self)
96     self.SetTable(table, True)
97 jonathan 460
98 jonathan 570
99 bh 476 self.SetSelectionMode(wxGrid.wxGridSelectRows)
100 jonathan 460 self.ClearSelection()
101    
102 jonathan 606 table.Reset(clazz, shapeType, group)
103 jonathan 570
104 jonathan 451 def GetCurrentSelection(self):
105 jonathan 460 """Return the currently highlighted rows as an increasing list
106     of row numbers."""
107 jonathan 451 sel = copy.copy(self.currentSelection)
108     sel.sort()
109     return sel
110    
111 jonathan 570 def GetSelectedRows(self):
112     return self.GetCurrentSelection()
113    
114 jonathan 638 #def SetCellRenderer(self, row, col, renderer):
115     #raise ValueError(_("Must not allow setting of renderer in ClassGrid!"))
116 jonathan 415
117 jonathan 460 #
118     # [Set|Get]Table is taken from http://wiki.wxpython.org
119     # they are needed as a work around to ensure that the table
120     # that is passed to SetTable is the one that is returned
121     # by GetTable.
122     #
123 jonathan 415 def SetTable(self, object, *attributes):
124     self.tableRef = weakref.ref(object)
125     return wxGrid.SetTable(self, object, *attributes)
126    
127     def GetTable(self):
128 jonathan 460 try:
129     return self.tableRef()
130     except:
131     return None
132 jonathan 415
133 jonathan 451 def DeleteSelectedRows(self):
134 jonathan 460 """Deletes all highlighted rows.
135    
136     If only one row is highlighted then after it is deleted the
137     row that was below the deleted row is highlighted."""
138    
139 jonathan 451 sel = self.GetCurrentSelection()
140 jonathan 415
141 jonathan 460 # nothing to do
142     if len(sel) == 0: return
143    
144     # if only one thing is selected check if it is the default
145     # data row, because we can't remove that
146 jonathan 451 if len(sel) == 1:
147 jonathan 485 #group = self.GetTable().GetValueAsCustom(sel[0], COL_SYMBOL, None)
148     group = self.GetTable().GetClassGroup(sel[0])
149 jonathan 451 if isinstance(group, ClassGroupDefault):
150     wxMessageDialog(self,
151     "The Default group cannot be removed.",
152     style = wxOK | wxICON_EXCLAMATION).ShowModal()
153     return
154    
155 jonathan 460
156 jonathan 451 self.ClearSelection()
157    
158 jonathan 460 # we need to remove things from the bottom up so we don't
159     # change the indexes of rows that will be deleted next
160 jonathan 451 sel.reverse()
161 jonathan 460
162     #
163     # actually remove the rows
164     #
165 jonathan 451 table = self.GetTable()
166     for row in sel:
167     table.DeleteRows(row)
168    
169 jonathan 460 #
170     # if there was only one row selected highlight the row
171     # that was directly below it, or move up one if the
172     # deleted row was the last row.
173     #
174 jonathan 451 if len(sel) == 1:
175     r = sel[0]
176     if r > self.GetNumberRows() - 1:
177     r = self.GetNumberRows() - 1
178     self.SelectRow(r)
179    
180 jonathan 570
181     def SelectGroup(self, group, makeVisible = True):
182     if group is None: return
183    
184 jonathan 606 assert isinstance(group, ClassGroup)
185 jonathan 570
186     table = self.GetTable()
187    
188 jonathan 606 assert table is not None
189 jonathan 570
190     for i in range(table.GetNumberRows()):
191     g = table.GetClassGroup(i)
192     if g is group:
193     self.SelectRow(i)
194     if makeVisible:
195     self.MakeCellVisible(i, 0)
196     break
197    
198 jonathan 451 #
199     # XXX: This isn't working, and there is no way to deselect rows wxPython!
200     #
201     # def DeselectRow(self, row):
202     # self.ProcessEvent(
203     # wxGridRangeSelectEvent(-1,
204     # wxEVT_GRID_RANGE_SELECT,
205     # self,
206     # (row, row), (row, row),
207     # sel = False))
208    
209 jonathan 460 def _OnCellDClick(self, event):
210 jonathan 638 """Handle a double click on a cell."""
211 jonathan 460
212 jonathan 451 r = event.GetRow()
213     c = event.GetCol()
214 jonathan 638
215 jonathan 460 if c == COL_SYMBOL:
216 jonathan 638 self.classifier.EditSymbol(r)
217     else:
218     event.Skip()
219 jonathan 460
220 jonathan 451 #
221     # _OnSelectedRange() and _OnSelectedCell() were borrowed
222 jonathan 460 # from http://wiki.wxpython.org to keep track of which
223     # cells are currently highlighted
224 jonathan 451 #
225     def _OnSelectedRange(self, event):
226     """Internal update to the selection tracking list"""
227     if event.Selecting():
228     for index in range( event.GetTopRow(), event.GetBottomRow()+1):
229     if index not in self.currentSelection:
230     self.currentSelection.append( index )
231     else:
232     for index in range( event.GetTopRow(), event.GetBottomRow()+1):
233     while index in self.currentSelection:
234     self.currentSelection.remove( index )
235     #self.ConfigureForSelection()
236    
237     event.Skip()
238    
239     def _OnSelectedCell( self, event ):
240     """Internal update to the selection tracking list"""
241     self.currentSelection = [ event.GetRow() ]
242     #self.ConfigureForSelection()
243     event.Skip()
244    
245 jonathan 606 def _OnCellResize(self, event):
246     self.FitInside()
247    
248 jonathan 376 class ClassTable(wxPyGridTableBase):
249 jonathan 460 """Represents the underlying data structure for the grid."""
250 jonathan 376
251 jonathan 638 __col_labels = [_("Visible"), _("Symbol"), _("Value"), _("Label")]
252 jonathan 415
253    
254 jonathan 570 def __init__(self, view = None):
255     #def __init__(self, clazz, shapeType, view = None):
256 jonathan 460 """Constructor.
257    
258     shapeType -- the type of shape that the layer uses
259    
260     view -- a wxGrid object that uses this class for its table
261     """
262    
263 jonathan 376 wxPyGridTableBase.__init__(self)
264 jonathan 485
265 jonathan 638 assert len(ClassTable.__col_labels) == NUM_COLS
266    
267 jonathan 615 self.clazz = None
268 jonathan 638 self.__colAttr = {}
269 jonathan 376
270 jonathan 638 self.SetView(view)
271 jonathan 415
272 jonathan 570 def Reset(self, clazz, shapeType, group = None):
273 jonathan 460 """Reset the table with the given data.
274 jonathan 415
275 jonathan 460 This is necessary because wxWindows does not allow a grid's
276     table to change once it has been intially set and so we
277     need a way of modifying the data.
278    
279     clazz -- the working classification that this table should
280     use for display. This may be different from the
281     classification in the layer.
282    
283     shapeType -- the type of shape that the layer uses
284     """
285    
286 jonathan 606 assert isinstance(clazz, Classification)
287 jonathan 460
288 jonathan 415 self.GetView().BeginBatch()
289    
290 jonathan 485 self.fieldType = clazz.GetFieldType()
291 jonathan 415 self.shapeType = shapeType
292    
293 jonathan 606 self.SetClassification(clazz, group)
294     self.__Modified(-1)
295 jonathan 415
296 jonathan 638 self.__colAttr = {}
297    
298     attr = wxGridCellAttr()
299     attr.SetEditor(wxGridCellBoolEditor())
300     attr.SetRenderer(wxGridCellBoolRenderer())
301     attr.SetAlignment(wxALIGN_CENTER, wxALIGN_CENTER)
302     self.__colAttr[COL_VISIBLE] = attr
303    
304     attr = wxGridCellAttr()
305     attr.SetRenderer(ClassRenderer(self.shapeType))
306     attr.SetReadOnly()
307     self.__colAttr[COL_SYMBOL] = attr
308    
309 jonathan 606 self.GetView().EndBatch()
310     self.GetView().FitInside()
311    
312 jonathan 615 def GetClassification(self):
313     return self.clazz
314    
315 jonathan 606 def SetClassification(self, clazz, group = None):
316    
317     self.GetView().BeginBatch()
318    
319     old_len = self.GetNumberRows()
320 jonathan 376
321 jonathan 570 row = -1
322 jonathan 615 self.clazz = clazz
323 jonathan 441
324 jonathan 606 self.__NotifyRowChanges(old_len, self.GetNumberRows())
325 jonathan 415
326 jonathan 638 #
327     # XXX: this is dead code at the moment
328     #
329 jonathan 570 if row > -1:
330     self.GetView().ClearSelection()
331     self.GetView().SelectRow(row)
332     self.GetView().MakeCellVisible(row, 0)
333 jonathan 606
334 jonathan 615 self.__Modified()
335    
336 jonathan 638
337 jonathan 451 self.GetView().EndBatch()
338 jonathan 606 self.GetView().FitInside()
339 jonathan 451
340     def __NotifyRowChanges(self, curRows, newRows):
341 jonathan 415 #
342     # silly message processing for updates to the number of
343     # rows and columns
344     #
345     if newRows > curRows:
346     msg = wxGridTableMessage(self,
347     wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
348     newRows - curRows) # how many
349     self.GetView().ProcessTableMessage(msg)
350 jonathan 519 self.GetView().FitInside()
351 jonathan 415 elif newRows < curRows:
352     msg = wxGridTableMessage(self,
353     wxGRIDTABLE_NOTIFY_ROWS_DELETED,
354 jonathan 606 curRows, # position
355 jonathan 415 curRows - newRows) # how many
356     self.GetView().ProcessTableMessage(msg)
357 jonathan 519 self.GetView().FitInside()
358 jonathan 415
359 jonathan 615
360 jonathan 441 def __SetRow(self, row, group):
361 jonathan 460 """Set a row's data to that of the group.
362 jonathan 441
363 jonathan 485 The table is considered modified after this operation.
364    
365 jonathan 606 row -- if row is < 0 'group' is inserted at the top of the table
366     if row is >= GetNumberRows() or None 'group' is append to
367     the end of the table.
368     otherwise 'group' replaces row 'row'
369 jonathan 460 """
370 jonathan 441
371 jonathan 460 # either append or replace
372 jonathan 606 if row is None or row >= self.GetNumberRows():
373 jonathan 615 self.clazz.AppendGroup(group)
374 jonathan 606 elif row < 0:
375 jonathan 615 self.clazz.InsertGroup(0, group)
376 jonathan 441 else:
377 jonathan 615 if row == 0:
378     self.clazz.SetDefaultGroup(group)
379     else:
380     self.clazz.ReplaceGroup(row - 1, group)
381 jonathan 441
382 jonathan 460 self.__Modified()
383    
384 jonathan 415 def GetColLabelValue(self, col):
385 jonathan 460 """Return the label for the given column."""
386 jonathan 415 return self.__col_labels[col]
387    
388     def GetRowLabelValue(self, row):
389 jonathan 460 """Return the label for the given row."""
390 jonathan 415
391 jonathan 615 if row == 0:
392     return _("Default")
393     else:
394     group = self.clazz.GetGroup(row - 1)
395     if isinstance(group, ClassGroupDefault): return _("Default")
396     if isinstance(group, ClassGroupSingleton): return _("Singleton")
397     if isinstance(group, ClassGroupRange): return _("Range")
398     if isinstance(group, ClassGroupMap): return _("Map")
399 jonathan 460
400 jonathan 606 assert False # shouldn't get here
401 jonathan 460 return _("")
402    
403 jonathan 376 def GetNumberRows(self):
404 jonathan 460 """Return the number of rows."""
405 jonathan 615 if self.clazz is None:
406     return 0
407 jonathan 376
408 jonathan 615 return self.clazz.GetNumGroups() + 1 # +1 for default group
409    
410 jonathan 376 def GetNumberCols(self):
411 jonathan 460 """Return the number of columns."""
412 jonathan 638 return NUM_COLS
413 jonathan 376
414     def IsEmptyCell(self, row, col):
415 jonathan 460 """Determine if a cell is empty. This is always false."""
416     return False
417 jonathan 376
418     def GetValue(self, row, col):
419 jonathan 460 """Return the object that is used to represent the given
420     cell coordinates. This may not be a string."""
421     return self.GetValueAsCustom(row, col, None)
422 jonathan 376
423     def SetValue(self, row, col, value):
424 jonathan 460 """Assign 'value' to the cell specified by 'row' and 'col'.
425    
426     The table is considered modified after this operation.
427     """
428    
429     self.SetValueAsCustom(row, col, None, value)
430 jonathan 415
431 jonathan 392 def GetValueAsCustom(self, row, col, typeName):
432 jonathan 460 """Return the object that is used to represent the given
433     cell coordinates. This may not be a string.
434    
435     typeName -- unused, but needed to overload wxPyGridTableBase
436     """
437 jonathan 376
438 jonathan 615 if row == 0:
439     group = self.clazz.GetDefaultGroup()
440     else:
441     group = self.clazz.GetGroup(row - 1)
442 jonathan 460
443 jonathan 615
444 jonathan 638 if col == COL_VISIBLE:
445     return group.IsVisible()
446    
447 jonathan 460 if col == COL_SYMBOL:
448 jonathan 485 return group.GetProperties()
449 jonathan 460
450     if col == COL_LABEL:
451     return group.GetLabel()
452    
453     # col must be COL_VALUE
454 jonathan 606 assert col == COL_VALUE
455 jonathan 460
456     if isinstance(group, ClassGroupDefault):
457     return _("DEFAULT")
458     elif isinstance(group, ClassGroupSingleton):
459     return group.GetValue()
460     elif isinstance(group, ClassGroupRange):
461     return _("%s - %s") % (group.GetMin(), group.GetMax())
462    
463 jonathan 615 assert(False) # shouldn't get here
464 jonathan 460 return None
465    
466 jonathan 415 def __ParseInput(self, value):
467     """Try to determine what kind of input value is
468 jonathan 460 (string, number, or range)
469    
470     Returns a tuple of length one if there is a single
471     value, or of length two if it is a range.
472 jonathan 415 """
473 jonathan 392
474 jonathan 485 type = self.fieldType
475 jonathan 415
476 jonathan 460 if type == FIELDTYPE_STRING:
477 jonathan 451 return (value,)
478 jonathan 630 elif type in (FIELDTYPE_INT, FIELDTYPE_DOUBLE):
479 jonathan 451
480 jonathan 460 if type == FIELDTYPE_INT:
481     conv = lambda p: int(float(p))
482     else:
483     conv = lambda p: p
484    
485 jonathan 451 #
486     # first try to take the input as a single number
487     # if there's an exception try to break it into
488     # a range seperated by a '-'. take care to ignore
489     # a leading '-' as that could be for a negative number.
490     # then try to parse the individual parts. if there
491     # is an exception here, let it pass up to the calling
492     # function.
493     #
494     try:
495 jonathan 460 return (conv(Str2Num(value)),)
496     except ValueError:
497 jonathan 451 i = value.find('-')
498     if i == 0:
499     i = value.find('-', 1)
500    
501 jonathan 460 return (conv(Str2Num(value[:i])), conv(Str2Num(value[i+1:])))
502    
503 jonathan 606 assert False # shouldn't get here
504 jonathan 485 return (0,)
505 jonathan 415
506    
507     def SetValueAsCustom(self, row, col, typeName, value):
508 jonathan 460 """Set the cell specified by 'row' and 'col' to 'value'.
509 jonathan 415
510 jonathan 460 If column represents the value column, the input is parsed
511     to determine if a string, number, or range was entered.
512     A new ClassGroup may be created if the type of data changes.
513    
514     The table is considered modified after this operation.
515    
516     typeName -- unused, but needed to overload wxPyGridTableBase
517     """
518    
519 jonathan 650 assert 0 <= col < self.GetNumberCols()
520     assert 0 <= row < self.GetNumberRows()
521 jonathan 460
522 jonathan 615 if row == 0:
523     group = self.clazz.GetDefaultGroup()
524     else:
525     group = self.clazz.GetGroup(row - 1)
526 jonathan 460
527 jonathan 485 mod = True # assume the data will change
528 jonathan 460
529 jonathan 638 if col == COL_VISIBLE:
530     group.SetVisible(value)
531     elif col == COL_SYMBOL:
532 jonathan 485 group.SetProperties(value)
533     elif col == COL_LABEL:
534     group.SetLabel(value)
535 jonathan 415 elif col == COL_VALUE:
536 jonathan 451 if isinstance(group, ClassGroupDefault):
537     # not allowed to modify the default value
538     pass
539     elif isinstance(group, ClassGroupMap):
540     # something special
541     pass
542     else: # SINGLETON, RANGE
543     try:
544     dataInfo = self.__ParseInput(value)
545 jonathan 460 except ValueError:
546 jonathan 451 # bad input, ignore the request
547 jonathan 485 mod = False
548 jonathan 451 else:
549 jonathan 415
550 jonathan 485 changed = False
551 jonathan 451 ngroup = group
552     props = group.GetProperties()
553 jonathan 460
554     #
555     # try to update the values, which may include
556     # changing the underlying group type if the
557     # group was a singleton and a range was entered
558     #
559 jonathan 451 if len(dataInfo) == 1:
560     if not isinstance(group, ClassGroupSingleton):
561     ngroup = ClassGroupSingleton(prop = props)
562 jonathan 485 changed = True
563 jonathan 451 ngroup.SetValue(dataInfo[0])
564     elif len(dataInfo) == 2:
565     if not isinstance(group, ClassGroupRange):
566     ngroup = ClassGroupRange(prop = props)
567 jonathan 485 changed = True
568 jonathan 451 ngroup.SetRange(dataInfo[0], dataInfo[1])
569 jonathan 415 else:
570 jonathan 606 assert False
571 jonathan 485 pass
572 jonathan 415
573 jonathan 485 if changed:
574     ngroup.SetLabel(group.GetLabel())
575     self.SetClassGroup(row, ngroup)
576     else:
577 jonathan 606 assert False # shouldn't be here
578 jonathan 485 pass
579 jonathan 460
580 jonathan 485 if mod:
581 jonathan 460 self.__Modified()
582     self.GetView().Refresh()
583 jonathan 415
584     def GetAttr(self, row, col, someExtraParameter):
585 jonathan 460 """Returns the cell attributes"""
586    
587 jonathan 638 return self.__colAttr.get(col, wxGridCellAttr()).Clone()
588 jonathan 415
589 jonathan 441 def GetClassGroup(self, row):
590 jonathan 460 """Return the ClassGroup object representing row 'row'."""
591 jonathan 415
592 jonathan 615 #return self.GetValueAsCustom(row, COL_SYMBOL, None)
593     if row == 0:
594     return self.clazz.GetDefaultGroup()
595     else:
596     return self.clazz.GetGroup(row - 1)
597 jonathan 415
598 jonathan 460 def SetClassGroup(self, row, group):
599 jonathan 485 self.__SetRow(row, group)
600     self.GetView().Refresh()
601 jonathan 460
602     def __Modified(self, mod = True):
603 jonathan 485 """Adjust the modified flag.
604 jonathan 460
605 jonathan 485 mod -- if -1 set the modified flag to False, otherwise perform
606     an 'or' operation with the current value of the flag and
607     'mod'
608     """
609    
610     if mod == -1:
611     self.modified = False
612     else:
613     self.modified = mod or self.modified
614    
615 jonathan 415 def IsModified(self):
616 jonathan 460 """True if this table is considered modified."""
617 jonathan 415 return self.modified
618    
619 jonathan 451 def DeleteRows(self, pos, numRows = 1):
620 jonathan 485 """Deletes 'numRows' beginning at row 'pos'.
621 jonathan 460
622 jonathan 485 The row representing the default group is not removed.
623    
624     The table is considered modified if any rows are removed.
625 jonathan 460 """
626    
627 jonathan 606 assert pos >= 0
628 jonathan 615 old_len = self.GetNumberRows()
629 jonathan 451 for row in range(pos, pos - numRows, -1):
630 jonathan 485 group = self.GetClassGroup(row)
631 jonathan 615 if row != 0:
632     self.clazz.RemoveGroup(row - 1)
633 jonathan 451 self.__Modified()
634    
635     if self.IsModified():
636 jonathan 615 self.__NotifyRowChanges(old_len, self.GetNumberRows())
637 jonathan 415
638 jonathan 451 def AppendRows(self, numRows = 1):
639 jonathan 485 """Append 'numRows' empty rows to the end of the table.
640 jonathan 460
641 jonathan 485 The table is considered modified if any rows are appended.
642     """
643    
644 jonathan 615 old_len = self.GetNumberRows()
645 jonathan 451 for i in range(numRows):
646     np = ClassGroupSingleton()
647 jonathan 606 self.__SetRow(None, np)
648 jonathan 451
649     if self.IsModified():
650 jonathan 615 self.__NotifyRowChanges(old_len, self.GetNumberRows())
651 jonathan 451
652    
653 jonathan 650 ID_PROPERTY_OK = 4001
654     ID_PROPERTY_REVERT = 4002
655     ID_PROPERTY_ADD = 4003
656     ID_PROPERTY_GENCLASS = 4004
657     ID_PROPERTY_REMOVE = 4005
658     ID_PROPERTY_MOVEUP = 4006
659     ID_PROPERTY_MOVEDOWN = 4007
660     ID_PROPERTY_TRY = 4008
661     ID_PROPERTY_EDITSYM = 4009
662     ID_PROPERTY_CLOSE = 4010
663     ID_PROPERTY_SELECT = 4011
664     ID_PROPERTY_TITLE = 4012
665     ID_PROPERTY_FIELDTEXT = 4013
666 jonathan 630
667     BTN_ADD = 0
668     BTN_EDIT = 1
669     BTN_GEN = 2
670     BTN_UP = 3
671     BTN_DOWN = 4
672     BTN_RM = 5
673    
674 jonathan 650 EB_LAYER_TITLE = 0
675     EB_SELECT_FIELD = 1
676     EB_GEN_CLASS = 2
677    
678 jonathan 485 class Classifier(NonModalDialog):
679 bh 535
680 jonathan 630 type2string = {None: _("None"),
681     FIELDTYPE_STRING: _("Text"),
682     FIELDTYPE_INT: _("Integer"),
683     FIELDTYPE_DOUBLE: _("Decimal")}
684    
685 jonathan 570 def __init__(self, parent, name, layer, group = None):
686 jonathan 650 NonModalDialog.__init__(self, parent, name, "")
687 jonathan 372
688 jonathan 650 self.__SetTitle(layer.Title())
689 jonathan 509
690 jonathan 415 self.layer = layer
691    
692 jonathan 485 self.originalClass = self.layer.GetClassification()
693     field = self.originalClass.GetField()
694     fieldType = self.originalClass.GetFieldType()
695    
696 jonathan 630 self.genDlg = None
697    
698 jonathan 372 topBox = wxBoxSizer(wxVERTICAL)
699    
700 jonathan 650 sizer = wxBoxSizer(wxHORIZONTAL)
701     sizer.Add(wxStaticText(self, -1, _("Title: ")),
702     0, wxALIGN_LEFT | wxALL | wxALIGN_CENTER_VERTICAL, 4)
703     sizer.Add(wxTextCtrl(self, ID_PROPERTY_TITLE, layer.Title()),
704     1, wxGROW | wxALL, 4)
705     EVT_TEXT(self, ID_PROPERTY_TITLE, self._OnTitleChanged)
706    
707     topBox.Add(sizer, 0, wxGROW, 4)
708    
709     topBox.Add(wxStaticText(self, -1,
710     _("Type: %s") % layer.ShapeType()),
711 jonathan 451 0, wxALIGN_LEFT | wxALL, 4)
712 jonathan 415
713 jonathan 650 panel = wxPanel(self, -1, size=(100, 100))
714 jonathan 372
715 jonathan 650 #####################
716    
717     #panelBox = wxBoxSizer(wxVERTICAL)
718     panelBox = wxStaticBoxSizer(
719     wxStaticBox(panel, -1, _("Classification")), wxVERTICAL)
720    
721    
722 jonathan 485 #
723     # make field combo box
724     #
725 jonathan 509 self.fields = wxComboBox(panel, ID_PROPERTY_SELECT, "",
726 jonathan 372 style = wxCB_READONLY)
727    
728     self.num_cols = layer.table.field_count()
729 jonathan 441 # just assume the first field in case one hasn't been
730     # specified in the file.
731 jonathan 451 self.__cur_field = 0
732 jonathan 460
733     self.fields.Append("<None>")
734    
735 jonathan 650 if self.originalClass.GetFieldType() is None:
736     self.fields.SetClientData(0, copy.deepcopy(self.originalClass))
737     else:
738     self.fields.SetClientData(0, None)
739    
740 jonathan 372 for i in range(self.num_cols):
741     type, name, len, decc = layer.table.field_info(i)
742 jonathan 451 self.fields.Append(name)
743    
744 jonathan 415 if name == field:
745 jonathan 460 self.__cur_field = i + 1
746 jonathan 615 self.fields.SetClientData(i + 1,
747     copy.deepcopy(self.originalClass))
748 jonathan 451 else:
749 jonathan 460 self.fields.SetClientData(i + 1, None)
750 jonathan 372
751    
752 jonathan 509 ###########
753 jonathan 485
754 jonathan 650
755     sizer = wxBoxSizer(wxHORIZONTAL)
756     sizer.Add(wxStaticText(panel, ID_PROPERTY_FIELDTEXT, _("Field: ")),
757     0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 4)
758     sizer.Add(self.fields, 1, wxGROW | wxALL, 4)
759     EVT_COMBOBOX(self, ID_PROPERTY_SELECT, self._OnFieldSelect)
760    
761     panelBox.Add(sizer, 0, wxGROW, 4)
762    
763 jonathan 509 self.fieldTypeText = wxStaticText(panel, -1, "")
764 jonathan 560 panelBox.Add(self.fieldTypeText, 0,
765     wxGROW | wxALIGN_LEFT | wxALL | wxADJUST_MINSIZE, 4)
766 jonathan 485
767 jonathan 451
768 jonathan 372 #
769 jonathan 570 # Control Box
770 jonathan 372 #
771 jonathan 415 controlBox = wxBoxSizer(wxHORIZONTAL)
772 jonathan 485
773 jonathan 460
774 jonathan 509 ###########
775 jonathan 485 #
776     # Control buttons:
777     #
778 jonathan 509 controlButtonBox = wxBoxSizer(wxVERTICAL)
779    
780 jonathan 650 button = wxButton(panel, ID_PROPERTY_GENCLASS, _("Generate Class"))
781 jonathan 485 controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
782 jonathan 451
783 jonathan 650 button = wxButton(panel, ID_PROPERTY_ADD, _("Add"))
784 jonathan 570 controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
785 jonathan 485
786 jonathan 650 button = wxButton(panel, ID_PROPERTY_MOVEUP, _("Move Up"))
787 jonathan 606 controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
788    
789 jonathan 650 button = wxButton(panel, ID_PROPERTY_MOVEDOWN, _("Move Down"))
790 jonathan 485 controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
791    
792 jonathan 650 button = wxButton(panel, ID_PROPERTY_EDITSYM, _("Edit Symbol"))
793 jonathan 485 controlButtonBox.Add(button, 0, wxGROW | wxALL, 4)
794    
795     controlButtonBox.Add(60, 20, 0, wxGROW | wxALL | wxALIGN_BOTTOM, 4)
796    
797 jonathan 650 button = wxButton(panel, ID_PROPERTY_REMOVE, _("Remove"))
798 jonathan 485 controlButtonBox.Add(button, 0, wxGROW | wxALL | wxALIGN_BOTTOM, 4)
799    
800 jonathan 570
801     ###########
802     #
803     # Classification data table
804     #
805    
806 jonathan 606 self.classGrid = ClassGrid(panel, self)
807 jonathan 570
808     # calling __SelectField after creating the classGrid fills in the
809     # grid with the correct information
810 jonathan 650 self.fields.SetSelection(self.__cur_field)
811     self.__SelectField(self.__cur_field, group = group)
812 jonathan 570
813     controlBox.Add(self.classGrid, 1, wxGROW, 0)
814    
815 jonathan 415 controlBox.Add(controlButtonBox, 0, wxGROW, 10)
816 jonathan 511 panelBox.Add(controlBox, 1, wxGROW, 10)
817 jonathan 415
818 jonathan 650 EVT_BUTTON(self, ID_PROPERTY_ADD, self._OnAdd)
819     EVT_BUTTON(self, ID_PROPERTY_EDITSYM, self._OnEditSymbol)
820     EVT_BUTTON(self, ID_PROPERTY_REMOVE, self._OnRemove)
821     EVT_BUTTON(self, ID_PROPERTY_GENCLASS, self._OnGenClass)
822     EVT_BUTTON(self, ID_PROPERTY_MOVEUP, self._OnMoveUp)
823     EVT_BUTTON(self, ID_PROPERTY_MOVEDOWN, self._OnMoveDown)
824 jonathan 415
825 jonathan 509 ###########
826    
827 jonathan 650 panel.SetAutoLayout(True)
828     panel.SetSizer(panelBox)
829     panelBox.SetSizeHints(panel)
830    
831     topBox.Add(panel, 1, wxGROW | wxALL, 4)
832    
833     ###########
834    
835 jonathan 372 buttonBox = wxBoxSizer(wxHORIZONTAL)
836 jonathan 650 buttonBox.Add(wxButton(self, ID_PROPERTY_TRY, _("Try")),
837 jonathan 372 0, wxALL, 4)
838 jonathan 485 buttonBox.Add(60, 20, 0, wxALL, 4)
839 jonathan 650 buttonBox.Add(wxButton(self, ID_PROPERTY_REVERT, _("Revert")),
840 jonathan 485 0, wxALL, 4)
841     buttonBox.Add(60, 20, 0, wxALL, 4)
842 jonathan 650 buttonBox.Add(wxButton(self, ID_PROPERTY_OK, _("OK")),
843 jonathan 615 0, wxALL, 4)
844     buttonBox.Add(60, 20, 0, wxALL, 4)
845 jonathan 650 buttonBox.Add(wxButton(self, ID_PROPERTY_CLOSE, _("Close")),
846 jonathan 372 0, wxALL, 4)
847 jonathan 650 topBox.Add(buttonBox, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_BOTTOM, 0)
848 jonathan 372
849 jonathan 650 EVT_BUTTON(self, ID_PROPERTY_OK, self._OnOK)
850     EVT_BUTTON(self, ID_PROPERTY_TRY, self._OnTry)
851     EVT_BUTTON(self, ID_PROPERTY_CLOSE, self._OnCloseBtn)
852     EVT_BUTTON(self, ID_PROPERTY_REVERT, self._OnRevert)
853 jonathan 372
854 jonathan 509 ###########
855    
856 jonathan 650 topBox.SetSizeHints(self)
857 jonathan 509 self.SetAutoLayout(True)
858 jonathan 511 self.SetSizer(topBox)
859 jonathan 509
860 jonathan 570 #self.Fit()
861 jonathan 549 ######################
862    
863     self.haveApplied = False
864    
865 jonathan 638 def EditSymbol(self, row):
866 jonathan 606 table = self.classGrid.GetTable()
867     prop = table.GetValueAsCustom(row, COL_SYMBOL, None)
868    
869     # get a new ClassGroupProperties object and copy the
870     # values over to our current object
871     propDlg = SelectPropertiesDialog(NULL, prop, self.layer.ShapeType())
872 jonathan 630
873     self.Enable(False)
874 jonathan 606 if propDlg.ShowModal() == wxID_OK:
875     new_prop = propDlg.GetClassGroupProperties()
876     table.SetValueAsCustom(row, COL_SYMBOL, None, new_prop)
877 jonathan 630 self.Enable(True)
878 jonathan 606 propDlg.Destroy()
879    
880 jonathan 630 def _SetClassification(self, clazz):
881    
882     self.fields.SetClientData(self.__cur_field, clazz)
883     self.classGrid.GetTable().SetClassification(clazz)
884 jonathan 606
885 jonathan 615 def __BuildClassification(self, fieldIndex, copyClass = False):
886 jonathan 415
887 jonathan 615 # numRows = self.classGrid.GetNumberRows()
888     # assert numRows > 0 # there should always be a default row
889 jonathan 496
890 jonathan 615 # clazz = Classification()
891 jonathan 496 if fieldIndex == 0:
892     fieldName = None
893     fieldType = None
894     else:
895     fieldName = self.fields.GetString(fieldIndex)
896     fieldType = self.layer.GetFieldType(fieldName)
897 jonathan 415
898 jonathan 615 clazz = self.classGrid.GetTable().GetClassification()
899    
900     if copyClass:
901     clazz = copy.deepcopy(clazz)
902    
903 jonathan 460 clazz.SetField(fieldName)
904     clazz.SetFieldType(fieldType)
905    
906 jonathan 415
907 jonathan 615 # table = self.classGrid.GetTable()
908     # clazz.SetDefaultGroup(table.GetClassGroup(0))
909 jonathan 415
910 jonathan 615 # for i in range(1, numRows):
911     # clazz.AppendGroup(table.GetClassGroup(i))
912 jonathan 460
913 jonathan 415 return clazz
914    
915 jonathan 570 def __SetGridTable(self, fieldIndex, group = None):
916 jonathan 415
917 jonathan 460 clazz = self.fields.GetClientData(fieldIndex)
918 jonathan 415
919 jonathan 460 if clazz is None:
920     clazz = Classification()
921     clazz.SetDefaultGroup(
922     ClassGroupDefault(
923 jonathan 485 self.layer.GetClassification().
924     GetDefaultGroup().GetProperties()))
925 jonathan 460
926     fieldName = self.fields.GetString(fieldIndex)
927     fieldType = self.layer.GetFieldType(fieldName)
928     clazz.SetFieldType(fieldType)
929    
930 jonathan 570 self.classGrid.CreateTable(clazz, self.layer.ShapeType(), group)
931 jonathan 460
932 jonathan 485 def __SetFieldTypeText(self, fieldIndex):
933     fieldName = self.fields.GetString(fieldIndex)
934     fieldType = self.layer.GetFieldType(fieldName)
935    
936 jonathan 606 assert Classifier.type2string.has_key(fieldType)
937 jonathan 485
938 jonathan 496 text = Classifier.type2string[fieldType]
939    
940 jonathan 650 self.fieldTypeText.SetLabel(_("Data Type: %s") % text)
941 jonathan 485
942 jonathan 570 def __SelectField(self, newIndex, oldIndex = -1, group = None):
943 jonathan 611 """This method assumes that the current selection for the
944     combo has already been set by a call to SetSelection().
945     """
946 jonathan 460
947 jonathan 606 assert oldIndex >= -1
948 jonathan 415
949 jonathan 498 if oldIndex != -1:
950     clazz = self.__BuildClassification(oldIndex)
951     self.fields.SetClientData(oldIndex, clazz)
952 jonathan 485
953 jonathan 570 self.__SetGridTable(newIndex, group)
954 jonathan 498
955 jonathan 650 self.__EnableButtons(EB_SELECT_FIELD, newIndex != 0)
956 jonathan 498
957     self.__SetFieldTypeText(newIndex)
958 jonathan 485
959 jonathan 650 def __SetTitle(self, title):
960     if title != "":
961     title = ": " + title
962    
963     self.SetTitle(_("Layer Properties") + title)
964 jonathan 496
965 jonathan 638 def _OnEditSymbol(self, event):
966 jonathan 606 sel = self.classGrid.GetCurrentSelection()
967    
968     if len(sel) == 1:
969 jonathan 638 self.EditSymbol(sel[0])
970 jonathan 606
971 jonathan 496 def _OnFieldSelect(self, event):
972 jonathan 498 index = self.fields.GetSelection()
973     self.__SelectField(index, self.__cur_field)
974     self.__cur_field = index
975 jonathan 485
976 jonathan 638 def _OnTry(self, event):
977 jonathan 415 """Put the data from the table into a new Classification and hand
978     it to the layer.
979     """
980    
981 jonathan 460 clazz = self.fields.GetClientData(self.__cur_field)
982 jonathan 415
983     #
984     # only build the classification if there wasn't one to
985     # to begin with or it has been modified
986     #
987     if clazz is None or self.classGrid.GetTable().IsModified():
988 jonathan 615 clazz = self.__BuildClassification(self.__cur_field, True)
989 jonathan 415
990     self.layer.SetClassification(clazz)
991    
992 jonathan 549 self.haveApplied = True
993    
994 jonathan 485 def _OnOK(self, event):
995 jonathan 638 self._OnTry(event)
996 jonathan 615 self.Close()
997 jonathan 415
998 jonathan 615 def _OnCloseBtn(self, event):
999     """Close is similar to Cancel except that any changes that were
1000     made and applied remain applied, but the currently displayed
1001     classification is discarded.
1002     """
1003    
1004     self.Close()
1005    
1006 jonathan 638 def _OnRevert(self, event):
1007 jonathan 485 """The layer's current classification stays the same."""
1008 jonathan 549 if self.haveApplied:
1009     self.layer.SetClassification(self.originalClass)
1010    
1011 jonathan 638 #self.Close()
1012 jonathan 415
1013 jonathan 460 def _OnAdd(self, event):
1014 jonathan 451 self.classGrid.AppendRows()
1015 jonathan 415
1016 jonathan 460 def _OnRemove(self, event):
1017 jonathan 451 self.classGrid.DeleteSelectedRows()
1018    
1019 jonathan 606 def _OnGenClass(self, event):
1020 jonathan 415
1021 jonathan 630 self.genDlg = ClassGenDialog(self, self.layer,
1022     self.fields.GetString(self.__cur_field))
1023 jonathan 606
1024 jonathan 630 EVT_CLOSE(self.genDlg, self._OnGenDialogClose)
1025 jonathan 606
1026 jonathan 650 self.__EnableButtons(EB_GEN_CLASS, False)
1027 jonathan 630
1028     self.genDlg.Show()
1029    
1030     def _OnGenDialogClose(self, event):
1031     self.genDlg.Destroy()
1032 jonathan 650 self.__EnableButtons(EB_GEN_CLASS, True)
1033 jonathan 630
1034 jonathan 460 def _OnMoveUp(self, event):
1035     sel = self.classGrid.GetCurrentSelection()
1036 jonathan 415
1037 jonathan 460 if len(sel) == 1:
1038     i = sel[0]
1039     if i > 1:
1040     table = self.classGrid.GetTable()
1041     x = table.GetClassGroup(i - 1)
1042     y = table.GetClassGroup(i)
1043     table.SetClassGroup(i - 1, y)
1044     table.SetClassGroup(i, x)
1045     self.classGrid.ClearSelection()
1046     self.classGrid.SelectRow(i - 1)
1047 jonathan 570 self.classGrid.MakeCellVisible(i - 1, 0)
1048 jonathan 460
1049     def _OnMoveDown(self, event):
1050     sel = self.classGrid.GetCurrentSelection()
1051    
1052     if len(sel) == 1:
1053     i = sel[0]
1054     table = self.classGrid.GetTable()
1055     if 0 < i < table.GetNumberRows() - 1:
1056     x = table.GetClassGroup(i)
1057     y = table.GetClassGroup(i + 1)
1058     table.SetClassGroup(i, y)
1059     table.SetClassGroup(i + 1, x)
1060     self.classGrid.ClearSelection()
1061     self.classGrid.SelectRow(i + 1)
1062 jonathan 570 self.classGrid.MakeCellVisible(i + 1, 0)
1063 jonathan 460
1064 jonathan 650 def _OnTitleChanged(self, event):
1065     obj = event.GetEventObject()
1066 jonathan 460
1067 jonathan 650 self.layer.SetTitle(obj.GetValue())
1068     self.__SetTitle(self.layer.Title())
1069    
1070     self.__EnableButtons(EB_LAYER_TITLE, self.layer.Title() != "")
1071    
1072     def __EnableButtons(self, case, enable):
1073    
1074     if case == EB_LAYER_TITLE:
1075     list = (ID_PROPERTY_OK,
1076     ID_PROPERTY_CLOSE)
1077    
1078     elif case == EB_SELECT_FIELD:
1079     list = (ID_PROPERTY_GENCLASS,
1080     ID_PROPERTY_ADD,
1081     ID_PROPERTY_MOVEUP,
1082     ID_PROPERTY_MOVEDOWN,
1083     ID_PROPERTY_EDITSYM,
1084     ID_PROPERTY_REMOVE)
1085    
1086     elif case == EB_GEN_CLASS:
1087     list = (ID_PROPERTY_SELECT,
1088     ID_PROPERTY_FIELDTEXT,
1089     ID_PROPERTY_GENCLASS,
1090     ID_PROPERTY_EDITSYM)
1091    
1092     for id in list:
1093     self.FindWindowById(id).Enable(enable)
1094    
1095 jonathan 415 ID_SELPROP_OK = 4001
1096     ID_SELPROP_CANCEL = 4002
1097     ID_SELPROP_SPINCTRL = 4002
1098 jonathan 430 ID_SELPROP_PREVIEW = 4003
1099     ID_SELPROP_STROKECLR = 4004
1100     ID_SELPROP_FILLCLR = 4005
1101 jonathan 485 ID_SELPROP_STROKECLRTRANS = 4006
1102     ID_SELPROP_FILLCLRTRANS = 4007
1103 jonathan 415
1104     class SelectPropertiesDialog(wxDialog):
1105    
1106     def __init__(self, parent, prop, shapeType):
1107     wxDialog.__init__(self, parent, -1, _("Select Properties"),
1108 jonathan 507 style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
1109 jonathan 415
1110 jonathan 441 self.prop = ClassGroupProperties(prop)
1111 jonathan 415
1112 jonathan 430 topBox = wxBoxSizer(wxVERTICAL)
1113 jonathan 415
1114 jonathan 430 itemBox = wxBoxSizer(wxHORIZONTAL)
1115    
1116     # preview box
1117     previewBox = wxBoxSizer(wxVERTICAL)
1118     previewBox.Add(wxStaticText(self, -1, _("Preview:")),
1119     0, wxALIGN_LEFT | wxALL, 4)
1120 jonathan 630
1121     self.previewWin = ClassGroupPropertiesCtrl(
1122     self, ID_SELPROP_PREVIEW, self.prop, shapeType,
1123     (40, 40), wxSIMPLE_BORDER)
1124    
1125     self.previewWin.AllowEdit(False)
1126    
1127 jonathan 615 previewBox.Add(self.previewWin, 1, wxGROW | wxALL, 4)
1128 jonathan 430
1129     itemBox.Add(previewBox, 1, wxALIGN_LEFT | wxALL | wxGROW, 0)
1130    
1131     # control box
1132     ctrlBox = wxBoxSizer(wxVERTICAL)
1133 jonathan 485
1134     lineColorBox = wxBoxSizer(wxHORIZONTAL)
1135     lineColorBox.Add(
1136 jonathan 500 wxButton(self, ID_SELPROP_STROKECLR, _("Change Line Color")),
1137 jonathan 485 1, wxALL | wxGROW, 4)
1138 jonathan 460 EVT_BUTTON(self, ID_SELPROP_STROKECLR, self._OnChangeLineColor)
1139 jonathan 430
1140 jonathan 485 lineColorBox.Add(
1141 jonathan 500 wxButton(self, ID_SELPROP_STROKECLRTRANS, _("Transparent")),
1142 jonathan 485 1, wxALL | wxGROW, 4)
1143     EVT_BUTTON(self, ID_SELPROP_STROKECLRTRANS,
1144     self._OnChangeLineColorTrans)
1145    
1146     ctrlBox.Add(lineColorBox, 0,
1147     wxALIGN_CENTER_HORIZONTAL | wxALL | wxGROW, 4)
1148    
1149 jonathan 430 if shapeType != SHAPETYPE_ARC:
1150 jonathan 485 fillColorBox = wxBoxSizer(wxHORIZONTAL)
1151     fillColorBox.Add(
1152 jonathan 500 wxButton(self, ID_SELPROP_FILLCLR, _("Change Fill Color")),
1153 jonathan 485 1, wxALL | wxGROW, 4)
1154 jonathan 460 EVT_BUTTON(self, ID_SELPROP_FILLCLR, self._OnChangeFillColor)
1155 jonathan 485 fillColorBox.Add(
1156 jonathan 500 wxButton(self, ID_SELPROP_FILLCLRTRANS, _("Transparent")),
1157 jonathan 485 1, wxALL | wxGROW, 4)
1158     EVT_BUTTON(self, ID_SELPROP_FILLCLRTRANS,
1159     self._OnChangeFillColorTrans)
1160     ctrlBox.Add(fillColorBox, 0,
1161     wxALIGN_CENTER_HORIZONTAL | wxALL | wxGROW, 4)
1162 jonathan 430
1163 jonathan 415 spinBox = wxBoxSizer(wxHORIZONTAL)
1164 jonathan 460 spinBox.Add(wxStaticText(self, -1, _("Line Width: ")),
1165 jonathan 430 0, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 4)
1166 jonathan 415 self.spinCtrl = wxSpinCtrl(self, ID_SELPROP_SPINCTRL,
1167     min=1, max=10,
1168 jonathan 460 value=str(prop.GetLineWidth()),
1169     initial=prop.GetLineWidth())
1170 jonathan 415
1171 jonathan 460 EVT_SPINCTRL(self, ID_SELPROP_SPINCTRL, self._OnSpin)
1172 jonathan 415
1173     spinBox.Add(self.spinCtrl, 0, wxALIGN_LEFT | wxALL, 4)
1174    
1175 jonathan 430 ctrlBox.Add(spinBox, 0, wxALIGN_RIGHT | wxALL, 0)
1176     itemBox.Add(ctrlBox, 0, wxALIGN_RIGHT | wxALL | wxGROW, 0)
1177     topBox.Add(itemBox, 1, wxALIGN_LEFT | wxALL | wxGROW, 0)
1178 jonathan 415
1179     #
1180     # Control buttons:
1181     #
1182     buttonBox = wxBoxSizer(wxHORIZONTAL)
1183 jonathan 485 buttonBox.Add(wxButton(self, ID_SELPROP_OK, _("OK")),
1184 jonathan 415 0, wxALL, 4)
1185 jonathan 485 buttonBox.Add(wxButton(self, ID_SELPROP_CANCEL, _("Cancel")),
1186 jonathan 415 0, wxALL, 4)
1187     topBox.Add(buttonBox, 0, wxALIGN_CENTER_HORIZONTAL|wxALIGN_BOTTOM, 10)
1188    
1189 jonathan 460 EVT_BUTTON(self, ID_SELPROP_OK, self._OnOK)
1190     EVT_BUTTON(self, ID_SELPROP_CANCEL, self._OnCancel)
1191 jonathan 415
1192 jonathan 509 self.SetAutoLayout(True)
1193 jonathan 415 self.SetSizer(topBox)
1194     topBox.Fit(self)
1195     topBox.SetSizeHints(self)
1196    
1197 jonathan 460 def _OnOK(self, event):
1198 jonathan 372 self.EndModal(wxID_OK)
1199    
1200 jonathan 460 def _OnCancel(self, event):
1201 jonathan 372 self.EndModal(wxID_CANCEL)
1202    
1203 jonathan 460 def _OnSpin(self, event):
1204     self.prop.SetLineWidth(self.spinCtrl.GetValue())
1205 jonathan 549 self.previewWin.Refresh()
1206 jonathan 392
1207 jonathan 430 def __GetColor(self, cur):
1208     dialog = wxColourDialog(self)
1209 jonathan 610 if cur is not Color.Transparent:
1210 jonathan 606 dialog.GetColourData().SetColour(Color2wxColour(cur))
1211    
1212 jonathan 430 ret = None
1213     if dialog.ShowModal() == wxID_OK:
1214     ret = wxColour2Color(dialog.GetColourData().GetColour())
1215    
1216     dialog.Destroy()
1217    
1218     return ret
1219    
1220 jonathan 460 def _OnChangeLineColor(self, event):
1221     clr = self.__GetColor(self.prop.GetLineColor())
1222 jonathan 430 if clr is not None:
1223 jonathan 460 self.prop.SetLineColor(clr)
1224 jonathan 549 self.previewWin.Refresh() # XXX: work around, see ClassDataPreviewer
1225 jonathan 430
1226 jonathan 485 def _OnChangeLineColorTrans(self, event):
1227 jonathan 610 self.prop.SetLineColor(Color.Transparent)
1228 jonathan 549 self.previewWin.Refresh() # XXX: work around, see ClassDataPreviewer
1229 jonathan 485
1230 jonathan 460 def _OnChangeFillColor(self, event):
1231 jonathan 430 clr = self.__GetColor(self.prop.GetFill())
1232     if clr is not None:
1233     self.prop.SetFill(clr)
1234 jonathan 549 self.previewWin.Refresh() # XXX: work around, see ClassDataPreviewer
1235 jonathan 430
1236 jonathan 485 def _OnChangeFillColorTrans(self, event):
1237 jonathan 610 self.prop.SetFill(Color.Transparent)
1238 jonathan 549 self.previewWin.Refresh() # XXX: work around, see ClassDataPreviewer
1239 jonathan 485
1240 jonathan 441 def GetClassGroupProperties(self):
1241 jonathan 415 return self.prop
1242 jonathan 392
1243    
1244 jonathan 549 class ClassDataPreviewWindow(wxWindow):
1245 jonathan 415
1246 jonathan 441 def __init__(self, rect, prop, shapeType,
1247 jonathan 430 parent = None, id = -1, size = wxDefaultSize):
1248     if parent is not None:
1249 jonathan 549 wxWindow.__init__(self, parent, id, (0, 0), size)
1250 jonathan 460 EVT_PAINT(self, self._OnPaint)
1251 jonathan 415
1252 jonathan 430 self.rect = rect
1253 jonathan 549
1254 jonathan 441 self.prop = prop
1255 jonathan 430 self.shapeType = shapeType
1256 jonathan 549 self.previewer = ClassDataPreviewer()
1257 jonathan 430
1258 jonathan 630 def GetProperties():
1259     return self.prop
1260    
1261 jonathan 460 def _OnPaint(self, event):
1262 jonathan 430 dc = wxPaintDC(self)
1263    
1264     # XXX: this doesn't seem to be having an effect:
1265     dc.DestroyClippingRegion()
1266    
1267 jonathan 549 if self.rect is None:
1268     w, h = self.GetSize()
1269     rect = wxRect(0, 0, w, h)
1270     else:
1271     rect = self.rect
1272 jonathan 430
1273 jonathan 549 self.previewer.Draw(dc, rect, self.prop, self.shapeType)
1274 jonathan 430
1275 jonathan 549 class ClassDataPreviewer:
1276 jonathan 430
1277 jonathan 549 def Draw(self, dc, rect, prop, shapeType):
1278    
1279 jonathan 606 assert dc is not None
1280     assert isinstance(prop, ClassGroupProperties)
1281 jonathan 549
1282 jonathan 430 if rect is None:
1283 jonathan 549 x = 0
1284     y = 0
1285     w, h = dc.GetSize()
1286 jonathan 430 else:
1287     x = rect.GetX()
1288     y = rect.GetY()
1289     w = rect.GetWidth()
1290     h = rect.GetHeight()
1291    
1292 jonathan 460 stroke = prop.GetLineColor()
1293 jonathan 610 if stroke is Color.Transparent:
1294 jonathan 392 pen = wxTRANSPARENT_PEN
1295     else:
1296 jonathan 430 pen = wxPen(Color2wxColour(stroke),
1297 jonathan 460 prop.GetLineWidth(),
1298 jonathan 392 wxSOLID)
1299    
1300 jonathan 441 stroke = prop.GetFill()
1301 jonathan 610 if stroke is Color.Transparent:
1302 jonathan 392 brush = wxTRANSPARENT_BRUSH
1303     else:
1304 jonathan 430 brush = wxBrush(Color2wxColour(stroke), wxSOLID)
1305 jonathan 392
1306     dc.SetPen(pen)
1307     dc.SetBrush(brush)
1308    
1309 jonathan 415 if shapeType == SHAPETYPE_ARC:
1310 jonathan 430 dc.DrawSpline([wxPoint(x, y + h),
1311     wxPoint(x + w/2, y + h/4),
1312     wxPoint(x + w/2, y + h/4*3),
1313     wxPoint(x + w, y)])
1314 jonathan 392
1315 jonathan 576 elif shapeType == SHAPETYPE_POINT:
1316 jonathan 415
1317 jonathan 430 dc.DrawCircle(x + w/2, y + h/2,
1318 jonathan 460 (min(w, h) - prop.GetLineWidth())/2)
1319 jonathan 392
1320 jonathan 576 elif shapeType == SHAPETYPE_POLYGON:
1321     dc.DrawRectangle(x, y, w, h)
1322    
1323 jonathan 415 class ClassRenderer(wxPyGridCellRenderer):
1324    
1325     def __init__(self, shapeType):
1326     wxPyGridCellRenderer.__init__(self)
1327 jonathan 549 self.shapeType = shapeType
1328     self.previewer = ClassDataPreviewer()
1329 jonathan 415
1330     def Draw(self, grid, attr, dc, rect, row, col, isSelected):
1331 jonathan 485 data = grid.GetTable().GetClassGroup(row)
1332 jonathan 415
1333     dc.SetClippingRegion(rect.GetX(), rect.GetY(),
1334     rect.GetWidth(), rect.GetHeight())
1335     dc.SetPen(wxPen(wxLIGHT_GREY))
1336     dc.SetBrush(wxBrush(wxLIGHT_GREY, wxSOLID))
1337     dc.DrawRectangle(rect.GetX(), rect.GetY(),
1338     rect.GetWidth(), rect.GetHeight())
1339    
1340 jonathan 441 if not isinstance(data, ClassGroupMap):
1341 jonathan 549 self.previewer.Draw(dc, rect, data.GetProperties(), self.shapeType)
1342 jonathan 415
1343     if isSelected:
1344 jonathan 615 dc.SetPen(wxPen(wxBLACK, 1, wxSOLID))
1345 jonathan 415 dc.SetBrush(wxTRANSPARENT_BRUSH)
1346 jonathan 615
1347 jonathan 415 dc.DrawRectangle(rect.GetX(), rect.GetY(),
1348     rect.GetWidth(), rect.GetHeight())
1349    
1350 jonathan 392 dc.DestroyClippingRegion()
1351    
1352 jonathan 630
1353     class ClassGroupPropertiesCtrl(wxWindow, wxControl):
1354    
1355     def __init__(self, parent, id, props, shapeType,
1356     size = wxDefaultSize, style = 0):
1357    
1358     wxWindow.__init__(self, parent, id, size = size, style = style)
1359    
1360     self.SetProperties(props)
1361     self.SetShapeType(shapeType)
1362     self.AllowEdit(True)
1363    
1364     EVT_PAINT(self, self._OnPaint)
1365     EVT_LEFT_DCLICK(self, self._OnLeftDClick)
1366    
1367     self.previewer = ClassDataPreviewer()
1368    
1369     def _OnPaint(self, event):
1370     dc = wxPaintDC(self)
1371    
1372     # XXX: this doesn't seem to be having an effect:
1373     dc.DestroyClippingRegion()
1374    
1375     w, h = self.GetClientSize()
1376    
1377     self.previewer.Draw(dc,
1378     wxRect(0, 0, w, h),
1379     self.GetProperties(),
1380     self.GetShapeType())
1381    
1382    
1383     def GetProperties(self):
1384     return self.props
1385    
1386     def SetProperties(self, props):
1387     self.props = props
1388     self.Refresh()
1389    
1390     def GetShapeType(self):
1391     return self.shapeType
1392    
1393     def SetShapeType(self, shapeType):
1394     self.shapeType = shapeType
1395     self.Refresh()
1396    
1397     def AllowEdit(self, allow):
1398     self.allowEdit = allow
1399    
1400     def DoEdit(self):
1401     if not self.allowEdit: return
1402    
1403     propDlg = SelectPropertiesDialog(NULL,
1404     self.GetProperties(),
1405     self.GetShapeType())
1406    
1407     if propDlg.ShowModal() == wxID_OK:
1408     new_prop = propDlg.GetClassGroupProperties()
1409     self.SetProperties(new_prop)
1410     self.Refresh()
1411    
1412     propDlg.Destroy()
1413    
1414     def _OnLeftDClick(self, event):
1415     self.DoEdit()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26