eric6/SqlBrowser/SqlBrowser.py

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

eric ide

mercurial