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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (hide annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 9457 byte(s)
made a copy
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 bh 1072
8 bh 1542 """The layer and table join dialog"""
9    
10     __version__ = "$Revision$"
11     # $Source$
12     # $Id$
13    
14    
15 bh 1072 import sys
16 dpinte 2700 import wx
17 bh 1072
18 jonathan 888 from Thuban import _
19 frank 1000
20     from Thuban.Model.transientdb import TransientJoinedTable
21 bh 1072 from Thuban.Model.data import DerivedShapeStore
22     from Thuban.UI.tableview import QueryTableFrame
23    
24 frank 1890 from common import ThubanBeginBusyCursor, ThubanEndBusyCursor
25    
26 frank 1000 ID_LEFT_TABLE = 4001
27     ID_RIGHT_TABLE = 4002
28 jonathan 888
29 frank 1000 CHOICE_WIDTH = 150
30    
31 dpinte 2700 class JoinDialog(wx.Dialog):
32 bh 1072
33 frank 1000 """Table join dialog.
34 jonathan 888
35 frank 1000 Join two tables (left/right) based on the user selected fields.
36 jan 1022 Opens a QueryTableFrame and registers the new table with the session.
37 frank 1000 """
38    
39 jonathan 888 def __init__(self, parent, title, session, layer = None):
40 dpinte 2700 wx.Dialog.__init__(self, parent, -1, title,
41     style = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER)
42 bh 1072 self.layer = layer
43 jonathan 888
44 bh 1072 if not layer:
45 dpinte 2700 self.choice_left_table = wx.Choice(self, ID_LEFT_TABLE)
46 bh 1072 width, height = self.choice_left_table.GetSizeTuple()
47 dpinte 2700 self.choice_left_table.SetSize(wx.Size(CHOICE_WIDTH, height))
48 bh 1072 self.left_table = None
49     else:
50     self.choice_left_table = None
51     self.left_table = layer.ShapeStore().Table()
52    
53 dpinte 2700 self.choice_right_table = wx.Choice(self, ID_RIGHT_TABLE)
54 frank 1000 width, height = self.choice_right_table.GetSizeTuple()
55 dpinte 2700 self.choice_right_table.SetSize(wx.Size(CHOICE_WIDTH, height))
56 jonathan 888
57 dpinte 2700 self.choice_left_field = wx.Choice(self, -1)
58 frank 1000 width, height = self.choice_left_field.GetSizeTuple()
59 dpinte 2700 self.choice_left_field.SetSize(wx.Size(CHOICE_WIDTH, height))
60     self.choice_right_field = wx.Choice(self, -1)
61 frank 1000 width, height = self.choice_right_field.GetSizeTuple()
62 dpinte 2700 self.choice_right_field.SetSize(wx.Size(CHOICE_WIDTH, height))
63 jonathan 888
64 dpinte 2700 self.button_join = wx.Button(self, wx.ID_OK, _("Join"))
65 jonathan 888 self.button_join.SetDefault()
66 dpinte 2700 self.button_close = wx.Button(self, wx.ID_CANCEL, _("Close"))
67 jonathan 888
68 dpinte 2700 self.Bind(wx.EVT_BUTTON, self.OnJoin, id=wx.ID_OK)
69 jonathan 888
70 bh 1072 if self.choice_left_table is not None:
71 bh 2040 self.choice_left_table.Append(_('Select...'), None)
72     self.choice_right_table.Append(_('Select...'), None)
73 bh 1072
74 frank 1000 for t in session.Tables():
75 bh 1072 if self.choice_left_table is not None:
76 bh 1521 self.choice_left_table.Append(t.Title(), t)
77 jonathan 888
78 bh 1072 # If there is no choice_left_table then self.left_table will
79     # be the keft table so we can simply leave it out on the
80     # right side.
81     if t is not self.left_table:
82 bh 1521 self.choice_right_table.Append(t.Title(), t)
83 bh 1072
84     if self.choice_left_table is None:
85     for col in self.left_table.Columns():
86     self.choice_left_field.Append(col.name, col)
87    
88     if self.choice_left_table is not None:
89 dpinte 2700 self.Bind(wx.EVT_CHOICE, self.OnLeftTable, id=ID_LEFT_TABLE)
90     self.Bind(wx.EVT_CHOICE, self.OnRightTable, id=ID_RIGHT_TABLE)
91 frank 1000
92 bh 1072 if self.choice_left_table is not None:
93     self.choice_left_table.SetSelection(0)
94 frank 1000 self.choice_right_table.SetSelection(0)
95 jonathan 888 self.button_join.Enable(False)
96    
97 dpinte 2700 topBox = wx.BoxSizer(wx.VERTICAL)
98 jonathan 888
99 dpinte 2700 sizer = wx.FlexGridSizer(2, 4)
100     sizer.Add(wx.StaticText(self, -1, _("Table:")), 0, wx.ALL, 4)
101 bh 1072 if self.choice_left_table is not None:
102     sizer.Add(self.choice_left_table, 1,
103 dpinte 2700 wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
104 bh 1072 else:
105 dpinte 2700 sizer.Add(wx.StaticText(self, -1, self.left_table.Title()), 0,
106     wx.ALL, 4)
107 bh 1072
108 dpinte 2700 sizer.Add(wx.StaticText(self, -1, _("Table:")), 0, wx.ALL, 4)
109 frank 1000 sizer.Add(self.choice_right_table, 1,
110 dpinte 2700 wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
111     sizer.Add(wx.StaticText(self, -1, _("Field:")), 0, wx.ALL, 4)
112 frank 1000 sizer.Add(self.choice_left_field, 1,
113 dpinte 2700 wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
114     sizer.Add(wx.StaticText(self, -1, _("Field:")), 0, wx.ALL, 4)
115 frank 1000 sizer.Add(self.choice_right_field, 1,
116 dpinte 2700 wx.EXPAND|wx.ALL|wx.ALIGN_CENTER_VERTICAL, 4)
117 jonathan 888
118     sizer.AddGrowableCol(1)
119     sizer.AddGrowableCol(3)
120    
121 dpinte 2700 topBox.Add(sizer, 0, wx.EXPAND|wx.ALL, 4)
122 jonathan 888
123 dpinte 2700 sizer = wx.BoxSizer(wx.HORIZONTAL)
124     self.check_outer_join = wx.CheckBox(self,-1,
125 frank 1010 _("Outer Join (preserves left table records)"))
126 dpinte 2700 sizer.Add(self.check_outer_join, 1, wx.ALL,4)
127     topBox.Add(sizer, 0, wx.ALIGN_LEFT|wx.ALIGN_TOP, 4)
128 jonathan 888
129 dpinte 2700 sizer = wx.BoxSizer(wx.HORIZONTAL)
130     sizer.Add(self.button_join, 0, wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM|wx.RIGHT,
131 bh 1072 10)
132 dpinte 2700 sizer.Add(self.button_close, 0, wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM|wx.RIGHT,
133 bh 1072 10)
134 jonathan 888
135 dpinte 2700 topBox.Add(sizer, 1, wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM|wx.BOTTOM|wx.TOP, 10)
136 frank 1010
137 jonathan 888 self.SetAutoLayout(True)
138     self.SetSizer(topBox)
139     topBox.Fit(self)
140     topBox.SetSizeHints(self)
141    
142 frank 1000 # Save same parameters for later use.
143     self.parent = parent
144     self.session = session
145 jonathan 888
146 frank 1000 def OnJoin(self, event):
147     """Get the table and field selections and perform the join."""
148 frank 1890 ThubanBeginBusyCursor()
149     try:
150     # left
151     if self.choice_left_table is not None:
152     i = self.choice_left_table.GetSelection()
153     left_table = self.choice_left_table.GetClientData(i)
154     else:
155     left_table = self.left_table
156     i = self.choice_left_field.GetSelection()
157     left_field = self.choice_left_field.GetString(i)
158     # right
159     i = self.choice_right_table.GetSelection()
160     right_table = self.choice_right_table.GetClientData(i)
161     i = self.choice_right_field.GetSelection()
162     right_field = self.choice_right_field.GetString(i)
163 dpinte 2700
164 frank 1890 outer_join = self.check_outer_join.IsChecked()
165 dpinte 2700
166 frank 1890 try:
167     joined_table = TransientJoinedTable(self.session.TransientDB(),
168     left_table, left_field,
169     right_table, right_field,
170     outer_join)
171     except:
172 dpinte 2700 dlg = wx.MessageDialog(None,
173 frank 1890 _('Join failed:\n %s') % sys.exc_info()[1],
174 dpinte 2700 _('Info'), wx.OK|wx.ICON_ERROR)
175 frank 1890 dlg.ShowModal()
176     dlg.Destroy()
177     return
178 dpinte 2700
179 frank 1890 joined_table = self.session.AddTable(joined_table)
180 jonathan 888
181 frank 1890 if self.layer is not None:
182     needed_rows = self.layer.ShapeStore().Table().NumRows()
183     joined_rows = joined_table.NumRows()
184     if needed_rows == joined_rows:
185     store = DerivedShapeStore(self.layer.ShapeStore(),
186     joined_table)
187     self.session.AddShapeStore(store)
188     self.layer.SetShapeStore(store)
189 frank 1010
190 frank 1890 finally:
191     ThubanEndBusyCursor()
192 bh 1007
193 bh 1072 if self.layer is not None:
194     if needed_rows != joined_rows:
195 bh 1572 msg = _("The joined table has %(joined)d rows but"
196     " it must have %(needed)d rows"
197     " to be used with the selected layer") \
198 bh 1072 % {"joined": joined_rows,
199     "needed": needed_rows}
200 dpinte 2700 dlg = wx.MessageDialog(None, msg, _('Join Failed'),
201     wx.OK|wx.ICON_ERROR)
202 bh 1072 dlg.ShowModal()
203     dlg.Destroy()
204     return
205 frank 1890
206 bh 1072 else:
207 frank 1000 name = joined_table.tablename
208 jan 1022 dialog = QueryTableFrame(self.parent, name,
209 bh 1072 _('Table: %s') % joined_table.Title(),
210     joined_table)
211 frank 1000 self.parent.add_dialog(name, dialog)
212 jan 1035 dialog.Show(True)
213 dpinte 2700
214 bh 1072 self.Close()
215 frank 1000
216     def OnClose(self, event):
217     """Close the dialog."""
218     self.Close()
219    
220     def OnLeftTable(self, event):
221     """Get the selected table and fill the field choice."""
222     i = self.choice_left_table.GetSelection()
223     table = self.choice_left_table.GetClientData(i)
224     self.choice_left_field.Clear()
225 bh 1072 if table is not None:
226     for col in table.Columns():
227     self.choice_left_field.Append(col.name, col)
228     self.update_sensitivity()
229 jonathan 888
230 frank 1000 def OnRightTable(self, event):
231     """Get the selected table and fill the field choice."""
232     i = self.choice_right_table.GetSelection()
233     table = self.choice_right_table.GetClientData(i)
234     self.choice_right_field.Clear()
235 bh 1072 if table is not None:
236     for col in table.Columns():
237     self.choice_right_field.Append(col.name, col)
238     self.update_sensitivity()
239 jonathan 888
240 bh 1072 def update_sensitivity(self):
241     if self.choice_left_table is not None:
242     lsel = self.choice_left_table.GetSelection()
243     else:
244     lsel = sys.maxint
245 frank 1000 sel = self.choice_right_table.GetSelection()
246 bh 1072 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