forked from Barracuda09/PyPSADiag
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcuZoneComboBox.py
More file actions
205 lines (170 loc) · 7.07 KB
/
EcuZoneComboBox.py
File metadata and controls
205 lines (170 loc) · 7.07 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
194
195
196
197
198
199
200
201
202
203
204
205
"""
EcuZoneComboBox.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.QtGui import QKeyEvent, QAction, QIcon, QPalette
from PySide6.QtCore import Qt, Slot, QEvent, QSize, QPoint
from PySide6.QtWidgets import QComboBox, QMenu
from i18n import i18n
import PyPSADiagGUI
class EcuZoneComboBox(QComboBox):
"""
"""
initialValue = 0
newValue = 0
style = ""
zoneObject = {}
itemReadOnly = False
def __init__(self, parent, zoneObject: dict, readOnly: bool):
super(EcuZoneComboBox, self).__init__(parent)
self.setStyleSheet("combobox-popup: 3;")
self.setFocusPolicy(Qt.StrongFocus)
self.itemReadOnly = readOnly
self.zoneObject = zoneObject
# Fill Combo Box
for paramObject in self.zoneObject["params"]:
name = i18n().tr(paramObject["name"])
if "mask" in paramObject:
# Store as string with "b:" prefix to avoid Qt int64 overflow
self.addItem(name, "b:" + paramObject["mask"])
else:
# Store as string with "h:" prefix for hex values
self.addItem(name, "h:" + paramObject["value"])
self.setCurrentIndex(0)
# Notify changes, to change color if changed
self.currentIndexChanged.connect(self.indexChanged)
# Make Undo possible
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.contextMenu)
# Save current style-sheet
self.style = self.styleSheet()
@Slot()
def indexChanged(self, index):
if index != self.newValue:
self.newValue = index
if self.newValue == self.initialValue:
self.setStyleSheet(self.style)
else:
self.setStyleSheet("combobox-popup: 3; background-color: rgb(42, 130, 218)")
@Slot()
def contextMenu(self, pos: QPoint):
contextMenu = QMenu(self)
undoIcon = QIcon.fromTheme(QIcon.ThemeIcon.EditUndo)
undoAction = QAction(undoIcon, i18n().tr("&Undo"), contextMenu)
contextMenu.addAction(undoAction)
action = contextMenu.exec_(self.mapToGlobal(pos))
if action == undoAction:
self.setCurrentIndex(self.initialValue)
def getItemDataAsInt(self, index: int) -> int:
"""Convert stored string data back to integer"""
data = self.itemData(index)
if isinstance(data, str):
if data.startswith("b:"):
return int(data[2:], 2)
elif data.startswith("h:"):
return int(data[2:], 16)
return int(data)
def event(self, event: QEvent):
if event.type() == QEvent.KeyPress:
keyEvent = QKeyEvent(event)
# When ESC -> Clear focus
if keyEvent.key() == Qt.Key_Escape:
# @TODO: Maybe give option to undo changes?
self.clearFocus()
return True
return super().event(event)
# Prevent scrolling without focus
def wheelEvent(self, e):
if self.hasFocus():
super().wheelEvent(e);
def getDescriptionName(self):
return self.zoneObject["name"]
def getCorrespondingByte(self):
return self.zoneObject["byte"]
def getCorrespondingByteSize(self):
if "mask" in self.zoneObject:
bits = int(self.zoneObject["mask"], 2).bit_length()
# round up to the nearest bit
return (bits + 7) // 8
return 1
def setCurrentIndex(self, val):
self.initialValue = val;
super().setCurrentIndex(val)
def isComboBoxChanged(self, virginWrite: bool):
return self.isEnabled() and not(self.itemReadOnly) and self.initialValue != self.currentIndex()
def getValuesAsCSV(self):
value = "Disabled"
if self.isEnabled():
index = self.currentIndex()
value = "%0.2X" % self.getItemDataAsInt(index)
return value
def clearZoneValue(self):
self.initialValue = 0
self.setCurrentIndex(0)
def getZoneAndHex(self, virginWrite: bool):
value = "None"
if self.isComboBoxChanged(virginWrite):
index = self.currentIndex()
value = "%0.2X" % self.getItemDataAsInt(index)
return value
def update(self, byte: str):
index = self.currentIndex()
mask = int(self.zoneObject["mask"], 2)
value = (int(byte, 16) & ~mask) | self.getItemDataAsInt(index)
size = self.getCorrespondingByteSize() * 2
byte = f"%0.{size}X" % value
return byte
def changeZoneOption(self, data: str, valueType: str):
value = int(data, 16)
if "mask" in self.zoneObject:
byteData = []
for i in range(0, len(data), 2):
byteData.append(data[i:i + 2])
# Is this option used for this Zone (NAC/RCC JSON Files)
if "zoneLength" in self.zoneObject:
zoneLength = self.zoneObject["zoneLength"]
if isinstance(zoneLength, list):
if len(byteData) not in zoneLength:
return 2
else:
if zoneLength != len(byteData):
return 2
byteNr = self.zoneObject["byte"]
mask = int(self.zoneObject["mask"], 2)
size = self.getCorrespondingByteSize()
# Integrity wrong, size does not match
if (byteNr + size) > len(byteData):
return 1
currByteData = byteData[byteNr : byteNr + size]
currData = ""
for i in range(len(currByteData)):
currData += currByteData[i]
value = int(currData, 16) & mask
else:
print(" No mask")
print(" Obj : " + str(self.zoneObject))
# Find the Option (byte) from the ComboBox
foundMatch = False
for i in range(self.count()):
if self.getItemDataAsInt(i) == value:
self.setCurrentIndex(i)
foundMatch = True
break
# Did we find item, else add it to combobox
if foundMatch == False:
print("** Add missing combobox item " + "0x%0.2X" % value + " **")
self.addItem("** 0x%0.2X" % value, "h:%X" % value)
self.setCurrentIndex(self.count() - 1)
return 0