|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter the connection parameters. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot |
|
11 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
12 from PyQt5.QtSql import QSqlDatabase |
|
13 |
|
14 from E5Gui.E5PathPicker import E5PathPickerModes |
|
15 |
|
16 from .Ui_SqlConnectionDialog import Ui_SqlConnectionDialog |
|
17 |
|
18 |
|
19 class SqlConnectionDialog(QDialog, Ui_SqlConnectionDialog): |
|
20 """ |
|
21 Class implementing a dialog to enter the connection parameters. |
|
22 """ |
|
23 def __init__(self, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param parent reference to the parent widget (QWidget) |
|
28 """ |
|
29 super().__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 self.databasePicker.setMode(E5PathPickerModes.OpenFileMode) |
|
33 |
|
34 self.okButton = self.buttonBox.button( |
|
35 QDialogButtonBox.StandardButton.Ok) |
|
36 |
|
37 drivers = QSqlDatabase.drivers() |
|
38 |
|
39 # remove compatibility names |
|
40 if "QMYSQL3" in drivers: |
|
41 drivers.remove("QMYSQL3") |
|
42 if "QOCI8" in drivers: |
|
43 drivers.remove("QOCI8") |
|
44 if "QODBC3" in drivers: |
|
45 drivers.remove("QODBC3") |
|
46 if "QPSQL7" in drivers: |
|
47 drivers.remove("QPSQL7") |
|
48 if "QTDS7" in drivers: |
|
49 drivers.remove("QTDS7") |
|
50 |
|
51 self.driverCombo.addItems(drivers) |
|
52 |
|
53 self.__updateDialog() |
|
54 |
|
55 msh = self.minimumSizeHint() |
|
56 self.resize(max(self.width(), msh.width()), msh.height()) |
|
57 |
|
58 def __updateDialog(self): |
|
59 """ |
|
60 Private slot to update the dialog depending on its contents. |
|
61 """ |
|
62 driver = self.driverCombo.currentText() |
|
63 if driver.startswith("QSQLITE"): |
|
64 self.databasePicker.setPickerEnabled(True) |
|
65 else: |
|
66 self.databasePicker.setPickerEnabled(False) |
|
67 |
|
68 if self.databasePicker.text() == "" or driver == "": |
|
69 self.okButton.setEnabled(False) |
|
70 else: |
|
71 self.okButton.setEnabled(True) |
|
72 |
|
73 @pyqtSlot(int) |
|
74 def on_driverCombo_activated(self, index): |
|
75 """ |
|
76 Private slot handling the selection of a database driver. |
|
77 |
|
78 @param index index of the selected entry |
|
79 @type int |
|
80 """ |
|
81 self.__updateDialog() |
|
82 |
|
83 @pyqtSlot(str) |
|
84 def on_databasePicker_textChanged(self, txt): |
|
85 """ |
|
86 Private slot handling the change of the database name. |
|
87 |
|
88 @param txt text of the edit (string) |
|
89 """ |
|
90 self.__updateDialog() |
|
91 |
|
92 def getData(self): |
|
93 """ |
|
94 Public method to retrieve the connection data. |
|
95 |
|
96 @return tuple giving the driver name (string), the database name |
|
97 (string), the user name (string), the password (string), the |
|
98 host name (string) and the port (integer) |
|
99 """ |
|
100 return ( |
|
101 self.driverCombo.currentText(), |
|
102 self.databasePicker.text(), |
|
103 self.usernameEdit.text(), |
|
104 self.passwordEdit.text(), |
|
105 self.hostnameEdit.text(), |
|
106 self.portSpinBox.value(), |
|
107 ) |