SqlBrowser/SqlConnectionWidget.py

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

eric ide

mercurial