/[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 1890 by frank, Thu Oct 30 16:20:41 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  """The layer and table join dialog"""
9    
10    __version__ = "$Revision$"
11    # $Source$
12    # $Id$
13    
14    
15    import sys
16  from wxPython.wx import *  from wxPython.wx import *
17                                                                                    
18  from Thuban import _  from Thuban import _
19                                                                                    
20  ID_TABLE1 = 4001  from Thuban.Model.transientdb import TransientJoinedTable
21  ID_TABLE2 = 4002  from Thuban.Model.data import DerivedShapeStore
22    from Thuban.UI.tableview import QueryTableFrame
23    
24    from common import ThubanBeginBusyCursor, ThubanEndBusyCursor
25    
26    ID_LEFT_TABLE = 4001
27    ID_RIGHT_TABLE = 4002
28    
29    CHOICE_WIDTH = 150
30    
31  class JoinDialog(wxDialog):  class JoinDialog(wxDialog):
32    
33        """Table join dialog.
34    
35        Join two tables (left/right) based on the user selected fields.
36        Opens a QueryTableFrame and registers the new table with the session.
37        """
38    
39      def __init__(self, parent, title, session, layer = None):      def __init__(self, parent, title, session, layer = None):
40          wxDialog.__init__(self, parent, -1, title,          wxDialog.__init__(self, parent, -1, title,
41                            style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)                            style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
42            self.layer = layer
43    
44          self.choice_table1 = wxChoice(self, ID_TABLE1)          if not layer:
45          self.choice_table2 = wxChoice(self, ID_TABLE2)              self.choice_left_table = wxChoice(self, ID_LEFT_TABLE)
46                width, height = self.choice_left_table.GetSizeTuple()
47          self.choice_fields1 = wxChoice(self, -1)              self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height))
48          self.choice_fields2 = wxChoice(self, -1)              self.left_table = None
49            else:
50                self.choice_left_table = None
51                self.left_table = layer.ShapeStore().Table()
52    
53            self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE)
54            width, height = self.choice_right_table.GetSizeTuple()
55            self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height))
56    
57            self.choice_left_field = wxChoice(self, -1)
58            width, height = self.choice_left_field.GetSizeTuple()
59            self.choice_left_field.SetSize(wxSize(CHOICE_WIDTH, height))
60            self.choice_right_field = wxChoice(self, -1)
61            width, height = self.choice_right_field.GetSizeTuple()
62            self.choice_right_field.SetSize(wxSize(CHOICE_WIDTH, height))
63    
64          self.button_join = wxButton(self, wxID_OK, _("Join"))          self.button_join = wxButton(self, wxID_OK, _("Join"))
65          self.button_join.SetDefault()          self.button_join.SetDefault()
66          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))
67    
68          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)  
   
         EVT_CHOICE(self, ID_TABLE1, self.OnTable1)  
         EVT_CHOICE(self, ID_TABLE2, self.OnTable2)  
69    
70          self.choice_table1.SetSelection(0)          if self.choice_left_table is not None:
71          self.choice_table2.SetSelection(0)              self.choice_left_table.Append('Select ...', None)
72            self.choice_right_table.Append('Select ...', None)
73    
74            for t in session.Tables():
75                if self.choice_left_table is not None:
76                    self.choice_left_table.Append(t.Title(), t)
77    
78                # 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                    self.choice_right_table.Append(t.Title(), t)
83    
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                EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable)
90            EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable)
91    
92            if self.choice_left_table is not None:
93                self.choice_left_table.SetSelection(0)
94            self.choice_right_table.SetSelection(0)
95          self.button_join.Enable(False)          self.button_join.Enable(False)
96    
97          topBox = wxBoxSizer(wxVERTICAL)          topBox = wxBoxSizer(wxVERTICAL)
98    
99          sizer = wxFlexGridSizer(2, 4)          sizer = wxFlexGridSizer(2, 4)
100          sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
101          sizer.Add(self.choice_table1, 1, wxEXPAND|wxALL, 4)          if self.choice_left_table is not None:
102          sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4)              sizer.Add(self.choice_left_table, 1,
103          sizer.Add(self.choice_table2, 1, wxEXPAND|wxALL, 4)                        wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
104          sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4)          else:
105          sizer.Add(self.choice_fields1, 1, wxEXPAND|wxALL, 4)              sizer.Add(wxStaticText(self, -1, self.left_table.Title()), 0,
106          sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4)                        wxALL, 4)
107          sizer.Add(self.choice_fields2, 1, wxEXPAND|wxALL, 4)  
108            sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
109            sizer.Add(self.choice_right_table, 1,
110                      wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
111            sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
112            sizer.Add(self.choice_left_field, 1,
113                      wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
114            sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
115            sizer.Add(self.choice_right_field, 1,
116                      wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
117    
118          sizer.AddGrowableCol(1)          sizer.AddGrowableCol(1)
119          sizer.AddGrowableCol(3)          sizer.AddGrowableCol(3)
120    
121          topBox.Add(sizer, 1, wxEXPAND|wxALL, 4)          topBox.Add(sizer, 0, wxEXPAND|wxALL, 4)
122    
123            sizer = wxBoxSizer(wxHORIZONTAL)
124            self.check_outer_join = wxCheckBox(self,-1,
125                                    _("Outer Join (preserves left table records)"))
126            sizer.Add(self.check_outer_join, 1, wxALL,4)
127            topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4)
128    
129          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
130          sizer.Add(self.button_join, 0, wxEXPAND|wxALL, 4)          sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT,
131          sizer.Add(60, 20, 0, wxEXPAND|wxALL, 4)                    10)
132          sizer.Add(self.button_close, 0, wxEXPAND|wxALL, 4)          sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT,
133                      10)
134    
135          topBox.Add(sizer, 0, wxALIGN_CENTER|wxALL, 4)          topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10)
136    
137          self.SetAutoLayout(True)          self.SetAutoLayout(True)
138          self.SetSizer(topBox)          self.SetSizer(topBox)
139          topBox.Fit(self)          topBox.Fit(self)
140          topBox.SetSizeHints(self)          topBox.SetSizeHints(self)
141    
142          self.valid = False          # Save same parameters for later use.
143            self.parent = parent
144      def OnOK(self, event):          self.session = session
145          print "OnOK"  
146        def OnJoin(self, event):
147      def OnCancel(self, event):          """Get the table and field selections and perform the join."""
148          print "OnCancel"          ThubanBeginBusyCursor()
149            try:
150      def OnTable1(self, event):              # left
151          print "OnTable1"              if self.choice_left_table is not None:
152          #table = self.choice_table1.GetClientData()                  i = self.choice_left_table.GetSelection()
153          #self.choice_fields1.Clear()                  left_table = self.choice_left_table.GetClientData(i)
154          #for col in table.Columns():              else:
155              #self.choice_fields1.Append(col.name, col)                  left_table = self.left_table
156                            i = self.choice_left_field.GetSelection()
157          sel = self.choice_table1.GetSelection()              left_field  = self.choice_left_field.GetString(i)
158          self.valid = sel != -1 and sel != self.choice_table2.GetSelection()              # right
159          self.button_join.Enable(self.valid)              i = self.choice_right_table.GetSelection()
160                right_table = self.choice_right_table.GetClientData(i)
161      def OnTable2(self, event):              i = self.choice_right_field.GetSelection()
162          print "OnTable2"              right_field  = self.choice_right_field.GetString(i)
163          #table = self.choice_table2.GetClientData()      
164          #self.choice_fields2.Clear()              outer_join = self.check_outer_join.IsChecked()
165          #for col in table.Columns():      
166              #self.choice_fields2.Append(col.name, col)              try:
167                    joined_table = TransientJoinedTable(self.session.TransientDB(),
168          sel = self.choice_table2.GetSelection()                                                      left_table, left_field,
169          self.valid = sel != -1 and sel != self.choice_table1.GetSelection()                                                      right_table, right_field,
170          self.button_join.Enable(self.valid)                                                      outer_join)
171                except:
172      def GetJoin(self):                  dlg = wxMessageDialog(None,
173          pass                                        _('Join failed:\n  %s') % sys.exc_info()[1],
174          # if self.valid:                                        _('Info'), wxOK|wxICON_ERROR)
175              #return ((self.choice_table1.GetClientData                  dlg.ShowModal()
176                    dlg.Destroy()
177                    return
178        
179                joined_table = self.session.AddTable(joined_table)
180    
181                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    
190            finally:
191                ThubanEndBusyCursor()
192    
193            if self.layer is not None:
194                if needed_rows != joined_rows:
195                    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                            % {"joined": joined_rows,
199                               "needed": needed_rows}
200                    dlg = wxMessageDialog(None, msg, _('Join Failed'),
201                                          wxOK|wxICON_ERROR)
202                    dlg.ShowModal()
203                    dlg.Destroy()
204                    return
205    
206            else:
207                name = joined_table.tablename
208                dialog = QueryTableFrame(self.parent, name,
209                                         _('Table: %s') % joined_table.Title(),
210                                         joined_table)
211                self.parent.add_dialog(name, dialog)
212                dialog.Show(True)
213        
214            self.Close()
215    
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            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    
230        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            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    
240        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            sel = self.choice_right_table.GetSelection()
246            self.button_join.Enable(sel > 0 and lsel > 0 and sel != lsel)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26