Skip to content

Commit

Permalink
Fix slider alignment in BrightnessContrastDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Jun 13, 2024
1 parent 9d79778 commit d8e10f5
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions labelme/widgets/brightness_contrast_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,39 @@ def __init__(self, img, callback, parent=None):
self.setModal(True)
self.setWindowTitle("Brightness/Contrast")

self.slider_brightness, widget_brightness = self._create_slider()
self.slider_contrast, widget_contrast = self._create_slider()
sliders = {}
layouts = {}
for title in ["Brightness:", "Contrast:"]:
layout = QtWidgets.QHBoxLayout()
title_label = QtWidgets.QLabel(self.tr(title))
title_label.setFixedWidth(75)
layout.addWidget(title_label)
#
slider = QtWidgets.QSlider(Qt.Horizontal)
slider.setRange(0, 3 * self._base_value)
slider.setValue(self._base_value)
layout.addWidget(slider)
#
value_label = QtWidgets.QLabel(f"{slider.value() / self._base_value:.2f}")
value_label.setAlignment(Qt.AlignRight)
layout.addWidget(value_label)
#
slider.valueChanged.connect(self.onNewValue)
slider.valueChanged.connect(
lambda: value_label.setText(f"{slider.value() / self._base_value:.2f}")
)
layouts[title] = layout
sliders[title] = slider

form_layout = QtWidgets.QFormLayout()
form_layout.addRow(self.tr("Brightness"), widget_brightness)
form_layout.addRow(self.tr("Contrast"), widget_contrast)
self.setLayout(form_layout)
self.slider_brightness = sliders["Brightness:"]
self.slider_contrast = sliders["Contrast:"]
del sliders

layout = QtWidgets.QVBoxLayout()
layout.addLayout(layouts["Brightness:"])
layout.addLayout(layouts["Contrast:"])
del layouts
self.setLayout(layout)

assert isinstance(img, PIL.Image.Image)
self.img = img
Expand All @@ -39,19 +65,3 @@ def onNewValue(self, _):
img.tobytes(), img.width, img.height, img.width * 3, QImage.Format_RGB888
)
self.callback(qimage)

def _create_slider(self):
slider = QtWidgets.QSlider(Qt.Horizontal)
slider.setRange(0, 3 * self._base_value)
slider.setValue(self._base_value)
slider.valueChanged.connect(self.onNewValue)
value_label = QtWidgets.QLabel(f"{slider.value() / self._base_value:.2f}")
slider.valueChanged.connect(
lambda value: value_label.setText(f"{value / self._base_value:.2f}")
)
layout = QtWidgets.QHBoxLayout()
layout.addWidget(slider)
layout.addWidget(value_label)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
return slider, widget

0 comments on commit d8e10f5

Please sign in to comment.