/[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 1022 by jan, Fri May 23 14:17:50 2003 UTC
# Line 9  import os, sys, math Line 9  import os, sys, math
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
14    from Thuban.UI.tableview import QueryTableFrame, LayerTableFrame
15                                                                                                                                                                    
16  ID_TABLE1 = 4001  ID_LEFT_TABLE = 4001
17  ID_TABLE2 = 4002  ID_RIGHT_TABLE = 4002
18    
19    CHOICE_WIDTH = 150
20    
21  class JoinDialog(wxDialog):  class JoinDialog(wxDialog):
22        
23        """Table join dialog.
24    
25        Join two tables (left/right) based on the user selected fields.
26        Opens a QueryTableFrame and registers the new table with the session.
27        """
28    
29      def __init__(self, parent, title, session, layer = None):      def __init__(self, parent, title, session, layer = None):
30          wxDialog.__init__(self, parent, -1, title,          wxDialog.__init__(self, parent, -1, title,
31                            style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)                            style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
32    
33          self.choice_table1 = wxChoice(self, ID_TABLE1)          self.choice_left_table = wxChoice(self, ID_LEFT_TABLE)
34          self.choice_table2 = wxChoice(self, ID_TABLE2)          width, height = self.choice_left_table.GetSizeTuple()
35            self.choice_left_table.SetSize(wxSize(CHOICE_WIDTH, height))
36          self.choice_fields1 = wxChoice(self, -1)          self.choice_right_table = wxChoice(self, ID_RIGHT_TABLE)
37          self.choice_fields2 = wxChoice(self, -1)          width, height = self.choice_right_table.GetSizeTuple()
38            self.choice_right_table.SetSize(wxSize(CHOICE_WIDTH, height))
39    
40            self.choice_left_field = wxChoice(self, -1)
41            width, height = self.choice_left_field.GetSizeTuple()
42            self.choice_left_field.SetSize(wxSize(CHOICE_WIDTH, height))
43            self.choice_right_field = wxChoice(self, -1)
44            width, height = self.choice_right_field.GetSizeTuple()
45            self.choice_right_field.SetSize(wxSize(CHOICE_WIDTH, height))
46    
47          self.button_join = wxButton(self, wxID_OK, _("Join"))          self.button_join = wxButton(self, wxID_OK, _("Join"))
48          self.button_join.SetDefault()          self.button_join.SetDefault()
49          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))          self.button_close = wxButton(self, wxID_CANCEL, _("Close"))
50    
51          tablenames = ["Picard", "Worf", "LaForge", "Troy"]          EVT_BUTTON(self, wxID_OK, self.OnJoin)
52          for name in tablenames:          #EVT_BUTTON(self, wxID_CANCEL, self.OnClose)
53              self.choice_table1.Append(name, None)  
54              self.choice_table2.Append(name, None)          self.choice_left_table.Append('Select ...', None)
55            self.choice_right_table.Append('Select ...', None)
56            
57            for t in session.Tables():
58                self.choice_left_table.Append(t.transient_table().Title(), t)
59                self.choice_right_table.Append(t.transient_table().Title(), t)
60    
61          EVT_CHOICE(self, ID_TABLE1, self.OnTable1)          EVT_CHOICE(self, ID_LEFT_TABLE, self.OnLeftTable)
62          EVT_CHOICE(self, ID_TABLE2, self.OnTable2)          EVT_CHOICE(self, ID_RIGHT_TABLE, self.OnRightTable)
63    
64          self.choice_table1.SetSelection(0)          self.choice_left_table.SetSelection(0)
65          self.choice_table2.SetSelection(0)          self.choice_right_table.SetSelection(0)
66          self.button_join.Enable(False)          self.button_join.Enable(False)
67    
68          topBox = wxBoxSizer(wxVERTICAL)          topBox = wxBoxSizer(wxVERTICAL)
69    
70          sizer = wxFlexGridSizer(2, 4)          sizer = wxFlexGridSizer(2, 4)
71          sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
72          sizer.Add(self.choice_table1, 1, wxEXPAND|wxALL, 4)          sizer.Add(self.choice_left_table, 1,
73          sizer.Add(wxStaticText(self, -1, _("Table Name:")), 0, wxALL, 4)                                  wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
74          sizer.Add(self.choice_table2, 1, wxEXPAND|wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Table:")), 0, wxALL, 4)
75          sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4)          sizer.Add(self.choice_right_table, 1,
76          sizer.Add(self.choice_fields1, 1, wxEXPAND|wxALL, 4)                                  wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
77          sizer.Add(wxStaticText(self, -1, _("Field Name:")), 0, wxALL, 4)          sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
78          sizer.Add(self.choice_fields2, 1, wxEXPAND|wxALL, 4)          sizer.Add(self.choice_left_field, 1,
79                                    wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
80            sizer.Add(wxStaticText(self, -1, _("Field:")), 0, wxALL, 4)
81            sizer.Add(self.choice_right_field, 1,
82                                    wxEXPAND|wxALL|wxALIGN_CENTER_VERTICAL, 4)
83    
84          sizer.AddGrowableCol(1)          sizer.AddGrowableCol(1)
85          sizer.AddGrowableCol(3)          sizer.AddGrowableCol(3)
86    
87          topBox.Add(sizer, 1, wxEXPAND|wxALL, 4)          topBox.Add(sizer, 0, wxEXPAND|wxALL, 4)
88    
89          sizer = wxBoxSizer(wxHORIZONTAL)          sizer = wxBoxSizer(wxHORIZONTAL)
90          sizer.Add(self.button_join, 0, wxEXPAND|wxALL, 4)          self.check_outer_join = wxCheckBox(self,-1,
91          sizer.Add(60, 20, 0, wxEXPAND|wxALL, 4)                                  _("Outer Join (preserves left table records)"))
92          sizer.Add(self.button_close, 0, wxEXPAND|wxALL, 4)          sizer.Add(self.check_outer_join, 1, wxALL,4)
93            topBox.Add(sizer, 0, wxALIGN_LEFT|wxALIGN_TOP, 4)
94    
95          topBox.Add(sizer, 0, wxALIGN_CENTER|wxALL, 4)          sizer = wxBoxSizer(wxHORIZONTAL)
96            sizer.Add(self.button_join, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10)
97            sizer.Add(self.button_close, 0, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxRIGHT, 10)
98    
99            topBox.Add(sizer, 1, wxALIGN_RIGHT|wxALIGN_BOTTOM|wxBOTTOM|wxTOP, 10)
100    
101          self.SetAutoLayout(True)          self.SetAutoLayout(True)
102          self.SetSizer(topBox)          self.SetSizer(topBox)
# Line 72  class JoinDialog(wxDialog): Line 105  class JoinDialog(wxDialog):
105    
106          self.valid = False          self.valid = False
107    
108      def OnOK(self, event):          # Save same parameters for later use.
109          print "OnOK"          self.db = session.TransientDB()
110            self.parent = parent
111      def OnCancel(self, event):          self.session = session
112          print "OnCancel"  
113        def OnJoin(self, event):
114      def OnTable1(self, event):          """Get the table and field selections and perform the join."""
115          print "OnTable1"          # left
116          #table = self.choice_table1.GetClientData()          i = self.choice_left_table.GetSelection()
117          #self.choice_fields1.Clear()          left_table = self.choice_left_table.GetClientData(i)
118          #for col in table.Columns():          i = self.choice_left_field.GetSelection()
119              #self.choice_fields1.Append(col.name, col)          left_field  = self.choice_left_field.GetString(i)
120            # right
121            i = self.choice_right_table.GetSelection()
122            right_table = self.choice_right_table.GetClientData(i)
123            i = self.choice_right_field.GetSelection()
124            right_field  = self.choice_right_field.GetString(i)
125    
126            outer_join = self.check_outer_join.IsChecked()
127    
128            result = True
129            try:
130                joined_table = TransientJoinedTable(self.db,
131                                                left_table,  left_field,
132                                                right_table, right_field,
133                                                outer_join)
134            except:
135                dlg = wxMessageDialog(None,
136                                      _('Join failed:\n  %s') % sys.exc_info()[1],
137                                      _('Info'), wxOK|wxICON_ERROR)
138                dlg.ShowModal()
139                dlg.Destroy()
140                result = False
141    
142            if result:
143                self.session.AddTable(joined_table)
144                name = joined_table.tablename
145                dialog = QueryTableFrame(self.parent, name,
146                                    _('Table: %s') % joined_table.Title(),
147                                    joined_table)
148                self.parent.add_dialog(name, dialog)
149                dialog.Show(true)
150    
151                self.Close()
152    
153        def OnClose(self, event):
154            """Close the dialog."""
155            self.Close()
156    
157        def OnLeftTable(self, event):
158            """Get the selected table and fill the field choice."""
159            i = self.choice_left_table.GetSelection()
160            table = self.choice_left_table.GetClientData(i)
161            self.choice_left_field.Clear()
162            for col in table.Columns():
163                self.choice_left_field.Append(col.name, col)
164                            
165          sel = self.choice_table1.GetSelection()          sel = self.choice_left_table.GetSelection()
166          self.valid = sel != -1 and sel != self.choice_table2.GetSelection()          rsel = self.choice_right_table.GetSelection()
167            self.valid = sel != -1 and (sel != rsel and rsel != 0)
168          self.button_join.Enable(self.valid)          self.button_join.Enable(self.valid)
169    
170      def OnTable2(self, event):      def OnRightTable(self, event):
171          print "OnTable2"          """Get the selected table and fill the field choice."""
172          #table = self.choice_table2.GetClientData()          i = self.choice_right_table.GetSelection()
173          #self.choice_fields2.Clear()          table = self.choice_right_table.GetClientData(i)
174          #for col in table.Columns():          self.choice_right_field.Clear()
175              #self.choice_fields2.Append(col.name, col)          for col in table.Columns():
176                self.choice_right_field.Append(col.name, col)
177          sel = self.choice_table2.GetSelection()  
178          self.valid = sel != -1 and sel != self.choice_table1.GetSelection()          sel = self.choice_right_table.GetSelection()
179            lsel = self.choice_left_table.GetSelection()
180            self.valid = sel != -1 and (sel != lsel and lsel != 0)
181          self.button_join.Enable(self.valid)          self.button_join.Enable(self.valid)
182    
     def GetJoin(self):  
         pass  
         # if self.valid:  
             #return ((self.choice_table1.GetClientData  
   

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26