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, |
29 |
|
|
30 |
ROW_SELECTED = "ROW_SELECTED" |
ROW_SELECTED = "ROW_SELECTED" |
31 |
|
|
32 |
|
QUERY_KEY = 'S' |
33 |
|
|
34 |
class DataTable(wxPyGridTableBase): |
class DataTable(wxPyGridTableBase): |
35 |
|
|
125 |
# of the table and will destroy it when done. Otherwise you |
# of the table and will destroy it when done. Otherwise you |
126 |
# would need to keep a reference to it and call its Destroy |
# would need to keep a reference to it and call its Destroy |
127 |
# method later. |
# method later. |
128 |
self.SetTable(self.table, true) |
self.SetTable(self.table, True) |
129 |
|
|
130 |
#self.SetMargins(0,0) |
#self.SetMargins(0,0) |
131 |
|
|
133 |
# column widths automatically but it would cause a traversal of |
# column widths automatically but it would cause a traversal of |
134 |
# the entire table which for large .dbf files can take a very |
# the entire table which for large .dbf files can take a very |
135 |
# long time. |
# long time. |
136 |
#self.AutoSizeColumns(false) |
#self.AutoSizeColumns(False) |
137 |
|
|
138 |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
139 |
|
|
140 |
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
#EVT_GRID_RANGE_SELECT(self, None) |
141 |
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
#EVT_GRID_SELECT_CELL(self, None) |
142 |
|
#EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
143 |
|
#EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
144 |
|
|
145 |
|
self.ToggleEventListeners(True) |
146 |
|
|
147 |
def SetTableObject(self, table): |
def SetTableObject(self, table): |
148 |
self.table.SetTable(table) |
self.table.SetTable(table) |
165 |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
166 |
event.Skip() |
event.Skip() |
167 |
|
|
168 |
|
def ToggleEventListeners(self, on): |
169 |
|
if on: |
170 |
|
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
171 |
|
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
172 |
|
else: |
173 |
|
EVT_GRID_RANGE_SELECT(self, None) |
174 |
|
EVT_GRID_SELECT_CELL(self, None) |
175 |
|
|
176 |
def disallow_messages(self): |
def disallow_messages(self): |
177 |
"""Disallow messages to be send. |
"""Disallow messages to be send. |
178 |
|
|
238 |
self.allow_messages() |
self.allow_messages() |
239 |
|
|
240 |
|
|
241 |
class TableFrame(NonModalDialog): |
class TableFrame(NonModalNonParentDialog): |
242 |
|
|
243 |
"""Frame that displays a Thuban table in a grid view""" |
"""Frame that displays a Thuban table in a grid view""" |
244 |
|
|
245 |
def __init__(self, parent, name, title, table): |
def __init__(self, parent, name, title, table): |
246 |
NonModalDialog.__init__(self, parent, name, title) |
NonModalNonParentDialog.__init__(self, parent, name, title) |
247 |
self.table = table |
self.table = table |
248 |
self.grid = self.make_grid(self.table) |
self.grid = self.make_grid(self.table) |
249 |
|
self.app = self.parent.application |
250 |
|
self.app.Subscribe(SESSION_REPLACED, self.close_on_session_replaced) |
251 |
|
self.session = self.app.Session() |
252 |
|
self.session.Subscribe(TABLE_REMOVED, self.close_on_table_removed) |
253 |
|
|
254 |
def make_grid(self, table): |
def make_grid(self, table): |
255 |
"""Return the table grid to use in the frame. |
"""Return the table grid to use in the frame. |
259 |
""" |
""" |
260 |
return TableGrid(self, table) |
return TableGrid(self, table) |
261 |
|
|
262 |
|
def OnClose(self, event): |
263 |
|
self.app.Unsubscribe(SESSION_REPLACED, self.close_on_session_replaced) |
264 |
|
self.session.Unsubscribe(TABLE_REMOVED, self.close_on_table_removed) |
265 |
|
NonModalNonParentDialog.OnClose(self, event) |
266 |
|
|
267 |
|
def close_on_session_replaced(self, *args): |
268 |
|
"""Subscriber for the SESSION_REPLACED messages. |
269 |
|
|
270 |
|
The table frame is tied to a session so close the window when |
271 |
|
the session changes. |
272 |
|
""" |
273 |
|
self.Close() |
274 |
|
|
275 |
|
def close_on_table_removed(self, table): |
276 |
|
"""Subscriber for the TABLE_REMOVED messages. |
277 |
|
|
278 |
|
The table frame is tied to a particular table so close the |
279 |
|
window when the table is removed. |
280 |
|
""" |
281 |
|
if table is self.table: |
282 |
|
self.Close() |
283 |
|
|
284 |
|
|
285 |
ID_QUERY = 4001 |
ID_QUERY = 4001 |
286 |
ID_SAVEAS = 4002 |
ID_EXPORT = 4002 |
287 |
|
|
288 |
class LayerTableFrame(TableFrame): |
class QueryTableFrame(TableFrame): |
289 |
|
|
290 |
"""Frame that displays a layer table in a grid view |
"""Frame that displays a table in a grid view and offers user actions |
291 |
|
selection and export |
292 |
|
|
293 |
A LayerTableFrame is TableFrame whose selection is connected to the |
A LayerTableFrame is TableFrame whose selection is connected to the |
294 |
selected object in a map. |
selected object in a map. |
295 |
""" |
""" |
296 |
|
|
297 |
def __init__(self, parent, name, title, layer, table): |
def __init__(self, parent, name, title, table): |
298 |
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) |
|
299 |
|
|
300 |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
301 |
self.choice_comp = wxChoice(self, -1, |
self.choice_comp = wxChoice(self, -1, |
307 |
_("Add to Selection")]) |
_("Add to Selection")]) |
308 |
|
|
309 |
button_query = wxButton(self, ID_QUERY, _("Query")) |
button_query = wxButton(self, ID_QUERY, _("Query")) |
310 |
button_saveas = wxButton(self, ID_SAVEAS, _("Save As...")) |
button_saveas = wxButton(self, ID_EXPORT, _("Export")) |
311 |
|
|
312 |
self.grid.SetSize((400, 200)) |
self.grid.SetSize((400, 200)) |
313 |
|
|
323 |
|
|
324 |
topBox = wxBoxSizer(wxVERTICAL) |
topBox = wxBoxSizer(wxVERTICAL) |
325 |
|
|
326 |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, _("Selections")), |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, |
327 |
|
_("Selection")), |
328 |
wxHORIZONTAL) |
wxHORIZONTAL) |
329 |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
330 |
sizer.Add(self.choice_comp, 0, wxALL, 4) |
sizer.Add(self.choice_comp, 0, wxALL, 4) |
331 |
sizer.Add(self.combo_value, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_value, 1, wxEXPAND|wxALL, 4) |
332 |
sizer.Add(self.choice_action, 0, wxALL, 4) |
sizer.Add(self.choice_action, 0, wxALL, 4) |
333 |
sizer.Add(button_query, 0, wxALL, 4) |
sizer.Add(button_query, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
334 |
sizer.Add(40, 20, 0, wxALL, 4) |
sizer.Add(40, 20, 0, wxALL, 4) |
335 |
sizer.Add(button_saveas, 0, wxALL, 4) |
sizer.Add(button_saveas, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
336 |
|
|
337 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
338 |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
342 |
topBox.Fit(self) |
topBox.Fit(self) |
343 |
topBox.SetSizeHints(self) |
topBox.SetSizeHints(self) |
344 |
|
|
345 |
|
self.grid.SetFocus() |
346 |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
347 |
EVT_BUTTON(self, ID_SAVEAS, self.OnSaveAs) |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
348 |
|
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
349 |
|
|
350 |
|
def OnKeyDown(self, event): |
351 |
|
"""Catch query key from grid""" |
352 |
|
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
353 |
|
self.combo_fields.SetFocus() |
354 |
|
self.combo_fields.refocus = True |
355 |
|
else: |
356 |
|
event.Skip() |
357 |
|
|
358 |
|
|
359 |
def OnQuery(self, event): |
def OnQuery(self, event): |
360 |
wxBeginBusyCursor() |
wxBeginBusyCursor() |
371 |
|
|
372 |
choice = self.choice_action.GetSelection() |
choice = self.choice_action.GetSelection() |
373 |
|
|
374 |
|
# |
375 |
|
# what used to be nice code got became a bit ugly because |
376 |
|
# each time we select a row a message is sent to the grid |
377 |
|
# which we are listening for and then we send further |
378 |
|
# messages. |
379 |
|
# |
380 |
|
# now, we disable those listeners select everything but |
381 |
|
# the first item, reenable the listeners, and select |
382 |
|
# the first element, which causes everything to be |
383 |
|
# updated properly. |
384 |
|
# |
385 |
|
self.grid.ToggleEventListeners(False) |
386 |
|
|
387 |
if choice == 0: |
if choice == 0: |
388 |
# Replace Selection |
# Replace Selection |
389 |
self.grid.ClearSelection() |
self.grid.ClearSelection() |
|
for id in ids: |
|
|
self.grid.SelectRow(id, True) |
|
390 |
elif choice == 1: |
elif choice == 1: |
391 |
# Refine Selection |
# Refine Selection |
392 |
sel = dict([(i, 0) for i in self.parent.SelectedShapes()]) |
sel = self.get_selected() |
393 |
self.grid.ClearSelection() |
self.grid.ClearSelection() |
394 |
for id in filter(sel.has_key, ids): |
ids = filter(sel.has_key, ids) |
|
self.grid.SelectRow(id, True) |
|
395 |
elif choice == 2: |
elif choice == 2: |
396 |
# Add to Selection |
# Add to Selection |
397 |
for id in ids: |
pass |
398 |
|
|
399 |
|
# |
400 |
|
# select the rows (all but the first) |
401 |
|
# |
402 |
|
firsttime = True |
403 |
|
for id in ids: |
404 |
|
if firsttime: |
405 |
|
firsttime = False |
406 |
|
else: |
407 |
self.grid.SelectRow(id, True) |
self.grid.SelectRow(id, True) |
408 |
|
|
409 |
|
self.grid.ToggleEventListeners(True) |
410 |
|
|
411 |
|
# |
412 |
|
# select the first row |
413 |
|
# |
414 |
|
if ids: |
415 |
|
self.grid.SelectRow(ids[0], True) |
416 |
|
|
417 |
wxEndBusyCursor() |
wxEndBusyCursor() |
418 |
|
|
419 |
def OnSaveAs(self, event): |
def OnSaveAs(self, event): |
420 |
dlg = wxFileDialog(self, _("Save Table As"), ".", "", |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
421 |
"DBF Files (*.dbf)|*.dbf|" + |
_("DBF Files (*.dbf)|*.dbf|") + |
422 |
"CSV Files (*.csv)|*.csv|" + |
_("CSV Files (*.csv)|*.csv|") + |
423 |
"All Files (*.*)|*.*", |
_("All Files (*.*)|*.*"), |
424 |
wxSAVE|wxOVERWRITE_PROMPT) |
wxSAVE|wxOVERWRITE_PROMPT) |
425 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
426 |
pass |
filename = dlg.GetPath() |
427 |
|
type = os.path.basename(filename).split('.')[-1:][0] |
428 |
dlg.Destroy() |
dlg.Destroy() |
429 |
|
if type.upper() == "DBF": |
430 |
|
table_to_dbf(self.table, filename) |
431 |
|
elif type.upper() == 'CSV': |
432 |
|
table_to_csv(self.table, filename) |
433 |
|
else: |
434 |
|
dlg = wxMessageDialog(None, "Unsupported format: %s" % type, |
435 |
|
"Table Export", wxOK|wxICON_WARNING) |
436 |
|
dlg.ShowModal() |
437 |
|
dlg.Destroy() |
438 |
|
else: |
439 |
|
dlg.Destroy() |
440 |
|
|
441 |
|
def OnClose(self, event): |
442 |
|
TableFrame.OnClose(self, event) |
443 |
|
|
444 |
|
def get_selected(self): |
445 |
|
"""Return a dictionary of the selected rows. |
446 |
|
|
447 |
|
The dictionary has sthe indexes as keys.""" |
448 |
|
return dict([(i, 0) for i in self.grid.GetSelectedRows()]) |
449 |
|
|
450 |
|
class LayerTableFrame(QueryTableFrame): |
451 |
|
|
452 |
|
"""Frame that displays a layer table in a grid view |
453 |
|
|
454 |
|
A LayerTableFrame is a QueryTableFrame whose selection is connected to the |
455 |
|
selected object in a map. |
456 |
|
""" |
457 |
|
|
458 |
|
def __init__(self, parent, name, title, layer, table): |
459 |
|
QueryTableFrame.__init__(self, parent, name, title, table) |
460 |
|
self.layer = layer |
461 |
|
self.grid.Subscribe(ROW_SELECTED, self.rows_selected) |
462 |
|
self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes) |
463 |
|
|
464 |
|
# if there is already a selection present, update the grid |
465 |
|
# accordingly |
466 |
|
sel = self.get_selected().keys() |
467 |
|
for i in sel: |
468 |
|
self.grid.SelectRow(i, True) |
469 |
|
|
470 |
def make_grid(self, table): |
def make_grid(self, table): |
471 |
"""Override the derived method to return a LayerTableGrid. |
"""Override the derived method to return a LayerTableGrid. |
472 |
""" |
""" |
473 |
return LayerTableGrid(self, table) |
return LayerTableGrid(self, table) |
474 |
|
|
475 |
|
def get_selected(self): |
476 |
|
"""Override the derived method to return a dictionary of the selected |
477 |
|
rows. |
478 |
|
""" |
479 |
|
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
480 |
|
|
481 |
def OnClose(self, event): |
def OnClose(self, event): |
482 |
|
"""Override the derived method to first unsubscribed.""" |
483 |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
484 |
TableFrame.OnClose(self, event) |
QueryTableFrame.OnClose(self, event) |
485 |
|
|
486 |
def select_shapes(self, layer, shapes): |
def select_shapes(self, layer, shapes): |
487 |
"""Subscribed to the SHAPES_SELECTED message. |
"""Subscribed to the SHAPES_SELECTED message. |
492 |
self.grid.select_shapes(layer, shapes) |
self.grid.select_shapes(layer, shapes) |
493 |
|
|
494 |
def rows_selected(self, rows): |
def rows_selected(self, rows): |
495 |
|
"""Return the selected rows of the layer as they are returned |
496 |
|
by Layer.SelectShapes(). |
497 |
|
""" |
498 |
if self.layer is not None: |
if self.layer is not None: |
499 |
self.parent.SelectShapes(self.layer, rows) |
self.parent.SelectShapes(self.layer, rows) |