/[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 1542 - (show annotations)
Fri Aug 1 16:13:49 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: 9008 byte(s)
Insert cvs keywords and doc-strings.

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 """The layer and table join dialog"""
9
10 __version__ = "$Revision$"
11 # $Source$
12 # $Id$
13
14
15 import sys
16 from wxPython.wx import *
17
18 from Thuban import _
19
20 from Thuban.Model.transientdb import TransientJoinedTable
21 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):
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):
38 wxDialog.__init__(self, parent, -1, title,
39 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
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"))
63 self.button_join.SetDefault()
64 self.button_close = wxButton(self, wxID_CANCEL, _("Close"))
65
66 EVT_BUTTON(self, wxID_OK, self.OnJoin)
67
68 if self.choice_left_table is not None:
69 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)
94
95 topBox = wxBoxSizer(wxVERTICAL)
96
97 sizer = wxFlexGridSizer(2, 4)
98 sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
99 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 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)
117 sizer.AddGrowableCol(3)
118
119 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)
128 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
133 topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10)
134
135 self.SetAutoLayout(True)
136 self.SetSizer(topBox)
137 topBox.Fit(self)
138 topBox.SetSizeHints(self)
139
140 # Save same parameters for later use.
141 self.parent = parent
142 self.session = session
143
144 def OnJoin(self, event):
145 """Get the table and field selections and perform the join."""
146 # left
147 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 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
160 outer_join = self.check_outer_join.IsChecked()
161
162 try:
163 joined_table = TransientJoinedTable(self.session.TransientDB(),
164 left_table, left_field,
165 right_table, right_field,
166 outer_join)
167 except:
168 dlg = wxMessageDialog(None,
169 _('Join failed:\n %s') % sys.exc_info()[1],
170 _('Info'), wxOK|wxICON_ERROR)
171 dlg.ShowModal()
172 dlg.Destroy()
173 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)

Properties

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26