|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter the connection parameters. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 from PyQt4.QtSql import QSqlDatabase |
|
13 |
|
14 from E4Gui.E4Completers import E4FileCompleter |
|
15 |
|
16 from Ui_SqlConnectionDialog import Ui_SqlConnectionDialog |
|
17 |
|
18 import Utilities |
|
19 |
|
20 class SqlConnectionDialog(QDialog, Ui_SqlConnectionDialog): |
|
21 """ |
|
22 Class implementing a dialog to enter the connection parameters. |
|
23 """ |
|
24 def __init__(self, parent = None): |
|
25 """ |
|
26 Constructor |
|
27 """ |
|
28 QDialog.__init__(self, parent) |
|
29 self.setupUi(self) |
|
30 |
|
31 self.databaseFileCompleter = E4FileCompleter() |
|
32 |
|
33 self.okButton = self.buttonBox.button(QDialogButtonBox.Ok) |
|
34 |
|
35 drivers = QSqlDatabase.drivers() |
|
36 |
|
37 # remove compatibility names |
|
38 if "QMYSQL3" in drivers: |
|
39 drivers.remove("QMYSQL3") |
|
40 if "QOCI8" in drivers: |
|
41 drivers.remove("QOCI8") |
|
42 if "QODBC3" in drivers: |
|
43 drivers.remove("QODBC3") |
|
44 if "QPSQL7" in drivers: |
|
45 drivers.remove("QPSQL7") |
|
46 if "QTDS7" in drivers: |
|
47 drivers.remove("QTDS7") |
|
48 |
|
49 self.driverCombo.addItems(drivers) |
|
50 |
|
51 self.__updateDialog() |
|
52 |
|
53 def __updateDialog(self): |
|
54 """ |
|
55 Private slot to update the dialog depending on it's contents. |
|
56 """ |
|
57 driver = self.driverCombo.currentText() |
|
58 if driver.startswith("QSQLITE"): |
|
59 self.databaseEdit.setCompleter(self.databaseFileCompleter) |
|
60 self.databaseFileButton.setEnabled(True) |
|
61 else: |
|
62 self.databaseEdit.setCompleter(None) |
|
63 self.databaseFileButton.setEnabled(False) |
|
64 |
|
65 if self.databaseEdit.text() == "" or driver == "": |
|
66 self.okButton.setEnabled(False) |
|
67 else: |
|
68 self.okButton.setEnabled(True) |
|
69 |
|
70 @pyqtSlot(str) |
|
71 def on_driverCombo_activated(self, txt): |
|
72 """ |
|
73 Private slot handling the selection of a database driver. |
|
74 """ |
|
75 self.__updateDialog() |
|
76 |
|
77 @pyqtSlot(str) |
|
78 def on_databaseEdit_textChanged(self, p0): |
|
79 """ |
|
80 Private slot handling the change of the database name. |
|
81 """ |
|
82 self.__updateDialog() |
|
83 |
|
84 @pyqtSlot() |
|
85 def on_databaseFileButton_clicked(self): |
|
86 """ |
|
87 Private slot to open a database file via a file selection dialog. |
|
88 """ |
|
89 startdir = self.databaseEdit.text() |
|
90 dbFile = QFileDialog.getOpenFileName( |
|
91 self, |
|
92 self.trUtf8("Select Database File"), |
|
93 startdir, |
|
94 self.trUtf8("All Files (*)"), |
|
95 None) |
|
96 |
|
97 if dbFile: |
|
98 self.databaseEdit.setText(Utilities.toNativeSeparators(dbFile)) |
|
99 |
|
100 def getData(self): |
|
101 """ |
|
102 Public method to retrieve the connection data. |
|
103 |
|
104 @return tuple giving the driver name (QString), the database name (QString), |
|
105 the user name (QString), the password (QString), the host name (QString) |
|
106 and the port (integer) |
|
107 """ |
|
108 return ( |
|
109 self.driverCombo.currentText(), |
|
110 self.databaseEdit.text(), |
|
111 self.usernameEdit.text(), |
|
112 self.passwordEdit.text(), |
|
113 self.hostnameEdit.text(), |
|
114 self.portSpinBox.value(), |
|
115 ) |