/[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 888 by jonathan, Fri May 9 16:40:50 2003 UTC revision 1521 by bh, Wed Jul 30 12:34:10 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  ID_TABLE1 = 4001  from Thuban.Model.transientdb import TransientJoinedTable
14  ID_TABLE2 = 4002  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):  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):      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          self.choice_table1 = wxChoice(self, ID_TABLE1)          if not layer:
36          self.choice_table2 = wxChoice(self, ID_TABLE2)              self.choice_left_table = wxChoice(self, ID_LEFT_TABLE)
37                width, height = self.choice_left_table.GetSizeTuple()
38          self.choice_fields1 = wxChoice(self, -1)              self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height))
39          self.choice_fields2 = wxChoice(self, -1)              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"))          self.button_join = wxButton(self, wxID_OK, _("Join"))
56          self.button_join.SetDefault()          self.button_join.SetDefault()
57          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))
58    
59          tablenames = ["Picard", "Worf", "LaForge", "Troy"]          EVT_BUTTON(self, wxID_OK, self.OnJoin)
         for name in tablenames:  
             self.choice_table1.Append(name, None)  
             self.choice_table2.Append(name, None)  
60    
61          EVT_CHOICE(self, ID_TABLE1, self.OnTable1)          if self.choice_left_table is not None:
62          EVT_CHOICE(self, ID_TABLE2, self.OnTable2)              self.choice_left_table.Append('Select ...', None)
63            self.choice_right_table.Append('Select ...', None)
64          self.choice_table1.SetSelection(0)  
65          self.choice_table2.SetSelection(0)          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)          self.button_join.Enable(False)
87    
88          topBox = wxBoxSizer(wxVERTICAL)          topBox = wxBoxSizer(wxVERTICAL)
89    
90          sizer = wxFlexGridSizer(2, 4)          sizer = wxFlexGridSizer(2, 4)
91          sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
92          sizer.Add(self.choice_table1, 1, wxEXPAND|wxALL, 4)          if self.choice_left_table is not None:
93          sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4)              sizer.Add(self.choice_left_table, 1,
94          sizer.Add(self.choice_table2, 1, wxEXPAND|wxALL, 4)                        wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
95          sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4)          else:
96          sizer.Add(self.choice_fields1, 1, wxEXPAND|wxALL, 4)              sizer.Add(wxStaticText(self, -1, self.left_table.Title()), 0,
97          sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4)                        wxALL, 4)
98          sizer.Add(self.choice_fields2, 1, wxEXPAND|wxALL, 4)  
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)          sizer.AddGrowableCol(1)
110          sizer.AddGrowableCol(3)          sizer.AddGrowableCol(3)
111    
112          topBox.Add(sizer, 1, wxEXPAND|wxALL, 4)          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)          sizer = wxBoxSizer(wxHORIZONTAL)
121          sizer.Add(self.button_join, 0, wxEXPAND|wxALL, 4)          sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT,
122          sizer.Add(60, 20, 0, wxEXPAND|wxALL, 4)                    10)
123          sizer.Add(self.button_close, 0, wxEXPAND|wxALL, 4)          sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT,
124                      10)
125    
126          topBox.Add(sizer, 0, wxALIGN_CENTER|wxALL, 4)          topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10)
127    
128          self.SetAutoLayout(True)          self.SetAutoLayout(True)
129          self.SetSizer(topBox)          self.SetSizer(topBox)
130          topBox.Fit(self)          topBox.Fit(self)
131          topBox.SetSizeHints(self)          topBox.SetSizeHints(self)
132    
133          self.valid = False          # Save same parameters for later use.
134            self.parent = parent
135      def OnOK(self, event):          self.session = session
136          print "OnOK"  
137        def OnJoin(self, event):
138      def OnCancel(self, event):          """Get the table and field selections and perform the join."""
139          print "OnCancel"          # left
140            if self.choice_left_table is not None:
141      def OnTable1(self, event):              i = self.choice_left_table.GetSelection()
142          print "OnTable1"              left_table = self.choice_left_table.GetClientData(i)
143          #table = self.choice_table1.GetClientData()          else:
144          #self.choice_fields1.Clear()              left_table = self.left_table
145          #for col in table.Columns():          i = self.choice_left_field.GetSelection()
146              #self.choice_fields1.Append(col.name, col)          left_field  = self.choice_left_field.GetString(i)
147                        # right
148          sel = self.choice_table1.GetSelection()          i = self.choice_right_table.GetSelection()
149          self.valid = sel != -1 and sel != self.choice_table2.GetSelection()          right_table = self.choice_right_table.GetClientData(i)
150          self.button_join.Enable(self.valid)          i = self.choice_right_field.GetSelection()
151            right_field  = self.choice_right_field.GetString(i)
152      def OnTable2(self, event):  
153          print "OnTable2"          outer_join = self.check_outer_join.IsChecked()
154          #table = self.choice_table2.GetClientData()  
155          #self.choice_fields2.Clear()          try:
156          #for col in table.Columns():              joined_table = TransientJoinedTable(self.session.TransientDB(),
157              #self.choice_fields2.Append(col.name, col)                                                  left_table, left_field,
158                                                    right_table, right_field,
159          sel = self.choice_table2.GetSelection()                                                  outer_join)
160          self.valid = sel != -1 and sel != self.choice_table1.GetSelection()          except:
161          self.button_join.Enable(self.valid)              dlg = wxMessageDialog(None,
162                                      _('Join failed:\n  %s') % sys.exc_info()[1],
163      def GetJoin(self):                                    _('Info'), wxOK|wxICON_ERROR)
164          pass              dlg.ShowModal()
165          # if self.valid:              dlg.Destroy()
166              #return ((self.choice_table1.GetClientData              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)

Legend:
Removed from v.888  
changed lines
  Added in v.1521

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26