25 |
|
|
26 |
ROW_SELECTED = "ROW_SELECTED" |
ROW_SELECTED = "ROW_SELECTED" |
27 |
|
|
28 |
|
QUERY_KEY = 'S' |
29 |
|
|
30 |
class DataTable(wxPyGridTableBase): |
class DataTable(wxPyGridTableBase): |
31 |
|
|
133 |
|
|
134 |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
135 |
|
|
136 |
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
#EVT_GRID_RANGE_SELECT(self, None) |
137 |
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
#EVT_GRID_SELECT_CELL(self, None) |
138 |
|
#EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
139 |
|
#EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
140 |
|
|
141 |
|
self.ToggleEventListeners(True) |
142 |
|
|
143 |
def SetTableObject(self, table): |
def SetTableObject(self, table): |
144 |
self.table.SetTable(table) |
self.table.SetTable(table) |
161 |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
self.issue(ROW_SELECTED, self.GetSelectedRows()) |
162 |
event.Skip() |
event.Skip() |
163 |
|
|
164 |
|
def ToggleEventListeners(self, on): |
165 |
|
if on: |
166 |
|
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
167 |
|
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
168 |
|
else: |
169 |
|
EVT_GRID_RANGE_SELECT(self, None) |
170 |
|
EVT_GRID_SELECT_CELL(self, None) |
171 |
|
|
172 |
def disallow_messages(self): |
def disallow_messages(self): |
173 |
"""Disallow messages to be send. |
"""Disallow messages to be send. |
174 |
|
|
253 |
|
|
254 |
|
|
255 |
ID_QUERY = 4001 |
ID_QUERY = 4001 |
256 |
ID_SAVEAS = 4002 |
ID_EXPORT = 4002 |
257 |
|
|
258 |
class LayerTableFrame(TableFrame): |
class QueryTableFrame(TableFrame): |
259 |
|
|
260 |
"""Frame that displays a layer table in a grid view |
"""Frame that displays a table in a grid view and offers user actions |
261 |
|
selection and export |
262 |
|
|
263 |
A LayerTableFrame is TableFrame whose selection is connected to the |
A LayerTableFrame is TableFrame whose selection is connected to the |
264 |
selected object in a map. |
selected object in a map. |
265 |
""" |
""" |
266 |
|
|
267 |
def __init__(self, parent, name, title, layer, table): |
def __init__(self, parent, name, title, table): |
268 |
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) |
|
269 |
|
|
270 |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
self.combo_fields = wxComboBox(self, -1, style=wxCB_READONLY) |
271 |
self.choice_comp = wxChoice(self, -1, |
self.choice_comp = wxChoice(self, -1, |
272 |
choices=["<", "<=", "=", "<>", ">=", ">"]) |
choices=["<", "<=", "==", "!=", ">=", ">"]) |
273 |
self.combo_value = wxComboBox(self, -1) |
self.combo_value = wxComboBox(self, -1) |
274 |
self.choice_action = wxChoice(self, -1, |
self.choice_action = wxChoice(self, -1, |
275 |
choices=[_("Replace Selection"), |
choices=[_("Replace Selection"), |
277 |
_("Add to Selection")]) |
_("Add to Selection")]) |
278 |
|
|
279 |
button_query = wxButton(self, ID_QUERY, _("Query")) |
button_query = wxButton(self, ID_QUERY, _("Query")) |
280 |
button_saveas = wxButton(self, ID_SAVEAS, _("Save As...")) |
button_saveas = wxButton(self, ID_EXPORT, _("Export")) |
281 |
|
|
282 |
self.grid.SetSize((400, 200)) |
self.grid.SetSize((400, 200)) |
283 |
|
|
284 |
self.combo_value.Append("") |
self.combo_value.Append("") |
285 |
for i in range(table.field_count()): |
for i in range(table.NumColumns()): |
286 |
type, name, len, decc = layer.table.field_info(i) |
name = table.Column(i).name |
287 |
self.combo_fields.Append(name) |
self.combo_fields.Append(name) |
288 |
self.combo_value.Append(name) |
self.combo_value.Append(name) |
289 |
|
|
290 |
# assume at least one field? |
# assume at least one field? |
291 |
self.combo_fields.SetSelection(0) |
self.combo_fields.SetSelection(0) |
292 |
self.combo_value.SetSelection(0) |
self.combo_value.SetSelection(0) |
293 |
|
|
294 |
topBox = wxBoxSizer(wxVERTICAL) |
topBox = wxBoxSizer(wxVERTICAL) |
295 |
|
|
296 |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, _("Selections")), |
sizer = wxStaticBoxSizer(wxStaticBox(self, -1, _("Selection")), |
297 |
wxHORIZONTAL) |
wxHORIZONTAL) |
298 |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_fields, 1, wxEXPAND|wxALL, 4) |
299 |
sizer.Add(self.choice_comp, 0, wxALL, 4) |
sizer.Add(self.choice_comp, 0, wxALL, 4) |
300 |
sizer.Add(self.combo_value, 1, wxEXPAND|wxALL, 4) |
sizer.Add(self.combo_value, 1, wxEXPAND|wxALL, 4) |
301 |
sizer.Add(self.choice_action, 0, wxALL, 4) |
sizer.Add(self.choice_action, 0, wxALL, 4) |
302 |
sizer.Add(button_query, 0, wxALL, 4) |
sizer.Add(button_query, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
303 |
sizer.Add(40, 20, 0, wxALL, 4) |
sizer.Add(40, 20, 0, wxALL, 4) |
304 |
sizer.Add(button_saveas, 0, wxALL, 4) |
sizer.Add(button_saveas, 0, wxALL | wxALIGN_CENTER_VERTICAL, 4) |
305 |
|
|
306 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
307 |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
topBox.Add(self.grid, 1, wxEXPAND|wxALL, 0) |
311 |
topBox.Fit(self) |
topBox.Fit(self) |
312 |
topBox.SetSizeHints(self) |
topBox.SetSizeHints(self) |
313 |
|
|
314 |
|
self.grid.SetFocus() |
315 |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
EVT_BUTTON(self, ID_QUERY, self.OnQuery) |
316 |
EVT_BUTTON(self, ID_SAVEAS, self.OnSaveAs) |
EVT_BUTTON(self, ID_EXPORT, self.OnSaveAs) |
317 |
|
EVT_KEY_DOWN(self.grid, self.OnKeyDown) |
318 |
|
|
319 |
|
def OnKeyDown(self, event): |
320 |
|
"""Catch query key from grid""" |
321 |
|
if event.AltDown() and event.GetKeyCode() == ord(QUERY_KEY): |
322 |
|
self.combo_fields.SetFocus() |
323 |
|
self.combo_fields.refocus = True |
324 |
|
else: |
325 |
|
event.Skip() |
326 |
|
|
327 |
|
|
328 |
def OnQuery(self, event): |
def OnQuery(self, event): |
329 |
wxBeginBusyCursor() |
wxBeginBusyCursor() |
330 |
|
|
331 |
if self.combo_value.GetSelection() < 1: |
if self.combo_value.GetSelection() < 1: |
332 |
value = self.combo_value.GetValue() |
value = self.combo_value.GetValue() |
|
print value |
|
333 |
else: |
else: |
334 |
value = self.table.Column(self.combo_value.GetValue()) |
value = self.table.Column(self.combo_value.GetValue()) |
335 |
|
|
336 |
#ids = self.table.Query( |
ids = self.table.SimpleQuery( |
337 |
#self.table.Column(self.combo_fields.GetStringSelection()), |
self.table.Column(self.combo_fields.GetStringSelection()), |
338 |
#self.choice_comp.GetStringSelection(), |
self.choice_comp.GetStringSelection(), |
339 |
#value) |
value) |
340 |
|
|
341 |
choice = self.choice_action.GetSelection() |
choice = self.choice_action.GetSelection() |
342 |
|
|
343 |
|
# |
344 |
|
# what used to be nice code got became a bit ugly because |
345 |
|
# each time we select a row a message is sent to the grid |
346 |
|
# which we are listening for and then we send further |
347 |
|
# messages. |
348 |
|
# |
349 |
|
# now, we disable those listeners select everything but |
350 |
|
# the first item, reenable the listeners, and select |
351 |
|
# the first element, which causes everything to be |
352 |
|
# updated properly. |
353 |
|
# |
354 |
|
self.grid.ToggleEventListeners(False) |
355 |
|
|
356 |
if choice == 0: |
if choice == 0: |
|
ids = [1, 2, 3, 4, 5] |
|
357 |
# Replace Selection |
# Replace Selection |
358 |
self.grid.ClearSelection() |
self.grid.ClearSelection() |
|
for id in ids: |
|
|
self.grid.SelectRow(id, True) |
|
359 |
elif choice == 1: |
elif choice == 1: |
|
ids = [1, 3, 5] |
|
360 |
# Refine Selection |
# Refine Selection |
361 |
sel = dict([(i, 0) for i in self.parent.SelectedShapes()]) |
sel = self.get_selected() |
362 |
self.grid.ClearSelection() |
self.grid.ClearSelection() |
363 |
for id in filter(sel.has_key, ids): |
ids = filter(sel.has_key, ids) |
|
self.grid.SelectRow(id, True) |
|
364 |
elif choice == 2: |
elif choice == 2: |
|
ids = [2, 4] |
|
365 |
# Add to Selection |
# Add to Selection |
366 |
for id in ids: |
pass |
367 |
|
|
368 |
|
# |
369 |
|
# select the rows (all but the first) |
370 |
|
# |
371 |
|
firsttime = True |
372 |
|
for id in ids: |
373 |
|
if firsttime: |
374 |
|
firsttime = False |
375 |
|
else: |
376 |
self.grid.SelectRow(id, True) |
self.grid.SelectRow(id, True) |
377 |
|
|
378 |
|
self.grid.ToggleEventListeners(True) |
379 |
|
|
380 |
|
# |
381 |
|
# select the first row |
382 |
|
# |
383 |
|
if ids: |
384 |
|
self.grid.SelectRow(ids[0], True) |
385 |
|
|
386 |
wxEndBusyCursor() |
wxEndBusyCursor() |
387 |
|
|
388 |
def OnSaveAs(self, event): |
def OnSaveAs(self, event): |
389 |
dlg = wxFileDialog(self, _("Save Table As"), ".", "", |
dlg = wxFileDialog(self, _("Export Table To"), ".", "", |
390 |
"DBF Files (*.dbf)|*.dbf|" + |
_("DBF Files (*.dbf)|*.dbf|") + |
391 |
"CSV Files (*.csv)|*.csv|" + |
_("CSV Files (*.csv)|*.csv|") + |
392 |
"All Files (*.*)|*.*", |
_("All Files (*.*)|*.*"), |
393 |
wxSAVE|wxOVERWRITE_PROMPT) |
wxSAVE|wxOVERWRITE_PROMPT) |
394 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
395 |
pass |
pass |
396 |
|
|
397 |
dlg.Destroy() |
dlg.Destroy() |
398 |
|
|
399 |
|
def OnClose(self, event): |
400 |
|
TableFrame.OnClose(self, event) |
401 |
|
|
402 |
|
def get_selected(self): |
403 |
|
"""Return a dictionary of the selected rows. |
404 |
|
|
405 |
|
The dictionary has sthe indexes as keys.""" |
406 |
|
return dict([(i, 0) for i in self.grid.GetSelectedRows()]) |
407 |
|
|
408 |
|
class LayerTableFrame(QueryTableFrame): |
409 |
|
|
410 |
|
"""Frame that displays a layer table in a grid view |
411 |
|
|
412 |
|
A LayerTableFrame is a QueryTableFrame whose selection is connected to the |
413 |
|
selected object in a map. |
414 |
|
""" |
415 |
|
|
416 |
|
def __init__(self, parent, name, title, layer, table): |
417 |
|
QueryTableFrame.__init__(self, parent, name, title, table) |
418 |
|
self.layer = layer |
419 |
|
self.grid.Subscribe(ROW_SELECTED, self.rows_selected) |
420 |
|
self.parent.Subscribe(SHAPES_SELECTED, self.select_shapes) |
421 |
|
|
422 |
def make_grid(self, table): |
def make_grid(self, table): |
423 |
"""Override the derived method to return a LayerTableGrid. |
"""Override the derived method to return a LayerTableGrid. |
424 |
""" |
""" |
425 |
return LayerTableGrid(self, table) |
return LayerTableGrid(self, table) |
426 |
|
|
427 |
|
def get_selected(self): |
428 |
|
"""Override the derived method to return a dictionary of the selected |
429 |
|
rows. |
430 |
|
""" |
431 |
|
return dict([(i, 0) for i in self.parent.SelectedShapes()]) |
432 |
|
|
433 |
def OnClose(self, event): |
def OnClose(self, event): |
434 |
|
"""Override the derived method to first unsubscribed.""" |
435 |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
self.parent.Unsubscribe(SHAPES_SELECTED, self.select_shapes) |
436 |
TableFrame.OnClose(self, event) |
QueryTableFrame.OnClose(self, event) |
437 |
|
|
438 |
def select_shapes(self, layer, shapes): |
def select_shapes(self, layer, shapes): |
439 |
"""Subscribed to the SHAPES_SELECTED message. |
"""Subscribed to the SHAPES_SELECTED message. |
444 |
self.grid.select_shapes(layer, shapes) |
self.grid.select_shapes(layer, shapes) |
445 |
|
|
446 |
def rows_selected(self, rows): |
def rows_selected(self, rows): |
447 |
|
"""Return the selected rows of the layer as they are returned |
448 |
|
by Layer.SelectShapes(). |
449 |
|
""" |
450 |
if self.layer is not None: |
if self.layer is not None: |
451 |
self.parent.SelectShapes(self.layer, rows) |
self.parent.SelectShapes(self.layer, rows) |