Skip to content

Commit

Permalink
Merge pull request #555 from olivierdalang/SaveSqlQueriesInDbManager
Browse files Browse the repository at this point in the history
[FEATURE] DBManager: allow to store SQL queries in the project file
  • Loading branch information
brushtyler committed May 22, 2013
2 parents 37a30bb + 6bd1b78 commit 64ab894
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 7 deletions.
47 changes: 47 additions & 0 deletions python/plugins/db_manager/dlg_sql_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )
Expand All @@ -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 """
Expand Down
51 changes: 44 additions & 7 deletions python/plugins/db_manager/ui/DlgSqlWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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>
Expand Down

0 comments on commit 64ab894

Please sign in to comment.