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