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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1035 by jan, Mon May 26 17:03:08 2003 UTC revision 1072 by bh, Tue May 27 16:47:48 2003 UTC
# Line 4  Line 4 
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  import sys
9  from wxPython.wx import *  from wxPython.wx import *
10                                                                                    
11  from Thuban import _  from Thuban import _
12    
13  from Thuban.Model.transientdb import TransientJoinedTable  from Thuban.Model.transientdb import TransientJoinedTable
14  from Thuban.UI.tableview import QueryTableFrame, LayerTableFrame  from Thuban.Model.data import DerivedShapeStore
15                                                                                    from Thuban.UI.tableview import QueryTableFrame
16    
17  ID_LEFT_TABLE = 4001  ID_LEFT_TABLE = 4001
18  ID_RIGHT_TABLE = 4002  ID_RIGHT_TABLE = 4002
19    
20  CHOICE_WIDTH = 150  CHOICE_WIDTH = 150
21    
22  class JoinDialog(wxDialog):  class JoinDialog(wxDialog):
23        
24      """Table join dialog.      """Table join dialog.
25    
26      Join two tables (left/right) based on the user selected fields.      Join two tables (left/right) based on the user selected fields.
# Line 29  class JoinDialog(wxDialog): Line 30  class JoinDialog(wxDialog):
30      def __init__(self, parent, title, session, layer = None):      def __init__(self, parent, title, session, layer = None):
31          wxDialog.__init__(self, parent, -1, title,          wxDialog.__init__(self, parent, -1, title,
32                            style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)                            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    
         self.choice_left_table = wxChoice(self, ID_LEFT_TABLE)  
         width, height = self.choice_left_table.GetSizeTuple()  
         self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height))  
44          self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE)          self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE)
45          width, height = self.choice_right_table.GetSizeTuple()          width, height = self.choice_right_table.GetSizeTuple()
46          self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height))          self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height))
# Line 49  class JoinDialog(wxDialog): Line 57  class JoinDialog(wxDialog):
57          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))
58    
59          EVT_BUTTON(self, wxID_OK, self.OnJoin)          EVT_BUTTON(self, wxID_OK, self.OnJoin)
         #EVT_BUTTON(self, wxID_CANCEL, self.OnClose)  
60    
61          self.choice_left_table.Append('Select ...', None)          if self.choice_left_table is not None:
62                self.choice_left_table.Append('Select ...', None)
63          self.choice_right_table.Append('Select ...', None)          self.choice_right_table.Append('Select ...', None)
64            
65          for t in session.Tables():          for t in session.Tables():
66              self.choice_left_table.Append(t.transient_table().Title(), t)              if self.choice_left_table is not None:
67              self.choice_right_table.Append(t.transient_table().Title(), t)                  self.choice_left_table.Append(t.transient_table().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.transient_table().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          EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable)          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)          EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable)
82    
83          self.choice_left_table.SetSelection(0)          if self.choice_left_table is not None:
84                self.choice_left_table.SetSelection(0)
85          self.choice_right_table.SetSelection(0)          self.choice_right_table.SetSelection(0)
86          self.button_join.Enable(False)          self.button_join.Enable(False)
87    
# Line 69  class JoinDialog(wxDialog): Line 89  class JoinDialog(wxDialog):
89    
90          sizer = wxFlexGridSizer(2, 4)          sizer = wxFlexGridSizer(2, 4)
91          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
92          sizer.Add(self.choice_left_table, 1,          if self.choice_left_table is not None:
93                                  wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)              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)          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
100          sizer.Add(self.choice_right_table, 1,          sizer.Add(self.choice_right_table, 1,
101                                  wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)                    wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
102          sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
103          sizer.Add(self.choice_left_field, 1,          sizer.Add(self.choice_left_field, 1,
104                                  wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)                    wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
105          sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
106          sizer.Add(self.choice_right_field, 1,          sizer.Add(self.choice_right_field, 1,
107                                  wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)                    wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
108    
109          sizer.AddGrowableCol(1)          sizer.AddGrowableCol(1)
110          sizer.AddGrowableCol(3)          sizer.AddGrowableCol(3)
# Line 93  class JoinDialog(wxDialog): Line 118  class JoinDialog(wxDialog):
118          topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4)          topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4)
119    
120          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
121          sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10)          sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT,
122          sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10)                    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)          topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10)
127    
# Line 103  class JoinDialog(wxDialog): Line 130  class JoinDialog(wxDialog):
130          topBox.Fit(self)          topBox.Fit(self)
131          topBox.SetSizeHints(self)          topBox.SetSizeHints(self)
132    
         self.valid = False  
   
133          # Save same parameters for later use.          # Save same parameters for later use.
         self.db = session.TransientDB()  
134          self.parent = parent          self.parent = parent
135          self.session = session          self.session = session
136    
137      def OnJoin(self, event):      def OnJoin(self, event):
138          """Get the table and field selections and perform the join."""          """Get the table and field selections and perform the join."""
139          # left          # left
140          i = self.choice_left_table.GetSelection()          if self.choice_left_table is not None:
141          left_table = self.choice_left_table.GetClientData(i)              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()          i = self.choice_left_field.GetSelection()
146          left_field  = self.choice_left_field.GetString(i)          left_field  = self.choice_left_field.GetString(i)
147          # right          # right
# Line 125  class JoinDialog(wxDialog): Line 152  class JoinDialog(wxDialog):
152    
153          outer_join = self.check_outer_join.IsChecked()          outer_join = self.check_outer_join.IsChecked()
154    
         result = True  
155          try:          try:
156              joined_table = TransientJoinedTable(self.db,              joined_table = TransientJoinedTable(self.session.TransientDB(),
157                                              left_table,  left_field,                                                  left_table, left_field,
158                                              right_table, right_field,                                                  right_table, right_field,
159                                              outer_join)                                                  outer_join)
160          except:          except:
161              dlg = wxMessageDialog(None,              dlg = wxMessageDialog(None,
162                                    _('Join failed:\n  %s') % sys.exc_info()[1],                                    _('Join failed:\n  %s') % sys.exc_info()[1],
163                                    _('Info'), wxOK|wxICON_ERROR)                                    _('Info'), wxOK|wxICON_ERROR)
164              dlg.ShowModal()              dlg.ShowModal()
165              dlg.Destroy()              dlg.Destroy()
166              result = False              return
167    
168          if result:          joined_table = self.session.AddTable(joined_table)
169              self.session.AddTable(joined_table)          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              name = joined_table.tablename
190              dialog = QueryTableFrame(self.parent, name,              dialog = QueryTableFrame(self.parent, name,
191                                  _('Table: %s') % joined_table.Title(),                                       _('Table: %s') % joined_table.Title(),
192                                  joined_table)                                       joined_table)
193              self.parent.add_dialog(name, dialog)              self.parent.add_dialog(name, dialog)
194              dialog.Show(True)              dialog.Show(True)
195    
196              self.Close()          self.Close()
197    
198      def OnClose(self, event):      def OnClose(self, event):
199          """Close the dialog."""          """Close the dialog."""
# Line 159  class JoinDialog(wxDialog): Line 204  class JoinDialog(wxDialog):
204          i = self.choice_left_table.GetSelection()          i = self.choice_left_table.GetSelection()
205          table = self.choice_left_table.GetClientData(i)          table = self.choice_left_table.GetClientData(i)
206          self.choice_left_field.Clear()          self.choice_left_field.Clear()
207          for col in table.Columns():          if table is not None:
208              self.choice_left_field.Append(col.name, col)              for col in table.Columns():
209                                self.choice_left_field.Append(col.name, col)
210          sel = self.choice_left_table.GetSelection()          self.update_sensitivity()
         rsel = self.choice_right_table.GetSelection()  
         self.valid = sel != -1 and (sel != rsel and rsel != 0)  
         self.button_join.Enable(self.valid)  
211    
212      def OnRightTable(self, event):      def OnRightTable(self, event):
213          """Get the selected table and fill the field choice."""          """Get the selected table and fill the field choice."""
214          i = self.choice_right_table.GetSelection()          i = self.choice_right_table.GetSelection()
215          table = self.choice_right_table.GetClientData(i)          table = self.choice_right_table.GetClientData(i)
216          self.choice_right_field.Clear()          self.choice_right_field.Clear()
217          for col in table.Columns():          if table is not None:
218              self.choice_right_field.Append(col.name, col)              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()          sel = self.choice_right_table.GetSelection()
228          lsel = self.choice_left_table.GetSelection()          self.button_join.Enable(sel > 0 and lsel > 0 and sel != lsel)
         self.valid = sel != -1 and (sel != lsel and lsel != 0)  
         self.button_join.Enable(self.valid)  
   

Legend:
Removed from v.1035  
changed lines
  Added in v.1072

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26