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 |
|
|
|
8 |
|
|
import os, sys, math |
9 |
|
|
from wxPython.wx import * |
10 |
|
|
|
11 |
|
|
from Thuban import _ |
12 |
frank |
1000 |
|
13 |
|
|
from Thuban.Model.transientdb import TransientJoinedTable |
14 |
jan |
1022 |
from Thuban.UI.tableview import QueryTableFrame, LayerTableFrame |
15 |
jonathan |
888 |
|
16 |
frank |
1000 |
ID_LEFT_TABLE = 4001 |
17 |
|
|
ID_RIGHT_TABLE = 4002 |
18 |
jonathan |
888 |
|
19 |
frank |
1000 |
CHOICE_WIDTH = 150 |
20 |
|
|
|
21 |
jonathan |
888 |
class JoinDialog(wxDialog): |
22 |
frank |
1000 |
|
23 |
|
|
"""Table join dialog. |
24 |
jonathan |
888 |
|
25 |
frank |
1000 |
Join two tables (left/right) based on the user selected fields. |
26 |
jan |
1022 |
Opens a QueryTableFrame and registers the new table with the session. |
27 |
frank |
1000 |
""" |
28 |
|
|
|
29 |
jonathan |
888 |
def __init__(self, parent, title, session, layer = None): |
30 |
|
|
wxDialog.__init__(self, parent, -1, title, |
31 |
|
|
style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER) |
32 |
|
|
|
33 |
frank |
1000 |
self.choice_left_table = wxChoice(self, ID_LEFT_TABLE) |
34 |
|
|
width, height = self.choice_left_table.GetSizeTuple() |
35 |
|
|
self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
36 |
|
|
self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE) |
37 |
|
|
width, height = self.choice_right_table.GetSizeTuple() |
38 |
|
|
self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height)) |
39 |
jonathan |
888 |
|
40 |
frank |
1000 |
self.choice_left_field = wxChoice(self, -1) |
41 |
|
|
width, height = self.choice_left_field.GetSizeTuple() |
42 |
|
|
self.choice_left_field.SetSize(wxSize(CHOICE_WIDTH, height)) |
43 |
|
|
self.choice_right_field = wxChoice(self, -1) |
44 |
|
|
width, height = self.choice_right_field.GetSizeTuple() |
45 |
|
|
self.choice_right_field.SetSize(wxSize(CHOICE_WIDTH, height)) |
46 |
jonathan |
888 |
|
47 |
|
|
self.button_join = wxButton(self, wxID_OK, _("Join")) |
48 |
|
|
self.button_join.SetDefault() |
49 |
|
|
self.button_close = wxButton(self, wxID_CANCEL, _("Close")) |
50 |
|
|
|
51 |
frank |
1000 |
EVT_BUTTON(self, wxID_OK, self.OnJoin) |
52 |
|
|
#EVT_BUTTON(self, wxID_CANCEL, self.OnClose) |
53 |
jonathan |
888 |
|
54 |
frank |
1000 |
self.choice_left_table.Append('Select ...', None) |
55 |
|
|
self.choice_right_table.Append('Select ...', None) |
56 |
|
|
|
57 |
|
|
for t in session.Tables(): |
58 |
frank |
1003 |
self.choice_left_table.Append(t.transient_table().Title(), t) |
59 |
|
|
self.choice_right_table.Append(t.transient_table().Title(), t) |
60 |
jonathan |
888 |
|
61 |
frank |
1000 |
EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable) |
62 |
|
|
EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable) |
63 |
|
|
|
64 |
|
|
self.choice_left_table.SetSelection(0) |
65 |
|
|
self.choice_right_table.SetSelection(0) |
66 |
jonathan |
888 |
self.button_join.Enable(False) |
67 |
|
|
|
68 |
|
|
topBox = wxBoxSizer(wxVERTICAL) |
69 |
|
|
|
70 |
|
|
sizer = wxFlexGridSizer(2, 4) |
71 |
frank |
1000 |
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
72 |
|
|
sizer.Add(self.choice_left_table, 1, |
73 |
|
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
74 |
|
|
sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4) |
75 |
|
|
sizer.Add(self.choice_right_table, 1, |
76 |
|
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
77 |
|
|
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
78 |
|
|
sizer.Add(self.choice_left_field, 1, |
79 |
|
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
80 |
|
|
sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4) |
81 |
|
|
sizer.Add(self.choice_right_field, 1, |
82 |
|
|
wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4) |
83 |
jonathan |
888 |
|
84 |
|
|
sizer.AddGrowableCol(1) |
85 |
|
|
sizer.AddGrowableCol(3) |
86 |
|
|
|
87 |
frank |
1010 |
topBox.Add(sizer, 0, wxEXPAND|wxALL, 4) |
88 |
jonathan |
888 |
|
89 |
|
|
sizer = wxBoxSizer(wxHORIZONTAL) |
90 |
frank |
1010 |
self.check_outer_join = wxCheckBox(self,-1, |
91 |
|
|
_("Outer Join (preserves left table records)")) |
92 |
|
|
sizer.Add(self.check_outer_join, 1, wxALL,4) |
93 |
|
|
topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4) |
94 |
jonathan |
888 |
|
95 |
frank |
1010 |
sizer = wxBoxSizer(wxHORIZONTAL) |
96 |
|
|
sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10) |
97 |
|
|
sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10) |
98 |
jonathan |
888 |
|
99 |
frank |
1010 |
topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10) |
100 |
|
|
|
101 |
jonathan |
888 |
self.SetAutoLayout(True) |
102 |
|
|
self.SetSizer(topBox) |
103 |
|
|
topBox.Fit(self) |
104 |
|
|
topBox.SetSizeHints(self) |
105 |
|
|
|
106 |
|
|
self.valid = False |
107 |
|
|
|
108 |
frank |
1000 |
# Save same parameters for later use. |
109 |
|
|
self.db = session.TransientDB() |
110 |
|
|
self.parent = parent |
111 |
|
|
self.session = session |
112 |
jonathan |
888 |
|
113 |
frank |
1000 |
def OnJoin(self, event): |
114 |
|
|
"""Get the table and field selections and perform the join.""" |
115 |
|
|
# left |
116 |
|
|
i = self.choice_left_table.GetSelection() |
117 |
|
|
left_table = self.choice_left_table.GetClientData(i) |
118 |
|
|
i = self.choice_left_field.GetSelection() |
119 |
|
|
left_field = self.choice_left_field.GetString(i) |
120 |
|
|
# right |
121 |
|
|
i = self.choice_right_table.GetSelection() |
122 |
|
|
right_table = self.choice_right_table.GetClientData(i) |
123 |
|
|
i = self.choice_right_field.GetSelection() |
124 |
|
|
right_field = self.choice_right_field.GetString(i) |
125 |
jonathan |
888 |
|
126 |
frank |
1010 |
outer_join = self.check_outer_join.IsChecked() |
127 |
|
|
|
128 |
frank |
1000 |
result = True |
129 |
|
|
try: |
130 |
frank |
1010 |
joined_table = TransientJoinedTable(self.db, |
131 |
|
|
left_table, left_field, |
132 |
|
|
right_table, right_field, |
133 |
|
|
outer_join) |
134 |
frank |
1000 |
except: |
135 |
jan |
1022 |
dlg = wxMessageDialog(None, |
136 |
|
|
_('Join failed:\n %s') % sys.exc_info()[1], |
137 |
|
|
_('Info'), wxOK|wxICON_ERROR) |
138 |
frank |
1000 |
dlg.ShowModal() |
139 |
bh |
1005 |
dlg.Destroy() |
140 |
frank |
1000 |
result = False |
141 |
bh |
1007 |
|
142 |
frank |
1000 |
if result: |
143 |
|
|
self.session.AddTable(joined_table) |
144 |
|
|
name = joined_table.tablename |
145 |
jan |
1022 |
dialog = QueryTableFrame(self.parent, name, |
146 |
|
|
_('Table: %s') % joined_table.Title(), |
147 |
|
|
joined_table) |
148 |
frank |
1000 |
self.parent.add_dialog(name, dialog) |
149 |
jan |
1035 |
dialog.Show(True) |
150 |
frank |
1000 |
|
151 |
|
|
self.Close() |
152 |
|
|
|
153 |
|
|
def OnClose(self, event): |
154 |
|
|
"""Close the dialog.""" |
155 |
|
|
self.Close() |
156 |
|
|
|
157 |
|
|
def OnLeftTable(self, event): |
158 |
|
|
"""Get the selected table and fill the field choice.""" |
159 |
|
|
i = self.choice_left_table.GetSelection() |
160 |
|
|
table = self.choice_left_table.GetClientData(i) |
161 |
|
|
self.choice_left_field.Clear() |
162 |
|
|
for col in table.Columns(): |
163 |
|
|
self.choice_left_field.Append(col.name, col) |
164 |
jonathan |
888 |
|
165 |
frank |
1000 |
sel = self.choice_left_table.GetSelection() |
166 |
|
|
rsel = self.choice_right_table.GetSelection() |
167 |
|
|
self.valid = sel != -1 and (sel != rsel and rsel != 0) |
168 |
jonathan |
888 |
self.button_join.Enable(self.valid) |
169 |
|
|
|
170 |
frank |
1000 |
def OnRightTable(self, event): |
171 |
|
|
"""Get the selected table and fill the field choice.""" |
172 |
|
|
i = self.choice_right_table.GetSelection() |
173 |
|
|
table = self.choice_right_table.GetClientData(i) |
174 |
|
|
self.choice_right_field.Clear() |
175 |
|
|
for col in table.Columns(): |
176 |
|
|
self.choice_right_field.Append(col.name, col) |
177 |
jonathan |
888 |
|
178 |
frank |
1000 |
sel = self.choice_right_table.GetSelection() |
179 |
|
|
lsel = self.choice_left_table.GetSelection() |
180 |
|
|
self.valid = sel != -1 and (sel != lsel and lsel != 0) |
181 |
jonathan |
888 |
self.button_join.Enable(self.valid) |
182 |
|
|
|