forked from Barracuda09/PyPSADiag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcuMultiZoneTreeWidgetItem.py
More file actions
194 lines (161 loc) · 7.48 KB
/
EcuMultiZoneTreeWidgetItem.py
File metadata and controls
194 lines (161 loc) · 7.48 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
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
"""
EcuZoneTable.py
Copyright (C) 2024 - 2025 Marc Postema (mpostema09 -at- gmail.com)
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.
This program 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
"""
from PySide6.QtCore import Qt, Slot
from PySide6.QtWidgets import QTreeWidgetItem, QTreeWidget
from PySide6.QtGui import QPalette, QColor
from EcuZoneLineEdit import EcuZoneLineEdit
from EcuZoneCheckBox import EcuZoneCheckBox
from EcuZoneComboBox import EcuZoneComboBox
from EcuZoneTreeWidgetItem import EcuZoneTreeWidgetItem
from i18n import i18n
import PyPSADiagGUI
class EcuMultiZoneTreeWidgetItem(QTreeWidgetItem):
zone = ""
zoneDescription = ""
zoneObject = {}
integrity = True
selfUpdate = False
def __init__(self, parent: QTreeWidget, row: int, zone: str, description: str, zoneObject: dict):
super(EcuMultiZoneTreeWidgetItem, self).__init__(parent, [zone.upper(), str("** " + i18n().tr(description) + " **")])
parent.insertTopLevelItem(row, self)
self.setToolTip(1, i18n().tr(description))
self.zoneObject = zoneObject
self.zone = zone.upper()
self.zoneDescription = description
def addRootWidgetItem(self, tree: QTreeWidget, widget):
widget.setReadOnly(True)
tree.setItemWidget(self, 2, widget)
self.__setupConnections(widget)
def addChildWidgetItem(self, tree: QTreeWidget, label, widget):
level = EcuZoneTreeWidgetItem(self, None, "", label)
level.setToolTip(1, i18n().tr(label))
tree.setItemWidget(level, 2, widget)
self.__setupConnections(widget)
def __setupConnections(self, widget):
if isinstance(widget, EcuZoneLineEdit):
widget.textChanged.connect(self.textChanged)
elif isinstance(widget, EcuZoneCheckBox):
widget.stateChanged.connect(self.stateChanged)
elif isinstance(widget, EcuZoneComboBox):
widget.currentIndexChanged.connect(self.currentIndexChanged)
def getValuesAsCSV(self):
widget = self.treeWidget().itemWidget(self, 2)
value = "None"
# Check if Integrity is correct, then return Zone data
if self.integrity and isinstance(widget, EcuZoneLineEdit):
value = widget.getValuesAsCSV()
return [self.zone, value, self.zoneDescription]
def clearZoneListValues(self):
rootWidget = self.treeWidget().itemWidget(self, 2)
rootWidget.clearZoneValue()
for index in range(self.childCount()):
cellItem = self.child(index)
widget = cellItem.treeWidget().itemWidget(cellItem, 2)
self.__clearWidget(widget, cellItem)
def getZoneAndHex(self, virginWrite: bool()):
widget = self.treeWidget().itemWidget(self, 2)
value = "None"
# Check if Integrity is correct, then return Zone data
if self.integrity and isinstance(widget, EcuZoneLineEdit):
value = widget.getZoneAndHex(virginWrite)
return [self.zone, value]
def changeZoneOption(self, root, data: str, valueType: str):
self.selfUpdate = True
try:
# Set Root value of Multi Config zone
widget = root.treeWidget().itemWidget(root, 2)
widget.changeZoneOption(data, "")
# Set individual Sub items
for index in range(root.childCount()):
cellItem = root.child(index)
widget = cellItem.treeWidget().itemWidget(cellItem, 2)
result = widget.changeZoneOption(data, valueType)
if result == 2:
print("Disabled(2): " + self.zone + " - " + widget.getDescriptionName())
p = widget.palette()
p.setColor(QPalette.Button, PyPSADiagGUI.RED)
widget.setPalette(p)
widget.setEnabled(False)
cellItem.setHidden(True)
elif result == 1:
print("Disabled(1): " + self.zone + " - " + widget.getDescriptionName())
self.integrity = False
except:
print("except: EcuMultiZoneTreeWidgetItem:changeZoneOption " + self.zone + " - " + widget.getDescriptionName())
self.selfUpdate = False
# Integrity wrong, disable the sub zones and coding
if not self.integrity:
for index in range(root.childCount()):
cellItem = root.child(index)
widget = cellItem.treeWidget().itemWidget(cellItem, 2)
p = widget.palette()
p.setColor(QPalette.Button, PyPSADiagGUI.RED)
widget.setPalette(p)
widget.setEnabled(False)
def __clearWidget(self, widget, cellItem):
widget.clearZoneValue()
p = widget.palette()
p.setColor(QPalette.Button, PyPSADiagGUI.BUTTON_COLOR)
widget.setPalette(p)
widget.setEnabled(True)
cellItem.setHidden(False)
def __update(self):
# If we are "self" updating (By loading CSV file) do not call update
if self.selfUpdate == True:
return
rootWidget = self.treeWidget().itemWidget(self, 2)
data = rootWidget.text()
# Make Bytes (2 chars) from input data
byteData = []
for i in range(0, len(data), 2):
byteData.append(data[i:i + 2])
for index in range(self.childCount()):
cellItem = self.child(index)
widget = cellItem.treeWidget().itemWidget(cellItem, 2)
# Is this widget used or disabled?
if widget.isEnabled() == False:
# print("Disabled widget - " + widget.getDescriptionName())
continue
# print("Enabled widget - " + widget.getDescriptionName())
byteNr = widget.getCorrespondingByte()
size = widget.getCorrespondingByteSize()
# Size do not correspond or not Enabled, Then Goto next item
if (byteNr + size) > len(byteData) or widget.isEnabled() == False:
continue
# Get the corresponing data for this "zone"
currData = str().join(byteData[byteNr : byteNr + size])
if isinstance(widget, EcuZoneLineEdit):
currData = widget.update(currData)
elif isinstance(widget, EcuZoneCheckBox):
currData = widget.update(currData)
elif isinstance(widget, EcuZoneComboBox):
currData = widget.update(currData)
# Put back changed "zone" data
for i in range(size):
j = (i * 2)
byteData[byteNr + i] = currData[j:j + 2]
# Update changed "zone" data
rootWidget.updateText(str().join(byteData))
@Slot()
def currentIndexChanged(self, item: int):
self.__update()
@Slot()
def textChanged(self, item: str):
self.__update()
@Slot()
def stateChanged(self, item: int):
self.__update()