-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
wrappers_postgis.py
172 lines (136 loc) · 6.44 KB
/
wrappers_postgis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
"""
***************************************************************************
postgis.py - Postgis widget wrappers
---------------------
Date : December 2016
Copyright : (C) 2016 by Arnaud Morvan
Email : arnaud dot morvan at camptocamp dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""
from qgis.core import (QgsProcessingParameterNumber,
QgsProcessingParameterFile,
QgsProcessingParameterField,
QgsProcessingParameterExpression,
QgsProcessingOutputString,
QgsProcessingParameterString)
from qgis.PyQt.QtWidgets import QComboBox
from processing.gui.wrappers import (
WidgetWrapper,
DIALOG_MODELER,
)
from processing.tools.postgis import GeoDB
class SchemaWidgetWrapper(WidgetWrapper):
"""
WidgetWrapper for ParameterString that create and manage a combobox widget
with existing schemas from a parent connection parameter.
"""
def createWidget(self, connection_param=None):
self._connection_param = connection_param
self._connection = None
self._database = None
self._combo = QComboBox()
self._combo.setEditable(True)
self.refreshItems()
self._combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
self._combo.lineEdit().editingFinished.connect(lambda: self.widgetValueHasChanged.emit(self))
return self._combo
def postInitialize(self, wrappers):
for wrapper in wrappers:
if wrapper.parameterDefinition().name() == self._connection_param:
self.connection_wrapper = wrapper
self.setConnection(wrapper.parameterValue())
wrapper.widgetValueHasChanged.connect(self.connectionChanged)
break
def connectionChanged(self, wrapper):
connection = wrapper.parameterValue()
if connection == self._connection:
return
self.setConnection(connection)
def setConnection(self, connection):
self._connection = connection
# when there is NO connection (yet), this gets called with a ''-connection
if isinstance(connection, str) and connection != '':
self._database = GeoDB.from_name(connection)
else:
self._database = None
self.refreshItems()
self.widgetValueHasChanged.emit(self)
def refreshItems(self):
value = self.comboValue(combobox=self._combo)
self._combo.clear()
if self._database is not None:
for schema in self._database.list_schemas():
self._combo.addItem(schema[1], schema[1])
if self.dialogType == DIALOG_MODELER:
strings = self.dialog.getAvailableValuesOfType(
[QgsProcessingParameterString, QgsProcessingParameterNumber, QgsProcessingParameterFile,
QgsProcessingParameterField, QgsProcessingParameterExpression], QgsProcessingOutputString)
for text, data in [(self.dialog.resolveValueDescription(s), s) for s in strings]:
self._combo.addItem(text, data)
self.setComboValue(value, self._combo)
def setValue(self, value):
self.setComboValue(value, self._combo)
self.widgetValueHasChanged.emit(self)
def value(self):
return self.comboValue(combobox=self._combo)
def database(self):
return self._database
class TableWidgetWrapper(WidgetWrapper):
"""
WidgetWrapper for ParameterString that create and manage a combobox widget
with existing tables from a parent schema parameter.
"""
def createWidget(self, schema_param=None):
self._schema_param = schema_param
self._database = None
self._schema = None
self._combo = QComboBox()
self._combo.setEditable(True)
self.refreshItems()
self._combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
self._combo.lineEdit().editingFinished.connect(lambda: self.widgetValueHasChanged.emit(self))
return self._combo
def postInitialize(self, wrappers):
for wrapper in wrappers:
if wrapper.parameterDefinition().name() == self._schema_param:
self.schema_wrapper = wrapper
self.setSchema(wrapper.database(), wrapper.parameterValue())
wrapper.widgetValueHasChanged.connect(self.schemaChanged)
break
def schemaChanged(self, wrapper):
database = wrapper.database()
schema = wrapper.parameterValue()
if database == self._database and schema == self._schema:
return
self.setSchema(database, schema)
def setSchema(self, database, schema):
self._database = database
self._schema = schema
self.refreshItems()
self.widgetValueHasChanged.emit(self)
def refreshItems(self):
value = self.comboValue(combobox=self._combo)
self._combo.clear()
if (self._database is not None and isinstance(self._schema, str)):
for table in self._database.list_geotables(self._schema):
self._combo.addItem(table[0], table[0])
if self.dialogType == DIALOG_MODELER:
strings = self.dialog.getAvailableValuesOfType(
[QgsProcessingParameterString, QgsProcessingParameterNumber, QgsProcessingParameterFile,
QgsProcessingParameterField, QgsProcessingParameterExpression], QgsProcessingOutputString)
for text, data in [(self.dialog.resolveValueDescription(s), s) for s in strings]:
self._combo.addItem(text, data)
self.setComboValue(value, self._combo)
def setValue(self, value):
self.setComboValue(value, self._combo)
self.widgetValueHasChanged.emit(self)
def value(self):
return self.comboValue(combobox=self._combo)