Skip to content

Commit

Permalink
Reformat method names to use camel case, add value getters/setters
Browse files Browse the repository at this point in the history
  • Loading branch information
jcornall committed Jan 2, 2025
1 parent 1313217 commit f7517ab
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
52 changes: 35 additions & 17 deletions eqt/ui/UISliderLEditWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def __init__(self, minimum=0.0, maximum=1.0, step_size=1.0, scale_factor=1.0):
self.minimum = minimum
self.maximum = maximum
self.scale_factor = scale_factor
self.step_size = step_size
self.tick_interval = self.step_size * self.scale_factor
self.step_size = step_size * self.scale_factor

# Configure the QSlider
self.slider = QSlider()
Expand All @@ -29,12 +28,12 @@ def __init__(self, minimum=0.0, maximum=1.0, step_size=1.0, scale_factor=1.0):
self.slider.setFocusPolicy(QtCore.Qt.StrongFocus)
self.slider.setTickPosition(QSlider.TicksBelow)
self.slider.setSingleStep(self.step_size)
self.slider.setTickInterval(self.step_size)
self.slider.setTickInterval(self.maximum * 0.25)

# Connect the QSlider to the QLineEdit
self.slider.sliderPressed.connect(self.update_line_edit)
self.slider.sliderMoved.connect(self.update_line_edit)
self.slider.sliderReleased.connect(self.update_line_edit)
self.slider.sliderPressed.connect(self.updateLineEdit)
self.slider.sliderMoved.connect(self.updateLineEdit)
self.slider.sliderReleased.connect(self.updateLineEdit)

# Configure the QDoubleValidator and QLineEdit
self.validator = QtGui.QDoubleValidator()
Expand All @@ -49,42 +48,61 @@ def __init__(self, minimum=0.0, maximum=1.0, step_size=1.0, scale_factor=1.0):
self.line_edit.setPlaceholderText(str(minimum))

# Connect the QLineEdit to the QSlider
self.line_edit.textEdited.connect(self.update_slider)
self.line_edit.returnPressed.connect(self.update_slider)
self.line_edit.textEdited.connect(self.updateSlider)
self.line_edit.returnPressed.connect(self.updateSlider)

# Configure the min/max QLabels
# Configure QLabels
self.min_label = QLabel()
self.min_label.setText(str(self.minimum))
self.median_label = QLabel()
self.median_label.setText(str(self.maximum * 0.5))
self.max_label = QLabel()
self.max_label.setText(str(self.maximum))

# Configure quartile QLabels
# self.lowerq_label = QLabel()
# self.lowerq_label.setText(str(self.maximum * 0.25))
# self.upperq_label = QLabel()
# self.upperq_label.setText(str(self.maximum * 0.75))

# Configure the QGridLayout
widget_layout = QGridLayout()
widget_layout.addWidget(self.slider, 0, 0, 1, -1)
widget_layout.addWidget(self.min_label, 1, 0, QtCore.Qt.AlignLeft)
widget_layout.addWidget(self.max_label, 1, 1, QtCore.Qt.AlignRight)
# widget_layout.addWidget(self.lowerq_label, 1, 1, QtCore.Qt.AlignLeft)
widget_layout.addWidget(self.median_label, 1, 1, QtCore.Qt.AlignCenter)
# widget_layout.addWidget(self.upperq_label, 1, 3, QtCore.Qt.AlignRight)
widget_layout.addWidget(self.max_label, 1, 2, QtCore.Qt.AlignRight)
widget_layout.addWidget(self.line_edit, 2, 0, 1, -1)

# Set the layout
self.setLayout(widget_layout)
self.show()

def get_slider_value(self):
def getValue(self):
return float(self.line_edit.text())

def setValue(self, value):
self.line_edit.setText(str(value))

def getSliderValue(self):
return self.slider.value()

def get_line_edit_value(self):
def getLineEditValue(self):
return self.line_edit.text()

def update_slider(self):
def updateSlider(self):
state = self.validator.validate(self.line_edit.text(), 0)
if state[0] == QtGui.QDoubleValidator.Acceptable:
line_edit_value = float(self.get_line_edit_value())
line_edit_value = float(self.getLineEditValue())
self.slider.setValue(line_edit_value)
self.setValue(line_edit_value)
else:
self.line_edit.setText(str(self.minimum))
line_edit_value = float(self.get_line_edit_value())
line_edit_value = float(self.getLineEditValue())
self.slider.setValue(line_edit_value)
self.setValue(line_edit_value)

def update_line_edit(self):
slider_value = str(float(self.get_slider_value()))
def updateLineEdit(self):
slider_value = str(float(self.getSliderValue()))
self.line_edit.setText(slider_value)
26 changes: 13 additions & 13 deletions eqt/ui/UISliderWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, line_edit, max_label, minimum=0.0, maximum=1.0, scale_factor=
self.minimum = minimum
self.maximum = maximum
self.scale_factor = scale_factor
self.step_size = step_size
self.step_size = step_size * self.scale_factor

# Configure the QSlider
self.setRange(self.minimum, self.maximum)
Expand All @@ -38,9 +38,9 @@ def __init__(self, line_edit, max_label, minimum=0.0, maximum=1.0, scale_factor=
self.setTickInterval(self.step_size)

# Connect the QSlider to the QLineEdit
self.sliderPressed.connect(self.update_line_edit)
self.sliderMoved.connect(self.update_line_edit)
self.sliderReleased.connect(self.update_line_edit)
self.sliderPressed.connect(self.updateLineEdit)
self.sliderMoved.connect(self.updateLineEdit)
self.sliderReleased.connect(self.updateLineEdit)

# Configure the QDoubleValidator and QLineEdit
self.validator = QtGui.QDoubleValidator()
Expand All @@ -54,30 +54,30 @@ def __init__(self, line_edit, max_label, minimum=0.0, maximum=1.0, scale_factor=
self.line_edit.setPlaceholderText(str(self.minimum))

# Connect the QLineEdit to the QSlider
self.line_edit.textEdited.connect(self.update_slider)
self.line_edit.returnPressed.connect(self.update_slider)
self.line_edit.textEdited.connect(self.updateSlider)
self.line_edit.returnPressed.connect(self.updateSlider)

# Configure QLabel to show maximum QSlider value
self.max_label = max_label
self.max_label.setAlignment(QtCore.Qt.AlignRight)
self.max_label.setText(str(self.maximum))

def get_slider_value(self):
def getSliderValue(self):
return self.value()

def get_line_edit_value(self):
def getLineEditValue(self):
return float(self.line_edit.text())

def update_slider(self):
def updateSlider(self):
state = self.validator.validate(self.line_edit.text(), 0)
if state[0] == QtGui.QDoubleValidator.Acceptable:
line_edit_value = self.get_line_edit_value()
line_edit_value = self.getLineEditValue()
self.setValue(line_edit_value)
else:
self.line_edit.setText(str(self.minimum))
line_edit_value = self.get_line_edit_value()
line_edit_value = self.getLineEditValue()
self.setValue(line_edit_value)

def update_line_edit(self):
slider_value = str(float(self.get_slider_value()))
def updateLineEdit(self):
slider_value = str(float(self.getSliderValue()))
self.line_edit.setText(slider_value)
15 changes: 8 additions & 7 deletions examples/dialog_example_uislider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ def openFormDialog(self):
scale_factor=10.0)

# add to the form widget
dialog.addWidget(uislider, 'QSlider 1:', 'input_slider1')
dialog.addWidget(uislider, 'UISlider 1:', 'input_slider1')
dialog.addWidget(max_label, '', 'input_max_label1')
dialog.addWidget(line_edit, '', 'input_line_edit1')

# add input 2 as UISliderLineEditWidget
uislider = UISliderLEditWidget.UISliderLEditWidget(minimum=0.0, maximum=100.0,
step_size=1.0)
scale_factor=10.0)

# add to the form widget
dialog.addWidget(uislider, 'QSlider 2:', 'input_slider2')
dialog.addWidget(uislider, 'UISlider 2:', 'input_slider2')

# store a reference
self.dialog = dialog
Expand All @@ -52,11 +52,12 @@ def openFormDialog(self):

def accepted(self):
print("accepted")
print(f"QSlider 1 Value: {self.dialog.widgets['input_slider1_field'].value()}")
print(f"QLabel 1 Value: {self.dialog.widgets['input_max_label1_field'].text()}")
print(f"QLineEdit 1 Value: {self.dialog.widgets['input_line_edit1_field'].text()}")
print(f"UISlider 1 QSlider: {self.dialog.widgets['input_slider1_field'].value()}")
print(f"QSlider 1 QLabel: {self.dialog.widgets['input_max_label1_field'].text()}")
print(f"QSlider 1 QLineEdit: {self.dialog.widgets['input_line_edit1_field'].text()}")

# print(f"QSlider 2 Value: {self.dialog.widgets['input_slider2_field'].value()}")
print(f"UISlider 2 QSlider: {self.dialog.widgets['input_slider2_field'].getSliderValue()}")
print(f"UISlider 2 QLineEdit: {self.dialog.widgets['input_slider2_field'].getValue()}")

self.dialog.close()

Expand Down

0 comments on commit f7517ab

Please sign in to comment.