/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/join.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/join.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1521 - (show annotations)
Wed Jul 30 12:34:10 2003 UTC (21 years, 7 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/join.py
File MIME type: text/x-python
File size: 8922 byte(s)
(JoinDialog.__init__): Use the table's Title()
method directly instead of going through the transient_table
method. This faster because transient_table may force the copy of
a DBF file into the transient database and setting a table's title
doesnm't affect the title of the associated transient table, so
this fixes RT #2042

1 # 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 sys
9 from wxPython.wx import *
10
11 from Thuban import _
12
13 from Thuban.Model.transientdb import TransientJoinedTable
14 from Thuban.Model.data import DerivedShapeStore
15 from Thuban.UI.tableview import QueryTableFrame
16
17 ID_LEFT_TABLE = 4001
18 ID_RIGHT_TABLE = 4002
19
20 CHOICE_WIDTH = 150
21
22 class JoinDialog(wxDialog):
23
24 """Table join dialog.
25
26 Join two tables (left/right) based on the user selected fields.
27 Opens a QueryTableFrame and registers the new table with the session.
28 """
29
30 def __init__(self, parent, title, session, layer = None):
31 wxDialog.__init__(self, parent, -1, title,
32 style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
33 self.layer = layer
34
35 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 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
48 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
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 EVT_BUTTON(self, wxID_OK, self.OnJoin)
60
61 if self.choice_left_table is not None:
62 self.choice_left_table.Append('Select ...', None)
63 self.choice_right_table.Append('Select ...', None)
64
65 for t in session.Tables():
66 if self.choice_left_table is not None:
67 self.choice_left_table.Append(t.Title(), t)
68
69 # 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 self.choice_right_table.Append(t.Title(), t)
74
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 EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable)
82
83 if self.choice_left_table is not None:
84 self.choice_left_table.SetSelection(0)
85 self.choice_right_table.SetSelection(0)
86 self.button_join.Enable(False)
87
88 topBox = wxBoxSizer(wxVERTICAL)
89
90 sizer = wxFlexGridSizer(2, 4)
91 sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
92 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 sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
100 sizer.Add(self.choice_right_table, 1,
101 wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
102 sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
103 sizer.Add(self.choice_left_field, 1,
104 wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
105 sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
106 sizer.Add(self.choice_right_field, 1,
107 wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
108
109 sizer.AddGrowableCol(1)
110 sizer.AddGrowableCol(3)
111
112 topBox.Add(sizer, 0, wxEXPAND|wxALL, 4)
113
114 sizer = wxBoxSizer(wxHORIZONTAL)
115 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
120 sizer = wxBoxSizer(wxHORIZONTAL)
121 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
126 topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10)
127
128 self.SetAutoLayout(True)
129 self.SetSizer(topBox)
130 topBox.Fit(self)
131 topBox.SetSizeHints(self)
132
133 # Save same parameters for later use.
134 self.parent = parent
135 self.session = session
136
137 def OnJoin(self, event):
138 """Get the table and field selections and perform the join."""
139 # left
140 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 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
153 outer_join = self.check_outer_join.IsChecked()
154
155 try:
156 joined_table = TransientJoinedTable(self.session.TransientDB(),
157 left_table, left_field,
158 right_table, right_field,
159 outer_join)
160 except:
161 dlg = wxMessageDialog(None,
162 _('Join failed:\n %s') % sys.exc_info()[1],
163 _('Info'), wxOK|wxICON_ERROR)
164 dlg.ShowModal()
165 dlg.Destroy()
166 return
167
168 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 name = joined_table.tablename
190 dialog = QueryTableFrame(self.parent, name,
191 _('Table: %s') % joined_table.Title(),
192 joined_table)
193 self.parent.add_dialog(name, dialog)
194 dialog.Show(True)
195
196 self.Close()
197
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 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
212 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 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
222 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 sel = self.choice_right_table.GetSelection()
228 self.button_join.Enable(sel > 0 and lsel > 0 and sel != lsel)

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26