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