diff --git a/python/plugins/db_manager/dlg_sql_window.py b/python/plugins/db_manager/dlg_sql_window.py index 1145929edca6..e6211b90a59f 100644 --- a/python/plugins/db_manager/dlg_sql_window.py +++ b/python/plugins/db_manager/dlg_sql_window.py @@ -24,6 +24,7 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * +from qgis.core import * from .db_plugins.plugin import BaseError from .dlg_db_error import DlgDbError @@ -61,6 +62,12 @@ def __init__(self, iface, db, parent=None): self.connect(self.btnClear, SIGNAL("clicked()"), self.clearSql) self.connect(self.buttonBox.button(QDialogButtonBox.Close), SIGNAL("clicked()"), self.close) + + self.connect(self.presetStore, SIGNAL("clicked()"), self.storePreset) + self.connect(self.presetDelete, SIGNAL("clicked()"), self.deletePreset) + self.connect(self.presetCombo, SIGNAL("activated(QString)"), self.loadPreset) + self.connect(self.presetCombo, SIGNAL("activated(QString)"), self.presetName.setText) + # hide the load query as layer if feature is not supported self._loadAsLayerAvailable = self.db.connector.hasCustomQuerySupport() self.loadAsLayerGroup.setVisible( self._loadAsLayerAvailable ) @@ -71,6 +78,46 @@ def __init__(self, iface, db, parent=None): self.connect(self.loadAsLayerGroup, SIGNAL("toggled(bool)"), self.loadAsLayerToggled) self.loadAsLayerToggled(False) + def showEvent(self, event): + QDialog.showEvent(self, event) + self.updatePresetsCombobox() + + + + def updatePresetsCombobox(self): + entries = QgsProject.instance().subkeyList('DBManager','savedQueries') + self.presetCombo.clear() + names = [] + for entry in entries: + name = QgsProject.instance().readEntry('DBManager','savedQueries/'+entry+'/name' )[0] + names.append( name ) + + names.sort() + + for name in names: + self.presetCombo.addItem(name) + + def storePreset(self): + query = self.editSql.toPlainText() + name = self.presetName.text() + QgsProject.instance().writeEntry('DBManager','savedQueries/q'+str(name.__hash__())+'/name', name ) + QgsProject.instance().writeEntry('DBManager','savedQueries/q'+str(name.__hash__())+'/query', query ) + index = self.presetCombo.findText(name) + if index == -1: + self.presetCombo.addItem(name) + self.presetCombo.setCurrentIndex(self.presetCombo.count()-1) + else: + self.presetCombo.setCurrentIndex(index) + + def deletePreset(self): + name = self.presetCombo.currentText() + QgsProject.instance().removeEntry('DBManager','savedQueries/q'+str(name.__hash__()) ) + self.presetCombo.removeItem( self.presetCombo.findText(name) ) + self.presetCombo.setCurrentIndex(-1) + def loadPreset(self, name): + query = QgsProject.instance().readEntry('DBManager','savedQueries/q'+str(name.__hash__())+'/query' )[0] + name = QgsProject.instance().readEntry('DBManager','savedQueries/q'+str(name.__hash__())+'/name' )[0] + self.editSql.setText(query) def closeEvent(self, e): """ save window state """ diff --git a/python/plugins/db_manager/ui/DlgSqlWindow.ui b/python/plugins/db_manager/ui/DlgSqlWindow.ui index 81a4798f8317..21b07f741715 100644 --- a/python/plugins/db_manager/ui/DlgSqlWindow.ui +++ b/python/plugins/db_manager/ui/DlgSqlWindow.ui @@ -22,11 +22,48 @@ - - - SQL query: - - + + + + + SQL query: + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + + Store + + + + + + + Delete + + + + @@ -40,8 +77,8 @@ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-family:'Ubuntu'; font-size:11pt;"><br /></span></p></body></html>