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 |
|
|
197 |
self.table.SetTable(table) |
self.table.SetTable(table) |
198 |
|
|
199 |
def OnRangeSelect(self, event): |
def OnRangeSelect(self, event): |
200 |
|
to_id = self.table.RowOrdinalToId |
201 |
if self.handleSelectEvents: |
if self.handleSelectEvents: |
202 |
self.rows = dict([(i, 0) for i in self.GetSelectedRows()]) |
self.rows = dict([(to_id(i), 0) for i in self.GetSelectedRows()]) |
203 |
|
|
204 |
# if we're selecting we need to include the selected range and |
# if we're selecting we need to include the selected range and |
205 |
# make sure that the current row is also included, which may |
# make sure that the current row is also included, which may |
206 |
# not be the case if you just click on a single row! |
# not be the case if you just click on a single row! |
207 |
if event.Selecting(): |
if event.Selecting(): |
208 |
for i in range(event.GetTopRow(), event.GetBottomRow() + 1): |
for i in range(event.GetTopRow(), event.GetBottomRow() + 1): |
209 |
self.rows[i] = 0 |
self.rows[to_id(i)] = 0 |
210 |
self.rows[event.GetTopLeftCoords().GetRow()] = 0 |
self.rows[to_id(event.GetTopLeftCoords().GetRow())] = 0 |
211 |
|
|
212 |
self.issue(ROW_SELECTED, self.rows.keys()) |
self.issue(ROW_SELECTED, self.rows.keys()) |
213 |
|
|
214 |
event.Skip() |
event.Skip() |
215 |
|
|
216 |
def OnSelectCell(self, event): |
def OnSelectCell(self, event): |
217 |
|
to_id = self.table.RowOrdinalToId |
218 |
if self.handleSelectEvents: |
if self.handleSelectEvents: |
219 |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
self.issue(ROW_SELECTED, |
220 |
|
[to_id(i) for i in self.GetSelectedRows()]) |
221 |
event.Skip() |
event.Skip() |
222 |
|
|
223 |
def ToggleEventListeners(self, on): |
def ToggleEventListeners(self, on): |
224 |
self.handleSelectEvents = on |
self.handleSelectEvents = on |
225 |
|
|
226 |
def GetNumberSelected(self): |
def GetNumberSelected(self): |
227 |
return len(self.rows) |
return len(self.rows) |
228 |
|
|
252 |
if self.allow_messages_count == 0: |
if self.allow_messages_count == 0: |
253 |
Publisher.issue(self, *args) |
Publisher.issue(self, *args) |
254 |
|
|
255 |
|
def SelectRowById(self, rowid, do_select): |
256 |
|
"""Select row with the id rowid""" |
257 |
|
self.SelectRow(self.table.RowIdToOrdinal(rowid), do_select) |
258 |
|
|
259 |
|
|
260 |
class LayerTableGrid(TableGrid): |
class LayerTableGrid(TableGrid): |
261 |
|
|
278 |
try: |
try: |
279 |
self.ClearSelection() |
self.ClearSelection() |
280 |
if len(shapes) > 0: |
if len(shapes) > 0: |
281 |
# |
# keep track of the lowest id so we can make it the |
282 |
# keep track of the lowest id so we can make it |
# first visible item |
283 |
# the first visible item |
first = -1 |
|
# |
|
|
first = shapes[0] |
|
284 |
|
|
285 |
|
to_ordinal = self.table.RowIdToOrdinal |
286 |
for shape in shapes: |
for shape in shapes: |
287 |
self.SelectRow(shape, True) |
row = to_ordinal(shape) |
288 |
if shape < first: |
self.SelectRow(row, True) |
289 |
first = shape |
if row < first: |
290 |
|
first = row |
291 |
|
|
292 |
self.SetGridCursor(first, 0) |
self.SetGridCursor(first, 0) |
293 |
self.MakeCellVisible(first, 0) |
self.MakeCellVisible(first, 0) |
310 |
self.session = self.app.Session() |
self.session = self.app.Session() |
311 |
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
312 |
|
|
313 |
|
def OnDestroy(self, event): |
314 |
|
"""Extend inherited method to unsubscribe messages""" |
315 |
|
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
316 |
|
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
317 |
|
ThubanFrame.OnDestroy(self, event) |
318 |
|
|
319 |
def make_grid(self, table): |
def make_grid(self, table): |
320 |
"""Return the table grid to use in the frame. |
"""Return the table grid to use in the frame. |
324 |
""" |
""" |
325 |
return TableGrid(self, table) |
return TableGrid(self, table) |
326 |
|
|
|
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) |
|
|
|
|
327 |
def close_on_session_replaced(self, *args): |
def close_on_session_replaced(self, *args): |
328 |
"""Subscriber for the SESSION_REPLACED messages. |
"""Subscriber for the SESSION_REPLACED messages. |
329 |
|
|
345 |
ID_QUERY = 4001 |
ID_QUERY = 4001 |
346 |
ID_EXPORT = 4002 |
ID_EXPORT = 4002 |
347 |
ID_COMBOVALUE = 4003 |
ID_COMBOVALUE = 4003 |
348 |
|
ID_EXPORTSEL = 4004 |
349 |
|
|
350 |
class QueryTableFrame(TableFrame): |
class QueryTableFrame(TableFrame): |
351 |
|
|
369 |
_("Add to Selection")]) |
_("Add to Selection")]) |
370 |
|
|
371 |
button_query = wxButton(self.panel, ID_QUERY, _("Query")) |
button_query = wxButton(self.panel, ID_QUERY, _("Query")) |
372 |
button_saveas = wxButton(self.panel, ID_EXPORT, _("Export")) |
button_export = wxButton(self.panel, ID_EXPORT, _("Export")) |
373 |
|
button_exportSel = wxButton(self.panel, ID_EXPORTSEL, _("Export Selection")) |
374 |
|
button_close = wxButton(self.panel, wxID_CLOSE, _("Close")) |
375 |
|
|
376 |
self.CreateStatusBar() |
self.CreateStatusBar() |
377 |
|
|
404 |
sizer.Add(self.choice_action, 0, wxALL, 4) |
sizer.Add(self.choice_action, 0, wxALL, 4) |
405 |
sizer.Add(button_query, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
sizer.Add(button_query, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
406 |
sizer.Add(40, 20, 0, wxALL, 4) |
sizer.Add(40, 20, 0, wxALL, 4) |
|
sizer.Add(button_saveas, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
|
407 |
|
|
408 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
409 |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
410 |
|
|
411 |
|
sizer = wxBoxSizer(wxHORIZONTAL) |
412 |
|
sizer.Add(button_export, 0, wxALL, 4) |
413 |
|
sizer.Add(button_exportSel, 0, wxALL, 4) |
414 |
|
sizer.Add(60, 20, 1, wxALL|wxEXPAND, 4) |
415 |
|
sizer.Add(button_close, 0, wxALL|wxALIGN_RIGHT, 4) |
416 |
|
topBox.Add(sizer, 0, wxALL | wxEXPAND, 4) |
417 |
|
|
418 |
self.panel.SetAutoLayout(True) |
self.panel.SetAutoLayout(True) |
419 |
self.panel.SetSizer(topBox) |
self.panel.SetSizer(topBox) |
420 |
topBox.Fit(self.panel) |
topBox.Fit(self.panel) |
430 |
self.grid.SetFocus() |
self.grid.SetFocus() |
431 |
|
|
432 |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
433 |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
EVT_BUTTON(self, ID_EXPORT, self.OnExport) |
434 |
|
EVT_BUTTON(self, ID_EXPORTSEL, self.OnExportSel) |
435 |
|
EVT_BUTTON(self, wxID_CLOSE, self.OnClose) |
436 |
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) |
|
437 |
|
|
438 |
def UpdateStatusText(self): |
self.grid.Subscribe(ROW_SELECTED, self.UpdateStatusText) |
439 |
|
|
440 |
|
def UpdateStatusText(self, rows=None): |
441 |
self.SetStatusText(_("%i rows (%i selected), %i columns") |
self.SetStatusText(_("%i rows (%i selected), %i columns") |
442 |
% (self.grid.GetNumberRows(), |
% (self.grid.GetNumberRows(), |
443 |
self.grid.GetNumberSelected(), |
self.grid.GetNumberSelected(), |
444 |
self.grid.GetNumberCols())) |
self.grid.GetNumberCols())) |
445 |
|
|
|
def OnGridSelectRange(self, event): |
|
|
self.UpdateStatusText() |
|
|
event.Skip() |
|
|
|
|
|
def OnGridSelectCell(self, event): |
|
|
self.UpdateStatusText() |
|
|
event.Skip() |
|
|
|
|
446 |
def OnKeyDown(self, event): |
def OnKeyDown(self, event): |
447 |
"""Catch query key from grid""" |
"""Catch query key from grid""" |
448 |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
452 |
event.Skip() |
event.Skip() |
453 |
|
|
454 |
def OnQuery(self, event): |
def OnQuery(self, event): |
455 |
wxBeginBusyCursor() |
ThubanBeginBusyCursor() |
456 |
try: |
try: |
457 |
|
|
458 |
text = self.combo_value.GetValue() |
text = self.combo_value.GetValue() |
503 |
if firsttime: |
if firsttime: |
504 |
firsttime = False |
firsttime = False |
505 |
else: |
else: |
506 |
self.grid.SelectRow(id, True) |
self.grid.SelectRowById(id, True) |
507 |
|
|
508 |
self.grid.ToggleEventListeners(True) |
self.grid.ToggleEventListeners(True) |
509 |
|
|
511 |
# select the first row |
# select the first row |
512 |
# |
# |
513 |
if ids: |
if ids: |
514 |
self.grid.SelectRow(ids[0], True) |
self.grid.SelectRowById(ids[0], True) |
515 |
|
|
516 |
finally: |
finally: |
517 |
wxEndBusyCursor() |
ThubanEndBusyCursor() |
518 |
|
|
519 |
|
def doExport(self, onlySelected): |
520 |
|
|
|
def OnSaveAs(self, event): |
|
521 |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
522 |
_("DBF Files (*.dbf)|*.dbf|") + |
_("DBF Files (*.dbf)|*.dbf|") + |
523 |
_("CSV Files (*.csv)|*.csv|") + |
_("CSV Files (*.csv)|*.csv|") + |
527 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
528 |
type = os.path.basename(filename).split('.')[-1:][0] |
type = os.path.basename(filename).split('.')[-1:][0] |
529 |
dlg.Destroy() |
dlg.Destroy() |
530 |
|
|
531 |
|
if onlySelected: |
532 |
|
records = self.grid.GetSelectedRows() |
533 |
|
else: |
534 |
|
records = None |
535 |
|
|
536 |
if type.upper() == "DBF": |
if type.upper() == "DBF": |
537 |
table_to_dbf(self.table, filename) |
table_to_dbf(self.table, filename, records) |
538 |
elif type.upper() == 'CSV': |
elif type.upper() == 'CSV': |
539 |
table_to_csv(self.table, filename) |
table_to_csv(self.table, filename, records) |
540 |
else: |
else: |
541 |
dlg = wxMessageDialog(None, "Unsupported format: %s" % type, |
dlg = wxMessageDialog(None, "Unsupported format: %s" % type, |
542 |
"Table Export", wxOK|wxICON_WARNING) |
"Table Export", wxOK|wxICON_WARNING) |
545 |
else: |
else: |
546 |
dlg.Destroy() |
dlg.Destroy() |
547 |
|
|
548 |
|
def OnExport(self, event): |
549 |
|
self.doExport(False) |
550 |
|
|
551 |
|
def OnExportSel(self, event): |
552 |
|
self.doExport(True) |
553 |
|
|
554 |
def OnClose(self, event): |
def OnClose(self, event): |
555 |
TableFrame.OnClose(self, event) |
TableFrame.OnClose(self, event) |
556 |
|
|
557 |
def get_selected(self): |
def get_selected(self): |
558 |
"""Return a dictionary of the selected rows. |
"""Return a dictionary of the selected rows. |
559 |
|
|
560 |
The dictionary has sthe indexes as keys.""" |
The dictionary has the indexes as keys.""" |
561 |
return dict([(i, 0) for i in self.grid.GetSelectedRows()]) |
to_id = self.table.RowOrdinalToId |
562 |
|
return dict([(to_id(i), 0) for i in self.grid.GetSelectedRows()]) |
563 |
|
|
564 |
|
|
565 |
class LayerTableFrame(QueryTableFrame): |
class LayerTableFrame(QueryTableFrame): |
566 |
|
|
582 |
# accordingly |
# accordingly |
583 |
sel = self.get_selected().keys() |
sel = self.get_selected().keys() |
584 |
for i in sel: |
for i in sel: |
585 |
self.grid.SelectRow(i, True) |
self.grid.SelectRowById(i, True) |
586 |
|
|
587 |
|
def OnDestroy(self, event): |
588 |
|
"""Extend inherited method to unsubscribe messages""" |
589 |
|
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
590 |
|
self.grid.Unsubscribe(ROW_SELECTED, self.rows_selected) |
591 |
|
self.map.Unsubscribe(MAP_LAYERS_REMOVED, self.map_layers_removed) |
592 |
|
QueryTableFrame.OnDestroy(self, event) |
593 |
|
|
594 |
def make_grid(self, table): |
def make_grid(self, table): |
595 |
"""Override the derived method to return a LayerTableGrid. |
"""Override the derived method to return a LayerTableGrid. |
602 |
""" |
""" |
603 |
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
604 |
|
|
|
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) |
|
|
|
|
605 |
def select_shapes(self, layer, shapes): |
def select_shapes(self, layer, shapes): |
606 |
"""Subscribed to the SHAPES_SELECTED message. |
"""Subscribed to the SHAPES_SELECTED message. |
607 |
|
|