1 |
jonathan |
888 |
# Copyright (c) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Jonathan Coles <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
bh |
1072 |
|
8 |
|
|
import sys |
9 |
jonathan |
888 |
from wxPython.wx import * |
10 |
bh |
1072 |
|
11 |
jonathan |
888 |
from Thuban import _ |
12 |
frank |
1000 |
|
13 |
|
|
from Thuban.Model.transientdb import TransientJoinedTable |
14 |
bh |
1072 |
from Thuban.Model.data import DerivedShapeStore |
15 |
|
|
from Thuban.UI.tableview import QueryTableFrame |
16 |
|
|
|
17 |
frank |
1000 |
ID_LEFT_TABLE = 4001 |
18 |
|
|
ID_RIGHT_TABLE = 4002 |
19 |
jonathan |
888 |
|
20 |
frank |
1000 |
CHOICE_WIDTH = 150 |
21 |
|
|
|
22 |
jonathan |
888 |
class JoinDialog(wxDialog): |
23 |
bh |
1072 |
|
24 |
frank |
1000 |
"""Table join dialog. |
25 |
jonathan |
888 |
|
26 |
frank |
1000 |
Join two tables (left/right) based on the user selected fields. |
27 |
jan |
1022 |
Opens a QueryTableFrame and registers the new table with the session. |
28 |
frank |
1000 |
""" |
29 |
|
|
|
30 |
jonathan |
888 |
def __init__(self, parent, title, session, layer = None): |
31 |
|
|
wxDialog.__init__(self, parent, -1, title, |
32 |
|
|
style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER) |
33 |
bh |
1072 |
self.layer = layer |
34 |
jonathan |
888 |
|
35 |
bh |
1072 |
if not layer: |
36 |
|
|
self.choice_left_table = wxChoice(self, ID_LEFT_TABLE) |
37 |
|
|
width, height = self.choice_left_table.GetSizeTuple() |
38 |
|
|
self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
39 |
|
|
self.left_table = None |
40 |
|
|
else: |
41 |
|
|
self.choice_left_table = None |
42 |
|
|
self.left_table = layer.ShapeStore().Table() |
43 |
|
|
|
44 |
frank |
1000 |
self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE) |
45 |
|
|
width, height = self.choice_right_table.GetSizeTuple() |
46 |
|
|
self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
47 |
jonathan |
888 |
|
48 |
frank |
1000 |
self.choice_left_field = wxChoice(self, -1) |
49 |
|
|
width, height = self.choice_left_field.GetSizeTuple() |
50 |
|
|
self.choice_left_field.SetSize(wxSize(CHOICE_WIDTH, height)) |
51 |
|
|
self.choice_right_field = wxChoice(self, -1) |
52 |
|
|
width, height = self.choice_right_field.GetSizeTuple() |
53 |
|
|
self.choice_right_field.SetSize(wxSize(CHOICE_WIDTH, height)) |
54 |
jonathan |
888 |
|
55 |
|
|
self.button_join = wxButton(self, wxID_OK, _("Join")) |
56 |
|
|
self.button_join.SetDefault() |
57 |
|
|
self.button_close = wxButton(self, wxID_CANCEL, _("Close")) |
58 |
|
|
|
59 |
frank |
1000 |
EVT_BUTTON(self, wxID_OK, self.OnJoin) |
60 |
jonathan |
888 |
|
61 |
bh |
1072 |
if self.choice_left_table is not None: |
62 |
|
|
self.choice_left_table.Append('Select ...', None) |
63 |
frank |
1000 |
self.choice_right_table.Append('Select ...', None) |
64 |
bh |
1072 |
|
65 |
frank |
1000 |
for t in session.Tables(): |
66 |
bh |
1072 |
if self.choice_left_table is not None: |
67 |
bh |
1521 |
self.choice_left_table.Append(t.Title(), t) |
68 |
jonathan |
888 |
|
69 |
bh |
1072 |
# If there is no choice_left_table then self.left_table will |
70 |
|
|
# be the keft table so we can simply leave it out on the |
71 |
|
|
# right side. |
72 |
|
|
if t is not self.left_table: |
73 |
bh |
1521 |
self.choice_right_table.Append(t.Title(), t) |
74 |
bh |
1072 |
|
75 |
|
|
if self.choice_left_table is None: |
76 |
|
|
for col in self.left_table.Columns(): |
77 |
|
|
self.choice_left_field.Append(col.name, col) |
78 |
|
|
|
79 |
|
|
if self.choice_left_table is not None: |
80 |
|
|
EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable) |
81 |
frank |
1000 |
EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable) |
82 |
|
|
|
83 |
bh |
1072 |
if self.choice_left_table is not None: |
84 |
|
|
self.choice_left_table.SetSelection(0) |
85 |
frank |
1000 |
self.choice_right_table.SetSelection(0) |
86 |
jonathan |
888 |
self.button_join.Enable(False) |
87 |
|
|
|
88 |
|
|
topBox = wxBoxSizer(wxVERTICAL) |
89 |
|
|
|
90 |
|
|
sizer = wxFlexGridSizer(2, 4) |
91 |
frank |
1000 |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
92 |
bh |
1072 |
if self.choice_left_table is not None: |
93 |
|
|
sizer.Add(self.choice_left_table, 1, |
94 |
|
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
95 |
|
|
else: |
96 |
|
|
sizer.Add(wxStaticText(self, -1, self.left_table.Title()), 0, |
97 |
|
|
wxALL, 4) |
98 |
|
|
|
99 |
frank |
1000 |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
100 |
|
|
sizer.Add(self.choice_right_table, 1, |
101 |
bh |
1072 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
102 |
frank |
1000 |
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
103 |
|
|
sizer.Add(self.choice_left_field, 1, |
104 |
bh |
1072 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
105 |
frank |
1000 |
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
106 |
|
|
sizer.Add(self.choice_right_field, 1, |
107 |
bh |
1072 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
108 |
jonathan |
888 |
|
109 |
|
|
sizer.AddGrowableCol(1) |
110 |
|
|
sizer.AddGrowableCol(3) |
111 |
|
|
|
112 |
frank |
1010 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
113 |
jonathan |
888 |
|
114 |
|
|
sizer = wxBoxSizer(wxHORIZONTAL) |
115 |
frank |
1010 |
self.check_outer_join = wxCheckBox(self,-1, |
116 |
|
|
_("Outer Join (preserves left table records)")) |
117 |
|
|
sizer.Add(self.check_outer_join, 1, wxALL,4) |
118 |
|
|
topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4) |
119 |
jonathan |
888 |
|
120 |
frank |
1010 |
sizer = wxBoxSizer(wxHORIZONTAL) |
121 |
bh |
1072 |
sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, |
122 |
|
|
10) |
123 |
|
|
sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, |
124 |
|
|
10) |
125 |
jonathan |
888 |
|
126 |
frank |
1010 |
topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10) |
127 |
|
|
|
128 |
jonathan |
888 |
self.SetAutoLayout(True) |
129 |
|
|
self.SetSizer(topBox) |
130 |
|
|
topBox.Fit(self) |
131 |
|
|
topBox.SetSizeHints(self) |
132 |
|
|
|
133 |
frank |
1000 |
# Save same parameters for later use. |
134 |
|
|
self.parent = parent |
135 |
|
|
self.session = session |
136 |
jonathan |
888 |
|
137 |
frank |
1000 |
def OnJoin(self, event): |
138 |
|
|
"""Get the table and field selections and perform the join.""" |
139 |
|
|
# left |
140 |
bh |
1072 |
if self.choice_left_table is not None: |
141 |
|
|
i = self.choice_left_table.GetSelection() |
142 |
|
|
left_table = self.choice_left_table.GetClientData(i) |
143 |
|
|
else: |
144 |
|
|
left_table = self.left_table |
145 |
frank |
1000 |
i = self.choice_left_field.GetSelection() |
146 |
|
|
left_field = self.choice_left_field.GetString(i) |
147 |
|
|
# right |
148 |
|
|
i = self.choice_right_table.GetSelection() |
149 |
|
|
right_table = self.choice_right_table.GetClientData(i) |
150 |
|
|
i = self.choice_right_field.GetSelection() |
151 |
|
|
right_field = self.choice_right_field.GetString(i) |
152 |
jonathan |
888 |
|
153 |
frank |
1010 |
outer_join = self.check_outer_join.IsChecked() |
154 |
|
|
|
155 |
frank |
1000 |
try: |
156 |
bh |
1072 |
joined_table = TransientJoinedTable(self.session.TransientDB(), |
157 |
|
|
left_table, left_field, |
158 |
|
|
right_table, right_field, |
159 |
|
|
outer_join) |
160 |
frank |
1000 |
except: |
161 |
jan |
1022 |
dlg = wxMessageDialog(None, |
162 |
|
|
_('Join failed:\n %s') % sys.exc_info()[1], |
163 |
|
|
_('Info'), wxOK|wxICON_ERROR) |
164 |
frank |
1000 |
dlg.ShowModal() |
165 |
bh |
1005 |
dlg.Destroy() |
166 |
bh |
1072 |
return |
167 |
bh |
1007 |
|
168 |
bh |
1072 |
joined_table = self.session.AddTable(joined_table) |
169 |
|
|
if self.layer is not None: |
170 |
|
|
needed_rows = self.layer.ShapeStore().Table().NumRows() |
171 |
|
|
joined_rows = joined_table.NumRows() |
172 |
|
|
if needed_rows != joined_rows: |
173 |
|
|
msg = _("The joined table has %(joined)d rows but " |
174 |
|
|
"it must have %(needed)d rows" |
175 |
|
|
"to be used with the selected layer") \ |
176 |
|
|
% {"joined": joined_rows, |
177 |
|
|
"needed": needed_rows} |
178 |
|
|
dlg = wxMessageDialog(None, msg, _('Join Failed'), |
179 |
|
|
wxOK|wxICON_ERROR) |
180 |
|
|
dlg.ShowModal() |
181 |
|
|
dlg.Destroy() |
182 |
|
|
return |
183 |
|
|
else: |
184 |
|
|
store = DerivedShapeStore(self.layer.ShapeStore(), |
185 |
|
|
joined_table) |
186 |
|
|
self.session.AddShapeStore(store) |
187 |
|
|
self.layer.SetShapeStore(store) |
188 |
|
|
else: |
189 |
frank |
1000 |
name = joined_table.tablename |
190 |
jan |
1022 |
dialog = QueryTableFrame(self.parent, name, |
191 |
bh |
1072 |
_('Table: %s') % joined_table.Title(), |
192 |
|
|
joined_table) |
193 |
frank |
1000 |
self.parent.add_dialog(name, dialog) |
194 |
jan |
1035 |
dialog.Show(True) |
195 |
frank |
1000 |
|
196 |
bh |
1072 |
self.Close() |
197 |
frank |
1000 |
|
198 |
|
|
def OnClose(self, event): |
199 |
|
|
"""Close the dialog.""" |
200 |
|
|
self.Close() |
201 |
|
|
|
202 |
|
|
def OnLeftTable(self, event): |
203 |
|
|
"""Get the selected table and fill the field choice.""" |
204 |
|
|
i = self.choice_left_table.GetSelection() |
205 |
|
|
table = self.choice_left_table.GetClientData(i) |
206 |
|
|
self.choice_left_field.Clear() |
207 |
bh |
1072 |
if table is not None: |
208 |
|
|
for col in table.Columns(): |
209 |
|
|
self.choice_left_field.Append(col.name, col) |
210 |
|
|
self.update_sensitivity() |
211 |
jonathan |
888 |
|
212 |
frank |
1000 |
def OnRightTable(self, event): |
213 |
|
|
"""Get the selected table and fill the field choice.""" |
214 |
|
|
i = self.choice_right_table.GetSelection() |
215 |
|
|
table = self.choice_right_table.GetClientData(i) |
216 |
|
|
self.choice_right_field.Clear() |
217 |
bh |
1072 |
if table is not None: |
218 |
|
|
for col in table.Columns(): |
219 |
|
|
self.choice_right_field.Append(col.name, col) |
220 |
|
|
self.update_sensitivity() |
221 |
jonathan |
888 |
|
222 |
bh |
1072 |
def update_sensitivity(self): |
223 |
|
|
if self.choice_left_table is not None: |
224 |
|
|
lsel = self.choice_left_table.GetSelection() |
225 |
|
|
else: |
226 |
|
|
lsel = sys.maxint |
227 |
frank |
1000 |
sel = self.choice_right_table.GetSelection() |
228 |
bh |
1072 |
self.button_join.Enable(sel > 0 and lsel > 0 and sel != lsel) |