Skip to content

Commit

Permalink
fix(widgets): add blockedWidgetContext to avoid cycles inside setJson…
Browse files Browse the repository at this point in the history
…Data

fix(widgets): fix bug with somethingChanged
  • Loading branch information
azagoruyko committed Aug 2, 2024
1 parent f370ceb commit 0420731
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
6 changes: 6 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ def printErrorStack():
indent += " "
print("Error: {}".format(exc_value))

@contextmanager
def blockedWidgetContext(widget):
widget.blockSignals(True)
yield widget
widget.blockSignals(False)

def centerWindow(window):
screen = QDesktopWidget().screenGeometry()
cp = screen.center()
Expand Down
69 changes: 41 additions & 28 deletions widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ def __init__(self, **kwargs):
layout.setContentsMargins(QMargins())

self.checkBox = QCheckBox()
self.checkBox.stateChanged.connect(self.somethingChanged.emit)
self.checkBox.stateChanged.connect(lambda _=None: self.somethingChanged.emit())
layout.addWidget(self.checkBox)

def getJsonData(self):
return {"checked": self.checkBox.isChecked(), "default": "checked"}

def setJsonData(self, value):
self.checkBox.setChecked(value["checked"])
with blockedWidgetContext(self.checkBox) as w:
w.setChecked(value["checked"])

class ComboBoxTemplateWidget(TemplateWidget):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -267,7 +268,8 @@ def setJsonData(self, value):
self.comboBox.setItemData(i, jsonColor(item), Qt.ForegroundRole)
skip.append(item)

self.comboBox.setCurrentIndex(value["items"].index(value["current"]))
with blockedWidgetContext(self.comboBox) as w:
w.setCurrentIndex(value["items"].index(value["current"]))

class LineEditOptionsDialog(QDialog):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -350,7 +352,7 @@ def textChanged(self):
def sliderValueChanged(self, v):
v /= 100.0
if self.validator == 1: # int
v = round(v)
v = round(v)
self.value = v
self.textWidget.setText(str(v))
self.somethingChanged.emit()
Expand Down Expand Up @@ -390,18 +392,23 @@ def setJsonData(self, data):
self.textWidget.setValidator(QDoubleValidator())

if self.validator:
self.sliderWidget.show()
if self.minValue:
self.sliderWidget.setMinimum(self.minValue*100) # slider values are int, so mult by 100
if self.maxValue:
self.sliderWidget.setMaximum(self.maxValue*100)
self.textWidget.validator().setRange(self.minValue, self.maxValue)

if self.value:
self.sliderWidget.setValue(float(self.value)*100)
self.sliderWidget.show()
with blockedWidgetContext(self.sliderWidget) as slider:
if self.minValue:
slider.setMinimum(self.minValue*100) # slider values are int, so mult by 100
if self.maxValue:
slider.setMaximum(self.maxValue*100)

if self.value:
slider.setValue(float(self.value)*100)
else:
self.sliderWidget.hide()

self.textWidget.setText(fromSmartConversion(self.value))
with blockedWidgetContext(self.textWidget) as w:
w.setText(fromSmartConversion(self.value))

self.colorizeValue()

class LineEditAndButtonTemplateWidget(TemplateWidget):
Expand Down Expand Up @@ -508,7 +515,9 @@ def getJsonData(self):

def setJsonData(self, data):
self.value = data["value"]
self.textWidget.setText(fromSmartConversion(self.value))
with blockedWidgetContext(self.textWidget) as w:
w.setText(fromSmartConversion(self.value))

self.buttonCommand = data["buttonCommand"]
self.buttonWidget.setText(data["buttonLabel"])
self.colorizeValue()
Expand Down Expand Up @@ -663,15 +672,16 @@ def getJsonData(self):
"default": "items"}

def setJsonData(self, value):
self.listWidget.clear()
with blockedWidgetContext(self.listWidget) as w:
w.clear()

for v in value["items"]:
self.listWidget.addItem(ListBoxItem(v))

for i in value.get("selected", []):
item = self.listWidget.item(i)
if item:
item.setSelected(True)
for v in value["items"]:
w.addItem(ListBoxItem(v))
for i in value.get("selected", []):
item = w.item(i)
if item:
item.setSelected(True)

self.resizeWidget()

Expand Down Expand Up @@ -762,7 +772,8 @@ def setJsonData(self, value):
if value["current"] not in range(len(value["items"])):
value["current"] = 0

self.buttonsGroupWidget.buttons()[value["current"]].setChecked(True)
with blockedWidgetContext(self.buttonsGroupWidget) as w:
w.buttons()[value["current"]].setChecked(True)
self.colorizeButtons()

class TableTemplateWidget(TemplateWidget):
Expand Down Expand Up @@ -941,7 +952,7 @@ def __init__(self, **kwargs):
layout.setContentsMargins(QMargins())

self.textWidget = QTextEdit()
self.textWidget.textChanged.connect(self.somethingChanged.emit)
self.textWidget.textChanged.connect(lambda _=None: self.somethingChanged.emit())

incSizeBtn = QPushButton("+")
incSizeBtn.setFixedSize(25, 25)
Expand Down Expand Up @@ -976,8 +987,9 @@ def getJsonData(self):
"default": "text"}

def setJsonData(self, data):
self.textWidget.setPlainText(data["text"])
self.textWidget.setFixedHeight(data.get("height", self.getDefaultData()["height"]))
with blockedWidgetContext(self.textWidget) as w:
w.setPlainText(data["text"])
w.setFixedHeight(data.get("height", self.getDefaultData()["height"]))

class VectorTemplateWidget(TemplateWidget):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -1496,9 +1508,10 @@ def getJsonData(self):

def setJsonData(self, value):
self.jsonWidget.setFixedHeight(value["height"])
self.jsonWidget.clear()
self.jsonWidget.loadFromJsonList(value["data"])
self.jsonWidget.setReadOnly(value["readonly"])
with blockedWidgetContext(self.jsonWidget) as w:
w.clear()
w.loadFromJsonList(value["data"])
w.setReadOnly(value["readonly"])

TemplateWidgets = {
"button": ButtonTemplateWidget,
Expand Down

0 comments on commit 0420731

Please sign in to comment.