-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
61 lines (45 loc) · 2.29 KB
/
models.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
from qgis.PyQt.QtCore import QAbstractListModel, Qt, QModelIndex
from qgis.PyQt.QtGui import QStandardItemModel, QStandardItem
from qgis.core import QgsProject, QgsFeatureSource, QgsApplication, QgsMapLayer
from .filters import loadAllFilterDefinitions
from .helpers import hasLayerException, isLayerSupported
DataRole = Qt.ItemDataRole.UserRole + 1
class FilterModel(QAbstractListModel):
def __init__(self, parent=None):
super().__init__(parent)
self.filters = loadAllFilterDefinitions()
self.filters.sort()
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if role == Qt.ItemDataRole.DisplayRole:
return self.filters[index.row()].name
elif role == DataRole:
return self.filters[index.row()]
def rowCount(self, parent=QModelIndex()):
return len(self.filters)
def removeRows(self, row: int, count: int = 1, parent: QModelIndex = ...) -> bool:
self.beginRemoveRows(QModelIndex(), row, row + count - 1)
self.filters = self.filters[:row] + self.tableData[row + count:]
self.endRemoveRows()
return True
class LayerModel(QStandardItemModel):
def __init__(self, parent=None):
super(LayerModel, self).__init__(parent)
for layer in [layerNode.layer() for layerNode in QgsProject.instance().layerTreeRoot().findLayers()]:
item = QStandardItem(layer.name())
item.setData(layer, role=DataRole)
if isLayerSupported(layer):
item.setEnabled(True)
if layer.dataProvider().hasSpatialIndex() == QgsFeatureSource.SpatialIndexPresence.SpatialIndexNotPresent:
item.setToolTip(self.tr('Layer has no spatial index'))
item.setIcon(QgsApplication.getThemeIcon('/mIconWarning.svg'))
else:
item.setEnabled(False)
item.setToolTip(self.tr('Layer type is not supported'))
self.initItemCheckState(layer, item)
self.appendRow(item)
def initItemCheckState(self, layer: QgsMapLayer, item: QStandardItem):
item.setFlags(item.flags() | Qt.ItemFlag.ItemIsUserCheckable)
if hasLayerException(layer):
item.setCheckState(Qt.CheckState.Checked)
else:
item.setCheckState(Qt.CheckState.Unchecked)