1 |
# Copyright (c) 2001, 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
3 |
# Martin Mueller <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
5 |
# |
6 |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
8 |
|
9 |
|
10 |
"""Dialogs to manage database connections""" |
11 |
|
12 |
import sys, traceback |
13 |
|
14 |
from wxPython.wx import * |
15 |
|
16 |
try: |
17 |
import ogr |
18 |
except ImportError: |
19 |
psycopg = None |
20 |
|
21 |
from Thuban import _ |
22 |
from Thuban.UI.dialogs import NonModalDialog |
23 |
from Thuban.Model.table import FIELDTYPE_INT |
24 |
from Extensions.ogr import ogrshapes |
25 |
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED |
26 |
from Thuban.UI.messages import SESSION_REPLACED |
27 |
|
28 |
|
29 |
ID_DB_ADD = 9101 |
30 |
ID_DB_REMOVE = 9102 |
31 |
|
32 |
ID_DBCHOOSE_RETRIEVE = 9201 |
33 |
ID_DBCHOOSE_OK = 9202 |
34 |
ID_DBCHOOSE_CANCEL = 9203 |
35 |
ID_LB_DCLICK = 9204 |
36 |
|
37 |
|
38 |
class ChooseFileFormat(wxDialog): |
39 |
|
40 |
def __init__(self, parent, session): |
41 |
wxDialog.__init__(self, parent, -1, _("Choose file format"), |
42 |
style = wxDIALOG_MODAL|wxCAPTION) |
43 |
self.session = session |
44 |
self.tables = [] |
45 |
|
46 |
# |
47 |
# Build the dialog |
48 |
# |
49 |
|
50 |
# Sizer for the entire dialog |
51 |
top = wxFlexGridSizer(2, 1, 0, 0) |
52 |
|
53 |
# Sizer for the main part with the list boxes |
54 |
main_sizer = wxBoxSizer(wxHORIZONTAL) |
55 |
top.Add(main_sizer, 1, wxEXPAND, 0) |
56 |
|
57 |
# The list box with the drivers |
58 |
static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("File formats")), |
59 |
wxHORIZONTAL) |
60 |
self.lb_drivers = wxListBox(self, -1) |
61 |
static_box.Add(self.lb_drivers, 0, wxEXPAND, 0) |
62 |
main_sizer.Add(static_box, 1, wxEXPAND, 0) |
63 |
|
64 |
for i in range(ogr.GetDriverCount()): |
65 |
self.lb_drivers.Append(ogr.GetDriver(i).GetName()) |
66 |
if self.lb_drivers.GetCount() > 0: |
67 |
self.lb_drivers.SetSelection(0, True) |
68 |
|
69 |
# The button box between the connections list box and the table |
70 |
# list box |
71 |
buttons = wxFlexGridSizer(3, 1, 0, 0) |
72 |
buttons.Add(20, 80, 0, wxEXPAND, 0) |
73 |
retrieve_button = wxButton(self, ID_DBCHOOSE_RETRIEVE, _("Retrieve")) |
74 |
EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve) |
75 |
buttons.Add(retrieve_button, 0, wxALL |
76 |
|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4) |
77 |
buttons.Add(20, 80, 0, wxEXPAND, 0) |
78 |
main_sizer.Add(buttons, 0, wxEXPAND, 0) |
79 |
|
80 |
# The list box with the tables |
81 |
static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")), |
82 |
wxHORIZONTAL) |
83 |
self.lb_tables = wxListBox(self, ID_LB_DCLICK) |
84 |
EVT_LISTBOX(self, ID_LB_DCLICK, self.OnTableSelect) |
85 |
EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick) |
86 |
static_box.Add(self.lb_tables, 0, wxEXPAND, 0) |
87 |
main_sizer.Add(static_box, 1, wxEXPAND, 0) |
88 |
|
89 |
# id column and geometry column selection |
90 |
box = wxBoxSizer(wxVERTICAL) |
91 |
box.Add(wxStaticText(self, -1, _("ID Column")), 0, |
92 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
93 |
self.text_id_column = wxComboBox(self, -1, "") |
94 |
box.Add(self.text_id_column, 0, |
95 |
wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
96 |
|
97 |
box.Add(wxStaticText(self, -1, _("Geometry Column")), 0, |
98 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
99 |
self.text_geo_column = wxComboBox(self, -1, "") |
100 |
box.Add(self.text_geo_column, 0, |
101 |
wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
102 |
main_sizer.Add(box, 1, wxEXPAND, 0) |
103 |
|
104 |
# The standard button box at the bottom of the dialog |
105 |
buttons = wxFlexGridSizer(1, 2, 0, 0) |
106 |
ok_button = wxButton(self, ID_DBCHOOSE_OK, _("OK")) |
107 |
EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK) |
108 |
buttons.Add(ok_button, 0, wxALL|wxALIGN_RIGHT, 4) |
109 |
cancel_button = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel")) |
110 |
EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel) |
111 |
buttons.Add(cancel_button, 0, wxALL, 4) |
112 |
top.Add(buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4) |
113 |
|
114 |
# Autosizing |
115 |
self.SetAutoLayout(1) |
116 |
self.SetSizer(top) |
117 |
top.Fit(self) |
118 |
top.SetSizeHints(self) |
119 |
self.Layout() |
120 |
|
121 |
|
122 |
def GetTable(self): |
123 |
i = self.lb_tables.GetSelection() |
124 |
if i >= 0: |
125 |
return (self.selected_conn, self.tables[i], |
126 |
self.text_id_column.GetValue(), |
127 |
self.text_geo_column.GetValue()) |
128 |
return None |
129 |
|
130 |
def OnRetrieve(self, event): |
131 |
i = self.lb_driver.GetSelection() |
132 |
if i >= 0: |
133 |
self.selected_conn = self.dbconns[i] |
134 |
self.tables = self.selected_conn.GeometryTables() |
135 |
self.lb_tables.Set(self.tables) |
136 |
|
137 |
def OnTableSelect(self, event): |
138 |
i = self.lb_tables.GetSelection() |
139 |
self.text_id_column.Clear() |
140 |
self.text_geo_column.Clear() |
141 |
if i >= 0: |
142 |
for name, typ in self.selected_conn.table_columns(self.tables[i]): |
143 |
if typ == "geometry": |
144 |
self.text_geo_column.Append(name) |
145 |
elif typ == FIELDTYPE_INT: |
146 |
self.text_id_column.Append(name) |
147 |
|
148 |
def OnLBDClick(self, event): |
149 |
if self.lb_tables.GetSelection() >= 0: |
150 |
self.EndModal(wxID_OK) |
151 |
self.Show(False) |
152 |
|
153 |
def OnOK(self, event): |
154 |
self.EndModal(wxID_OK) |
155 |
self.Show(False) |
156 |
|
157 |
def OnCancel(self, event): |
158 |
self.EndModal(wxID_CANCEL) |
159 |
self.Show(False) |
160 |
|
161 |
|
162 |
class ChooseLayer(wxDialog): |
163 |
|
164 |
def __init__(self, parent, filename): |
165 |
wxDialog.__init__(self, parent, -1, _("Choose layer"), |
166 |
style = wxDIALOG_MODAL|wxCAPTION) |
167 |
self.tables = [] |
168 |
|
169 |
# |
170 |
# Build the dialog |
171 |
# |
172 |
|
173 |
# Sizer for the entire dialog |
174 |
top = wxFlexGridSizer(2, 1, 0, 0) |
175 |
|
176 |
# Sizer for the main part with the list boxes |
177 |
main_sizer = wxBoxSizer(wxHORIZONTAL) |
178 |
top.Add(main_sizer, 1, wxEXPAND, 0) |
179 |
|
180 |
# The list box with the drivers |
181 |
static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Layers")), |
182 |
wxHORIZONTAL) |
183 |
self.lb_drivers = wxListBox(self, -1) |
184 |
static_box.Add(self.lb_drivers, 0, wxEXPAND, 0) |
185 |
main_sizer.Add(static_box, 1, wxEXPAND, 0) |
186 |
|
187 |
datasource = ogr.Open(filename) |
188 |
self.layer = [] |
189 |
for i in range(datasource.GetLayerCount()): |
190 |
self.layer.append(datasource.GetLayer(i)) |
191 |
self.lb_drivers.Append(datasource.GetLayer(i).GetName()) |
192 |
if self.lb_drivers.GetCount() > 0: |
193 |
self.lb_drivers.SetSelection(0, True) |
194 |
|
195 |
# The standard button box at the bottom of the dialog |
196 |
buttons = wxFlexGridSizer(1, 2, 0, 0) |
197 |
ok_button = wxButton(self, ID_DBCHOOSE_OK, _("OK")) |
198 |
EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK) |
199 |
buttons.Add(ok_button, 0, wxALL|wxALIGN_RIGHT, 4) |
200 |
cancel_button = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel")) |
201 |
EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel) |
202 |
buttons.Add(cancel_button, 0, wxALL, 4) |
203 |
top.Add(buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4) |
204 |
|
205 |
# Autosizing |
206 |
self.SetAutoLayout(1) |
207 |
self.SetSizer(top) |
208 |
top.Fit(self) |
209 |
top.SetSizeHints(self) |
210 |
self.Layout() |
211 |
|
212 |
def end_dialog(self, result): |
213 |
self.result = result |
214 |
if result is not None: |
215 |
self.EndModal(wxID_OK) |
216 |
else: |
217 |
self.EndModal(wxID_CANCEL) |
218 |
self.Show(False) |
219 |
|
220 |
def OnOK(self, event): |
221 |
self.end_dialog(self.lb_drivers.GetSelection()) |
222 |
|
223 |
def OnCancel(self, event): |
224 |
self.end_dialog(None) |
225 |
|
226 |
def GetLayer(self): |
227 |
return self.layer[self.lb_drivers.GetSelection()].GetName() |
228 |
|
229 |
|
230 |
class ChooseOGRDBTableDialog(wxDialog): |
231 |
|
232 |
def __init__(self, parent, session): |
233 |
wxDialog.__init__(self, parent, -1, _("Choose layer from database"), |
234 |
style = wxDIALOG_MODAL|wxCAPTION) |
235 |
self.session = session |
236 |
self.dbconns = self.session.DBConnections() |
237 |
self.tables = [] |
238 |
|
239 |
# |
240 |
# Build the dialog |
241 |
# |
242 |
|
243 |
# Sizer for the entire dialog |
244 |
top = wxFlexGridSizer(2, 1, 0, 0) |
245 |
|
246 |
# Sizer for the main part with the list boxes |
247 |
main_sizer = wxBoxSizer(wxHORIZONTAL) |
248 |
top.Add(main_sizer, 1, wxEXPAND, 0) |
249 |
|
250 |
# The list box with the connections |
251 |
static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Databases")), |
252 |
wxHORIZONTAL) |
253 |
self.lb_connections = wxListBox(self, -1) |
254 |
static_box.Add(self.lb_connections, 0, wxEXPAND, 0) |
255 |
main_sizer.Add(static_box, 1, wxEXPAND, 0) |
256 |
|
257 |
for i in range(len(self.dbconns)): |
258 |
self.lb_connections.Append(self.dbconns[i].BriefDescription()) |
259 |
if self.lb_connections.GetCount() > 0: |
260 |
self.lb_connections.SetSelection(0, True) |
261 |
|
262 |
# The button box between the connections list box and the table |
263 |
# list box |
264 |
buttons = wxFlexGridSizer(3, 1, 0, 0) |
265 |
buttons.Add(20, 80, 0, wxEXPAND, 0) |
266 |
retrieve_button = wxButton(self, ID_DBCHOOSE_RETRIEVE, _("Retrieve")) |
267 |
EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve) |
268 |
buttons.Add(retrieve_button, 0, wxALL |
269 |
|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 4) |
270 |
buttons.Add(20, 80, 0, wxEXPAND, 0) |
271 |
main_sizer.Add(buttons, 0, wxEXPAND, 0) |
272 |
|
273 |
# The list box with the tables |
274 |
static_box = wxStaticBoxSizer(wxStaticBox(self, -1, _("Tables")), |
275 |
wxHORIZONTAL) |
276 |
self.lb_tables = wxListBox(self, ID_LB_DCLICK) |
277 |
EVT_LISTBOX(self, ID_LB_DCLICK, self.OnTableSelect) |
278 |
EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick) |
279 |
static_box.Add(self.lb_tables, 0, wxEXPAND, 0) |
280 |
main_sizer.Add(static_box, 1, wxEXPAND, 0) |
281 |
|
282 |
# The standard button box at the bottom of the dialog |
283 |
buttons = wxFlexGridSizer(1, 2, 0, 0) |
284 |
ok_button = wxButton(self, ID_DBCHOOSE_OK, _("OK")) |
285 |
EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK) |
286 |
buttons.Add(ok_button, 0, wxALL|wxALIGN_RIGHT, 4) |
287 |
cancel_button = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel")) |
288 |
EVT_BUTTON(self, ID_DBCHOOSE_CANCEL, self.OnCancel) |
289 |
buttons.Add(cancel_button, 0, wxALL, 4) |
290 |
top.Add(buttons, 1, wxALL|wxALIGN_CENTER_HORIZONTAL, 4) |
291 |
|
292 |
# Autosizing |
293 |
self.SetAutoLayout(1) |
294 |
self.SetSizer(top) |
295 |
top.Fit(self) |
296 |
top.SetSizeHints(self) |
297 |
self.Layout() |
298 |
|
299 |
|
300 |
def GetTable(self): |
301 |
i = self.lb_tables.GetSelection() |
302 |
if i >= 0: |
303 |
return (self.selected_conn, self.tables[i]) |
304 |
return None |
305 |
|
306 |
def OnRetrieve(self, event): |
307 |
i = self.lb_connections.GetSelection() |
308 |
if i >= 0: |
309 |
self.selected_conn = self.dbconns[i] |
310 |
self.tables = self.selected_conn.GeometryTables() |
311 |
self.lb_tables.Set(self.tables) |
312 |
|
313 |
def OnTableSelect(self, event): |
314 |
i = self.lb_tables.GetSelection() |
315 |
# self.text_id_column.Clear() |
316 |
# self.text_geo_column.Clear() |
317 |
# if i >= 0: |
318 |
# for name, typ in self.selected_conn.table_columns(self.tables[i]): |
319 |
# if typ == "geometry": |
320 |
# self.text_geo_column.Append(name) |
321 |
# elif typ == FIELDTYPE_INT: |
322 |
# self.text_id_column.Append(name) |
323 |
|
324 |
def OnLBDClick(self, event): |
325 |
if self.lb_tables.GetSelection() >= 0: |
326 |
self.EndModal(wxID_OK) |
327 |
self.Show(False) |
328 |
|
329 |
def OnOK(self, event): |
330 |
self.EndModal(wxID_OK) |
331 |
self.Show(False) |
332 |
|
333 |
def OnCancel(self, event): |
334 |
self.EndModal(wxID_CANCEL) |
335 |
self.Show(False) |
336 |
|
337 |
|