forked from anitagraser/TimeManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorlayerdialog.py
161 lines (132 loc) · 5.54 KB
/
vectorlayerdialog.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
import abc
from PyQt4 import uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis._core import QgsMapLayerRegistry
import qgis_utils as qgs
import layer_settings as ls
import conf
from tmlogging import warn
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.add_connections()
# 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 get_ids_already_in_out_table(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 add_layer_to_select(self, name):
self.dialog.comboBoxLayers.addItem(name)
def layer_count(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):
i = 0
for (id, layer) in idlayers:
unicode_name = unicode(layer.name())
self.add_layer_to_select(unicode_name)
self.tempLayerIndexToId[i] = id
i += 1
if self.layer_count() == 0:
msg = 'There are no unmanaged layers of requested type in the project!'
QMessageBox.information(self.dialog, 'Error', msg)
raise Exception(msg)
# add the attributes of the first layer in the select for gui initialization
self.add_layer_attributes(0)
@abc.abstractmethod
def add_layer_attributes(self, id):
pass
def add_layer_to_table(self):
"""Add selected layer attributes to table"""
settings = self.extract_settings()
ls.addSettingsToRow(settings, self.out_table)
@abc.abstractmethod
def extract_settings(self):
pass
@abc.abstractmethod
def show(self):
pass
def add_connections(self):
self.dialog.comboBoxLayers.currentIndexChanged.connect(self.add_layer_attributes)
self.dialog.buttonBox.accepted.connect(self.add_layer_to_table)
class VectorLayerDialog(AddLayerDialog):
def __init__(self, *args):
super(VectorLayerDialog, self).__init__(*args)
def extract_settings(self):
return ls.getSettingsFromAddVectorLayersUI(self.dialog, self.tempLayerIndexToId)
def add_layer_attributes(self, idx):
"""get list layer attributes, 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 add_connections(self):
super(VectorLayerDialog, self).add_connections()
QObject.connect(self.dialog.comboBoxInterpolation,
SIGNAL("currentIndexChanged(const QString &)"),
self.maybeEnableIDBox)
self.dialog.exportEmptyCheckbox.setChecked(Qt.Unchecked)
def show(self):
self.clear()
idsToIgnore = set(self.get_ids_already_in_out_table())
allVectorIds = set(qgs.getAllLayerIds(lambda x: not qgs.isRaster(x)))
try:
self.populate(allVectorIds - idsToIgnore)
except Exception, e:
warn(e)
return
self.add_interpolation_modes(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 add_interpolation_modes(self, comboBox):
comboBox.clear()
comboBox.addItem(conf.NO_INTERPOLATION)
for mode in conf.INTERPOLATION_MODE_TO_CLASS.keys():
comboBox.addItem(mode)