|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + postgis.py - Postgis widget wrappers |
| 6 | + --------------------- |
| 7 | + Date : December 2016 |
| 8 | + Copyright : (C) 2016 by Arnaud Morvan |
| 9 | + Email : arnaud dot morvan at camptocamp dot com |
| 10 | +*************************************************************************** |
| 11 | +* * |
| 12 | +* This program is free software; you can redistribute it and/or modify * |
| 13 | +* it under the terms of the GNU General Public License as published by * |
| 14 | +* the Free Software Foundation; either version 2 of the License, or * |
| 15 | +* (at your option) any later version. * |
| 16 | +* * |
| 17 | +*************************************************************************** |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +from qgis.PyQt.QtCore import QSettings |
| 22 | +from qgis.PyQt.QtWidgets import QComboBox |
| 23 | + |
| 24 | +from processing.core.parameters import ( |
| 25 | + ParameterString, |
| 26 | + ParameterNumber, |
| 27 | + ParameterFile, |
| 28 | + ParameterTableField, |
| 29 | + ParameterExpression |
| 30 | +) |
| 31 | +from processing.core.outputs import OutputString |
| 32 | +from processing.gui.wrappers import ( |
| 33 | + WidgetWrapper, |
| 34 | + ExpressionWidgetWrapperMixin, |
| 35 | + DIALOG_MODELER, |
| 36 | +) |
| 37 | +from processing.tools.postgis import GeoDB |
| 38 | + |
| 39 | + |
| 40 | +class ConnectionWidgetWrapper(WidgetWrapper, ExpressionWidgetWrapperMixin): |
| 41 | + """ |
| 42 | + WidgetWrapper for ParameterString that create and manage a combobox widget |
| 43 | + with existing postgis connections. |
| 44 | + """ |
| 45 | + |
| 46 | + def createWidget(self): |
| 47 | + self._combo = QComboBox() |
| 48 | + for group in self.items(): |
| 49 | + self._combo.addItem(*group) |
| 50 | + self._combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self)) |
| 51 | + return self.wrapWithExpressionButton(self._combo) |
| 52 | + |
| 53 | + def items(self): |
| 54 | + settings = QSettings() |
| 55 | + settings.beginGroup('/PostgreSQL/connections/') |
| 56 | + items = [(group, group) for group in settings.childGroups()] |
| 57 | + |
| 58 | + if self.dialogType == DIALOG_MODELER: |
| 59 | + strings = self.dialog.getAvailableValuesOfType( |
| 60 | + [ParameterString, ParameterNumber, ParameterFile, |
| 61 | + ParameterTableField, ParameterExpression], OutputString) |
| 62 | + items = items + [(self.dialog.resolveValueDescription(s), s) for s in strings] |
| 63 | + |
| 64 | + return items |
| 65 | + |
| 66 | + def setValue(self, value): |
| 67 | + self.setComboValue(value, self._combo) |
| 68 | + |
| 69 | + def value(self): |
| 70 | + return self.comboValue(combobox=self._combo) |
| 71 | + |
| 72 | + |
| 73 | +class SchemaWidgetWrapper(WidgetWrapper, ExpressionWidgetWrapperMixin): |
| 74 | + """ |
| 75 | + WidgetWrapper for ParameterString that create and manage a combobox widget |
| 76 | + with existing schemas from a parent connection parameter. |
| 77 | + """ |
| 78 | + |
| 79 | + def createWidget(self, connection_param=None): |
| 80 | + self._connection_param = connection_param |
| 81 | + self._connection = None |
| 82 | + self._database = None |
| 83 | + |
| 84 | + self._combo = QComboBox() |
| 85 | + self._combo.setEditable(True) |
| 86 | + self.refreshItems() |
| 87 | + self._combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self)) |
| 88 | + self._combo.lineEdit().editingFinished.connect(lambda: self.widgetValueHasChanged.emit(self)) |
| 89 | + |
| 90 | + return self.wrapWithExpressionButton(self._combo) |
| 91 | + |
| 92 | + def postInitialize(self, wrappers): |
| 93 | + for wrapper in wrappers: |
| 94 | + if wrapper.param.name == self._connection_param: |
| 95 | + self.connection_wrapper = wrapper |
| 96 | + self.setConnection(wrapper.value()) |
| 97 | + wrapper.widgetValueHasChanged.connect(self.connectionChanged) |
| 98 | + break |
| 99 | + |
| 100 | + def connectionChanged(self, wrapper): |
| 101 | + connection = wrapper.value() |
| 102 | + if connection == self._connection: |
| 103 | + return |
| 104 | + self.setConnection(connection) |
| 105 | + |
| 106 | + def setConnection(self, connection): |
| 107 | + self._connection = connection |
| 108 | + if isinstance(connection, str): |
| 109 | + self._database = GeoDB.from_name(connection) |
| 110 | + else: |
| 111 | + self._database = None |
| 112 | + self.refreshItems() |
| 113 | + self.widgetValueHasChanged.emit(self) |
| 114 | + |
| 115 | + def refreshItems(self): |
| 116 | + value = self.comboValue(combobox=self._combo) |
| 117 | + |
| 118 | + self._combo.clear() |
| 119 | + |
| 120 | + if self._database is not None: |
| 121 | + for schema in self._database.list_schemas(): |
| 122 | + self._combo.addItem(schema[1], schema[1]) |
| 123 | + |
| 124 | + if self.dialogType == DIALOG_MODELER: |
| 125 | + strings = self.dialog.getAvailableValuesOfType( |
| 126 | + [ParameterString, ParameterNumber, ParameterFile, |
| 127 | + ParameterTableField, ParameterExpression], OutputString) |
| 128 | + for text, data in [(self.dialog.resolveValueDescription(s), s) for s in strings]: |
| 129 | + self._combo.addItem(text, data) |
| 130 | + |
| 131 | + self.setComboValue(value, self._combo) |
| 132 | + |
| 133 | + def setValue(self, value): |
| 134 | + self.setComboValue(value, self._combo) |
| 135 | + self.widgetValueHasChanged.emit(self) |
| 136 | + |
| 137 | + def value(self): |
| 138 | + return self.comboValue(combobox=self._combo) |
| 139 | + |
| 140 | + def database(self): |
| 141 | + return self._database |
| 142 | + |
| 143 | + |
| 144 | +class TableWidgetWrapper(WidgetWrapper, ExpressionWidgetWrapperMixin): |
| 145 | + """ |
| 146 | + WidgetWrapper for ParameterString that create and manage a combobox widget |
| 147 | + with existing tables from a parent schema parameter. |
| 148 | + """ |
| 149 | + |
| 150 | + def createWidget(self, schema_param=None): |
| 151 | + self._schema_param = schema_param |
| 152 | + self._database = None |
| 153 | + self._schema = None |
| 154 | + |
| 155 | + self._combo = QComboBox() |
| 156 | + self._combo.setEditable(True) |
| 157 | + self.refreshItems() |
| 158 | + self._combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self)) |
| 159 | + self._combo.lineEdit().editingFinished.connect(lambda: self.widgetValueHasChanged.emit(self)) |
| 160 | + |
| 161 | + return self.wrapWithExpressionButton(self._combo) |
| 162 | + |
| 163 | + def postInitialize(self, wrappers): |
| 164 | + for wrapper in wrappers: |
| 165 | + if wrapper.param.name == self._schema_param: |
| 166 | + self.schema_wrapper = wrapper |
| 167 | + self.setSchema(wrapper.database(), wrapper.value()) |
| 168 | + wrapper.widgetValueHasChanged.connect(self.schemaChanged) |
| 169 | + break |
| 170 | + |
| 171 | + def schemaChanged(self, wrapper): |
| 172 | + database = wrapper.database() |
| 173 | + schema = wrapper.value() |
| 174 | + if database == self._database and schema == self._schema: |
| 175 | + return |
| 176 | + self.setSchema(database, schema) |
| 177 | + |
| 178 | + def setSchema(self, database, schema): |
| 179 | + self._database = database |
| 180 | + self._schema = schema |
| 181 | + self.refreshItems() |
| 182 | + self.widgetValueHasChanged.emit(self) |
| 183 | + |
| 184 | + def refreshItems(self): |
| 185 | + value = self.comboValue(combobox=self._combo) |
| 186 | + |
| 187 | + self._combo.clear() |
| 188 | + |
| 189 | + if (self._database is not None and isinstance(self._schema, str)): |
| 190 | + for table in self._database.list_geotables(self._schema): |
| 191 | + self._combo.addItem(table[0], table[0]) |
| 192 | + |
| 193 | + if self.dialogType == DIALOG_MODELER: |
| 194 | + strings = self.dialog.getAvailableValuesOfType( |
| 195 | + [ParameterString, ParameterNumber, ParameterFile, |
| 196 | + ParameterTableField, ParameterExpression], OutputString) |
| 197 | + for text, data in [(self.dialog.resolveValueDescription(s), s) for s in strings]: |
| 198 | + self._combo.addItem(text, data) |
| 199 | + |
| 200 | + self.setComboValue(value, self._combo) |
| 201 | + |
| 202 | + def setValue(self, value): |
| 203 | + self.setComboValue(value, self._combo) |
| 204 | + self.widgetValueHasChanged.emit(self) |
| 205 | + |
| 206 | + def value(self): |
| 207 | + return self.comboValue(combobox=self._combo) |
0 commit comments