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, table_to_dbf, table_to_csv |
FIELDTYPE_STRING, table_to_dbf, table_to_csv |
|
import view |
|
20 |
from dialogs import ThubanFrame |
from dialogs import ThubanFrame |
21 |
|
|
22 |
from messages import SHAPES_SELECTED, SESSION_REPLACED |
from messages import SHAPES_SELECTED, SESSION_REPLACED |
23 |
from Thuban.Model.messages import TABLE_REMOVED, MAP_LAYERS_REMOVED |
from Thuban.Model.messages import TABLE_REMOVED, MAP_LAYERS_REMOVED |
24 |
|
from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor |
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, |
71 |
# Renderer understands the type too,) not just strings as in the |
# Renderer understands the type too,) not just strings as in the |
72 |
# C++ version. |
# C++ version. |
73 |
def GetValue(self, row, col): |
def GetValue(self, row, col): |
74 |
record = self.table.ReadRowAsDict(row) |
record = self.table.ReadRowAsDict(row, row_is_ordinal = 1) |
75 |
return record[self.columns[col][0]] |
return record[self.columns[col][0]] |
76 |
|
|
77 |
def SetValue(self, row, col, value): |
def SetValue(self, row, col, value): |
102 |
return self.CanGetValueAs(row, col, typeName) |
return self.CanGetValueAs(row, col, typeName) |
103 |
|
|
104 |
|
|
105 |
|
# |
106 |
|
def RowIdToOrdinal(self, rowid): |
107 |
|
"""Return the ordinal of the row given by its id""" |
108 |
|
return self.table.RowIdToOrdinal(rowid) |
109 |
|
|
110 |
|
def RowOrdinalToId(self, ordinal): |
111 |
|
"""Return the id of the row given by its ordinal""" |
112 |
|
return self.table.RowOrdinalToId(ordinal) |
113 |
|
|
114 |
|
|
115 |
class NullRenderer(wxPyGridCellRenderer): |
class NullRenderer(wxPyGridCellRenderer): |
116 |
|
|
193 |
self.RegisterDataType(wxGRID_VALUE_FLOAT, |
self.RegisterDataType(wxGRID_VALUE_FLOAT, |
194 |
NullRenderer(wxGridCellFloatRenderer()), None) |
NullRenderer(wxGridCellFloatRenderer()), None) |
195 |
|
|
196 |
|
EVT_WINDOW_DESTROY(self, self.OnDestroy) |
197 |
|
|
198 |
|
def OnDestroy(self, event): |
199 |
|
Publisher.Destroy(self) |
200 |
|
|
201 |
def SetTableObject(self, table): |
def SetTableObject(self, table): |
202 |
self.table.SetTable(table) |
self.table.SetTable(table) |
203 |
|
|
204 |
def OnRangeSelect(self, event): |
def OnRangeSelect(self, event): |
205 |
|
to_id = self.table.RowOrdinalToId |
206 |
if self.handleSelectEvents: |
if self.handleSelectEvents: |
207 |
self.rows = dict([(i, 0) for i in self.GetSelectedRows()]) |
self.rows = dict([(to_id(i), 0) for i in self.GetSelectedRows()]) |
208 |
|
|
209 |
# if we're selecting we need to include the selected range and |
# if we're selecting we need to include the selected range and |
210 |
# make sure that the current row is also included, which may |
# make sure that the current row is also included, which may |
211 |
# not be the case if you just click on a single row! |
# not be the case if you just click on a single row! |
212 |
if event.Selecting(): |
if event.Selecting(): |
213 |
for i in range(event.GetTopRow(), event.GetBottomRow() + 1): |
for i in range(event.GetTopRow(), event.GetBottomRow() + 1): |
214 |
self.rows[i] = 0 |
self.rows[to_id(i)] = 0 |
215 |
self.rows[event.GetTopLeftCoords().GetRow()] = 0 |
self.rows[to_id(event.GetTopLeftCoords().GetRow())] = 0 |
216 |
|
|
217 |
self.issue(ROW_SELECTED, self.rows.keys()) |
self.issue(ROW_SELECTED, self.rows.keys()) |
218 |
|
|
219 |
event.Skip() |
event.Skip() |
220 |
|
|
221 |
def OnSelectCell(self, event): |
def OnSelectCell(self, event): |
222 |
|
to_id = self.table.RowOrdinalToId |
223 |
if self.handleSelectEvents: |
if self.handleSelectEvents: |
224 |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
self.issue(ROW_SELECTED, |
225 |
|
[to_id(i) for i in self.GetSelectedRows()]) |
226 |
event.Skip() |
event.Skip() |
227 |
|
|
228 |
def ToggleEventListeners(self, on): |
def ToggleEventListeners(self, on): |
229 |
self.handleSelectEvents = on |
self.handleSelectEvents = on |
230 |
|
|
231 |
def GetNumberSelected(self): |
def GetNumberSelected(self): |
232 |
return len(self.rows) |
return len(self.rows) |
233 |
|
|
257 |
if self.allow_messages_count == 0: |
if self.allow_messages_count == 0: |
258 |
Publisher.issue(self, *args) |
Publisher.issue(self, *args) |
259 |
|
|
260 |
|
def SelectRowById(self, rowid, do_select): |
261 |
|
"""Select row with the id rowid""" |
262 |
|
self.SelectRow(self.table.RowIdToOrdinal(rowid), do_select) |
263 |
|
|
264 |
|
|
265 |
class LayerTableGrid(TableGrid): |
class LayerTableGrid(TableGrid): |
266 |
|
|
277 |
nothing. If shape or layer is None also do nothing. |
nothing. If shape or layer is None also do nothing. |
278 |
""" |
""" |
279 |
if layer is not None \ |
if layer is not None \ |
280 |
and layer.table is self.table.table: |
and layer.ShapeStore().Table() is self.table.table: |
281 |
|
|
282 |
self.disallow_messages() |
self.disallow_messages() |
283 |
try: |
try: |
284 |
self.ClearSelection() |
self.ClearSelection() |
285 |
if len(shapes) > 0: |
if len(shapes) > 0: |
286 |
# |
# keep track of the lowest id so we can make it the |
287 |
# keep track of the lowest id so we can make it |
# first visible item |
288 |
# the first visible item |
first = -1 |
|
# |
|
|
first = shapes[0] |
|
289 |
|
|
290 |
|
to_ordinal = self.table.RowIdToOrdinal |
291 |
for shape in shapes: |
for shape in shapes: |
292 |
self.SelectRow(shape, True) |
row = to_ordinal(shape) |
293 |
if shape < first: |
self.SelectRow(row, True) |
294 |
first = shape |
if row < first: |
295 |
|
first = row |
296 |
|
|
297 |
self.SetGridCursor(first, 0) |
self.SetGridCursor(first, 0) |
298 |
self.MakeCellVisible(first, 0) |
self.MakeCellVisible(first, 0) |
306 |
|
|
307 |
def __init__(self, parent, name, title, table): |
def __init__(self, parent, name, title, table): |
308 |
ThubanFrame.__init__(self, parent, name, title) |
ThubanFrame.__init__(self, parent, name, title) |
309 |
|
self.panel = wxPanel(self, -1) |
310 |
|
|
311 |
self.table = table |
self.table = table |
312 |
self.grid = self.make_grid(self.table) |
self.grid = self.make_grid(self.table) |
313 |
self.app = self.parent.application |
self.app = self.parent.application |
315 |
self.session = self.app.Session() |
self.session = self.app.Session() |
316 |
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
317 |
|
|
318 |
|
def OnDestroy(self, event): |
319 |
|
"""Extend inherited method to unsubscribe messages""" |
320 |
|
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
321 |
|
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
322 |
|
ThubanFrame.OnDestroy(self, event) |
323 |
|
|
324 |
def make_grid(self, table): |
def make_grid(self, table): |
325 |
"""Return the table grid to use in the frame. |
"""Return the table grid to use in the frame. |
326 |
|
|
329 |
""" |
""" |
330 |
return TableGrid(self, table) |
return TableGrid(self, table) |
331 |
|
|
|
def OnClose(self, event): |
|
|
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
|
|
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
|
|
ThubanFrame.OnClose(self, event) |
|
|
|
|
332 |
def close_on_session_replaced(self, *args): |
def close_on_session_replaced(self, *args): |
333 |
"""Subscriber for the SESSION_REPLACED messages. |
"""Subscriber for the SESSION_REPLACED messages. |
334 |
|
|
350 |
ID_QUERY = 4001 |
ID_QUERY = 4001 |
351 |
ID_EXPORT = 4002 |
ID_EXPORT = 4002 |
352 |
ID_COMBOVALUE = 4003 |
ID_COMBOVALUE = 4003 |
353 |
|
ID_EXPORTSEL = 4004 |
354 |
|
|
355 |
class QueryTableFrame(TableFrame): |
class QueryTableFrame(TableFrame): |
356 |
|
|
364 |
def __init__(self, parent, name, title, table): |
def __init__(self, parent, name, title, table): |
365 |
TableFrame.__init__(self, parent, name, title, table) |
TableFrame.__init__(self, parent, name, title, table) |
366 |
|
|
367 |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
self.combo_fields = wxComboBox(self.panel, -1, style=wxCB_READONLY) |
368 |
self.choice_comp = wxChoice(self, -1, |
self.choice_comp = wxChoice(self.panel, -1, |
369 |
choices=["<", "<=", "==", "!=", ">=", ">"]) |
choices=["<", "<=", "==", "!=", ">=", ">"]) |
370 |
self.combo_value = wxComboBox(self, ID_COMBOVALUE) |
self.combo_value = wxComboBox(self.panel, ID_COMBOVALUE) |
371 |
self.choice_action = wxChoice(self, -1, |
self.choice_action = wxChoice(self.panel, -1, |
372 |
choices=[_("Replace Selection"), |
choices=[_("Replace Selection"), |
373 |
_("Refine Selection"), |
_("Refine Selection"), |
374 |
_("Add to Selection")]) |
_("Add to Selection")]) |
375 |
|
|
376 |
button_query = wxButton(self, ID_QUERY, _("Query")) |
button_query = wxButton(self.panel, ID_QUERY, _("Query")) |
377 |
button_saveas = wxButton(self, ID_EXPORT, _("Export")) |
button_export = wxButton(self.panel, ID_EXPORT, _("Export")) |
378 |
|
button_exportSel = wxButton(self.panel, ID_EXPORTSEL, _("Export Selection")) |
379 |
|
button_close = wxButton(self.panel, wxID_CLOSE, _("Close")) |
380 |
|
|
381 |
self.CreateStatusBar() |
self.CreateStatusBar() |
|
self.SetStatusText(_("0 rows (0 selected), 0 columns")) |
|
382 |
|
|
383 |
self.grid.SetSize((400, 200)) |
self.grid.SetSize((400, 200)) |
384 |
|
|
394 |
self.choice_action.SetSelection(0) |
self.choice_action.SetSelection(0) |
395 |
self.choice_comp.SetSelection(0) |
self.choice_comp.SetSelection(0) |
396 |
|
|
397 |
|
self.grid.Reparent(self.panel) |
398 |
|
|
399 |
|
self.UpdateStatusText() |
400 |
|
|
401 |
topBox = wxBoxSizer(wxVERTICAL) |
topBox = wxBoxSizer(wxVERTICAL) |
402 |
|
|
403 |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, |
sizer = wxStaticBoxSizer(wxStaticBox(self.panel, -1, |
404 |
_("Selection")), |
_("Selection")), |
405 |
wxHORIZONTAL) |
wxHORIZONTAL) |
406 |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
409 |
sizer.Add(self.choice_action, 0, wxALL, 4) |
sizer.Add(self.choice_action, 0, wxALL, 4) |
410 |
sizer.Add(button_query, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
sizer.Add(button_query, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
411 |
sizer.Add(40, 20, 0, wxALL, 4) |
sizer.Add(40, 20, 0, wxALL, 4) |
|
sizer.Add(button_saveas, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
|
412 |
|
|
413 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
414 |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
415 |
|
|
416 |
|
sizer = wxBoxSizer(wxHORIZONTAL) |
417 |
|
sizer.Add(button_export, 0, wxALL, 4) |
418 |
|
sizer.Add(button_exportSel, 0, wxALL, 4) |
419 |
|
sizer.Add(60, 20, 1, wxALL|wxEXPAND, 4) |
420 |
|
sizer.Add(button_close, 0, wxALL|wxALIGN_RIGHT, 4) |
421 |
|
topBox.Add(sizer, 0, wxALL | wxEXPAND, 4) |
422 |
|
|
423 |
|
self.panel.SetAutoLayout(True) |
424 |
|
self.panel.SetSizer(topBox) |
425 |
|
topBox.Fit(self.panel) |
426 |
|
topBox.SetSizeHints(self.panel) |
427 |
|
|
428 |
|
panelSizer = wxBoxSizer(wxVERTICAL) |
429 |
|
panelSizer.Add(self.panel, 1, wxEXPAND, 0) |
430 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
431 |
self.SetSizer(topBox) |
self.SetSizer(panelSizer) |
432 |
topBox.Fit(self) |
panelSizer.Fit(self) |
433 |
topBox.SetSizeHints(self) |
panelSizer.SetSizeHints(self) |
434 |
|
|
435 |
self.grid.SetFocus() |
self.grid.SetFocus() |
436 |
|
|
437 |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
438 |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
EVT_BUTTON(self, ID_EXPORT, self.OnExport) |
439 |
|
EVT_BUTTON(self, ID_EXPORTSEL, self.OnExportSel) |
440 |
|
EVT_BUTTON(self, wxID_CLOSE, self.OnClose) |
441 |
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
|
EVT_GRID_RANGE_SELECT(self.grid, self.OnGridSelectRange) |
|
|
EVT_GRID_SELECT_CELL(self.grid, self.OnGridSelectCell) |
|
442 |
|
|
443 |
def UpdateStatusText(self): |
self.grid.Subscribe(ROW_SELECTED, self.UpdateStatusText) |
444 |
|
|
445 |
|
def UpdateStatusText(self, rows=None): |
446 |
self.SetStatusText(_("%i rows (%i selected), %i columns") |
self.SetStatusText(_("%i rows (%i selected), %i columns") |
447 |
% (self.grid.GetNumberRows(), |
% (self.grid.GetNumberRows(), |
448 |
self.grid.GetNumberSelected(), |
self.grid.GetNumberSelected(), |
449 |
self.grid.GetNumberCols())) |
self.grid.GetNumberCols())) |
450 |
|
|
|
def OnGridSelectRange(self, event): |
|
|
self.UpdateStatusText() |
|
|
event.Skip() |
|
|
|
|
|
def OnGridSelectCell(self, event): |
|
|
self.UpdateStatusText() |
|
|
event.Skip() |
|
|
|
|
451 |
def OnKeyDown(self, event): |
def OnKeyDown(self, event): |
452 |
"""Catch query key from grid""" |
"""Catch query key from grid""" |
453 |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
457 |
event.Skip() |
event.Skip() |
458 |
|
|
459 |
def OnQuery(self, event): |
def OnQuery(self, event): |
460 |
wxBeginBusyCursor() |
ThubanBeginBusyCursor() |
461 |
try: |
try: |
462 |
|
|
463 |
text = self.combo_value.GetValue() |
text = self.combo_value.GetValue() |
508 |
if firsttime: |
if firsttime: |
509 |
firsttime = False |
firsttime = False |
510 |
else: |
else: |
511 |
self.grid.SelectRow(id, True) |
self.grid.SelectRowById(id, True) |
512 |
|
|
513 |
self.grid.ToggleEventListeners(True) |
self.grid.ToggleEventListeners(True) |
514 |
|
|
516 |
# select the first row |
# select the first row |
517 |
# |
# |
518 |
if ids: |
if ids: |
519 |
self.grid.SelectRow(ids[0], True) |
self.grid.SelectRowById(ids[0], True) |
520 |
|
|
521 |
finally: |
finally: |
522 |
wxEndBusyCursor() |
ThubanEndBusyCursor() |
523 |
|
|
524 |
|
def doExport(self, onlySelected): |
525 |
|
|
|
def OnSaveAs(self, event): |
|
526 |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
527 |
_("DBF Files (*.dbf)|*.dbf|") + |
_("DBF Files (*.dbf)|*.dbf|") + |
528 |
_("CSV Files (*.csv)|*.csv|") + |
_("CSV Files (*.csv)|*.csv|") + |
532 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
533 |
type = os.path.basename(filename).split('.')[-1:][0] |
type = os.path.basename(filename).split('.')[-1:][0] |
534 |
dlg.Destroy() |
dlg.Destroy() |
535 |
|
|
536 |
|
if onlySelected: |
537 |
|
records = self.grid.GetSelectedRows() |
538 |
|
else: |
539 |
|
records = None |
540 |
|
|
541 |
if type.upper() == "DBF": |
if type.upper() == "DBF": |
542 |
table_to_dbf(self.table, filename) |
table_to_dbf(self.table, filename, records) |
543 |
elif type.upper() == 'CSV': |
elif type.upper() == 'CSV': |
544 |
table_to_csv(self.table, filename) |
table_to_csv(self.table, filename, records) |
545 |
else: |
else: |
546 |
dlg = wxMessageDialog(None, "Unsupported format: %s" % type, |
dlg = wxMessageDialog(None, "Unsupported format: %s" % type, |
547 |
"Table Export", wxOK|wxICON_WARNING) |
"Table Export", wxOK|wxICON_WARNING) |
550 |
else: |
else: |
551 |
dlg.Destroy() |
dlg.Destroy() |
552 |
|
|
553 |
|
def OnExport(self, event): |
554 |
|
self.doExport(False) |
555 |
|
|
556 |
|
def OnExportSel(self, event): |
557 |
|
self.doExport(True) |
558 |
|
|
559 |
def OnClose(self, event): |
def OnClose(self, event): |
560 |
TableFrame.OnClose(self, event) |
TableFrame.OnClose(self, event) |
561 |
|
|
562 |
def get_selected(self): |
def get_selected(self): |
563 |
"""Return a dictionary of the selected rows. |
"""Return a dictionary of the selected rows. |
564 |
|
|
565 |
The dictionary has sthe indexes as keys.""" |
The dictionary has the indexes as keys.""" |
566 |
return dict([(i, 0) for i in self.grid.GetSelectedRows()]) |
to_id = self.table.RowOrdinalToId |
567 |
|
return dict([(to_id(i), 0) for i in self.grid.GetSelectedRows()]) |
568 |
|
|
569 |
|
|
570 |
class LayerTableFrame(QueryTableFrame): |
class LayerTableFrame(QueryTableFrame): |
571 |
|
|
587 |
# accordingly |
# accordingly |
588 |
sel = self.get_selected().keys() |
sel = self.get_selected().keys() |
589 |
for i in sel: |
for i in sel: |
590 |
self.grid.SelectRow(i, True) |
self.grid.SelectRowById(i, True) |
591 |
|
|
592 |
|
def OnDestroy(self, event): |
593 |
|
"""Extend inherited method to unsubscribe messages""" |
594 |
|
# There's no need to unsubscribe from self.grid's messages |
595 |
|
# because it will get a DESTROY event too (since destroying the |
596 |
|
# frame basically means that all child windows are also |
597 |
|
# destroyed) and this it will clear all subscriptions |
598 |
|
# automatically. It may even have been destroyed already (this |
599 |
|
# does happen on w2000 for instance) so calling any of its |
600 |
|
# methods here would be an error. |
601 |
|
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
602 |
|
self.map.Unsubscribe(MAP_LAYERS_REMOVED, self.map_layers_removed) |
603 |
|
QueryTableFrame.OnDestroy(self, event) |
604 |
|
|
605 |
def make_grid(self, table): |
def make_grid(self, table): |
606 |
"""Override the derived method to return a LayerTableGrid. |
"""Override the derived method to return a LayerTableGrid. |
613 |
""" |
""" |
614 |
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
615 |
|
|
|
def OnClose(self, event): |
|
|
"""Override the derived method to first unsubscribed.""" |
|
|
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
|
|
self.grid.Unsubscribe(ROW_SELECTED, self.rows_selected) |
|
|
self.map.Unsubscribe(MAP_LAYERS_REMOVED, self.map_layers_removed) |
|
|
QueryTableFrame.OnClose(self, event) |
|
|
|
|
616 |
def select_shapes(self, layer, shapes): |
def select_shapes(self, layer, shapes): |
617 |
"""Subscribed to the SHAPES_SELECTED message. |
"""Subscribed to the SHAPES_SELECTED message. |
618 |
|
|