Skip to content

Commit

Permalink
fix filter: readd QgsLayerTree widget. pep8
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti committed Oct 2, 2019
1 parent c17a624 commit 0d6d842
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 62 deletions.
10 changes: 10 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
def name():
return u"PostgreSQL history viewer"


def description():
return u"History viewer for a PostgreSQL base with audit triggers"


def version():
return u"1.0"


def qgisMinimumVersion():
return u"3.4"


def qgisMaximumVersion():
return u"9.99"


def classFactory(iface):
from .main import Plugin
return Plugin(iface)
30 changes: 29 additions & 1 deletion config.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>629</width>
<height>154</height>
<height>472</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -76,6 +76,26 @@
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Layer</string>
</property>
</widget>
</item>
<item>
<widget class="QgsLayerTreeView" name="treeView"/>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Corresponding database table</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="tableCombo"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
Expand All @@ -88,6 +108,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsLayerTreeView</class>
<extends>QTreeView</extends>
<header>qgis.gui</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
Expand Down
47 changes: 40 additions & 7 deletions config_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,47 @@

from .connection_wrapper import ConnectionWrapper

FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'config.ui'))
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'config.ui'))


class ConfigDialog(QDialog, FORM_CLASS):
def __init__(self, parent, db_connection = "", audit_table = "", table_map = {}, replay_function = None):
def __init__(self, parent, db_connection="", audit_table="", table_map={}, replay_function=None):
"""Constructor.
@param parent parent widget
"""
super(ConfigDialog, self).__init__(parent)
self.setupUi(self)

self.reloadBtn.setIcon(QIcon(os.path.join(os.path.dirname(__file__), 'icons', 'repeat.svg')))
self.reloadBtn.setIcon(
QIcon(os.path.join(os.path.dirname(__file__), 'icons', 'repeat.svg')))

self._table_map = table_map

self.tree_group = QgsProject.instance().layerTreeRoot().clone()
self.tree_model = QgsLayerTreeModel(self.tree_group)
self.treeView.setModel(self.tree_model)

self.treeView.currentLayerChanged.connect(self.onLayerChanged)

# Create database connection wrapper.
# Disabled transaction group.
self.connection_wrapper = ConnectionWrapper()
self.connection_wrapper.disableTransactionGroup(True)

self.reloadBtn.clicked.connect(self.onDatabaseChanged)
self.dbConnectionBtn.clicked.connect(self.onBrowseConnection)
self.tableCombo.currentIndexChanged.connect(self.onTableEdit)

if db_connection:
self.dbConnectionText.setText(db_connection)
self.reloadBtn.click()
if audit_table:
self.auditTableCombo.setCurrentIndex(self.auditTableCombo.findText(audit_table))
self.auditTableCombo.setCurrentIndex(
self.auditTableCombo.findText(audit_table))
if replay_function:
self.replayFunctionCombo.setCurrentIndex(self.replayFunctionCombo.findText(replay_function))
self.replayFunctionCombo.setCurrentIndex(
self.replayFunctionCombo.findText(replay_function))
self.replayFunctionChk.setChecked(True)

self.tables = None
Expand Down Expand Up @@ -84,7 +96,8 @@ def onBrowseConnection(self):
s.beginGroup("/PostgreSQL/connections")
children = s.childGroups()
connections = {}
map = {"dbname":"database", "host":"host", "port":"port", "service":"service", "password":"password", "user":"username", "sslmode": "sslmode"}
map = {"dbname": "database", "host": "host", "port": "port", "service": "service",
"password": "password", "user": "username", "sslmode": "sslmode"}
for g in children:
s.beginGroup(g)
cstring = ""
Expand All @@ -111,7 +124,7 @@ def onMenu(action):
self.reloadBtn.click()

menu.triggered.connect(onMenu)
menu.exec_(self.dbConnectionBtn.mapToGlobal(QPoint(0,0)))
menu.exec_(self.dbConnectionBtn.mapToGlobal(QPoint(0, 0)))

def onDatabaseChanged(self):
dbparams = self.dbConnectionText.text()
Expand All @@ -136,9 +149,13 @@ def onDatabaseChanged(self):

cur.execute(q)

self.tableCombo.clear()
self.tableCombo.addItem("")

for r in cur.fetchall():
t = r[0] + "." + r[1]
self.auditTableCombo.addItem(t)
self.tableCombo.addItem(t)

# populate functions
q = "select routine_schema, routine_name from information_schema.routines where " \
Expand All @@ -152,6 +169,22 @@ def onDatabaseChanged(self):
t = r[0] + "." + r[1]
self.replayFunctionCombo.addItem(t)

def onLayerChanged(self, layer):
if layer is None:
return
table_name = self._table_map.get(layer.id())
if table_name is not None:
idx = self.tableCombo.findText(table_name)
self.tableCombo.setCurrentIndex(idx)
else:
self.tableCombo.setCurrentIndex(0)

def onTableEdit(self, idx):
table_name = self.tableCombo.itemText(idx)
current = self.treeView.currentLayer()
if current is not None:
self._table_map[current.id()] = table_name

def table_map(self):
return self._table_map

Expand Down
10 changes: 7 additions & 3 deletions connection_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
# In some case transaction group cannot be used. To disable transaction
# group use disableTransactionGroup().
# Direct connection allow the use of cursor() for cursor creation.


class ConnectionWrapper():

# Disable transaction group.
Expand Down Expand Up @@ -161,7 +163,7 @@ def isConnected(self):

# QGis.QgsTransactionGroup database connection.
qgisTransactionGroupConnection = None
qgisTransactionGroupDisabled = False
qgisTransactionGroupDisabled = False

def __exit__(self, exc_type, exc_value, traceback):
self.closeConnection()
Expand Down Expand Up @@ -200,7 +202,8 @@ def createConnectionFromTransactionsGroup(self, db_connection):
uriStr = sourceUri.connectionInfo()

try:
print("Getting transactions group for provider ", providerKey, " and database connection: ", uriStr)
print("Getting transactions group for provider ",
providerKey, " and database connection: ", uriStr)
return QgsProject.instance().transactionGroup(providerKey, uriStr)

except:
Expand Down Expand Up @@ -233,7 +236,8 @@ def createSingleConnection(self, db_connection):

# User has validated: get credentials & create single connection again.
else:
db_connection = db_connection + "user='" + self.credDlg.getUserText() + "' password='" + self.credDlg.getPasswordText() + "'"
db_connection = db_connection + "user='" + self.credDlg.getUserText() + \
"' password='" + self.credDlg.getPasswordText() + "'"

return self.createSingleConnection(db_connection)

Expand Down
5 changes: 4 additions & 1 deletion credentials_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QDialog

FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'credentials_dialog.ui'))
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'credentials_dialog.ui'))

# Display a dialog for user credentials input.


class CredentialsDialog(QDialog, FORM_CLASS):
def setErrorText(self, text):
self.errorText.setText(text)
Expand Down
1 change: 1 addition & 0 deletions error_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'error_dialog.ui'))


class ErrorDialog(QDialog, FORM_CLASS):
def __init__(self, parent):
super(ErrorDialog, self).__init__(parent)
Expand Down
Loading

0 comments on commit 0d6d842

Please sign in to comment.