Skip to content
Permalink
Browse files

Merge pull request #555 from olivierdalang/SaveSqlQueriesInDbManager

[FEATURE] DBManager: allow to store SQL queries in the project file
  • Loading branch information
brushtyler committed May 22, 2013
2 parents 37a30bb + 6bd1b78 commit 64ab894630bec06a3f8a8c8a9e198531c8c517fa
Showing with 91 additions and 7 deletions.
  1. +47 −0 python/plugins/db_manager/dlg_sql_window.py
  2. +44 −7 python/plugins/db_manager/ui/DlgSqlWindow.ui
@@ -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 """
@@ -22,11 +22,48 @@
<widget class="QWidget" name="layoutWidget">
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>SQL query:</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>SQL query:</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLineEdit" name="presetName"/>
</item>
<item>
<widget class="QComboBox" name="presetCombo"/>
</item>
<item>
<widget class="QPushButton" name="presetStore">
<property name="text">
<string>Store</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="presetDelete">
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="CompletionTextEdit" name="editSql">
@@ -40,8 +77,8 @@
<string notr="true">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Ubuntu'; font-size:11pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-family:'Ubuntu'; font-size:11pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>

0 comments on commit 64ab894

Please sign in to comment.
You can’t perform that action at this time.