|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the SQL Browser main window. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 from PyQt4.QtSql import QSqlError, QSqlDatabase |
|
13 |
|
14 from E4Gui.E4Action import E4Action |
|
15 |
|
16 from SqlBrowserWidget import SqlBrowserWidget |
|
17 |
|
18 import UI.PixmapCache |
|
19 import UI.Config |
|
20 |
|
21 class SqlBrowser(QMainWindow): |
|
22 """ |
|
23 Class implementing the SQL Browser main window. |
|
24 """ |
|
25 def __init__(self, connections = [], parent = None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param connections list of database connections to add (list of strings) |
|
30 @param reference to the parent widget (QWidget) |
|
31 """ |
|
32 QMainWindow.__init__(self, parent) |
|
33 self.setObjectName("SqlBrowser") |
|
34 |
|
35 self.setWindowTitle(self.trUtf8("SQL Browser")) |
|
36 self.setWindowIcon(UI.PixmapCache.getIcon("eric.png")) |
|
37 |
|
38 self.__browser = SqlBrowserWidget(self) |
|
39 self.setCentralWidget(self.__browser) |
|
40 |
|
41 self.connect(self.__browser, SIGNAL("statusMessage(QString)"), |
|
42 self.statusBar().showMessage) |
|
43 |
|
44 self.__initActions() |
|
45 self.__initMenus() |
|
46 self.__initToolbars() |
|
47 |
|
48 self.resize(self.__browser.size()) |
|
49 |
|
50 self.__warnings = [] |
|
51 |
|
52 for connection in connections: |
|
53 url = QUrl(connection, QUrl.TolerantMode) |
|
54 if not url.isValid(): |
|
55 self.__warnings.append(self.trUtf8("Invalid URL: {0}").format(connection)) |
|
56 continue |
|
57 |
|
58 err = self.__browser.addConnection(url.scheme(), url.path(), |
|
59 url.userName(), url.password(), |
|
60 url.host(), url.port(-1)) |
|
61 if err.type() != QSqlError.NoError: |
|
62 self.__warnings.append( |
|
63 self.trUtf8("Unable to open connection: %s" % err.text())) |
|
64 |
|
65 QTimer.singleShot(0, self.__uiStartUp) |
|
66 |
|
67 def __uiStartUp(self): |
|
68 """ |
|
69 Private slot to do some actions after the UI has started and the main loop is up. |
|
70 """ |
|
71 for warning in self.__warnings: |
|
72 QMessageBox.warning(self, |
|
73 self.trUtf8("SQL Browser startup problem"), |
|
74 warning) |
|
75 |
|
76 if len(QSqlDatabase.connectionNames()) == 0: |
|
77 self.__browser.addConnectionByDialog() |
|
78 |
|
79 def __initActions(self): |
|
80 """ |
|
81 Private method to define the user interface actions. |
|
82 """ |
|
83 # list of all actions |
|
84 self.__actions = [] |
|
85 |
|
86 self.addConnectionAct = E4Action(self.trUtf8('Add Connection'), |
|
87 UI.PixmapCache.getIcon("databaseConnection.png"), |
|
88 self.trUtf8('Add &Connection...'), |
|
89 0, 0, self, 'sql_file_add_connection') |
|
90 self.addConnectionAct.setStatusTip(self.trUtf8( |
|
91 'Open a dialog to add a new database connection')) |
|
92 self.addConnectionAct.setWhatsThis(self.trUtf8( |
|
93 """<b>Add Connection</b>""" |
|
94 """<p>This opens a dialog to add a new database connection.</p>""" |
|
95 )) |
|
96 self.connect(self.addConnectionAct, SIGNAL('triggered()'), |
|
97 self.__browser.addConnectionByDialog) |
|
98 self.__actions.append(self.addConnectionAct) |
|
99 |
|
100 self.exitAct = E4Action(self.trUtf8('Quit'), |
|
101 UI.PixmapCache.getIcon("exit.png"), |
|
102 self.trUtf8('&Quit'), |
|
103 QKeySequence(self.trUtf8("Ctrl+Q","File|Quit")), |
|
104 0, self, 'sql_file_quit') |
|
105 self.exitAct.setStatusTip(self.trUtf8('Quit the SQL browser')) |
|
106 self.exitAct.setWhatsThis(self.trUtf8( |
|
107 """<b>Quit</b>""" |
|
108 """<p>Quit the SQL browser.</p>""" |
|
109 )) |
|
110 self.connect(self.exitAct, SIGNAL('triggered()'), |
|
111 qApp, SLOT('closeAllWindows()')) |
|
112 |
|
113 self.aboutAct = E4Action(self.trUtf8('About'), |
|
114 self.trUtf8('&About'), |
|
115 0, 0, self, 'sql_help_about') |
|
116 self.aboutAct.setStatusTip(self.trUtf8('Display information about this software')) |
|
117 self.aboutAct.setWhatsThis(self.trUtf8( |
|
118 """<b>About</b>""" |
|
119 """<p>Display some information about this software.</p>""" |
|
120 )) |
|
121 self.connect(self.aboutAct, SIGNAL('triggered()'), self.__about) |
|
122 self.__actions.append(self.aboutAct) |
|
123 |
|
124 self.aboutQtAct = E4Action(self.trUtf8('About Qt'), |
|
125 self.trUtf8('About &Qt'), |
|
126 0, 0, self, 'sql_help_about_qt') |
|
127 self.aboutQtAct.setStatusTip(\ |
|
128 self.trUtf8('Display information about the Qt toolkit')) |
|
129 self.aboutQtAct.setWhatsThis(self.trUtf8( |
|
130 """<b>About Qt</b>""" |
|
131 """<p>Display some information about the Qt toolkit.</p>""" |
|
132 )) |
|
133 self.connect(self.aboutQtAct, SIGNAL('triggered()'), self.__aboutQt) |
|
134 self.__actions.append(self.aboutQtAct) |
|
135 |
|
136 def __initMenus(self): |
|
137 """ |
|
138 Private method to create the menus. |
|
139 """ |
|
140 mb = self.menuBar() |
|
141 |
|
142 menu = mb.addMenu(self.trUtf8('&File')) |
|
143 menu.setTearOffEnabled(True) |
|
144 menu.addAction(self.addConnectionAct) |
|
145 menu.addSeparator() |
|
146 menu.addAction(self.exitAct) |
|
147 |
|
148 mb.addSeparator() |
|
149 |
|
150 menu = mb.addMenu(self.trUtf8('&Help')) |
|
151 menu.setTearOffEnabled(True) |
|
152 menu.addAction(self.aboutAct) |
|
153 menu.addAction(self.aboutQtAct) |
|
154 |
|
155 def __initToolbars(self): |
|
156 """ |
|
157 Private method to create the toolbars. |
|
158 """ |
|
159 filetb = self.addToolBar(self.trUtf8("File")) |
|
160 filetb.setObjectName("FileToolBar") |
|
161 filetb.setIconSize(UI.Config.ToolBarIconSize) |
|
162 filetb.addAction(self.addConnectionAct) |
|
163 filetb.addSeparator() |
|
164 filetb.addAction(self.exitAct) |
|
165 |
|
166 def __about(self): |
|
167 """ |
|
168 Private slot to show the about information. |
|
169 """ |
|
170 QMessageBox.about(self, self.trUtf8("SQL Browser"), self.trUtf8( |
|
171 """<h3>About SQL Browser</h3>""" |
|
172 """<p>The SQL browser window is a little tool to examine """ |
|
173 """the data and the schema of a database and to execute """ |
|
174 """queries on a database.</p>""" |
|
175 )) |
|
176 |
|
177 def __aboutQt(self): |
|
178 """ |
|
179 Private slot to show info about Qt. |
|
180 """ |
|
181 QMessageBox.aboutQt(self, self.trUtf8("SQL Browser")) |