20 |
|
|
21 |
from Thuban import _ |
from Thuban import _ |
22 |
from Thuban.UI.dialogs import NonModalDialog |
from Thuban.UI.dialogs import NonModalDialog |
23 |
from Thuban.Model.postgisdb import PostGISConnection |
from Thuban.Model.postgisdb import ConnectionError, PostGISConnection |
24 |
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED |
from Thuban.Model.messages import DBCONN_ADDED, DBCONN_REMOVED |
25 |
from messages import SESSION_REPLACED |
from messages import SESSION_REPLACED |
26 |
|
|
|
ID_DBADD_OK = 9001 |
|
|
ID_DBADD_CANCEL = 9002 |
|
27 |
|
|
28 |
ID_DB_ADD = 9101 |
ID_DB_ADD = 9101 |
29 |
ID_DB_REMOVE = 9102 |
ID_DB_REMOVE = 9102 |
36 |
|
|
37 |
class ChooseDBTableDialog(wxDialog): |
class ChooseDBTableDialog(wxDialog): |
38 |
|
|
39 |
def __init__(self, session, *args, **kwds): |
def __init__(self, parent, session): |
40 |
kwds["style"] = wxDIALOG_MODAL|wxCAPTION |
wxDialog.__init__(self, parent, -1, _("Choose layer from database"), |
41 |
wxDialog.__init__(self, *args, **kwds) |
style = wxDIALOG_MODAL|wxCAPTION) |
42 |
self.session = session |
self.session = session |
43 |
self.dbconns = self.session.DBConnections() |
self.dbconns = self.session.DBConnections() |
44 |
self.tables = [] |
self.tables = [] |
45 |
self.list_box_4 = wxListBox(self, -1) |
self.list_box_4 = wxListBox(self, -1) |
46 |
for i in range(len(self.dbconns)): |
for i in range(len(self.dbconns)): |
47 |
self.list_box_4.Append(self.dbconns[i].BriefDescription()) |
self.list_box_4.Append(self.dbconns[i].BriefDescription()) |
48 |
|
if self.list_box_4.GetCount() > 0: |
49 |
|
self.list_box_4.SetSelection(0, True) |
50 |
self.DB_CHOOSE_RETRIEVE = wxButton(self, ID_DBCHOOSE_RETRIEVE, |
self.DB_CHOOSE_RETRIEVE = wxButton(self, ID_DBCHOOSE_RETRIEVE, |
51 |
_("Retrieve")) |
_("Retrieve")) |
52 |
self.list_box_5 = wxListBox(self, ID_LB_DCLICK) |
self.list_box_5 = wxListBox(self, ID_LB_DCLICK) |
53 |
self.DB_CHOOSE_OK = wxButton(self, ID_DBCHOOSE_OK, _("OK")) |
self.DB_CHOOSE_OK = wxButton(self, ID_DBCHOOSE_OK, _("OK")) |
54 |
self.DB_CHOOSE_CANCEL = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel")) |
self.DB_CHOOSE_CANCEL = wxButton(self, ID_DBCHOOSE_CANCEL, _("Cancel")) |
|
self.__set_properties() |
|
55 |
self.__do_layout() |
self.__do_layout() |
56 |
|
|
57 |
EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK) |
EVT_BUTTON(self, ID_DBCHOOSE_OK, self.OnOK) |
59 |
EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve) |
EVT_BUTTON(self, ID_DBCHOOSE_RETRIEVE, self.OnRetrieve) |
60 |
EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick) |
EVT_LISTBOX_DCLICK(self, ID_LB_DCLICK, self.OnLBDClick) |
61 |
|
|
|
|
|
|
def __set_properties(self): |
|
|
self.SetTitle(_("Choose layer from database")) |
|
|
self.list_box_4.SetSelection(0) |
|
|
self.list_box_5.SetSelection(0) |
|
|
|
|
62 |
def __do_layout(self): |
def __do_layout(self): |
63 |
grid_sizer_1 = wxFlexGridSizer(2, 1, 0, 0) |
grid_sizer_1 = wxFlexGridSizer(2, 1, 0, 0) |
64 |
grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0) |
grid_sizer_3 = wxFlexGridSizer(1, 2, 0, 0) |
103 |
def OnLBDClick(self, event): |
def OnLBDClick(self, event): |
104 |
if self.list_box_5.GetSelection() >= 0: |
if self.list_box_5.GetSelection() >= 0: |
105 |
self.EndModal(wxID_OK) |
self.EndModal(wxID_OK) |
106 |
self.Show(false) |
self.Show(False) |
107 |
|
|
108 |
def OnOK(self, event): |
def OnOK(self, event): |
109 |
self.EndModal(wxID_OK) |
self.EndModal(wxID_OK) |
110 |
self.Show(false) |
self.Show(False) |
111 |
|
|
112 |
def OnCancel(self, event): |
def OnCancel(self, event): |
113 |
self.EndModal(wxID_CANCEL) |
self.EndModal(wxID_CANCEL) |
114 |
self.Show(false) |
self.Show(False) |
115 |
|
|
116 |
|
|
117 |
class DBDialog(wxDialog): |
class DBDialog(wxDialog): |
118 |
|
|
119 |
"""Dialog for the parameters of a database connection""" |
"""Dialog for the parameters of a database connection""" |
120 |
|
|
121 |
def __init__(self, selection=None, *args, **kwds): |
def __init__(self, parent, title, parameters, message = ""): |
122 |
kwds["style"] = wxDEFAULT_DIALOG_STYLE |
"""Initialize the dialog box. |
|
wxDialog.__init__(self, *args, **kwds) |
|
|
self.DBAdd_Label_Host = wxStaticText(self, -1, _("Hostname:")) |
|
|
self.DBAdd_Host = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_Port = wxStaticText(self, -1, _("Port:")) |
|
|
self.DBAdd_Port = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_Database = wxStaticText(self, -1, _("Database:")) |
|
|
self.DBAdd_Database = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_User = wxStaticText(self, -1, _("User:")) |
|
|
self.DBAdd_User = wxTextCtrl(self, -1, "") |
|
|
self.DBAdd_Label_Pwd = wxStaticText(self, -1, _("Password:")) |
|
|
self.DBAdd_Pwd = wxTextCtrl(self, -1, "", style=wxTE_PASSWORD) |
|
|
self.DBAdd_OK = wxButton(self, ID_DBADD_OK, _("OK")) |
|
|
self.DBAdd_Cancel = wxButton(self, ID_DBADD_CANCEL, _("Cancel")) |
|
|
if selection != None: |
|
|
self.DBAdd_Host.SetEditable(false) |
|
|
self.DBAdd_Host.SetValue(selection.host) |
|
|
self.DBAdd_Port.SetValue(selection.port) |
|
|
self.DBAdd_Database.SetEditable(false) |
|
|
self.DBAdd_Database.SetValue(selection.dbname()) |
|
|
self.DBAdd_User.SetValue(selection.user) |
|
|
self.__set_properties() |
|
|
self.__do_layout() |
|
|
EVT_BUTTON(self, ID_DBADD_OK, self.OnOK) |
|
|
EVT_BUTTON(self, ID_DBADD_CANCEL, self.OnCancel) |
|
123 |
|
|
124 |
def __set_properties(self): |
The parameters argument should be a dictionary containing known |
125 |
self.SetTitle(_("Add database")) |
connection parameters. |
|
self.SetSize((480, 185)) |
|
|
self.DBAdd_Label_Host.SetSize((60, 16)) |
|
|
self.DBAdd_Host.SetSize((200, 22)) |
|
|
self.DBAdd_Label_Database.SetSize((60, 16)) |
|
|
self.DBAdd_Label_User.SetSize((60, 16)) |
|
126 |
|
|
127 |
def __do_layout(self): |
The optional message parameter will be displayed at the top of |
128 |
grid_sizer_1 = wxFlexGridSizer(4, 1, 0, 0) |
the dialog box and can be used to display error messages when |
129 |
grid_sizer_2 = wxGridSizer(1, 2, 0, 0) |
using the dialog to ask for correct parameters when the |
130 |
grid_sizer_5 = wxFlexGridSizer(1, 5, 0, 0) |
connection can't be established. |
131 |
grid_sizer_6 = wxFlexGridSizer(1, 5, 0, 0) |
""" |
132 |
grid_sizer_3 = wxFlexGridSizer(1, 5, 0, 0) |
wxDialog.__init__(self, parent, -1, title) |
133 |
grid_sizer_3.Add(self.DBAdd_Label_Host, 0, |
|
134 |
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
top = wxBoxSizer(wxVERTICAL) |
|
grid_sizer_3.Add(self.DBAdd_Host, 0, wxALL, 4) |
|
|
grid_sizer_3.Add(20, 20, 0, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_3.Add(self.DBAdd_Label_Port, 0, |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_3.Add(self.DBAdd_Port, 0, wxALL, 4) |
|
|
grid_sizer_1.Add(grid_sizer_3, 1, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_6.Add(self.DBAdd_Label_Database, 0, |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_6.Add(self.DBAdd_Database, 0, wxALL, 4) |
|
|
grid_sizer_6.Add(20, 20, 0, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_1.Add(grid_sizer_6, 1, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_5.Add(self.DBAdd_Label_User, 0, |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_5.Add(self.DBAdd_User, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_5.Add(20, 20, 0, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_5.Add(self.DBAdd_Label_Pwd, 0, |
|
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_5.Add(self.DBAdd_Pwd, 0, wxALL|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_1.Add(grid_sizer_5, 1, wxALL|wxEXPAND, 4) |
|
|
grid_sizer_2.Add(self.DBAdd_OK, 0, |
|
|
wxALL|wxALIGN_RIGHT|wxALIGN_CENTER_VERTICAL, 4) |
|
|
grid_sizer_2.Add(self.DBAdd_Cancel, 0, wxALL|wxALIGN_CENTER_VERTICAL, |
|
|
4) |
|
|
grid_sizer_1.Add(grid_sizer_2, 1, wxALL|wxEXPAND, 4) |
|
|
self.SetAutoLayout(1) |
|
|
self.SetSizer(grid_sizer_1) |
|
|
self.Layout() |
|
135 |
|
|
136 |
|
if message: |
137 |
|
top.Add(wxStaticText(self, -1, message), 0, |
138 |
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
139 |
|
|
140 |
def GetHostname(self): |
box = wxBoxSizer(wxHORIZONTAL) |
141 |
return self.DBAdd_Host.GetValue() |
box.Add(wxStaticText(self, -1, _("Hostname:")), 0, |
142 |
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
143 |
|
self.text_host = wxTextCtrl(self, -1, parameters.get("host", "")) |
144 |
|
box.Add(self.text_host, 2, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
145 |
|
box.Add(wxStaticText(self, -1, _("Port:")), 0, |
146 |
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
147 |
|
self.text_port = wxTextCtrl(self, -1, parameters.get("port", "")) |
148 |
|
box.Add(self.text_port, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
149 |
|
top.Add(box, 0, wxEXPAND) |
150 |
|
|
151 |
|
box = wxBoxSizer(wxHORIZONTAL) |
152 |
|
box.Add(wxStaticText(self, -1, _("Database Name:")), 0, |
153 |
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
154 |
|
self.text_dbname = wxTextCtrl(self, -1, |
155 |
|
parameters.get("dbname", "")) |
156 |
|
box.Add(self.text_dbname, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
157 |
|
top.Add(box, 0, wxEXPAND) |
158 |
|
|
159 |
def GetPort(self): |
box = wxBoxSizer(wxHORIZONTAL) |
160 |
return self.DBAdd_Port.GetValue() |
box.Add(wxStaticText(self, -1, _("User:")), 0, |
161 |
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
162 |
|
self.text_user = wxTextCtrl(self, -1, |
163 |
|
parameters.get("user", "")) |
164 |
|
box.Add(self.text_user, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, 4) |
165 |
|
box.Add(wxStaticText(self, -1, _("Password:")), 0, |
166 |
|
wxALL|wxALIGN_CENTER_VERTICAL, 4) |
167 |
|
self.text_password = wxTextCtrl(self, -1, |
168 |
|
parameters.get("password", ""), |
169 |
|
style = wxTE_PASSWORD) |
170 |
|
box.Add(self.text_password, 1, wxALL|wxALIGN_CENTER_VERTICAL|wxEXPAND, |
171 |
|
4) |
172 |
|
top.Add(box, 0, wxEXPAND) |
173 |
|
|
174 |
def GetDatabase(self): |
buttons = wxBoxSizer(wxHORIZONTAL) |
175 |
return self.DBAdd_Database.GetValue() |
button = wxButton(self, wxID_OK, _("OK")) |
176 |
|
buttons.Add(button, 0, wxALL, 4) |
177 |
|
button = wxButton(self, wxID_CANCEL, _("Cancel")) |
178 |
|
buttons.Add(button, 0, wxALL, 4) |
179 |
|
top.Add(buttons, 0, wxALIGN_RIGHT, 4) |
180 |
|
|
181 |
|
self.SetAutoLayout(1) |
182 |
|
self.SetSizer(top) |
183 |
|
top.Fit(self) |
184 |
|
top.SetSizeHints(self) |
185 |
|
|
186 |
def GetUser(self): |
EVT_BUTTON(self, wxID_OK, self.OnOK) |
187 |
return self.DBAdd_User.GetValue() |
EVT_BUTTON(self, wxID_CANCEL, self.OnCancel) |
188 |
|
|
189 |
def GetPassword(self): |
def RunDialog(self): |
190 |
return self.DBAdd_Pwd.GetValue() |
self.ShowModal() |
191 |
|
self.Destroy() |
192 |
|
return self.result |
193 |
|
|
194 |
|
def end_dialog(self, result): |
195 |
|
self.result = result |
196 |
|
if result is not None: |
197 |
|
self.EndModal(wxID_OK) |
198 |
|
else: |
199 |
|
self.EndModal(wxID_CANCEL) |
200 |
|
self.Show(False) |
201 |
|
|
202 |
def OnOK(self, event): |
def OnOK(self, event): |
203 |
self.EndModal(wxID_OK) |
result = {} |
204 |
self.Show(false) |
for name in ("host", "port", "dbname", "user", "password"): |
205 |
|
result[name] = getattr(self, "text_" + name).GetValue() |
206 |
|
self.end_dialog(result) |
207 |
|
|
208 |
def OnCancel(self, event): |
def OnCancel(self, event): |
209 |
self.EndModal(wxID_CANCEL) |
self.end_dialog(None) |
210 |
self.Show(false) |
|
211 |
|
|
212 |
|
|
213 |
class DBFrame(NonModalDialog): |
class DBFrame(NonModalDialog): |
239 |
def __set_properties(self): |
def __set_properties(self): |
240 |
self.SetTitle(_("Database Management")) |
self.SetTitle(_("Database Management")) |
241 |
self.DB_ListBox.SetSize((200, 157)) |
self.DB_ListBox.SetSize((200, 157)) |
|
self.DB_ListBox.SetSelection(0) |
|
242 |
|
|
243 |
def __do_layout(self): |
def __do_layout(self): |
244 |
top = wxBoxSizer(wxVERTICAL) |
top = wxBoxSizer(wxVERTICAL) |
309 |
return result |
return result |
310 |
|
|
311 |
def OnAdd(self, event): |
def OnAdd(self, event): |
312 |
adddialog = DBDialog(None, self, -1, "") |
message = "" |
313 |
|
parameters = {} |
314 |
while 1: |
while 1: |
315 |
if adddialog.ShowModal() == wxID_OK: |
dialog = DBDialog(self, _("Add Database"), parameters, message) |
316 |
host = adddialog.GetHostname() |
parameters = dialog.RunDialog() |
317 |
port = adddialog.GetPort() |
if parameters is not None: |
318 |
database = adddialog.GetDatabase() |
host = parameters["host"] |
319 |
user = adddialog.GetUser() |
database = parameters["dbname"] |
|
password = adddialog.GetPassword() |
|
320 |
for conn in self.session.DBConnections(): |
for conn in self.session.DBConnections(): |
321 |
if (host == conn.host and |
if (host == conn.host and |
322 |
database == conn.dbname): |
database == conn.dbname): |
323 |
self.RunMessageBox(_("Add Database"), |
self.RunMessageBox(_("Add Database"), |
324 |
_("Connection to '%s' already exists") |
_("Connection to '%s' already exists") |
325 |
% database) |
% database) |
326 |
break |
break |
327 |
try: |
try: |
328 |
conn = PostGISConnection(database, host = host, port=port, |
conn = PostGISConnection(**parameters) |
329 |
user = user, password = password) |
except ConnectionError, val: |
330 |
|
message = str(val) |
331 |
|
else: |
332 |
self.session.AddDBConnection(conn) |
self.session.AddDBConnection(conn) |
333 |
break |
break |
|
except psycopg.OperationalError, strerror: |
|
|
self.RunMessageBox(_("Error"), str(strerror)) |
|
|
except: |
|
|
self.RunMessageBox(_("Add Database"), |
|
|
_("Can't establish connection to '%s'." |
|
|
% database)) |
|
334 |
else: |
else: |
335 |
break |
break |
|
adddialog.Destroy() |
|
336 |
|
|
337 |
def OnRemove(self, event): |
def OnRemove(self, event): |
338 |
i = self.DB_ListBox.GetSelection() |
i = self.DB_ListBox.GetSelection() |