Skip to content

Commit c737d34

Browse files
committed
Add: Features: change the color of changed zones and Undo changes
1 parent 86ea462 commit c737d34

File tree

3 files changed

+92
-22
lines changed

3 files changed

+92
-22
lines changed

EcuZoneCheckBox.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,39 @@
1919
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
2020
"""
2121

22-
from PySide6.QtCore import Qt
22+
from PySide6.QtCore import Qt, Slot
2323
from PySide6.QtWidgets import QCheckBox
2424

2525

2626
class EcuZoneCheckBox(QCheckBox):
2727
"""
2828
"""
2929
initialValue = 0
30+
newValue = 0
31+
style = ""
3032
zoneObject = {}
3133
itemReadOnly = False
3234
def __init__(self, parent, zoneObject: dict, readOnly: bool):
3335
super(EcuZoneCheckBox, self).__init__(parent)
3436
self.itemReadOnly = readOnly
3537
self.zoneObject = zoneObject
3638

39+
# Notify changes, to change color if changed
40+
self.checkStateChanged.connect(self.stateChange)
41+
42+
# Save current style-sheet
43+
self.style = self.styleSheet()
44+
45+
@Slot()
46+
def stateChange(self, state):
47+
if state != self.newValue:
48+
self.newValue = state
49+
50+
if self.newValue == self.initialValue or self.initialValue == 0:
51+
self.setStyleSheet(self.style)
52+
else:
53+
self.setStyleSheet("background-color: rgb(42, 130, 218)")
54+
3755
def getDescriptionName(self):
3856
return self.zoneObject["name"]
3957

@@ -47,7 +65,7 @@ def setCheckState(self, val):
4765
self.initialValue = val;
4866
super().setCheckState(val)
4967

50-
def isCheckBoxChanged(self, virginWrite: bool()):
68+
def isCheckBoxChanged(self, virginWrite: bool):
5169
return self.isEnabled() and not(self.itemReadOnly) and self.initialValue != 0 and self.initialValue != self.checkState()
5270

5371
def getValuesAsCSV(self):
@@ -60,10 +78,10 @@ def getValuesAsCSV(self):
6078
return value
6179

6280
def clearZoneValue(self):
63-
initialValue = 0
81+
self.initialValue = 0
6482
self.setCheckState(Qt.Unchecked)
6583

66-
def getZoneAndHex(self, virginWrite: bool()):
84+
def getZoneAndHex(self, virginWrite: bool):
6785
value = "None"
6886
if "mask" in self.zoneObject:
6987
print("EcuZoneCheckBox.getZoneAndHex(..) has mask?")

EcuZoneComboBox.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
2020
"""
2121

22-
from PySide6.QtGui import QKeyEvent
23-
from PySide6.QtCore import Qt, QEvent
24-
from PySide6.QtWidgets import QComboBox
22+
from PySide6.QtGui import QKeyEvent, QAction, QIcon, QPalette
23+
from PySide6.QtCore import Qt, Slot, QEvent, QSize, QPoint
24+
from PySide6.QtWidgets import QComboBox, QMenu
2525

2626
from i18n import i18n
27+
import PyPSADiagGUI
2728

2829
class EcuZoneComboBox(QComboBox):
2930
"""
3031
"""
31-
value = 0
32+
initialValue = 0
33+
newValue = 0
34+
style = ""
3235
zoneObject = {}
3336
itemReadOnly = False
3437
def __init__(self, parent, zoneObject: dict, readOnly: bool):
@@ -48,6 +51,37 @@ def __init__(self, parent, zoneObject: dict, readOnly: bool):
4851
self.addItem(name, "h:" + paramObject["value"])
4952
self.setCurrentIndex(0)
5053

54+
# Notify changes, to change color if changed
55+
self.currentIndexChanged.connect(self.indexChanged)
56+
57+
# Make Undo possible
58+
self.setContextMenuPolicy(Qt.CustomContextMenu)
59+
self.customContextMenuRequested.connect(self.contextMenu)
60+
61+
# Save current style-sheet
62+
self.style = self.styleSheet()
63+
64+
@Slot()
65+
def indexChanged(self, index):
66+
if index != self.newValue:
67+
self.newValue = index
68+
69+
if self.newValue == self.initialValue:
70+
self.setStyleSheet(self.style)
71+
else:
72+
self.setStyleSheet("combobox-popup: 3; background-color: rgb(42, 130, 218)")
73+
74+
@Slot()
75+
def contextMenu(self, pos: QPoint):
76+
contextMenu = QMenu(self)
77+
undoIcon = QIcon.fromTheme(QIcon.ThemeIcon.EditUndo)
78+
undoAction = QAction(undoIcon, i18n().tr("&Undo"), contextMenu)
79+
contextMenu.addAction(undoAction)
80+
81+
action = contextMenu.exec_(self.mapToGlobal(pos))
82+
if action == undoAction:
83+
self.setCurrentIndex(self.initialValue)
84+
5185
def getItemDataAsInt(self, index: int) -> int:
5286
"""Convert stored string data back to integer"""
5387
data = self.itemData(index)
@@ -87,11 +121,11 @@ def getCorrespondingByteSize(self):
87121
return 1
88122

89123
def setCurrentIndex(self, val):
90-
self.value = val;
124+
self.initialValue = val;
91125
super().setCurrentIndex(val)
92126

93-
def isComboBoxChanged(self, virginWrite: bool()):
94-
return self.isEnabled() and not(self.itemReadOnly) and self.value != self.currentIndex()
127+
def isComboBoxChanged(self, virginWrite: bool):
128+
return self.isEnabled() and not(self.itemReadOnly) and self.initialValue != self.currentIndex()
95129

96130
def getValuesAsCSV(self):
97131
value = "Disabled"
@@ -101,10 +135,10 @@ def getValuesAsCSV(self):
101135
return value
102136

103137
def clearZoneValue(self):
104-
value = 0
138+
self.initialValue = 0
105139
self.setCurrentIndex(0)
106140

107-
def getZoneAndHex(self, virginWrite: bool()):
141+
def getZoneAndHex(self, virginWrite: bool):
108142
value = "None"
109143
if self.isComboBoxChanged(virginWrite):
110144
index = self.currentIndex()

EcuZoneLineEdit.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import json
2323
import os
2424
from PySide6.QtGui import QKeyEvent
25-
from PySide6.QtCore import Qt, QEvent
25+
from PySide6.QtCore import Qt, QEvent, Slot
2626
from PySide6.QtWidgets import QLineEdit
2727

2828

@@ -31,15 +31,33 @@ class EcuZoneLineEdit(QLineEdit):
3131
"""
3232
zoneObject = {}
3333
valueType = ""
34-
initialValue = ""
34+
initialLineValue = ""
35+
newLineLineValue = ""
3536
initialRaw = ""
37+
style = ""
3638
itemReadOnly = False
3739
def __init__(self, parent, zoneObject: dict, readOnly: bool):
3840
super(EcuZoneLineEdit, self).__init__(parent)
3941
self.itemReadOnly = readOnly
4042
self.setReadOnly(readOnly)
4143
self.zoneObject = zoneObject
4244

45+
# Notify changes, to change color if changed
46+
self.textEdited.connect(self.textChange)
47+
48+
# Save current style-sheet
49+
self.style = self.styleSheet()
50+
51+
@Slot()
52+
def textChange(self, text):
53+
if text != self.newLineLineValue:
54+
self.newLineLineValue = text
55+
56+
if self.newLineLineValue == self.initialLineValue:
57+
self.setStyleSheet(self.style)
58+
else:
59+
self.setStyleSheet("background-color: rgb(42, 130, 218)")
60+
4361
def event(self, event: QEvent):
4462
if event.type() == QEvent.KeyPress:
4563
keyEvent = QKeyEvent(event)
@@ -64,14 +82,14 @@ def getCorrespondingByteSize(self):
6482
return 1
6583

6684
def __setText(self, val):
67-
self.initialValue = val;
85+
self.initialLineValue = val;
6886
super().setText(val)
6987

7088
def updateText(self, val):
7189
super().setText(val)
7290

73-
def isLineEditChanged(self, virginWrite: bool()):
74-
return self.isEnabled() and not(self.itemReadOnly) and (self.initialValue != self.text() or virginWrite)
91+
def isLineEditChanged(self, virginWrite: bool):
92+
return self.isEnabled() and not(self.itemReadOnly) and (self.initialLineValue != self.text() or virginWrite)
7593

7694
def __convertZoneData(self):
7795
if self.valueType == "string_ascii":
@@ -92,12 +110,12 @@ def getValuesAsCSV(self):
92110
return "Disabled"
93111

94112
def clearZoneValue(self):
95-
valueType = ""
96-
initialValue = ""
97-
initialRaw = ""
113+
self.initialLineValue = ""
114+
self.valueType = ""
115+
self.initialRaw = ""
98116
self.clear()
99117

100-
def getZoneAndHex(self, virginWrite: bool()):
118+
def getZoneAndHex(self, virginWrite: bool):
101119
value = "None"
102120
if self.isLineEditChanged(virginWrite):
103121
return self.__convertZoneData()

0 commit comments

Comments
 (0)