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 |
20 |
import view |
import view |
21 |
from dialogs import NonModalNonParentDialog |
from dialogs import ThubanFrame |
22 |
|
|
23 |
from messages import SHAPES_SELECTED, SESSION_REPLACED |
from messages import SHAPES_SELECTED, SESSION_REPLACED |
24 |
from Thuban.Model.messages import TABLE_REMOVED |
from Thuban.Model.messages import TABLE_REMOVED, MAP_LAYERS_REMOVED |
25 |
|
from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor |
26 |
|
|
27 |
wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER, |
wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER, |
28 |
FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT, |
FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT, |
151 |
|
|
152 |
self.allow_messages_count = 0 |
self.allow_messages_count = 0 |
153 |
|
|
154 |
|
# keep track of which rows are selected. |
155 |
|
self.rows = {} |
156 |
|
|
157 |
self.table = DataTable(table) |
self.table = DataTable(table) |
158 |
|
|
159 |
# The second parameter means that the grid is to take ownership |
# The second parameter means that the grid is to take ownership |
173 |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
174 |
|
|
175 |
self.ToggleEventListeners(True) |
self.ToggleEventListeners(True) |
176 |
|
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
177 |
|
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
178 |
|
|
179 |
# Replace the normal renderers with our own versions which |
# Replace the normal renderers with our own versions which |
180 |
# render NULL/None values specially |
# render NULL/None values specially |
189 |
self.table.SetTable(table) |
self.table.SetTable(table) |
190 |
|
|
191 |
def OnRangeSelect(self, event): |
def OnRangeSelect(self, event): |
192 |
rows = dict([(i, 0) for i in self.GetSelectedRows()]) |
if self.handleSelectEvents: |
193 |
|
self.rows = dict([(i, 0) for i in self.GetSelectedRows()]) |
194 |
|
|
195 |
# if we're selecting we need to include the selected range and |
# if we're selecting we need to include the selected range and |
196 |
# make sure that the current row is also included, which may |
# make sure that the current row is also included, which may |
197 |
# not be the case if you just click on a single row! |
# not be the case if you just click on a single row! |
198 |
if event.Selecting(): |
if event.Selecting(): |
199 |
for i in range(event.GetTopRow(), event.GetBottomRow() + 1): |
for i in range(event.GetTopRow(), event.GetBottomRow() + 1): |
200 |
rows[i] = 0 |
self.rows[i] = 0 |
201 |
rows[event.GetTopLeftCoords().GetRow()] = 0 |
self.rows[event.GetTopLeftCoords().GetRow()] = 0 |
202 |
|
|
203 |
|
self.issue(ROW_SELECTED, self.rows.keys()) |
204 |
|
|
|
self.issue(ROW_SELECTED, rows.keys()) |
|
205 |
event.Skip() |
event.Skip() |
206 |
|
|
207 |
def OnSelectCell(self, event): |
def OnSelectCell(self, event): |
208 |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
if self.handleSelectEvents: |
209 |
|
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
210 |
event.Skip() |
event.Skip() |
211 |
|
|
212 |
def ToggleEventListeners(self, on): |
def ToggleEventListeners(self, on): |
213 |
if on: |
self.handleSelectEvents = on |
|
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
|
|
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
|
|
else: |
|
|
EVT_GRID_RANGE_SELECT(self, None) |
|
|
EVT_GRID_SELECT_CELL(self, None) |
|
214 |
|
|
215 |
|
def GetNumberSelected(self): |
216 |
|
return len(self.rows) |
217 |
|
|
218 |
def disallow_messages(self): |
def disallow_messages(self): |
219 |
"""Disallow messages to be send. |
"""Disallow messages to be send. |
220 |
|
|
257 |
nothing. If shape or layer is None also do nothing. |
nothing. If shape or layer is None also do nothing. |
258 |
""" |
""" |
259 |
if layer is not None \ |
if layer is not None \ |
260 |
and layer.table is self.table.table: |
and layer.ShapeStore().Table() is self.table.table: |
261 |
|
|
262 |
self.disallow_messages() |
self.disallow_messages() |
263 |
try: |
try: |
280 |
self.allow_messages() |
self.allow_messages() |
281 |
|
|
282 |
|
|
283 |
class TableFrame(NonModalNonParentDialog): |
class TableFrame(ThubanFrame): |
284 |
|
|
285 |
"""Frame that displays a Thuban table in a grid view""" |
"""Frame that displays a Thuban table in a grid view""" |
286 |
|
|
287 |
def __init__(self, parent, name, title, table): |
def __init__(self, parent, name, title, table): |
288 |
NonModalNonParentDialog.__init__(self, parent, name, title) |
ThubanFrame.__init__(self, parent, name, title) |
289 |
|
self.panel = wxPanel(self, -1) |
290 |
|
|
291 |
self.table = table |
self.table = table |
292 |
self.grid = self.make_grid(self.table) |
self.grid = self.make_grid(self.table) |
293 |
self.app = self.parent.application |
self.app = self.parent.application |
295 |
self.session = self.app.Session() |
self.session = self.app.Session() |
296 |
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
297 |
|
|
298 |
|
|
299 |
def make_grid(self, table): |
def make_grid(self, table): |
300 |
"""Return the table grid to use in the frame. |
"""Return the table grid to use in the frame. |
301 |
|
|
307 |
def OnClose(self, event): |
def OnClose(self, event): |
308 |
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
309 |
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
310 |
NonModalNonParentDialog.OnClose(self, event) |
ThubanFrame.OnClose(self, event) |
311 |
|
|
312 |
def close_on_session_replaced(self, *args): |
def close_on_session_replaced(self, *args): |
313 |
"""Subscriber for the SESSION_REPLACED messages. |
"""Subscriber for the SESSION_REPLACED messages. |
329 |
|
|
330 |
ID_QUERY = 4001 |
ID_QUERY = 4001 |
331 |
ID_EXPORT = 4002 |
ID_EXPORT = 4002 |
332 |
|
ID_COMBOVALUE = 4003 |
333 |
|
|
334 |
class QueryTableFrame(TableFrame): |
class QueryTableFrame(TableFrame): |
335 |
|
|
343 |
def __init__(self, parent, name, title, table): |
def __init__(self, parent, name, title, table): |
344 |
TableFrame.__init__(self, parent, name, title, table) |
TableFrame.__init__(self, parent, name, title, table) |
345 |
|
|
346 |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
self.combo_fields = wxComboBox(self.panel, -1, style=wxCB_READONLY) |
347 |
self.choice_comp = wxChoice(self, -1, |
self.choice_comp = wxChoice(self.panel, -1, |
348 |
choices=["<", "<=", "==", "!=", ">=", ">"]) |
choices=["<", "<=", "==", "!=", ">=", ">"]) |
349 |
self.combo_value = wxComboBox(self, -1) |
self.combo_value = wxComboBox(self.panel, ID_COMBOVALUE) |
350 |
self.choice_action = wxChoice(self, -1, |
self.choice_action = wxChoice(self.panel, -1, |
351 |
choices=[_("Replace Selection"), |
choices=[_("Replace Selection"), |
352 |
_("Refine Selection"), |
_("Refine Selection"), |
353 |
_("Add to Selection")]) |
_("Add to Selection")]) |
354 |
|
|
355 |
button_query = wxButton(self, ID_QUERY, _("Query")) |
button_query = wxButton(self.panel, ID_QUERY, _("Query")) |
356 |
button_saveas = wxButton(self, ID_EXPORT, _("Export")) |
button_saveas = wxButton(self.panel, ID_EXPORT, _("Export")) |
357 |
|
|
358 |
|
self.CreateStatusBar() |
359 |
|
|
360 |
self.grid.SetSize((400, 200)) |
self.grid.SetSize((400, 200)) |
361 |
|
|
368 |
# assume at least one field? |
# assume at least one field? |
369 |
self.combo_fields.SetSelection(0) |
self.combo_fields.SetSelection(0) |
370 |
self.combo_value.SetSelection(0) |
self.combo_value.SetSelection(0) |
371 |
|
self.choice_action.SetSelection(0) |
372 |
|
self.choice_comp.SetSelection(0) |
373 |
|
|
374 |
|
self.grid.Reparent(self.panel) |
375 |
|
|
376 |
|
self.UpdateStatusText() |
377 |
|
|
378 |
topBox = wxBoxSizer(wxVERTICAL) |
topBox = wxBoxSizer(wxVERTICAL) |
379 |
|
|
380 |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, |
sizer = wxStaticBoxSizer(wxStaticBox(self.panel, -1, |
381 |
_("Selection")), |
_("Selection")), |
382 |
wxHORIZONTAL) |
wxHORIZONTAL) |
383 |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
391 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
392 |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
393 |
|
|
394 |
|
self.panel.SetAutoLayout(True) |
395 |
|
self.panel.SetSizer(topBox) |
396 |
|
topBox.Fit(self.panel) |
397 |
|
topBox.SetSizeHints(self.panel) |
398 |
|
|
399 |
|
panelSizer = wxBoxSizer(wxVERTICAL) |
400 |
|
panelSizer.Add(self.panel, 1, wxEXPAND, 0) |
401 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
402 |
self.SetSizer(topBox) |
self.SetSizer(panelSizer) |
403 |
topBox.Fit(self) |
panelSizer.Fit(self) |
404 |
topBox.SetSizeHints(self) |
panelSizer.SetSizeHints(self) |
405 |
|
|
406 |
self.grid.SetFocus() |
self.grid.SetFocus() |
407 |
|
|
408 |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
409 |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
410 |
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
411 |
|
EVT_GRID_RANGE_SELECT(self.grid, self.OnGridSelectRange) |
412 |
|
EVT_GRID_SELECT_CELL(self.grid, self.OnGridSelectCell) |
413 |
|
|
414 |
|
def UpdateStatusText(self): |
415 |
|
self.SetStatusText(_("%i rows (%i selected), %i columns") |
416 |
|
% (self.grid.GetNumberRows(), |
417 |
|
self.grid.GetNumberSelected(), |
418 |
|
self.grid.GetNumberCols())) |
419 |
|
|
420 |
|
def OnGridSelectRange(self, event): |
421 |
|
self.UpdateStatusText() |
422 |
|
event.Skip() |
423 |
|
|
424 |
|
def OnGridSelectCell(self, event): |
425 |
|
self.UpdateStatusText() |
426 |
|
event.Skip() |
427 |
|
|
428 |
def OnKeyDown(self, event): |
def OnKeyDown(self, event): |
429 |
"""Catch query key from grid""" |
"""Catch query key from grid""" |
430 |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
433 |
else: |
else: |
434 |
event.Skip() |
event.Skip() |
435 |
|
|
|
|
|
436 |
def OnQuery(self, event): |
def OnQuery(self, event): |
437 |
wxBeginBusyCursor() |
ThubanBeginBusyCursor() |
438 |
try: |
try: |
439 |
|
|
440 |
if self.combo_value.GetSelection() < 1: |
text = self.combo_value.GetValue() |
441 |
value = self.combo_value.GetValue() |
if self.combo_value.GetSelection() < 1 \ |
442 |
|
or self.combo_value.FindString(text) == -1: |
443 |
|
value = text |
444 |
else: |
else: |
445 |
value = self.table.Column(self.combo_value.GetValue()) |
value = self.table.Column(text) |
446 |
|
|
447 |
ids = self.table.SimpleQuery( |
ids = self.table.SimpleQuery( |
448 |
self.table.Column(self.combo_fields.GetStringSelection()), |
self.table.Column(self.combo_fields.GetStringSelection()), |
462 |
# the first element, which causes everything to be |
# the first element, which causes everything to be |
463 |
# updated properly. |
# updated properly. |
464 |
# |
# |
465 |
self.grid.ToggleEventListeners(False) |
if ids: |
466 |
|
self.grid.ToggleEventListeners(False) |
467 |
|
|
468 |
if choice == 0: |
if choice == 0: |
469 |
# Replace Selection |
# Replace Selection |
494 |
# |
# |
495 |
if ids: |
if ids: |
496 |
self.grid.SelectRow(ids[0], True) |
self.grid.SelectRow(ids[0], True) |
497 |
|
|
498 |
finally: |
finally: |
499 |
wxEndBusyCursor() |
ThubanEndBusyCursor() |
500 |
|
|
501 |
def OnSaveAs(self, event): |
def OnSaveAs(self, event): |
502 |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
542 |
self.layer = layer |
self.layer = layer |
543 |
self.grid.Subscribe(ROW_SELECTED, self.rows_selected) |
self.grid.Subscribe(ROW_SELECTED, self.rows_selected) |
544 |
self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes) |
self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes) |
545 |
|
self.map = self.parent.Map() |
546 |
|
self.map.Subscribe(MAP_LAYERS_REMOVED, self.map_layers_removed) |
547 |
|
|
548 |
# if there is already a selection present, update the grid |
# if there is already a selection present, update the grid |
549 |
# accordingly |
# accordingly |
566 |
"""Override the derived method to first unsubscribed.""" |
"""Override the derived method to first unsubscribed.""" |
567 |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
568 |
self.grid.Unsubscribe(ROW_SELECTED, self.rows_selected) |
self.grid.Unsubscribe(ROW_SELECTED, self.rows_selected) |
569 |
|
self.map.Unsubscribe(MAP_LAYERS_REMOVED, self.map_layers_removed) |
570 |
QueryTableFrame.OnClose(self, event) |
QueryTableFrame.OnClose(self, event) |
571 |
|
|
572 |
def select_shapes(self, layer, shapes): |
def select_shapes(self, layer, shapes): |
583 |
""" |
""" |
584 |
if self.layer is not None: |
if self.layer is not None: |
585 |
self.parent.SelectShapes(self.layer, rows) |
self.parent.SelectShapes(self.layer, rows) |
586 |
|
|
587 |
|
def map_layers_removed(self, *args): |
588 |
|
"""Receiver for the map's MAP_LAYERS_REMOVED message |
589 |
|
|
590 |
|
Close the dialog if the layer whose table we're showing is not |
591 |
|
in the map anymore. |
592 |
|
""" |
593 |
|
if self.layer not in self.map.Layers(): |
594 |
|
self.Close() |
595 |
|
|