-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdddialog.py
More file actions
273 lines (227 loc) · 9.79 KB
/
dddialog.py
File metadata and controls
273 lines (227 loc) · 9.79 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding: utf-8 -*-
"""
dddialog
-----------------------------------
"""
"""
/***************************************************************************
DataDrivenDialog
A QGIS plugin
Applies a data-driven input mask to any PostGIS-Layer
-------------------
begin : 2012-06-21
copyright : (C) 2012 by Bernhard Ströbl / Kommunale Immobilien Jena
email : bernhard.stroebl@jena.de
***************************************************************************/
/***************************************************************************
* *
* 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 builtins import str
from qgis.PyQt import QtGui, QtCore, QtWidgets
from qgis.core import *
from qgis.gui import *
import xml.etree.ElementTree as ET
import os
# create the dialog
class DdDialog(QtWidgets.QDialog):
'''Each mask is a DdDialog instance, thus a child of QDialog'''
def __init__(self, ddManager, ui, layer, feature, db, multiEdit = False,
parent = None, title = None):
super().__init__(parent)
# Set up the user interface from Designer.
self.ddManager = ddManager
self.ui = ui
self.layer = layer
self.feature = feature
self.db = db
self.forwardReturn = True # use return pressed to accept self
if multiEdit:
self.mode = 2
else:
self.mode = 0
self.forEdit = self.layer.isEditable()
self.ui.setupUi(self, self.db)
okBtn = self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Ok)
okBtn.setEnabled(self.forEdit)
okBtn.setVisible(self.forEdit)
self.setTitle(title)
self.initialize()
def debug(self, str):
QgsMessageLog.logMessage(str)
def setTitle(self, title = None):
if title == None:
title = self.layer.name()
title += " - "
if self.mode == 2:
title += QtWidgets.QApplication.translate("DdInfo", "%s selected features") % str(self.layer.selectedFeatureCount())
else:
if self.feature.id() < 0:
title += QtWidgets.QApplication.translate("DdInfo", "New Feature")
else:
title += QtWidgets.QApplication.translate("DdInfo", "Feature " ) + str(self.feature.id())
self.setWindowTitle(title)
def initialize(self):
self.ui.initialize(self.layer, self.feature, self.db, self.mode)
def setForwardReturn(self, doForward = True):
self.forwardReturn = doForward
def accept(self):
if self.forwardReturn:
if self.ui.checkInput(self.layer, self.feature):
hasChanges = self.ui.save(self.layer, self.feature, self.db)
if hasChanges:
self.done(1)
else:
self.done(2)
def reject(self):
self.ui.discard()
self.done(0)
def helpRequested(self):
dlg = QtWidgets.QDialog(None)
layout = QtWidgets.QVBoxLayout(dlg)
layout.setObjectName("layout")
dlg.setObjectName("Help")
dlg.setWindowModality(QtCore.Qt.ApplicationModal)
textBrowser = QtWidgets.QTextBrowser(dlg)
textBrowser.setReadOnly(True)
textBrowser.setText(self.ui.helpText)
textBrowser.setTextInteractionFlags(QtCore.Qt.TextBrowserInteraction)
textBrowser.setOpenExternalLinks(True)
layout.addWidget(textBrowser)
buttonBox = QtWidgets.QDialogButtonBox(dlg)
buttonBox.setOrientation(QtCore.Qt.Horizontal)
buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Close)
buttonBox.setObjectName("buttonBox")
layout.addWidget(buttonBox)
buttonBox.rejected.connect(dlg.reject)
dlg.setLayout(layout)
dlg.setWindowTitle(QtWidgets.QApplication.translate("DdInfo", "Help"))
dlg.exec_()
class DdSearchDialog(QtWidgets.QDialog):
'''Each searchDialog is a child of QDialog'''
def __init__(self, ui, layer, db, parent = None, root = None):
super().__init__(parent)
self.ddManager = QgsApplication.instance().ddManager
self.ui = ui
self.layer = layer
self.db = db
self.mode = 1
self.feature = QgsFeature(-3333)
fields = self.layer.fields()
self.feature.initAttributes(fields.count())
self.ui.setupUi(self, self.db)
self.ui.buttonBox.accepted.disconnect(self.accept)
self.ui.buttonBox.rejected.disconnect(self.reject)
self.ui.buttonBox.clicked.connect(self.clicked)
self.ui.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel| \
QtWidgets.QDialogButtonBox.Ok|QtWidgets.QDialogButtonBox.Save| \
QtWidgets.QDialogButtonBox.Open|QtWidgets.QDialogButtonBox.Reset)
okBtn = self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Ok)
okBtn.setEnabled(True)
self.forwardReturn = True # use return pressed to accept self
self.setTitle()
self.initialize()
if root == None:
root = self.ddManager.ddLayers[self.layer.id()][6]
if root == None:
root = self.createSearch()
self.applySearch(root)
def debug(self, str):
QgsMessageLog.logMessage(str)
def setTitle(self):
title = self.layer.name()
title += " - "
title += QtWidgets.QApplication.translate("DdInfo", "Search")
self.setWindowTitle(title)
def initialize(self):
self.ui.initialize(self.layer, self.feature, self.db, self.mode)
def setForwardReturn(self, doForward = True):
self.forwardReturn = doForward
def clicked(self, thisButton):
if thisButton == self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Ok):
if self.forwardReturn:
root = self.createSearch()
if self.ddManager.setLastSearch(self.layer, root):
self.accept()
elif thisButton == self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Cancel):
self.reject()
elif thisButton == self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Save):
self.saveSearch()
elif thisButton == self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Open):
root = self.loadSearch()
self.applySearch(root)
elif thisButton == self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Reset):
self.initialize()
def accept(self):
searchString = self.ui.search(self.layer)
if searchString != "":
oldSubsetString = self.layer.subsetString()
if oldSubsetString == "":
newSubsetString = searchString
else:
newSubsetString = "(" + oldSubsetString + ") AND (" + searchString + ")"
self.layer.setSubsetString(newSubsetString)
self.layer.reload()
self.layer.selectAll()
selFids = self.layer.selectedFeatureIds()
if self.layer.selectedFeatureCount() == 0:
QtWidgets.QMessageBox.information(None, "",
QtWidgets.QApplication.translate("DdInfo", "No matches found"))
return None
else:
if self.layer.geometryType() != 4: # layer with geometry
self.ddManager.iface.mapCanvas().zoomToSelected(self.layer)
if self.layer.geometryType() != 4:
self.ddManager.iface.mapCanvas().refresh()
self.layer.setSubsetString(oldSubsetString)
self.layer.reload()
self.layer.selectByIds(selFids)
self.done(1)
else:
self.done(0)
def createSearch(self):
root = ET.Element('DdSearch')
self.ui.createSearch(root)
return root
def saveSearch(self):
path = self.ddManager.getSearchPath()
title = QtWidgets.QApplication.translate("DdLabel", "Save search as")
filter = "DdXML (*.ddsx)"
if os.name == "posix":
fd = QtWidgets.QFileDialog(None, title, path, filter)
fd.setDefaultSuffix("ddsx")
fd.setAcceptMode(QtWidgets.QFileDialog.AcceptSave)
if fd.exec_() == 1:
saveAs = fd.selectedFiles()[0]
else:
saveAs = ""
else:
saveAs = QtWidgets.QFileDialog.getSaveFileName(None, title, path, filter)
if saveAs != "":
root = self.createSearch()
tree = ET.ElementTree()
tree._setroot(root)
tree.write(saveAs,encoding = "utf-8", xml_declaration = True)
path = os.path.abspath(os.path.dirname(saveAs))
self.ddManager.saveSearchPath(path)
def applySearch(self, root):
self.ui.applySearch(root)
def loadSearch(self):
path = self.ddManager.getSearchPath()
loadThis = QtWidgets.QFileDialog.getOpenFileName(None,
QtWidgets.QApplication.translate("DdLabel", "Load search"),
path, "DdXML (*.ddsx)")
if loadThis != u"":
tree = ET.parse(loadThis)
root = tree.getroot()
else:
root = ET.Element('DdSearch')
return root
def reject(self):
self.ui.discard()
self.done(0)