forked from desertwolf7/NumericalDigitize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueChecker.py
179 lines (150 loc) · 7.69 KB
/
valueChecker.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Numerical digitize - sets up a Qgis actions for append and edit features
by inserting or changing numerical values of vertex's coordinates
A QGIS plugin
-------------------
begin : 2019 year
git sha : $Format:%H$
copyright (C) 2019 Igor Chumichev
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 qgis.PyQt.QtWidgets import QTableWidget, QTableWidgetItem
from qgis.PyQt.QtCore import QVariant, QMetaType, QCoreApplication, Qt, QModelIndex
from qgis.PyQt.QtGui import QBrush, QColor
from qgis.core import QgsWkbTypes, QgsError
from qgis.gui import QgsErrorDialog
from enum import Enum
from math import isnan
class CellValue(Enum):
ValueNone = 0
ValueNotFloat = 1
ValueFloat = 2
# Values checker class
class ValueChecker:
def __init__(self, p_tableViewWidget: QTableWidget, p_geometryType):
self.tableViewWidget = p_tableViewWidget
self.geometryType = p_geometryType
@staticmethod
def checkValue(value: QVariant):
if value.isNull():
return CellValue.ValueNone
if value.convert(QMetaType.QString):
if str(value.value()) == "":
return CellValue.ValueNone
if value.convert(QMetaType.Float):
if isnan(float(value.value())):
return CellValue.ValueNotFloat
else:
return CellValue.ValueFloat
else:
return CellValue.ValueNotFloat
def checkCellValue(self, item: QTableWidgetItem):
if item is None:
return CellValue.ValueNone
return self.checkValue(QVariant(item.text()))
def checkModelValue(self, i, j):
model = self.tableViewWidget.model()
if i > model.rowCount() or i < 0:
return CellValue.ValueNone
if j > model.columnCount() or j < 0:
return CellValue.ValueNone
return self.checkValue(QVariant(model.data(model.index(i, j, QModelIndex()), Qt.EditRole)))
def isCurrentPartValid(self, highlightErrors=False):
partValid = True
model = self.tableViewWidget.model()
for i in range(model.rowCount() - 1):
partValid = partValid and self.isRowValid(i, highlightErrors)
if self.isLastRowEmpty():
return partValid
else:
partValid = partValid and self.isRowValid(model.rowCount() - 1, highlightErrors)
return partValid
def isRowValid(self, RowNum, highlightErrors=False):
RowValid = True
model = self.tableViewWidget.model()
if RowNum > model.rowCount() - 1 or RowNum < 0:
return False
for i in range(model.columnCount()):
theValue = self.checkModelValue(RowNum, i)
RowValid = RowValid and (theValue == CellValue.ValueFloat)
if highlightErrors and theValue == CellValue.ValueNotFloat:
self.tableViewWidget.item(RowNum, i).setForeground(QBrush(QColor(255, 0, 0)))
return RowValid
# check last row is empty?
def isLastRowEmpty(self):
lastRowEmpty = True
model = self.tableViewWidget.model()
i = model.rowCount() - 1
for j in range(model.columnCount()):
lastRowEmpty = lastRowEmpty and (self.checkModelValue(i, j) == CellValue.ValueNone)
return lastRowEmpty
def checkCoordsMatrix(self, coords):
strResult = QgsError()
for i in range(len(coords)):
partNumber = str(coords[i][0])
listValues = coords[i][1]
if len(listValues) == 0:
strResult.append(self.translate_str("Part ") +
str(partNumber) + self.translate_str(" is empty"), self.translate_str("Value error"))
elif self.geometryType == QgsWkbTypes.LineGeometry and len(listValues) < 2:
strResult.append(self.translate_str("Part ") + str(partNumber) +
self.translate_str(" contains below 2 points."
" It's not enough for creating line geometry"),
self.translate_str("Value error"))
elif self.geometryType == QgsWkbTypes.Polygon and len(listValues) < 3:
strResult.append(self.translate_str("Part ") + str(partNumber) +
self.translate_str(" contains below 3 points. It's not enough "
"for creating polygon geometry"), self.translate_str("Value error"))
else:
for j in range(len(listValues)):
currentTuple = listValues[j]
for k in range(len(currentTuple)):
cellStatus = self.checkValue(QVariant(currentTuple[k]))
if cellStatus == CellValue.ValueNone:
strResult.append(self.translate_str("Part ") + str(partNumber) +
self.translate_str(" row ") + str(j + 1) +
self.translate_str(" column ") + str(k + 1) +
self.translate_str(" contains empty value"),
self.translate_str("Value error"))
elif cellStatus == CellValue.ValueNotFloat:
strResult.append(self.translate_str("Part ") + str(partNumber) +
self.translate_str(" row ") + str(j + 1) +
self.translate_str(" column ") + str(k + 1) +
self.translate_str(" contains incorrect value"),
self.translate_str("Value error"))
if not strResult.isEmpty():
errorDialog = QgsErrorDialog(strResult,
self.translate_str('Values errors found'),
self.tableViewWidget.parent())
errorDialog.showNormal()
return False
else:
return True
def setOkButtonState(self):
# Enable OK button when:
# If layertype - point and entered point => 1
# If layertype - polyline and entered point => 2
# If layertype - polygone and entered point => 3
if self.geometryType == QgsWkbTypes.PointGeometry and (self.tableViewWidget.rowCount()) >= 1:
return True
elif self.geometryType == QgsWkbTypes.LineGeometry and (self.tableViewWidget.rowCount()-1) >= 2:
return True
elif self.geometryType == QgsWkbTypes.PolygonGeometry and (self.tableViewWidget.rowCount()-1) >= 3:
return True
else:
return False
@staticmethod
def translate_str(message):
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate("ValueChecker", message)