1 |
bh |
77 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
bh |
6 |
# 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 |
bh |
34 |
from Thuban.Lib.connector import Publisher |
14 |
bh |
6 |
from Thuban.Model.table import FIELDTYPE_INT, FIELDTYPE_DOUBLE, \ |
15 |
|
|
FIELDTYPE_STRING |
16 |
|
|
import view |
17 |
bh |
30 |
from dialogs import NonModalDialog |
18 |
bh |
6 |
from messages import SELECTED_SHAPE |
19 |
|
|
|
20 |
|
|
wx_value_type_map = {FIELDTYPE_INT: wxGRID_VALUE_NUMBER, |
21 |
|
|
FIELDTYPE_DOUBLE: wxGRID_VALUE_FLOAT, |
22 |
|
|
FIELDTYPE_STRING: wxGRID_VALUE_STRING} |
23 |
|
|
|
24 |
bh |
34 |
ROW_SELECTED = "ROW_SELECTED" |
25 |
|
|
|
26 |
|
|
|
27 |
bh |
6 |
class DataTable(wxPyGridTableBase): |
28 |
|
|
|
29 |
bh |
34 |
"""Wrapper around a Thuban table object suitable for a wxGrid""" |
30 |
|
|
|
31 |
bh |
6 |
def __init__(self, table = None): |
32 |
|
|
wxPyGridTableBase.__init__(self) |
33 |
|
|
self.num_cols = 0 |
34 |
|
|
self.num_rows = 0 |
35 |
|
|
self.columns = [] |
36 |
|
|
self.table = None |
37 |
|
|
self.SetTable(table) |
38 |
|
|
|
39 |
|
|
def SetTable(self, table): |
40 |
|
|
self.table = table |
41 |
|
|
self.num_cols = table.field_count() |
42 |
|
|
self.num_rows = table.record_count() |
43 |
|
|
|
44 |
|
|
self.columns = [] |
45 |
|
|
for i in range(self.num_cols): |
46 |
|
|
type, name, len, decc = table.field_info(i) |
47 |
|
|
self.columns.append((name, wx_value_type_map[type], len, decc)) |
48 |
|
|
|
49 |
|
|
# |
50 |
|
|
# required methods for the wxPyGridTableBase interface |
51 |
|
|
# |
52 |
|
|
|
53 |
|
|
def GetNumberRows(self): |
54 |
|
|
return self.num_rows |
55 |
|
|
|
56 |
|
|
def GetNumberCols(self): |
57 |
|
|
return self.num_cols |
58 |
|
|
|
59 |
|
|
def IsEmptyCell(self, row, col): |
60 |
|
|
return 0 |
61 |
|
|
|
62 |
|
|
# Get/Set values in the table. The Python version of these |
63 |
|
|
# methods can handle any data-type, (as long as the Editor and |
64 |
|
|
# Renderer understands the type too,) not just strings as in the |
65 |
|
|
# C++ version. |
66 |
|
|
def GetValue(self, row, col): |
67 |
|
|
record = self.table.read_record(row) |
68 |
|
|
return record[self.columns[col][0]] |
69 |
|
|
|
70 |
|
|
def SetValue(self, row, col, value): |
71 |
|
|
pass |
72 |
|
|
|
73 |
|
|
# |
74 |
|
|
# Some optional methods |
75 |
|
|
# |
76 |
|
|
|
77 |
|
|
# Called when the grid needs to display labels |
78 |
|
|
def GetColLabelValue(self, col): |
79 |
|
|
return self.columns[col][0] |
80 |
|
|
|
81 |
|
|
# Called to determine the kind of editor/renderer to use by |
82 |
|
|
# default, doesn't necessarily have to be the same type used |
83 |
|
|
# nativly by the editor/renderer if they know how to convert. |
84 |
|
|
def GetTypeName(self, row, col): |
85 |
|
|
return self.columns[col][1] |
86 |
|
|
|
87 |
|
|
# Called to determine how the data can be fetched and stored by the |
88 |
|
|
# editor and renderer. This allows you to enforce some type-safety |
89 |
|
|
# in the grid. |
90 |
|
|
def CanGetValueAs(self, row, col, typeName): |
91 |
|
|
# perhaps we should allow conversion int->double? |
92 |
|
|
return self.GetTypeName(row, col) == typeName |
93 |
|
|
|
94 |
|
|
def CanSetValueAs(self, row, col, typeName): |
95 |
|
|
return self.CanGetValueAs(row, col, typeName) |
96 |
|
|
|
97 |
|
|
|
98 |
bh |
34 |
class TableGrid(wxGrid, Publisher): |
99 |
bh |
6 |
|
100 |
bh |
34 |
"""A grid view for a Thuban table""" |
101 |
bh |
6 |
|
102 |
|
|
def __init__(self, parent, table = None): |
103 |
|
|
wxGrid.__init__(self, parent, -1) |
104 |
|
|
|
105 |
|
|
self.table = DataTable(table) |
106 |
|
|
|
107 |
|
|
# The second parameter means that the grid is to take ownership |
108 |
|
|
# of the table and will destroy it when done. Otherwise you |
109 |
bh |
77 |
# would need to keep a reference to it and call its Destroy |
110 |
bh |
6 |
# method later. |
111 |
|
|
self.SetTable(self.table, true) |
112 |
|
|
|
113 |
|
|
#self.SetMargins(0,0) |
114 |
|
|
|
115 |
bh |
173 |
# AutoSizeColumns would allow us to make the grid have optimal |
116 |
|
|
# column widths automatically but it would cause a traversal of |
117 |
|
|
# the entire table which for large .dbf files can take a very |
118 |
|
|
# long time. |
119 |
|
|
#self.AutoSizeColumns(false) |
120 |
|
|
|
121 |
bh |
6 |
self.SetSelectionMode(wxGrid.wxGridSelectRows) |
122 |
bh |
278 |
|
123 |
bh |
6 |
EVT_GRID_RANGE_SELECT(self, self.OnRangeSelect) |
124 |
|
|
EVT_GRID_SELECT_CELL(self, self.OnSelectCell) |
125 |
|
|
|
126 |
|
|
def SetTableObject(self, table): |
127 |
|
|
self.table.SetTable(table) |
128 |
|
|
|
129 |
|
|
def OnRangeSelect(self, event): |
130 |
|
|
if event.Selecting(): |
131 |
bh |
34 |
self.issue(ROW_SELECTED, event.GetTopLeftCoords().GetRow()) |
132 |
bh |
6 |
|
133 |
|
|
def OnSelectCell(self, event): |
134 |
bh |
34 |
self.issue(ROW_SELECTED, event.GetRow()) |
135 |
bh |
6 |
|
136 |
bh |
278 |
|
137 |
|
|
class LayerTableGrid(TableGrid): |
138 |
|
|
|
139 |
|
|
"""Table grid for the layer tables. |
140 |
|
|
|
141 |
|
|
The LayerTableGrid is basically the same as a TableGrid but it's |
142 |
|
|
selection is usually coupled to the selected object in the map. |
143 |
|
|
""" |
144 |
|
|
|
145 |
bh |
6 |
def select_shape(self, layer, shape): |
146 |
bh |
278 |
"""Select the row corresponding to the specified shape and layer |
147 |
|
|
|
148 |
|
|
If layer is not the layer the table is associated with do |
149 |
|
|
nothing. If shape or layer is None also do nothing. |
150 |
|
|
""" |
151 |
|
|
print "LayerTableGrid.select_shape", layer, shape |
152 |
bh |
30 |
if layer is not None and layer.table is self.table.table \ |
153 |
|
|
and shape is not None: |
154 |
bh |
6 |
self.SelectRow(shape) |
155 |
|
|
self.SetGridCursor(shape, 0) |
156 |
|
|
self.MakeCellVisible(shape, 0) |
157 |
|
|
|
158 |
|
|
|
159 |
bh |
30 |
class TableFrame(NonModalDialog): |
160 |
bh |
6 |
|
161 |
bh |
34 |
"""Frame that displays a Thuban table in a grid view""" |
162 |
|
|
|
163 |
bh |
278 |
def __init__(self, parent, interactor, name, title, table): |
164 |
bh |
30 |
NonModalDialog.__init__(self, parent, interactor, name, title) |
165 |
bh |
278 |
self.table = table |
166 |
|
|
self.grid = self.make_grid(self.table) |
167 |
|
|
|
168 |
|
|
def make_grid(self, table): |
169 |
|
|
"""Return the table grid to use in the frame. |
170 |
|
|
|
171 |
|
|
The default implementation returns a TableGrid instance. |
172 |
|
|
Override in derived classes to use different grid classes. |
173 |
|
|
""" |
174 |
|
|
return TableGrid(self, table) |
175 |
|
|
|
176 |
|
|
|
177 |
|
|
class LayerTableFrame(TableFrame): |
178 |
|
|
|
179 |
|
|
"""Frame that displays a layer table in a grid view |
180 |
|
|
|
181 |
|
|
A LayerTableFrame is TableFrame whose selection is connected to the |
182 |
|
|
selected object in a map. |
183 |
|
|
""" |
184 |
|
|
|
185 |
|
|
def __init__(self, parent, interactor, name, title, layer, table): |
186 |
|
|
TableFrame.__init__(self, parent, interactor, name, title, table) |
187 |
bh |
34 |
self.layer = layer |
188 |
|
|
self.grid.Subscribe(ROW_SELECTED, self.row_selected) |
189 |
bh |
30 |
self.interactor.Subscribe(SELECTED_SHAPE, self.select_shape) |
190 |
|
|
|
191 |
bh |
278 |
def make_grid(self, table): |
192 |
|
|
"""Override the derived method to return a LayerTableGrid. |
193 |
|
|
""" |
194 |
|
|
return LayerTableGrid(self, table) |
195 |
|
|
|
196 |
bh |
30 |
def OnClose(self, event): |
197 |
|
|
self.interactor.Unsubscribe(SELECTED_SHAPE, self.select_shape) |
198 |
bh |
278 |
TableFrame.OnClose(self, event) |
199 |
bh |
30 |
|
200 |
|
|
def select_shape(self, layer, shape): |
201 |
|
|
self.grid.select_shape(layer, shape) |
202 |
bh |
34 |
|
203 |
|
|
def row_selected(self, row): |
204 |
|
|
if self.layer is not None: |
205 |
|
|
self.interactor.SelectLayerAndShape(self.layer, row) |