forked from anitagraser/TimeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorlayerdialog.py
More file actions
168 lines (140 loc) · 6 KB
/
Copy pathvectorlayerdialog.py
File metadata and controls
168 lines (140 loc) · 6 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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import abc
from PyQt4 import uic
from PyQt4.QtCore import QObject, SIGNAL, Qt
from PyQt4.QtGui import QMessageBox
from qgis._core import QgsMapLayerRegistry
from tmlogging import warn
import qgis_utils as qgs
import layer_settings
import conf
class AddLayerDialog:
__metaclass__ = abc.ABCMeta
def __init__(self, iface, ui_path, out_table):
self.iface = iface
self.tempLayerIndexToId = {}
self.dialog = uic.loadUi(ui_path)
self.out_table = out_table
self.addConnections()
# TODO assert it has a buttonbox and comboBoxLayers
def getDialog(self):
return self.dialog
def getSelectedLayerName(self):
return self.dialog.comboBoxLayers.currentText()
def clear(self):
self.tempLayerIndexToId = {}
self.dialog.comboBoxLayers.clear()
def getSelectedLayer(self):
idx = self.dialog.comboBoxLayers.currentIndex()
layerId = self.tempLayerIndexToId[idx]
return qgs.getLayerFromId(layerId)
def getIdsAlreadyInOutTable(self):
"""Get list of layer ids listed in the tableWidget"""
layerList = []
if self.out_table is None:
return layerList
for row in range(self.out_table.rowCount()):
layerId = self.out_table.item(row, 4).text()
layerList.append(layerId)
return layerList
def addLayerToSelect(self, name):
"""Add layer name to layer combo box"""
self.dialog.comboBoxLayers.addItem(name)
def getLayerCount(self):
return self.dialog.comboBoxLayers.count()
def populate(self, layerIds):
idlayers_it = QgsMapLayerRegistry.instance().mapLayers().iteritems()
selected_idlayers = filter(lambda idlayer: idlayer[0] in layerIds, idlayers_it)
self.populateFromLayers(selected_idlayers)
def populateFromLayers(self, idlayers):
"""Populate layer combo box"""
i = 0
for (id, layer) in idlayers:
unicode_name = unicode(layer.name())
self.addLayerToSelect(unicode_name)
self.tempLayerIndexToId[i] = id
i += 1
if self.getLayerCount() == 0:
msg = 'All suitable project layers have already been added to TimeManager!'
QMessageBox.information(self.dialog, 'Error', msg)
raise Exception(msg)
# add the attributes of the first layer in the select for gui initialization
self.addLayerAttributes(0)
@abc.abstractmethod
def addLayerAttributes(self, id):
pass
def addLayerToTable(self):
"""Add selected layer attributes to table"""
settings = self.extractSettings()
layer_settings.addSettingsToRow(settings, self.out_table)
@abc.abstractmethod
def extractSettings(self):
pass
@abc.abstractmethod
def show(self):
pass
def addConnections(self):
self.dialog.comboBoxLayers.currentIndexChanged.connect(self.addLayerAttributes)
self.dialog.buttonBox.accepted.connect(self.addLayerToTable)
class VectorLayerDialog(AddLayerDialog):
def __init__(self, *args):
super(VectorLayerDialog, self).__init__(*args)
def extractSettings(self):
return layer_settings.getSettingsFromAddVectorLayersUI(self.dialog, self.tempLayerIndexToId)
def addLayerAttributes(self, idx):
"""Get layer attributes and fill the combo boxes"""
if not self.tempLayerIndexToId:
return
layerId = self.tempLayerIndexToId[self.dialog.comboBoxLayers.currentIndex()]
fieldmap = qgs.getLayerAttributes(layerId)
if fieldmap is None:
return
self.dialog.comboBoxStart.clear()
self.dialog.comboBoxEnd.clear()
self.dialog.comboBoxID.clear()
self.dialog.comboBoxEnd.addItem('Same as start')
self.dialog.comboBoxEnd.addItem('No end time - accumulate features')
self.dialog.comboBoxID.addItem(conf.NO_ID_TEXT)
for attr in fieldmap:
self.dialog.comboBoxStart.addItem(attr.name())
self.dialog.comboBoxEnd.addItem(attr.name())
self.dialog.comboBoxID.addItem(attr.name())
def addConnections(self):
super(VectorLayerDialog, self).addConnections()
QObject.connect(self.dialog.comboBoxInterpolation,
SIGNAL("currentIndexChanged(const QString &)"),
self.maybeEnableIDBox)
self.dialog.exportEmptyCheckbox.setChecked(Qt.Unchecked)
def show(self):
"""Update GUI elements and show the dialog"""
self.clear()
# determine which layers are vector and can be time controlled
idsToIgnore = set(self.getIdsAlreadyInOutTable())
allVectorIds = set(qgs.getAllLayerIds(lambda x: not qgs.isRaster(x)))
unsupportedVectorIds = set(qgs.getAllLayerIds(lambda x: qgs.isWFS(x)))
# todo: plugin layers, e.g. from QuickMapServices should also be excluded
try:
self.populate(allVectorIds - idsToIgnore - unsupportedVectorIds)
except Exception, e:
warn(e)
return
# finalize and show dialog
self.addInterpolationModes(self.dialog.comboBoxInterpolation)
self.dialog.show()
def maybeEnableIDBox(self, interpolation_mode):
if interpolation_mode != '' and conf.INTERPOLATION_MODES[interpolation_mode]:
self.dialog.comboBoxID.setEnabled(True)
self.dialog.labelID1.setEnabled(True)
self.dialog.labelID2.setEnabled(True)
self.dialog.comboBoxEnd.setEnabled(False) # end field not yet supported when interpolating
else:
self.dialog.comboBoxID.setEnabled(False)
self.dialog.labelID1.setEnabled(False)
self.dialog.labelID2.setEnabled(False)
self.dialog.comboBoxEnd.setEnabled(True)
def addInterpolationModes(self, comboBox):
comboBox.clear()
comboBox.addItem(conf.NO_INTERPOLATION)
for mode in conf.INTERPOLATION_MODE_TO_CLASS.keys():
comboBox.addItem(mode)