1 |
bh |
6 |
# Copyright (c) 2001 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
__version__ = "$Revision$" |
9 |
|
|
|
10 |
|
|
from wxPython.wx import * |
11 |
|
|
from wxPython.grid import * |
12 |
|
|
|
13 |
|
|
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
14 |
|
|
FIELDTYPE_STRING |
15 |
|
|
import view |
16 |
bh |
30 |
from dialogs import NonModalDialog |
17 |
bh |
6 |
from messages import SELECTED_SHAPE |
18 |
|
|
|
19 |
|
|
wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER, |
20 |
|
|
FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT, |
21 |
|
|
FIELDTYPE_STRING: wxGRID_VALUE_STRING} |
22 |
|
|
|
23 |
|
|
class DataTable(wxPyGridTableBase): |
24 |
|
|
|
25 |
|
|
def __init__(self, table = None): |
26 |
|
|
wxPyGridTableBase.__init__(self) |
27 |
|
|
self.num_cols = 0 |
28 |
|
|
self.num_rows = 0 |
29 |
|
|
self.columns = [] |
30 |
|
|
self.table = None |
31 |
|
|
self.SetTable(table) |
32 |
|
|
|
33 |
|
|
def SetTable(self, table): |
34 |
|
|
self.table = table |
35 |
|
|
self.num_cols = table.field_count() |
36 |
|
|
self.num_rows = table.record_count() |
37 |
|
|
|
38 |
|
|
self.columns = [] |
39 |
|
|
for i in range(self.num_cols): |
40 |
|
|
type, name, len, decc = table.field_info(i) |
41 |
|
|
self.columns.append((name, wx_value_type_map[type], len, decc)) |
42 |
|
|
|
43 |
|
|
# |
44 |
|
|
# required methods for the wxPyGridTableBase interface |
45 |
|
|
# |
46 |
|
|
|
47 |
|
|
def GetNumberRows(self): |
48 |
|
|
return self.num_rows |
49 |
|
|
|
50 |
|
|
def GetNumberCols(self): |
51 |
|
|
return self.num_cols |
52 |
|
|
|
53 |
|
|
def IsEmptyCell(self, row, col): |
54 |
|
|
return 0 |
55 |
|
|
|
56 |
|
|
# Get/Set values in the table. The Python version of these |
57 |
|
|
# methods can handle any data-type, (as long as the Editor and |
58 |
|
|
# Renderer understands the type too,) not just strings as in the |
59 |
|
|
# C++ version. |
60 |
|
|
def GetValue(self, row, col): |
61 |
|
|
record = self.table.read_record(row) |
62 |
|
|
return record[self.columns[col][0]] |
63 |
|
|
|
64 |
|
|
def SetValue(self, row, col, value): |
65 |
|
|
pass |
66 |
|
|
|
67 |
|
|
# |
68 |
|
|
# Some optional methods |
69 |
|
|
# |
70 |
|
|
|
71 |
|
|
# Called when the grid needs to display labels |
72 |
|
|
def GetColLabelValue(self, col): |
73 |
|
|
return self.columns[col][0] |
74 |
|
|
|
75 |
|
|
# Called to determine the kind of editor/renderer to use by |
76 |
|
|
# default, doesn't necessarily have to be the same type used |
77 |
|
|
# nativly by the editor/renderer if they know how to convert. |
78 |
|
|
def GetTypeName(self, row, col): |
79 |
|
|
return self.columns[col][1] |
80 |
|
|
|
81 |
|
|
# Called to determine how the data can be fetched and stored by the |
82 |
|
|
# editor and renderer. This allows you to enforce some type-safety |
83 |
|
|
# in the grid. |
84 |
|
|
def CanGetValueAs(self, row, col, typeName): |
85 |
|
|
# perhaps we should allow conversion int->double? |
86 |
|
|
return self.GetTypeName(row, col) == typeName |
87 |
|
|
|
88 |
|
|
def CanSetValueAs(self, row, col, typeName): |
89 |
|
|
return self.CanGetValueAs(row, col, typeName) |
90 |
|
|
|
91 |
|
|
|
92 |
|
|
# Thuban stuff |
93 |
|
|
def SelectRow(self, row): |
94 |
|
|
import main |
95 |
|
|
interactor = main.app.interactor |
96 |
|
|
return interactor.SelectShape(self.table, row) |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
class TableGrid(wxGrid): |
100 |
|
|
|
101 |
|
|
def __init__(self, parent, table = None): |
102 |
|
|
wxGrid.__init__(self, parent, -1) |
103 |
|
|
|
104 |
|
|
self.table = DataTable(table) |
105 |
|
|
|
106 |
|
|
# The second parameter means that the grid is to take ownership |
107 |
|
|
# of the table and will destroy it when done. Otherwise you |
108 |
|
|
# would need to keep a reference to it and call it's Destroy |
109 |
|
|
# method later. |
110 |
|
|
self.SetTable(self.table, true) |
111 |
|
|
|
112 |
|
|
#self.SetMargins(0,0) |
113 |
|
|
self.AutoSizeColumns(false) |
114 |
|
|
|
115 |
|
|
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
116 |
|
|
|
117 |
|
|
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
118 |
|
|
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
119 |
|
|
|
120 |
|
|
def SetTableObject(self, table): |
121 |
|
|
self.table.SetTable(table) |
122 |
|
|
|
123 |
|
|
def OnRangeSelect(self, event): |
124 |
|
|
if event.Selecting(): |
125 |
|
|
if not self.table.SelectRow(event.GetTopLeftCoords().GetRow()): |
126 |
|
|
event.Skip() |
127 |
|
|
|
128 |
|
|
def OnSelectCell(self, event): |
129 |
|
|
if not self.table.SelectRow(event.GetRow()): |
130 |
|
|
event.Skip() |
131 |
|
|
|
132 |
|
|
def select_shape(self, layer, shape): |
133 |
bh |
30 |
if layer is not None and layer.table is self.table.table \ |
134 |
|
|
and shape is not None: |
135 |
bh |
6 |
self.SelectRow(shape) |
136 |
|
|
self.SetGridCursor(shape, 0) |
137 |
|
|
self.MakeCellVisible(shape, 0) |
138 |
|
|
|
139 |
|
|
|
140 |
bh |
30 |
class TableFrame(NonModalDialog): |
141 |
bh |
6 |
|
142 |
bh |
30 |
def __init__(self, parent, interactor, name, title, table = None): |
143 |
|
|
NonModalDialog.__init__(self, parent, interactor, name, title) |
144 |
|
|
self.grid = TableGrid(self, table) |
145 |
|
|
self.interactor.Subscribe(SELECTED_SHAPE, self.select_shape) |
146 |
|
|
|
147 |
|
|
def OnClose(self, event): |
148 |
|
|
self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape) |
149 |
|
|
NonModalDialog.OnClose(self, event) |
150 |
|
|
|
151 |
|
|
def select_shape(self, layer, shape): |
152 |
|
|
self.grid.select_shape(layer, shape) |