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 |
ID_TABLE1 = 4001 |
from Thuban.Model.transientdb import TransientJoinedTable |
21 |
ID_TABLE2 = 4002 |
from Thuban.Model.data import DerivedShapeStore |
22 |
|
from Thuban.UI.tableview import QueryTableFrame |
23 |
|
|
24 |
|
ID_LEFT_TABLE = 4001 |
25 |
|
ID_RIGHT_TABLE = 4002 |
26 |
|
|
27 |
|
CHOICE_WIDTH = 150 |
28 |
|
|
29 |
class JoinDialog(wxDialog): |
class JoinDialog(wxDialog): |
30 |
|
|
31 |
|
"""Table join dialog. |
32 |
|
|
33 |
|
Join two tables (left/right) based on the user selected fields. |
34 |
|
Opens a QueryTableFrame and registers the new table with the session. |
35 |
|
""" |
36 |
|
|
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 |
self.choice_table1 = wxChoice(self, ID_TABLE1) |
if not layer: |
43 |
self.choice_table2 = wxChoice(self, ID_TABLE2) |
self.choice_left_table = wxChoice(self, ID_LEFT_TABLE) |
44 |
|
width, height = self.choice_left_table.GetSizeTuple() |
45 |
self.choice_fields1 = wxChoice(self, -1) |
self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
46 |
self.choice_fields2 = wxChoice(self, -1) |
self.left_table = None |
47 |
|
else: |
48 |
|
self.choice_left_table = None |
49 |
|
self.left_table = layer.ShapeStore().Table() |
50 |
|
|
51 |
|
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 |
|
|
55 |
|
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 |
|
|
62 |
self.button_join = wxButton(self, wxID_OK, _("Join")) |
self.button_join = wxButton(self, wxID_OK, _("Join")) |
63 |
self.button_join.SetDefault() |
self.button_join.SetDefault() |
64 |
self.button_close = wxButton(self, wxID_CANCEL, _("Close")) |
self.button_close = wxButton(self, wxID_CANCEL, _("Close")) |
65 |
|
|
66 |
tablenames = ["Picard", "Worf", "LaForge", "Troy"] |
EVT_BUTTON(self, wxID_OK, self.OnJoin) |
|
for name in tablenames: |
|
|
self.choice_table1.Append(name, None) |
|
|
self.choice_table2.Append(name, None) |
|
|
|
|
|
EVT_CHOICE(self, ID_TABLE1, self.OnTable1) |
|
|
EVT_CHOICE(self, ID_TABLE2, self.OnTable2) |
|
67 |
|
|
68 |
self.choice_table1.SetSelection(0) |
if self.choice_left_table is not None: |
69 |
self.choice_table2.SetSelection(0) |
self.choice_left_table.Append('Select ...', None) |
70 |
|
self.choice_right_table.Append('Select ...', None) |
71 |
|
|
72 |
|
for t in session.Tables(): |
73 |
|
if self.choice_left_table is not None: |
74 |
|
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 |
|
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) |
89 |
|
|
90 |
|
if self.choice_left_table is not None: |
91 |
|
self.choice_left_table.SetSelection(0) |
92 |
|
self.choice_right_table.SetSelection(0) |
93 |
self.button_join.Enable(False) |
self.button_join.Enable(False) |
94 |
|
|
95 |
topBox = wxBoxSizer(wxVERTICAL) |
topBox = wxBoxSizer(wxVERTICAL) |
96 |
|
|
97 |
sizer = wxFlexGridSizer(2, 4) |
sizer = wxFlexGridSizer(2, 4) |
98 |
sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4) |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
99 |
sizer.Add(self.choice_table1, 1, wxEXPAND|wxALL, 4) |
if self.choice_left_table is not None: |
100 |
sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4) |
sizer.Add(self.choice_left_table, 1, |
101 |
sizer.Add(self.choice_table2, 1, wxEXPAND|wxALL, 4) |
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
102 |
sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4) |
else: |
103 |
sizer.Add(self.choice_fields1, 1, wxEXPAND|wxALL, 4) |
sizer.Add(wxStaticText(self, -1, self.left_table.Title()), 0, |
104 |
sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4) |
wxALL, 4) |
105 |
sizer.Add(self.choice_fields2, 1, wxEXPAND|wxALL, 4) |
|
106 |
|
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
107 |
|
sizer.Add(self.choice_right_table, 1, |
108 |
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
109 |
|
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
110 |
|
sizer.Add(self.choice_left_field, 1, |
111 |
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
112 |
|
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
113 |
|
sizer.Add(self.choice_right_field, 1, |
114 |
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
115 |
|
|
116 |
sizer.AddGrowableCol(1) |
sizer.AddGrowableCol(1) |
117 |
sizer.AddGrowableCol(3) |
sizer.AddGrowableCol(3) |
118 |
|
|
119 |
topBox.Add(sizer, 1, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
120 |
|
|
121 |
|
sizer = wxBoxSizer(wxHORIZONTAL) |
122 |
|
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 |
|
|
127 |
sizer = wxBoxSizer(wxHORIZONTAL) |
sizer = wxBoxSizer(wxHORIZONTAL) |
128 |
sizer.Add(self.button_join, 0, wxEXPAND|wxALL, 4) |
sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, |
129 |
sizer.Add(60, 20, 0, wxEXPAND|wxALL, 4) |
10) |
130 |
sizer.Add(self.button_close, 0, wxEXPAND|wxALL, 4) |
sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, |
131 |
|
10) |
132 |
|
|
133 |
topBox.Add(sizer, 0, wxALIGN_CENTER|wxALL, 4) |
topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10) |
134 |
|
|
135 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
136 |
self.SetSizer(topBox) |
self.SetSizer(topBox) |
137 |
topBox.Fit(self) |
topBox.Fit(self) |
138 |
topBox.SetSizeHints(self) |
topBox.SetSizeHints(self) |
139 |
|
|
140 |
self.valid = False |
# Save same parameters for later use. |
141 |
|
self.parent = parent |
142 |
def OnOK(self, event): |
self.session = session |
143 |
print "OnOK" |
|
144 |
|
def OnJoin(self, event): |
145 |
def OnCancel(self, event): |
"""Get the table and field selections and perform the join.""" |
146 |
print "OnCancel" |
# left |
147 |
|
if self.choice_left_table is not None: |
148 |
def OnTable1(self, event): |
i = self.choice_left_table.GetSelection() |
149 |
print "OnTable1" |
left_table = self.choice_left_table.GetClientData(i) |
150 |
#table = self.choice_table1.GetClientData() |
else: |
151 |
#self.choice_fields1.Clear() |
left_table = self.left_table |
152 |
#for col in table.Columns(): |
i = self.choice_left_field.GetSelection() |
153 |
#self.choice_fields1.Append(col.name, col) |
left_field = self.choice_left_field.GetString(i) |
154 |
|
# right |
155 |
sel = self.choice_table1.GetSelection() |
i = self.choice_right_table.GetSelection() |
156 |
self.valid = sel != -1 and sel != self.choice_table2.GetSelection() |
right_table = self.choice_right_table.GetClientData(i) |
157 |
self.button_join.Enable(self.valid) |
i = self.choice_right_field.GetSelection() |
158 |
|
right_field = self.choice_right_field.GetString(i) |
159 |
def OnTable2(self, event): |
|
160 |
print "OnTable2" |
outer_join = self.check_outer_join.IsChecked() |
161 |
#table = self.choice_table2.GetClientData() |
|
162 |
#self.choice_fields2.Clear() |
try: |
163 |
#for col in table.Columns(): |
joined_table = TransientJoinedTable(self.session.TransientDB(), |
164 |
#self.choice_fields2.Append(col.name, col) |
left_table, left_field, |
165 |
|
right_table, right_field, |
166 |
sel = self.choice_table2.GetSelection() |
outer_join) |
167 |
self.valid = sel != -1 and sel != self.choice_table1.GetSelection() |
except: |
168 |
self.button_join.Enable(self.valid) |
dlg = wxMessageDialog(None, |
169 |
|
_('Join failed:\n %s') % sys.exc_info()[1], |
170 |
def GetJoin(self): |
_('Info'), wxOK|wxICON_ERROR) |
171 |
pass |
dlg.ShowModal() |
172 |
# if self.valid: |
dlg.Destroy() |
173 |
#return ((self.choice_table1.GetClientData |
return |
174 |
|
|
175 |
|
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 |
|
name = joined_table.tablename |
197 |
|
dialog = QueryTableFrame(self.parent, name, |
198 |
|
_('Table: %s') % joined_table.Title(), |
199 |
|
joined_table) |
200 |
|
self.parent.add_dialog(name, dialog) |
201 |
|
dialog.Show(True) |
202 |
|
|
203 |
|
self.Close() |
204 |
|
|
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 |
|
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 |
|
|
219 |
|
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 |
|
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 |
|
|
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() |
235 |
|
self.button_join.Enable(sel > 0 and lsel > 0 and sel != lsel) |