forked from nextgis/qgis_metatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetatoolseditor.py
269 lines (223 loc) · 8.89 KB
/
metatoolseditor.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
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
# -*- coding: utf-8 -*-
#******************************************************************************
#
# Metatools
# ---------------------------------------------------------
# Metadata browser/editor
#
# Copyright (C) 2011 BV ([email protected])
# Copyright (C) 2011 NextGIS ([email protected])
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
# to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
#
#******************************************************************************
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import *
from qgis.core import *
from qgis.gui import *
import sys
from dom_model import DomModel, FilterDomModel
from ui.ui_editor import Ui_MetatoolsEditor
class MetatoolsEditor(QDialog, Ui_MetatoolsEditor):
def __init__(self):
QDialog.__init__(self)
self.setupUi(self)
self.setWindowFlags(Qt.Window | Qt.WindowMaximizeButtonHint)
self.tabWidget.setCurrentIndex(0)
self.lblNodePath.setText("")
self.btnSave = self.buttonBox.button(QDialogButtonBox.Save)
self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
self.btnApply = QPushButton(self.tr("Apply"))
self.btnDiscard = QPushButton(self.tr("Discard"))
self.editorButtonBox.clear()
self.editorButtonBox.addButton(self.btnApply, QDialogButtonBox.AcceptRole)
self.editorButtonBox.addButton(self.btnDiscard, QDialogButtonBox.RejectRole)
#contextmenu
self.lblNodePath.setContextMenuPolicy(Qt.ActionsContextMenu)
self.lblNodePath.addAction(self.actionCopyPath)
self.connect(self.actionCopyPath, SIGNAL("activated()"), self.slotCopyPath)
# full metadata view
self.treeFull.clicked.connect(self.itemSelected)
self.treeFull.collapsed.connect(self.collapsedExpanded)
self.treeFull.expanded.connect(self.collapsedExpanded)
# filtered metadata view
self.tbwFiltered.currentCellChanged.connect(self.cellSelected)
self.textValue.textChanged.connect(self.valueModified)
self.tabWidget.currentChanged.connect(self.tabChanged)
self.btnApply.clicked.connect(self.applyEdits)
self.btnDiscard.clicked.connect(self.resetEdits)
self.buttonBox.accepted.disconnect(self.accept)
self.btnSave.clicked.connect(self.saveMetadata)
def slotCopyPath(self):
QApplication.clipboard().setText(self.lblNodePath.text())
def setContent(self, metaProvider):
self.metaProvider = metaProvider
# load main model
#self.file = QFile(metaFilePath)
self.metaXML = QDomDocument()
metadata = self.metaProvider.getMetadata().encode("utf-8")
self.metaXML.setContent(metadata)
self.model = DomModel(self.metaXML, self)
# set full view
self.treeFull.setModel(self.model)
self.treeFull.hideColumn(1) # hide attrs
self.treeFull.resizeColumnToContents(0) # resize value column
# load filtered list
self.filteredIndexes = None # lazy init
# set filtered view
#self.fillTableWidget()
self.btnSave.setEnabled(False)
def itemSelected(self, mindex):
# Display item selected in TreeView in edit box.
self.textValue.clear()
path = ""
editable = False
self.text = None
# full view
self.mindex = self.model.index(mindex.row(), 2, mindex.parent())
path = self.model.nodePath(self.mindex)
editable = self.model.isEditable(self.mindex)
self.text = self.model.data(self.mindex, 0)
self.lblNodePath.setText(path)
if editable:
self.textValue.setPlainText(self.text)
self.groupBox.setEnabled(True)
self.editorButtonBox.setEnabled(False)
else:
self.textValue.clear()
self.groupBox.setEnabled(False)
def cellSelected(self, currentRow, currentColumn, previousRow, previousColumn):
# Display item selected in TableWidget in edit box.
self.textValue.clear()
path = ""
editable = False
self.text = None
self.mindex = self.filteredIndexes[currentRow][1]
path = self.model.nodePath(self.mindex)
editable = self.model.isEditable(self.mindex)
self.text = self.model.data(self.mindex, 0)
self.lblNodePath.setText(path)
if editable:
self.textValue.setPlainText(self.text)
self.groupBox.setEnabled(True)
self.editorButtonBox.setEnabled(False)
else:
self.textValue.clear()
self.groupBox.setEnabled(False)
def collapsedExpanded(self, mindex):
if self.tabWidget.currentIndex() == 0:
self.treeFull.resizeColumnToContents(0)
else:
self.tbwFiltered.resizeColumnToContents(0)
def valueModified(self):
self.editorButtonBox.setEnabled(True)
def tabChanged(self, tab):
self.textValue.clear()
path = ""
editable = False
self.text = None
if tab == 0:
mindex = self.treeFull.currentIndex()
self.mindex = self.model.index(mindex.row(), 2, mindex.parent())
path = self.model.nodePath(self.mindex)
editable = self.model.isEditable(self.mindex)
self.text = self.model.data(self.mindex, 0)
else:
# lazy init
if not self.filteredIndexes:
filter_lines = self.loadFilter()
self.filteredIndexes = self.searchNodes(self.model, filter_lines)
self.tbwFiltered.horizontalHeader().setVisible(True) # pyuic4 bug
self.tbwFiltered.setRowCount(len(self.filteredIndexes))
# refresh table
self.fillTableWidget()
# refresh selection
selectedItems = self.tbwFiltered.selectedItems()
if len(selectedItems):
self.mindex = self.filteredIndexes[selectedItems[0].row()][1]
path = self.model.nodePath(self.mindex)
editable = self.model.isEditable(self.mindex)
self.text = self.model.data(self.mindex, 0)
self.lblNodePath.setText(path)
if editable:
self.textValue.setPlainText(self.text)
self.groupBox.setEnabled(True)
self.editorButtonBox.setEnabled(False)
else:
self.textValue.clear()
self.groupBox.setEnabled(False)
def applyEdits(self):
self.model.setData(self.mindex, self.textValue.toPlainText())
self.text = self.model.data(self.mindex, 0)
self.btnSave.setEnabled(True)
self.editorButtonBox.setEnabled(False)
if self.tabWidget.currentIndex() != 0:
self.fillTableWidget()
def resetEdits(self):
self.textValue.setPlainText(self.text)
self.editorButtonBox.setEnabled(False)
def saveMetadata(self):
try:
self.metaProvider.setMetadata(unicode(self.metaXML.toString()))
# TODO: create preview image if need
self.btnSave.setEnabled(False)
except:
QMessageBox.warning(self,
self.tr("Metatools"),
self.tr("Metadata can't be saved:\n") + unicode(sys.exc_info()[0])
)
def loadFilter(self):
settings = QSettings("NextGIS", "metatools")
fileName = settings.value("general/filterFile", "")
if fileName == "":
return []
# read filter from file
filter_lines = []
f = QFile(fileName)
if not f.open(QIODevice.ReadOnly):
QMessageBox.warning(self,
self.tr('I/O error'),
self.tr("Can't open file %s") % (fileName)
)
return []
stream = QTextStream(f)
while not stream.atEnd():
line = stream.readLine()
filter_lines.append(line)
f.close()
return filter_lines
def searchNodes(self, model, filters):
allItemsIndexes = model.match(model.index(0, 0, QModelIndex()), Qt.DisplayRole, '*', -1, Qt.MatchWildcard | Qt.MatchRecursive)
searchedItems = []
for itemIndex in allItemsIndexes:
if self.model.nodePath(itemIndex) in filters:
valueItemIndex = self.model.index(0, 2, itemIndex.parent())
if not self.model.isEditable(itemIndex) and self.model.hasOneGco(itemIndex) :
valueItemIndex = self.model.index(0, 2, itemIndex)
searchedItems.append([itemIndex, valueItemIndex])
return searchedItems
def fillTableWidget(self):
row = 0
for nameItemIndex, valueItemIndex in self.filteredIndexes:
name = unicode(self.model.data(nameItemIndex, Qt.DisplayRole))
value = unicode(self.model.data(valueItemIndex, Qt.DisplayRole))
self.tbwFiltered.setItem(row, 0, QTableWidgetItem(name))
self.tbwFiltered.setItem(row, 1, QTableWidgetItem(value))
row += 1
self.tbwFiltered.resizeColumnToContents(0)
def accept(self):
QDialog.accept(self)