4 |
# |
# |
5 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
8 |
import os, sys, math |
"""The layer and table join dialog""" |
9 |
|
|
10 |
|
__version__ = "$Revision$" |
11 |
|
# $Source$ |
12 |
|
# $Id$ |
13 |
|
|
14 |
|
|
15 |
|
import sys |
16 |
from wxPython.wx import * |
from wxPython.wx import * |
17 |
|
|
18 |
from Thuban import _ |
from Thuban import _ |
19 |
|
|
20 |
from Thuban.Model.transientdb import TransientJoinedTable |
from Thuban.Model.transientdb import TransientJoinedTable |
21 |
from Thuban.UI.tableview import QueryTableFrame, LayerTableFrame |
from Thuban.Model.data import DerivedShapeStore |
22 |
|
from Thuban.UI.tableview import QueryTableFrame |
23 |
|
|
24 |
ID_LEFT_TABLE = 4001 |
ID_LEFT_TABLE = 4001 |
25 |
ID_RIGHT_TABLE = 4002 |
ID_RIGHT_TABLE = 4002 |
26 |
|
|
27 |
CHOICE_WIDTH = 150 |
CHOICE_WIDTH = 150 |
28 |
|
|
29 |
class JoinDialog(wxDialog): |
class JoinDialog(wxDialog): |
30 |
|
|
31 |
"""Table join dialog. |
"""Table join dialog. |
32 |
|
|
33 |
Join two tables (left/right) based on the user selected fields. |
Join two tables (left/right) based on the user selected fields. |
37 |
def __init__(self, parent, title, session, layer = None): |
def __init__(self, parent, title, session, layer = None): |
38 |
wxDialog.__init__(self, parent, -1, title, |
wxDialog.__init__(self, parent, -1, title, |
39 |
style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER) |
style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER) |
40 |
|
self.layer = layer |
41 |
|
|
42 |
|
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 |
|
|
|
self.choice_left_table = wxChoice(self, ID_LEFT_TABLE) |
|
|
width, height = self.choice_left_table.GetSizeTuple() |
|
|
self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
|
51 |
self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE) |
self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE) |
52 |
width, height = self.choice_right_table.GetSizeTuple() |
width, height = self.choice_right_table.GetSizeTuple() |
53 |
self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
64 |
self.button_close = wxButton(self, wxID_CANCEL, _("Close")) |
self.button_close = wxButton(self, wxID_CANCEL, _("Close")) |
65 |
|
|
66 |
EVT_BUTTON(self, wxID_OK, self.OnJoin) |
EVT_BUTTON(self, wxID_OK, self.OnJoin) |
|
#EVT_BUTTON(self, wxID_CANCEL, self.OnClose) |
|
67 |
|
|
68 |
self.choice_left_table.Append('Select ...', None) |
if self.choice_left_table is not None: |
69 |
|
self.choice_left_table.Append('Select ...', None) |
70 |
self.choice_right_table.Append('Select ...', None) |
self.choice_right_table.Append('Select ...', None) |
71 |
|
|
72 |
for t in session.Tables(): |
for t in session.Tables(): |
73 |
self.choice_left_table.Append(t.transient_table().Title(), t) |
if self.choice_left_table is not None: |
74 |
self.choice_right_table.Append(t.transient_table().Title(), t) |
self.choice_left_table.Append(t.Title(), t) |
75 |
|
|
76 |
|
# 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 |
|
self.choice_right_table.Append(t.Title(), t) |
81 |
|
|
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 |
EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable) |
if self.choice_left_table is not None: |
87 |
|
EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable) |
88 |
EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable) |
EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable) |
89 |
|
|
90 |
self.choice_left_table.SetSelection(0) |
if self.choice_left_table is not None: |
91 |
|
self.choice_left_table.SetSelection(0) |
92 |
self.choice_right_table.SetSelection(0) |
self.choice_right_table.SetSelection(0) |
93 |
self.button_join.Enable(False) |
self.button_join.Enable(False) |
94 |
|
|
96 |
|
|
97 |
sizer = wxFlexGridSizer(2, 4) |
sizer = wxFlexGridSizer(2, 4) |
98 |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
99 |
sizer.Add(self.choice_left_table, 1, |
if self.choice_left_table is not None: |
100 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
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 |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
107 |
sizer.Add(self.choice_right_table, 1, |
sizer.Add(self.choice_right_table, 1, |
108 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
109 |
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
110 |
sizer.Add(self.choice_left_field, 1, |
sizer.Add(self.choice_left_field, 1, |
111 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
112 |
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
113 |
sizer.Add(self.choice_right_field, 1, |
sizer.Add(self.choice_right_field, 1, |
114 |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
115 |
|
|
116 |
sizer.AddGrowableCol(1) |
sizer.AddGrowableCol(1) |
117 |
sizer.AddGrowableCol(3) |
sizer.AddGrowableCol(3) |
125 |
topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4) |
topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4) |
126 |
|
|
127 |
sizer = wxBoxSizer(wxHORIZONTAL) |
sizer = wxBoxSizer(wxHORIZONTAL) |
128 |
sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10) |
sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, |
129 |
sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10) |
10) |
130 |
|
sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, |
131 |
|
10) |
132 |
|
|
133 |
topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10) |
topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10) |
134 |
|
|
137 |
topBox.Fit(self) |
topBox.Fit(self) |
138 |
topBox.SetSizeHints(self) |
topBox.SetSizeHints(self) |
139 |
|
|
|
self.valid = False |
|
|
|
|
140 |
# Save same parameters for later use. |
# Save same parameters for later use. |
|
self.db = session.TransientDB() |
|
141 |
self.parent = parent |
self.parent = parent |
142 |
self.session = session |
self.session = session |
143 |
|
|
144 |
def OnJoin(self, event): |
def OnJoin(self, event): |
145 |
"""Get the table and field selections and perform the join.""" |
"""Get the table and field selections and perform the join.""" |
146 |
# left |
# left |
147 |
i = self.choice_left_table.GetSelection() |
if self.choice_left_table is not None: |
148 |
left_table = self.choice_left_table.GetClientData(i) |
i = self.choice_left_table.GetSelection() |
149 |
|
left_table = self.choice_left_table.GetClientData(i) |
150 |
|
else: |
151 |
|
left_table = self.left_table |
152 |
i = self.choice_left_field.GetSelection() |
i = self.choice_left_field.GetSelection() |
153 |
left_field = self.choice_left_field.GetString(i) |
left_field = self.choice_left_field.GetString(i) |
154 |
# right |
# right |
159 |
|
|
160 |
outer_join = self.check_outer_join.IsChecked() |
outer_join = self.check_outer_join.IsChecked() |
161 |
|
|
|
result = True |
|
162 |
try: |
try: |
163 |
joined_table = TransientJoinedTable(self.db, |
joined_table = TransientJoinedTable(self.session.TransientDB(), |
164 |
left_table, left_field, |
left_table, left_field, |
165 |
right_table, right_field, |
right_table, right_field, |
166 |
outer_join) |
outer_join) |
167 |
except: |
except: |
168 |
dlg = wxMessageDialog(None, |
dlg = wxMessageDialog(None, |
169 |
_('Join failed:\n %s') % sys.exc_info()[1], |
_('Join failed:\n %s') % sys.exc_info()[1], |
170 |
_('Info'), wxOK|wxICON_ERROR) |
_('Info'), wxOK|wxICON_ERROR) |
171 |
dlg.ShowModal() |
dlg.ShowModal() |
172 |
dlg.Destroy() |
dlg.Destroy() |
173 |
result = False |
return |
174 |
|
|
175 |
if result: |
joined_table = self.session.AddTable(joined_table) |
176 |
self.session.AddTable(joined_table) |
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 |
name = joined_table.tablename |
name = joined_table.tablename |
197 |
dialog = QueryTableFrame(self.parent, name, |
dialog = QueryTableFrame(self.parent, name, |
198 |
_('Table: %s') % joined_table.Title(), |
_('Table: %s') % joined_table.Title(), |
199 |
joined_table) |
joined_table) |
200 |
self.parent.add_dialog(name, dialog) |
self.parent.add_dialog(name, dialog) |
201 |
dialog.Show(True) |
dialog.Show(True) |
202 |
|
|
203 |
self.Close() |
self.Close() |
204 |
|
|
205 |
def OnClose(self, event): |
def OnClose(self, event): |
206 |
"""Close the dialog.""" |
"""Close the dialog.""" |
211 |
i = self.choice_left_table.GetSelection() |
i = self.choice_left_table.GetSelection() |
212 |
table = self.choice_left_table.GetClientData(i) |
table = self.choice_left_table.GetClientData(i) |
213 |
self.choice_left_field.Clear() |
self.choice_left_field.Clear() |
214 |
for col in table.Columns(): |
if table is not None: |
215 |
self.choice_left_field.Append(col.name, col) |
for col in table.Columns(): |
216 |
|
self.choice_left_field.Append(col.name, col) |
217 |
sel = self.choice_left_table.GetSelection() |
self.update_sensitivity() |
|
rsel = self.choice_right_table.GetSelection() |
|
|
self.valid = sel != -1 and (sel != rsel and rsel != 0) |
|
|
self.button_join.Enable(self.valid) |
|
218 |
|
|
219 |
def OnRightTable(self, event): |
def OnRightTable(self, event): |
220 |
"""Get the selected table and fill the field choice.""" |
"""Get the selected table and fill the field choice.""" |
221 |
i = self.choice_right_table.GetSelection() |
i = self.choice_right_table.GetSelection() |
222 |
table = self.choice_right_table.GetClientData(i) |
table = self.choice_right_table.GetClientData(i) |
223 |
self.choice_right_field.Clear() |
self.choice_right_field.Clear() |
224 |
for col in table.Columns(): |
if table is not None: |
225 |
self.choice_right_field.Append(col.name, col) |
for col in table.Columns(): |
226 |
|
self.choice_right_field.Append(col.name, col) |
227 |
|
self.update_sensitivity() |
228 |
|
|
229 |
|
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 |
sel = self.choice_right_table.GetSelection() |
sel = self.choice_right_table.GetSelection() |
235 |
lsel = self.choice_left_table.GetSelection() |
self.button_join.Enable(sel > 0 and lsel > 0 and sel != lsel) |
|
self.valid = sel != -1 and (sel != lsel and lsel != 0) |
|
|
self.button_join.Enable(self.valid) |
|
|
|
|