8 """ |
8 """ |
9 |
9 |
10 from PyQt6.QtCore import pyqtSignal, Qt |
10 from PyQt6.QtCore import pyqtSignal, Qt |
11 from PyQt6.QtGui import QAction |
11 from PyQt6.QtGui import QAction |
12 from PyQt6.QtWidgets import ( |
12 from PyQt6.QtWidgets import ( |
13 QWidget, QHeaderView, QTreeWidget, QVBoxLayout, QTreeWidgetItem |
13 QWidget, |
|
14 QHeaderView, |
|
15 QTreeWidget, |
|
16 QVBoxLayout, |
|
17 QTreeWidgetItem, |
14 ) |
18 ) |
15 from PyQt6.QtSql import QSqlDatabase |
19 from PyQt6.QtSql import QSqlDatabase |
16 |
20 |
17 |
21 |
18 class SqlConnectionWidget(QWidget): |
22 class SqlConnectionWidget(QWidget): |
19 """ |
23 """ |
20 Class implementing a widget showing the SQL connections. |
24 Class implementing a widget showing the SQL connections. |
21 |
25 |
22 @signal tableActivated(str) emitted after the entry for a table has been |
26 @signal tableActivated(str) emitted after the entry for a table has been |
23 activated |
27 activated |
24 @signal schemaRequested(str) emitted when the schema display is requested |
28 @signal schemaRequested(str) emitted when the schema display is requested |
25 @signal cleared() emitted after the connection tree has been cleared |
29 @signal cleared() emitted after the connection tree has been cleared |
26 """ |
30 """ |
|
31 |
27 tableActivated = pyqtSignal(str) |
32 tableActivated = pyqtSignal(str) |
28 schemaRequested = pyqtSignal(str) |
33 schemaRequested = pyqtSignal(str) |
29 cleared = pyqtSignal() |
34 cleared = pyqtSignal() |
30 |
35 |
31 def __init__(self, parent=None): |
36 def __init__(self, parent=None): |
32 """ |
37 """ |
33 Constructor |
38 Constructor |
34 |
39 |
35 @param parent reference to the parent widget (QWidget) |
40 @param parent reference to the parent widget (QWidget) |
36 """ |
41 """ |
37 super().__init__(parent) |
42 super().__init__(parent) |
38 |
43 |
39 layout = QVBoxLayout(self) |
44 layout = QVBoxLayout(self) |
40 layout.setContentsMargins(0, 0, 0, 0) |
45 layout.setContentsMargins(0, 0, 0, 0) |
41 |
46 |
42 self.__connectionTree = QTreeWidget(self) |
47 self.__connectionTree = QTreeWidget(self) |
43 self.__connectionTree.setObjectName("connectionTree") |
48 self.__connectionTree.setObjectName("connectionTree") |
44 self.__connectionTree.setHeaderLabels([self.tr("Database")]) |
49 self.__connectionTree.setHeaderLabels([self.tr("Database")]) |
45 self.__connectionTree.header().setSectionResizeMode( |
50 self.__connectionTree.header().setSectionResizeMode( |
46 QHeaderView.ResizeMode.Stretch) |
51 QHeaderView.ResizeMode.Stretch |
|
52 ) |
47 refreshAction = QAction(self.tr("Refresh"), self.__connectionTree) |
53 refreshAction = QAction(self.tr("Refresh"), self.__connectionTree) |
48 self.__schemaAction = QAction( |
54 self.__schemaAction = QAction(self.tr("Show Schema"), self.__connectionTree) |
49 self.tr("Show Schema"), self.__connectionTree) |
55 |
50 |
|
51 refreshAction.triggered.connect(self.refresh) |
56 refreshAction.triggered.connect(self.refresh) |
52 self.__schemaAction.triggered.connect(self.showSchema) |
57 self.__schemaAction.triggered.connect(self.showSchema) |
53 |
58 |
54 self.__connectionTree.addAction(refreshAction) |
59 self.__connectionTree.addAction(refreshAction) |
55 self.__connectionTree.addAction(self.__schemaAction) |
60 self.__connectionTree.addAction(self.__schemaAction) |
56 self.__connectionTree.setContextMenuPolicy( |
61 self.__connectionTree.setContextMenuPolicy( |
57 Qt.ContextMenuPolicy.ActionsContextMenu) |
62 Qt.ContextMenuPolicy.ActionsContextMenu |
58 |
63 ) |
|
64 |
59 layout.addWidget(self.__connectionTree) |
65 layout.addWidget(self.__connectionTree) |
60 |
66 |
61 self.__activating = False |
67 self.__activating = False |
62 |
68 |
63 self.__connectionTree.itemActivated.connect(self.__itemActivated) |
69 self.__connectionTree.itemActivated.connect(self.__itemActivated) |
64 self.__connectionTree.currentItemChanged.connect( |
70 self.__connectionTree.currentItemChanged.connect(self.__currentItemChanged) |
65 self.__currentItemChanged) |
71 |
66 |
|
67 self.__activeDb = "" |
72 self.__activeDb = "" |
68 |
73 |
69 def refresh(self): |
74 def refresh(self): |
70 """ |
75 """ |
71 Public slot to refresh the connection tree. |
76 Public slot to refresh the connection tree. |
72 """ |
77 """ |
73 self.__connectionTree.clear() |
78 self.__connectionTree.clear() |
74 self.cleared.emit() |
79 self.cleared.emit() |
75 |
80 |
76 connectionNames = QSqlDatabase.connectionNames() |
81 connectionNames = QSqlDatabase.connectionNames() |
77 |
82 |
78 foundActiveDb = False |
83 foundActiveDb = False |
79 for name in connectionNames: |
84 for name in connectionNames: |
80 root = QTreeWidgetItem(self.__connectionTree) |
85 root = QTreeWidgetItem(self.__connectionTree) |
81 db = QSqlDatabase.database(name, False) |
86 db = QSqlDatabase.database(name, False) |
82 root.setText(0, self.__dbCaption(db)) |
87 root.setText(0, self.__dbCaption(db)) |
86 if db.isOpen(): |
91 if db.isOpen(): |
87 tables = db.tables() |
92 tables = db.tables() |
88 for table in tables: |
93 for table in tables: |
89 itm = QTreeWidgetItem(root) |
94 itm = QTreeWidgetItem(root) |
90 itm.setText(0, table) |
95 itm.setText(0, table) |
91 |
96 |
92 if not foundActiveDb and connectionNames: |
97 if not foundActiveDb and connectionNames: |
93 self.__activeDb = connectionNames[0] |
98 self.__activeDb = connectionNames[0] |
94 self.__setActive(self.__connectionTree.topLevelItem(0)) |
99 self.__setActive(self.__connectionTree.topLevelItem(0)) |
95 |
100 |
96 def showSchema(self): |
101 def showSchema(self): |
97 """ |
102 """ |
98 Public slot to show schema data of a database. |
103 Public slot to show schema data of a database. |
99 """ |
104 """ |
100 cItm = self.__connectionTree.currentItem() |
105 cItm = self.__connectionTree.currentItem() |
101 if cItm is None or cItm.parent() is None: |
106 if cItm is None or cItm.parent() is None: |
102 return |
107 return |
103 self.__setActive(cItm.parent()) |
108 self.__setActive(cItm.parent()) |
104 self.schemaRequested.emit(cItm.text(0)) |
109 self.schemaRequested.emit(cItm.text(0)) |
105 |
110 |
106 def __itemActivated(self, itm, column): |
111 def __itemActivated(self, itm, column): |
107 """ |
112 """ |
108 Private slot handling the activation of an item. |
113 Private slot handling the activation of an item. |
109 |
114 |
110 @param itm reference to the item (QTreeWidgetItem) |
115 @param itm reference to the item (QTreeWidgetItem) |
111 @param column column that was activated (integer) |
116 @param column column that was activated (integer) |
112 """ |
117 """ |
113 if itm is None: |
118 if itm is None: |
114 return |
119 return |
115 |
120 |
116 if not self.__activating: |
121 if not self.__activating: |
117 self.__activating = True |
122 self.__activating = True |
118 if itm.parent() is None: |
123 if itm.parent() is None: |
119 self.__setActive(itm) |
124 self.__setActive(itm) |
120 else: |
125 else: |
121 self.__setActive(itm.parent()) |
126 self.__setActive(itm.parent()) |
122 self.tableActivated.emit(itm.text(0)) |
127 self.tableActivated.emit(itm.text(0)) |
123 self.__activating = False |
128 self.__activating = False |
124 |
129 |
125 def __currentItemChanged(self, current, previous): |
130 def __currentItemChanged(self, current, previous): |
126 """ |
131 """ |
127 Private slot handling a change of the current item. |
132 Private slot handling a change of the current item. |
128 |
133 |
129 @param current reference to the new current item (QTreeWidgetItem) |
134 @param current reference to the new current item (QTreeWidgetItem) |
130 @param previous reference to the previous current item |
135 @param previous reference to the previous current item |
131 (QTreeWidgetItem) |
136 (QTreeWidgetItem) |
132 """ |
137 """ |
133 self.__schemaAction.setEnabled( |
138 self.__schemaAction.setEnabled( |
134 current is not None and current.parent() is not None) |
139 current is not None and current.parent() is not None |
135 |
140 ) |
|
141 |
136 def __dbCaption(self, db): |
142 def __dbCaption(self, db): |
137 """ |
143 """ |
138 Private method to assemble a string for the caption. |
144 Private method to assemble a string for the caption. |
139 |
145 |
140 @param db reference to the database object (QSqlDatabase) |
146 @param db reference to the database object (QSqlDatabase) |
141 @return caption string (string) |
147 @return caption string (string) |
142 """ |
148 """ |
143 nm = db.driverName() |
149 nm = db.driverName() |
144 nm += ":" |
150 nm += ":" |
145 if db.userName(): |
151 if db.userName(): |
146 nm += db.userName() |
152 nm += db.userName() |
147 nm += "@" |
153 nm += "@" |
148 nm += db.databaseName() |
154 nm += db.databaseName() |
149 return nm |
155 return nm |
150 |
156 |
151 def __setBold(self, itm, bold): |
157 def __setBold(self, itm, bold): |
152 """ |
158 """ |
153 Private slot to set the font to bold. |
159 Private slot to set the font to bold. |
154 |
160 |
155 @param itm reference to the item to be changed (QTreeWidgetItem) |
161 @param itm reference to the item to be changed (QTreeWidgetItem) |
156 @param bold flag indicating bold (boolean) |
162 @param bold flag indicating bold (boolean) |
157 """ |
163 """ |
158 font = itm.font(0) |
164 font = itm.font(0) |
159 font.setBold(bold) |
165 font.setBold(bold) |
160 itm.setFont(0, font) |
166 itm.setFont(0, font) |
161 |
167 |
162 def currentDatabase(self): |
168 def currentDatabase(self): |
163 """ |
169 """ |
164 Public method to get the current database. |
170 Public method to get the current database. |
165 |
171 |
166 @return reference to the current database (QSqlDatabase) |
172 @return reference to the current database (QSqlDatabase) |
167 """ |
173 """ |
168 return QSqlDatabase.database(self.__activeDb) |
174 return QSqlDatabase.database(self.__activeDb) |
169 |
175 |
170 def __setActive(self, itm): |
176 def __setActive(self, itm): |
171 """ |
177 """ |
172 Private slot to set an item to active. |
178 Private slot to set an item to active. |
173 |
179 |
174 @param itm reference to the item to set as the active item |
180 @param itm reference to the item to set as the active item |
175 (QTreeWidgetItem) |
181 (QTreeWidgetItem) |
176 """ |
182 """ |
177 for index in range(self.__connectionTree.topLevelItemCount()): |
183 for index in range(self.__connectionTree.topLevelItemCount()): |
178 if self.__connectionTree.topLevelItem(index).font(0).bold(): |
184 if self.__connectionTree.topLevelItem(index).font(0).bold(): |
179 self.__setBold( |
185 self.__setBold(self.__connectionTree.topLevelItem(index), False) |
180 self.__connectionTree.topLevelItem(index), False) |
186 |
181 |
|
182 if itm is None: |
187 if itm is None: |
183 return |
188 return |
184 |
189 |
185 self.__setBold(itm, True) |
190 self.__setBold(itm, True) |
186 self.__activeDb = QSqlDatabase.connectionNames()[ |
191 self.__activeDb = QSqlDatabase.connectionNames()[ |
187 self.__connectionTree.indexOfTopLevelItem(itm)] |
192 self.__connectionTree.indexOfTopLevelItem(itm) |
|
193 ] |