7 |
|
|
8 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
9 |
|
|
10 |
|
import os.path |
11 |
|
|
12 |
from Thuban import _ |
from Thuban import _ |
13 |
|
|
14 |
from wxPython.wx import * |
from wxPython.wx import * |
16 |
|
|
17 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
18 |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
19 |
FIELDTYPE_STRING |
FIELDTYPE_STRING, table_to_dbf, table_to_csv |
20 |
import view |
import view |
21 |
from dialogs import NonModalDialog |
from dialogs import NonModalNonParentDialog |
22 |
from messages import SHAPES_SELECTED |
|
23 |
|
from messages import SHAPES_SELECTED, SESSION_REPLACED |
24 |
|
from Thuban.Model.messages import TABLE_REMOVED |
25 |
|
|
26 |
wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER, |
wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER, |
27 |
FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT, |
FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT, |
102 |
return self.CanGetValueAs(row, col, typeName) |
return self.CanGetValueAs(row, col, typeName) |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
class NullRenderer(wxPyGridCellRenderer): |
107 |
|
|
108 |
|
"""Renderer that draws NULL as a gray rectangle |
109 |
|
|
110 |
|
Other values are delegated to a normal renderer which is given as |
111 |
|
the parameter to the constructor. |
112 |
|
""" |
113 |
|
|
114 |
|
def __init__(self, non_null_renderer): |
115 |
|
wxPyGridCellRenderer.__init__(self) |
116 |
|
self.non_null_renderer = non_null_renderer |
117 |
|
|
118 |
|
def Draw(self, grid, attr, dc, rect, row, col, isSelected): |
119 |
|
value = grid.table.GetValue(row, col) |
120 |
|
if value is None: |
121 |
|
dc.SetBackgroundMode(wxSOLID) |
122 |
|
dc.SetBrush(wxBrush(wxColour(192, 192, 192), wxSOLID)) |
123 |
|
dc.SetPen(wxTRANSPARENT_PEN) |
124 |
|
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height) |
125 |
|
else: |
126 |
|
self.non_null_renderer.Draw(grid, attr, dc, rect, row, col, |
127 |
|
isSelected) |
128 |
|
|
129 |
|
def GetBestSize(self, grid, attr, dc, row, col): |
130 |
|
self.non_null_renderer.GetBestSize(grid, attr, dc, row, col) |
131 |
|
|
132 |
|
def Clone(self): |
133 |
|
return NullRenderer(self.non_null_renderer) |
134 |
|
|
135 |
|
|
136 |
class TableGrid(wxGrid, Publisher): |
class TableGrid(wxGrid, Publisher): |
137 |
|
|
138 |
"""A grid view for a Thuban table |
"""A grid view for a Thuban table |
156 |
# of the table and will destroy it when done. Otherwise you |
# of the table and will destroy it when done. Otherwise you |
157 |
# would need to keep a reference to it and call its Destroy |
# would need to keep a reference to it and call its Destroy |
158 |
# method later. |
# method later. |
159 |
self.SetTable(self.table, true) |
self.SetTable(self.table, True) |
160 |
|
|
161 |
#self.SetMargins(0,0) |
#self.SetMargins(0,0) |
162 |
|
|
164 |
# column widths automatically but it would cause a traversal of |
# column widths automatically but it would cause a traversal of |
165 |
# the entire table which for large .dbf files can take a very |
# the entire table which for large .dbf files can take a very |
166 |
# long time. |
# long time. |
167 |
#self.AutoSizeColumns(false) |
#self.AutoSizeColumns(False) |
168 |
|
|
169 |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
170 |
|
|
|
#EVT_GRID_RANGE_SELECT(self, None) |
|
|
#EVT_GRID_SELECT_CELL(self, None) |
|
|
#EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
|
|
#EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
|
|
|
|
171 |
self.ToggleEventListeners(True) |
self.ToggleEventListeners(True) |
172 |
|
|
173 |
|
# Replace the normal renderers with our own versions which |
174 |
|
# render NULL/None values specially |
175 |
|
self.RegisterDataType(wxGRID_VALUE_STRING, |
176 |
|
NullRenderer(wxGridCellStringRenderer()), None) |
177 |
|
self.RegisterDataType(wxGRID_VALUE_NUMBER, |
178 |
|
NullRenderer(wxGridCellNumberRenderer()), None) |
179 |
|
self.RegisterDataType(wxGRID_VALUE_FLOAT, |
180 |
|
NullRenderer(wxGridCellFloatRenderer()), None) |
181 |
|
|
182 |
def SetTableObject(self, table): |
def SetTableObject(self, table): |
183 |
self.table.SetTable(table) |
self.table.SetTable(table) |
184 |
|
|
273 |
self.allow_messages() |
self.allow_messages() |
274 |
|
|
275 |
|
|
276 |
class TableFrame(NonModalDialog): |
class TableFrame(NonModalNonParentDialog): |
277 |
|
|
278 |
"""Frame that displays a Thuban table in a grid view""" |
"""Frame that displays a Thuban table in a grid view""" |
279 |
|
|
280 |
def __init__(self, parent, name, title, table): |
def __init__(self, parent, name, title, table): |
281 |
NonModalDialog.__init__(self, parent, name, title) |
NonModalNonParentDialog.__init__(self, parent, name, title) |
282 |
self.table = table |
self.table = table |
283 |
self.grid = self.make_grid(self.table) |
self.grid = self.make_grid(self.table) |
284 |
|
self.app = self.parent.application |
285 |
|
self.app.Subscribe(SESSION_REPLACED, self.close_on_session_replaced) |
286 |
|
self.session = self.app.Session() |
287 |
|
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
288 |
|
|
289 |
def make_grid(self, table): |
def make_grid(self, table): |
290 |
"""Return the table grid to use in the frame. |
"""Return the table grid to use in the frame. |
294 |
""" |
""" |
295 |
return TableGrid(self, table) |
return TableGrid(self, table) |
296 |
|
|
297 |
|
def OnClose(self, event): |
298 |
|
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
299 |
|
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
300 |
|
NonModalNonParentDialog.OnClose(self, event) |
301 |
|
|
302 |
|
def close_on_session_replaced(self, *args): |
303 |
|
"""Subscriber for the SESSION_REPLACED messages. |
304 |
|
|
305 |
|
The table frame is tied to a session so close the window when |
306 |
|
the session changes. |
307 |
|
""" |
308 |
|
self.Close() |
309 |
|
|
310 |
|
def close_on_table_removed(self, table): |
311 |
|
"""Subscriber for the TABLE_REMOVED messages. |
312 |
|
|
313 |
|
The table frame is tied to a particular table so close the |
314 |
|
window when the table is removed. |
315 |
|
""" |
316 |
|
if table is self.table: |
317 |
|
self.Close() |
318 |
|
|
319 |
|
|
320 |
ID_QUERY = 4001 |
ID_QUERY = 4001 |
321 |
ID_SAVEAS = 4002 |
ID_EXPORT = 4002 |
322 |
|
|
323 |
class LayerTableFrame(TableFrame): |
class QueryTableFrame(TableFrame): |
324 |
|
|
325 |
"""Frame that displays a layer table in a grid view |
"""Frame that displays a table in a grid view and offers user actions |
326 |
|
selection and export |
327 |
|
|
328 |
A LayerTableFrame is TableFrame whose selection is connected to the |
A QueryTableFrame is TableFrame whose selection is connected to the |
329 |
selected object in a map. |
selected object in a map. |
330 |
""" |
""" |
331 |
|
|
332 |
def __init__(self, parent, name, title, layer, table): |
def __init__(self, parent, name, title, table): |
333 |
TableFrame.__init__(self, parent, name, title, table) |
TableFrame.__init__(self, parent, name, title, table) |
|
self.layer = layer |
|
|
self.grid.Subscribe(ROW_SELECTED, self.rows_selected) |
|
|
self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes) |
|
334 |
|
|
335 |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
336 |
self.choice_comp = wxChoice(self, -1, |
self.choice_comp = wxChoice(self, -1, |
342 |
_("Add to Selection")]) |
_("Add to Selection")]) |
343 |
|
|
344 |
button_query = wxButton(self, ID_QUERY, _("Query")) |
button_query = wxButton(self, ID_QUERY, _("Query")) |
345 |
button_saveas = wxButton(self, ID_SAVEAS, _("Export")) |
button_saveas = wxButton(self, ID_EXPORT, _("Export")) |
346 |
|
|
347 |
self.grid.SetSize((400, 200)) |
self.grid.SetSize((400, 200)) |
348 |
|
|
358 |
|
|
359 |
topBox = wxBoxSizer(wxVERTICAL) |
topBox = wxBoxSizer(wxVERTICAL) |
360 |
|
|
361 |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, _("Selections")), |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, |
362 |
|
_("Selection")), |
363 |
wxHORIZONTAL) |
wxHORIZONTAL) |
364 |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
365 |
sizer.Add(self.choice_comp, 0, wxALL, 4) |
sizer.Add(self.choice_comp, 0, wxALL, 4) |
379 |
|
|
380 |
self.grid.SetFocus() |
self.grid.SetFocus() |
381 |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
382 |
EVT_BUTTON(self, ID_SAVEAS, self.OnSaveAs) |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
383 |
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
384 |
|
|
385 |
def OnKeyDown(self, event): |
def OnKeyDown(self, event): |
386 |
"""Catch query key from grid""" |
"""Catch query key from grid""" |
|
print "In OnKeyDown" |
|
387 |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
|
print "Got the Key!" |
|
388 |
self.combo_fields.SetFocus() |
self.combo_fields.SetFocus() |
389 |
self.combo_fields.refocus = True |
self.combo_fields.refocus = True |
390 |
else: |
else: |
393 |
|
|
394 |
def OnQuery(self, event): |
def OnQuery(self, event): |
395 |
wxBeginBusyCursor() |
wxBeginBusyCursor() |
396 |
|
try: |
397 |
|
|
398 |
if self.combo_value.GetSelection() < 1: |
if self.combo_value.GetSelection() < 1: |
399 |
value = self.combo_value.GetValue() |
value = self.combo_value.GetValue() |
|
else: |
|
|
value = self.table.Column(self.combo_value.GetValue()) |
|
|
|
|
|
ids = self.table.SimpleQuery( |
|
|
self.table.Column(self.combo_fields.GetStringSelection()), |
|
|
self.choice_comp.GetStringSelection(), |
|
|
value) |
|
|
|
|
|
choice = self.choice_action.GetSelection() |
|
|
|
|
|
# |
|
|
# what used to be nice code got became a bit ugly because |
|
|
# each time we select a row a message is sent to the grid |
|
|
# which we are listening for and then we send further |
|
|
# messages. |
|
|
# |
|
|
# now, we disable those listeners select everything but |
|
|
# the first item, reenable the listeners, and select |
|
|
# the first element, which causes everything to be |
|
|
# updated properly. |
|
|
# |
|
|
self.grid.ToggleEventListeners(False) |
|
|
|
|
|
if choice == 0: |
|
|
# Replace Selection |
|
|
self.grid.ClearSelection() |
|
|
elif choice == 1: |
|
|
# Refine Selection |
|
|
sel = dict([(i, 0) for i in self.parent.SelectedShapes()]) |
|
|
self.grid.ClearSelection() |
|
|
ids = filter(sel.has_key, ids) |
|
|
elif choice == 2: |
|
|
# Add to Selection |
|
|
pass |
|
|
|
|
|
# |
|
|
# select the rows (all but the first) |
|
|
# |
|
|
firsttime = True |
|
|
for id in ids: |
|
|
if firsttime: |
|
|
firsttime = False |
|
400 |
else: |
else: |
401 |
self.grid.SelectRow(id, True) |
value = self.table.Column(self.combo_value.GetValue()) |
|
|
|
|
self.grid.ToggleEventListeners(True) |
|
402 |
|
|
403 |
# |
ids = self.table.SimpleQuery( |
404 |
# select the first row |
self.table.Column(self.combo_fields.GetStringSelection()), |
405 |
# |
self.choice_comp.GetStringSelection(), |
406 |
if ids: |
value) |
|
self.grid.SelectRow(ids[0], True) |
|
407 |
|
|
408 |
wxEndBusyCursor() |
choice = self.choice_action.GetSelection() |
409 |
|
|
410 |
|
# |
411 |
|
# what used to be nice code got became a bit ugly because |
412 |
|
# each time we select a row a message is sent to the grid |
413 |
|
# which we are listening for and then we send further |
414 |
|
# messages. |
415 |
|
# |
416 |
|
# now, we disable those listeners select everything but |
417 |
|
# the first item, reenable the listeners, and select |
418 |
|
# the first element, which causes everything to be |
419 |
|
# updated properly. |
420 |
|
# |
421 |
|
self.grid.ToggleEventListeners(False) |
422 |
|
|
423 |
|
if choice == 0: |
424 |
|
# Replace Selection |
425 |
|
self.grid.ClearSelection() |
426 |
|
elif choice == 1: |
427 |
|
# Refine Selection |
428 |
|
sel = self.get_selected() |
429 |
|
self.grid.ClearSelection() |
430 |
|
ids = filter(sel.has_key, ids) |
431 |
|
elif choice == 2: |
432 |
|
# Add to Selection |
433 |
|
pass |
434 |
|
|
435 |
|
# |
436 |
|
# select the rows (all but the first) |
437 |
|
# |
438 |
|
firsttime = True |
439 |
|
for id in ids: |
440 |
|
if firsttime: |
441 |
|
firsttime = False |
442 |
|
else: |
443 |
|
self.grid.SelectRow(id, True) |
444 |
|
|
445 |
|
self.grid.ToggleEventListeners(True) |
446 |
|
|
447 |
|
# |
448 |
|
# select the first row |
449 |
|
# |
450 |
|
if ids: |
451 |
|
self.grid.SelectRow(ids[0], True) |
452 |
|
finally: |
453 |
|
wxEndBusyCursor() |
454 |
|
|
455 |
def OnSaveAs(self, event): |
def OnSaveAs(self, event): |
456 |
dlg = wxFileDialog(self, _("Save Table As"), ".", "", |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
457 |
"DBF Files (*.dbf)|*.dbf|" + |
_("DBF Files (*.dbf)|*.dbf|") + |
458 |
"CSV Files (*.csv)|*.csv|" + |
_("CSV Files (*.csv)|*.csv|") + |
459 |
"All Files (*.*)|*.*", |
_("All Files (*.*)|*.*"), |
460 |
wxSAVE|wxOVERWRITE_PROMPT) |
wxSAVE|wxOVERWRITE_PROMPT) |
461 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
462 |
pass |
filename = dlg.GetPath() |
463 |
|
type = os.path.basename(filename).split('.')[-1:][0] |
464 |
dlg.Destroy() |
dlg.Destroy() |
465 |
|
if type.upper() == "DBF": |
466 |
|
table_to_dbf(self.table, filename) |
467 |
|
elif type.upper() == 'CSV': |
468 |
|
table_to_csv(self.table, filename) |
469 |
|
else: |
470 |
|
dlg = wxMessageDialog(None, "Unsupported format: %s" % type, |
471 |
|
"Table Export", wxOK|wxICON_WARNING) |
472 |
|
dlg.ShowModal() |
473 |
|
dlg.Destroy() |
474 |
|
else: |
475 |
|
dlg.Destroy() |
476 |
|
|
477 |
|
def OnClose(self, event): |
478 |
|
TableFrame.OnClose(self, event) |
479 |
|
|
480 |
|
def get_selected(self): |
481 |
|
"""Return a dictionary of the selected rows. |
482 |
|
|
483 |
|
The dictionary has sthe indexes as keys.""" |
484 |
|
return dict([(i, 0) for i in self.grid.GetSelectedRows()]) |
485 |
|
|
486 |
|
class LayerTableFrame(QueryTableFrame): |
487 |
|
|
488 |
|
"""Frame that displays a layer table in a grid view |
489 |
|
|
490 |
|
A LayerTableFrame is a QueryTableFrame whose selection is connected to the |
491 |
|
selected object in a map. |
492 |
|
""" |
493 |
|
|
494 |
|
def __init__(self, parent, name, title, layer, table): |
495 |
|
QueryTableFrame.__init__(self, parent, name, title, table) |
496 |
|
self.layer = layer |
497 |
|
self.grid.Subscribe(ROW_SELECTED, self.rows_selected) |
498 |
|
self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes) |
499 |
|
|
500 |
|
# if there is already a selection present, update the grid |
501 |
|
# accordingly |
502 |
|
sel = self.get_selected().keys() |
503 |
|
for i in sel: |
504 |
|
self.grid.SelectRow(i, True) |
505 |
|
|
506 |
def make_grid(self, table): |
def make_grid(self, table): |
507 |
"""Override the derived method to return a LayerTableGrid. |
"""Override the derived method to return a LayerTableGrid. |
508 |
""" |
""" |
509 |
return LayerTableGrid(self, table) |
return LayerTableGrid(self, table) |
510 |
|
|
511 |
|
def get_selected(self): |
512 |
|
"""Override the derived method to return a dictionary of the selected |
513 |
|
rows. |
514 |
|
""" |
515 |
|
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
516 |
|
|
517 |
def OnClose(self, event): |
def OnClose(self, event): |
518 |
|
"""Override the derived method to first unsubscribed.""" |
519 |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
520 |
TableFrame.OnClose(self, event) |
self.grid.Unsubscribe(ROW_SELECTED, self.rows_selected) |
521 |
|
QueryTableFrame.OnClose(self, event) |
522 |
|
|
523 |
def select_shapes(self, layer, shapes): |
def select_shapes(self, layer, shapes): |
524 |
"""Subscribed to the SHAPES_SELECTED message. |
"""Subscribed to the SHAPES_SELECTED message. |
529 |
self.grid.select_shapes(layer, shapes) |
self.grid.select_shapes(layer, shapes) |
530 |
|
|
531 |
def rows_selected(self, rows): |
def rows_selected(self, rows): |
532 |
|
"""Return the selected rows of the layer as they are returned |
533 |
|
by Layer.SelectShapes(). |
534 |
|
""" |
535 |
if self.layer is not None: |
if self.layer is not None: |
536 |
self.parent.SelectShapes(self.layer, rows) |
self.parent.SelectShapes(self.layer, rows) |