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 TableFrame, 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 TableFrame 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().tablename, t) |
59 |
|
self.choice_right_table.Append(t.transient_table().tablename, 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) |
87 |
topBox.Add(sizer, 1, wxEXPAND|wxALL, 4) |
topBox.Add(sizer, 1, wxEXPAND|wxALL, 4) |
88 |
|
|
89 |
sizer = wxBoxSizer(wxHORIZONTAL) |
sizer = wxBoxSizer(wxHORIZONTAL) |
90 |
sizer.Add(self.button_join, 0, wxEXPAND|wxALL, 4) |
sizer.Add(self.button_join, 0, wxEXPAND|wxRIGHT, 10) |
91 |
sizer.Add(60, 20, 0, wxEXPAND|wxALL, 4) |
sizer.Add(self.button_close, 0, wxEXPAND|wxRIGHT, 10) |
|
sizer.Add(self.button_close, 0, wxEXPAND|wxALL, 4) |
|
92 |
|
|
93 |
topBox.Add(sizer, 0, wxALIGN_CENTER|wxALL, 4) |
topBox.Add(sizer, 0, wxALIGN_RIGHT|wxBOTTOM|wxTOP, 10) |
94 |
|
|
95 |
self.SetAutoLayout(True) |
self.SetAutoLayout(True) |
96 |
self.SetSizer(topBox) |
self.SetSizer(topBox) |
99 |
|
|
100 |
self.valid = False |
self.valid = False |
101 |
|
|
102 |
def OnOK(self, event): |
# Save same parameters for later use. |
103 |
print "OnOK" |
self.db = session.TransientDB() |
104 |
|
self.parent = parent |
105 |
def OnCancel(self, event): |
self.session = session |
106 |
print "OnCancel" |
|
107 |
|
def OnJoin(self, event): |
108 |
def OnTable1(self, event): |
"""Get the table and field selections and perform the join.""" |
109 |
print "OnTable1" |
# left |
110 |
#table = self.choice_table1.GetClientData() |
i = self.choice_left_table.GetSelection() |
111 |
#self.choice_fields1.Clear() |
left_table = self.choice_left_table.GetClientData(i) |
112 |
#for col in table.Columns(): |
i = self.choice_left_field.GetSelection() |
113 |
#self.choice_fields1.Append(col.name, col) |
left_field = self.choice_left_field.GetString(i) |
114 |
|
# right |
115 |
|
i = self.choice_right_table.GetSelection() |
116 |
|
right_table = self.choice_right_table.GetClientData(i) |
117 |
|
i = self.choice_right_field.GetSelection() |
118 |
|
right_field = self.choice_right_field.GetString(i) |
119 |
|
|
120 |
|
result = True |
121 |
|
try: |
122 |
|
joined_table = TransientJoinedTable(self.db, left_table, left_field, |
123 |
|
right_table, right_field) |
124 |
|
except: |
125 |
|
exc_type, exc_value, exc_traceback = sys.exc_info() |
126 |
|
dlg = wxMessageDialog(None, 'Join failed:\n %s' % exc_value, |
127 |
|
'Info',wxOK|wxICON_ERROR) |
128 |
|
dlg.ShowModal() |
129 |
|
dlg.Destroy |
130 |
|
result = False |
131 |
|
|
132 |
|
if result: |
133 |
|
self.session.AddTable(joined_table) |
134 |
|
name = joined_table.tablename |
135 |
|
dialog = TableFrame(self.parent, name, |
136 |
|
_("Table: %s") % name, joined_table) |
137 |
|
self.parent.add_dialog(name, dialog) |
138 |
|
dialog.Show(true) |
139 |
|
|
140 |
|
self.Close() |
141 |
|
|
142 |
|
def OnClose(self, event): |
143 |
|
"""Close the dialog.""" |
144 |
|
self.Close() |
145 |
|
|
146 |
|
def OnLeftTable(self, event): |
147 |
|
"""Get the selected table and fill the field choice.""" |
148 |
|
i = self.choice_left_table.GetSelection() |
149 |
|
table = self.choice_left_table.GetClientData(i) |
150 |
|
self.choice_left_field.Clear() |
151 |
|
for col in table.Columns(): |
152 |
|
self.choice_left_field.Append(col.name, col) |
153 |
|
|
154 |
sel = self.choice_table1.GetSelection() |
sel = self.choice_left_table.GetSelection() |
155 |
self.valid = sel != -1 and sel != self.choice_table2.GetSelection() |
rsel = self.choice_right_table.GetSelection() |
156 |
|
self.valid = sel != -1 and (sel != rsel and rsel != 0) |
157 |
self.button_join.Enable(self.valid) |
self.button_join.Enable(self.valid) |
158 |
|
|
159 |
def OnTable2(self, event): |
def OnRightTable(self, event): |
160 |
print "OnTable2" |
"""Get the selected table and fill the field choice.""" |
161 |
#table = self.choice_table2.GetClientData() |
i = self.choice_right_table.GetSelection() |
162 |
#self.choice_fields2.Clear() |
table = self.choice_right_table.GetClientData(i) |
163 |
#for col in table.Columns(): |
self.choice_right_field.Clear() |
164 |
#self.choice_fields2.Append(col.name, col) |
for col in table.Columns(): |
165 |
|
self.choice_right_field.Append(col.name, col) |
166 |
sel = self.choice_table2.GetSelection() |
|
167 |
self.valid = sel != -1 and sel != self.choice_table1.GetSelection() |
sel = self.choice_right_table.GetSelection() |
168 |
|
lsel = self.choice_left_table.GetSelection() |
169 |
|
self.valid = sel != -1 and (sel != lsel and lsel != 0) |
170 |
self.button_join.Enable(self.valid) |
self.button_join.Enable(self.valid) |
171 |
|
|
|
def GetJoin(self): |
|
|
pass |
|
|
# if self.valid: |
|
|
#return ((self.choice_table1.GetClientData |
|
|
|
|