From a7e9fb91a2722582544f8e0eab4e4f27fe064ce0 Mon Sep 17 00:00:00 2001 From: YektaY Date: Thu, 26 Sep 2024 15:29:33 -0700 Subject: [PATCH 1/7] fixed too issues with the slider. one bring able to click on the handle of the slider to be able to drag it and the other to be able to middle click and see the PV name attached to the slider --- pydm/widgets/slider.py | 210 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 191 insertions(+), 19 deletions(-) diff --git a/pydm/widgets/slider.py b/pydm/widgets/slider.py index 85ed080d4..f018c551b 100644 --- a/pydm/widgets/slider.py +++ b/pydm/widgets/slider.py @@ -1,7 +1,7 @@ import logging import numpy as np from decimal import Decimal -from qtpy.QtCore import Qt, Signal, Slot, Property +from qtpy.QtCore import Qt, Signal, Slot, Property, QRect, QPoint, QSize from qtpy.QtWidgets import ( QFrame, QLabel, @@ -26,41 +26,211 @@ class PyDMPrimitiveSlider(QSlider): + # middleClicked = pyqtSignal() + + def __init__(self, orientation=Qt.Horizontal, parent=None): + super().__init__(orientation, parent) + self.isDraggingHandle = False + self.dragStartPos = None + self.dragStartValue = None + def mousePressEvent(self, event): + """ + Handle mouse press events on the slider. + + Parameters + ---------- + event : QMouseEvent + The mouse event containing information about the press. + """ if event.button() == Qt.MiddleButton: return if event.button() == Qt.RightButton: super().mousePressEvent(event) + return if event.button() == Qt.LeftButton: - if self.orientation() == Qt.Horizontal: - handle_pos = self.value() * (self.width() - self.handleWidth()) / (self.maximum() - self.minimum()) - click_pos = event.pos().x() + handle_rect = self.getHandleRect() + + if handle_rect.contains(event.pos()): + self.isDraggingHandle = True + self.dragStartPos = event.pos() + self.dragStartValue = self.value() + self.setCursor(Qt.ClosedHandCursor) + event.accept() else: - handle_pos = ( - (self.maximum() - self.value()) - * (self.height() - self.handleWidth()) - / (self.maximum() - self.minimum()) - ) - click_pos = event.pos().y() + handle_pos, click_pos = self.getPositions(event) - if self.orientation() == Qt.Horizontal: - if click_pos > handle_pos + self.handleWidth() / 2: + if self.shouldIncrement(handle_pos, click_pos): self.setValue(self.value() + self.singleStep()) else: self.setValue(self.value() - self.singleStep()) + + def mouseMoveEvent(self, event): + """ + Handle mouse move events to update the slider value during dragging. + + Parameters + ---------- + event : QMouseEvent + The mouse event containing information about the movement. + """ + if self.isDraggingHandle: + delta = event.pos() - self.dragStartPos + + if self.orientation() == Qt.Horizontal: + delta_value = (delta.x() / self.getSliderLength()) * (self.maximum() - self.minimum()) else: - if click_pos < handle_pos + self.handleWidth() / 2: - self.setValue(self.value() + self.singleStep()) - else: - self.setValue(self.value() - self.singleStep()) + delta_value = (-delta.y() / self.getSliderLength()) * (self.maximum() - self.minimum()) + + new_value = self.dragStartValue + delta_value + new_value = min(max(self.minimum(), new_value), self.maximum()) + self.setValue(new_value) + event.accept() + else: + super().mouseMoveEvent(event) + + def mouseReleaseEvent(self, event): + """ + Handle mouse release events to stop dragging. + + Parameters + ---------- + event : QMouseEvent + The mouse event containing information about the release. + """ + if event.button() == Qt.LeftButton and self.isDraggingHandle: + self.isDraggingHandle = False + self.setCursor(Qt.ArrowCursor) + event.accept() + else: + super().mouseReleaseEvent(event) + + def getHandleRect(self): + """ + Calculate the rectangle representing the slider handle's position and size. + + Returns + ------- + QRect + The rectangle of the slider handle. + """ + handle_size = self.getHandleSize() + slider_pos = self.getSliderPosition() + + if self.orientation() == Qt.Horizontal: + x = slider_pos - handle_size.width() / 2 + y = (self.height() - handle_size.height()) / 2 + rect = QRect(QPoint(int(x), int(y)), handle_size) + else: + x = (self.width() - handle_size.width()) / 2 + y = slider_pos - handle_size.height() / 2 + rect = QRect(QPoint(int(x), int(y)), handle_size) + + return rect + + def getPositions(self, event): + """ + Retrieve the handle position and the click position based on the orientation. + + Parameters + ---------- + event : QMouseEvent + The mouse event containing information about the press. + + Returns + ------- + tuple of float + A tuple containing the handle position and the click position. + """ + slider_pos = self.getSliderPosition() + + if self.orientation() == Qt.Horizontal: + handle_pos = slider_pos + click_pos = event.pos().x() + else: + handle_pos = slider_pos + click_pos = event.pos().y() + + return handle_pos, click_pos + + def shouldIncrement(self, handle_pos, click_pos): + """ + Determine whether the slider value should be incremented based on positions. + + Parameters + ---------- + handle_pos : float + The position of the slider handle. + click_pos : float + The position where the user clicked. + + Returns + ------- + bool + True if the slider value should be incremented, False otherwise. + """ + if self.orientation() == Qt.Horizontal: + return click_pos > handle_pos + else: + return click_pos < handle_pos + + def getHandleSize(self): + """ + Compute the size of the slider handle. + + Returns + ------- + QSize + The size of the slider handle. + """ + handle_length = 20 # Fixed length for the handle + if self.orientation() == Qt.Horizontal: + return QSize(handle_length, self.height() / 2) + else: + return QSize(self.width() / 2, handle_length) + + def getSliderLength(self): + """ + Calculate the usable length of the slider track excluding the handle size. + + Returns + ------- + float + The length of the slider track. + """ + handle_size = self.getHandleSize() + if self.orientation() == Qt.Horizontal: + return self.width() - handle_size.width() + else: + return self.height() - handle_size.height() + + def getSliderPosition(self): + """ + Compute the position of the slider handle along the track. + + Returns + ------- + float + The position of the slider handle along the slider track. + """ + slider_min = self.minimum() + slider_max = self.maximum() + slider_range = slider_max - slider_min + slider_value = self.value() - slider_min + + if slider_range == 0: + proportion = 0 + else: + proportion = slider_value / slider_range - def handleWidth(self): if self.orientation() == Qt.Horizontal: - return self.style().pixelMetric(self.style().PM_SliderLength, None, self) + slider_length = self.getSliderLength() + return proportion * slider_length + self.getHandleSize().width() / 2 else: - return self.style().pixelMetric(self.style().PM_SliderThickness, None, self) + slider_length = self.getSliderLength() + return (1 - proportion) * slider_length + self.getHandleSize().height() / 2 class PyDMSlider(QFrame, TextFormatter, PyDMWritableWidget, new_properties=_step_size_properties): @@ -118,6 +288,8 @@ def __init__(self, parent=None, init_channel=None): self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = PyDMPrimitiveSlider(parent=self) + self._slider.installEventFilter(self) + # self._slider.middleClicked.connect() self._slider.setOrientation(Qt.Horizontal) self._orig_wheel_event = self._slider.wheelEvent From 046866a8df1819daf2ba2f2c3c9361751465c504 Mon Sep 17 00:00:00 2001 From: YektaY Date: Thu, 26 Sep 2024 15:35:46 -0700 Subject: [PATCH 2/7] removed commented out code --- pydm/widgets/slider.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pydm/widgets/slider.py b/pydm/widgets/slider.py index f018c551b..d5779e625 100644 --- a/pydm/widgets/slider.py +++ b/pydm/widgets/slider.py @@ -26,8 +26,6 @@ class PyDMPrimitiveSlider(QSlider): - # middleClicked = pyqtSignal() - def __init__(self, orientation=Qt.Horizontal, parent=None): super().__init__(orientation, parent) self.isDraggingHandle = False From 55fff5813e2a9957a51169cf2ba41e24db12d89e Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 27 Sep 2024 15:38:26 -0700 Subject: [PATCH 3/7] added unit tests for thePyDMPrimitiveSlider class --- pydm/tests/widgets/test_slider.py | 137 ++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 8 deletions(-) diff --git a/pydm/tests/widgets/test_slider.py b/pydm/tests/widgets/test_slider.py index ba12cbb77..ab99a7d5c 100644 --- a/pydm/tests/widgets/test_slider.py +++ b/pydm/tests/widgets/test_slider.py @@ -1,19 +1,140 @@ -# Unit Tests for the PyDMSlider Widget - import pytest from logging import ERROR import numpy as np -from qtpy.QtWidgets import QLabel, QVBoxLayout, QHBoxLayout, QSizePolicy -from qtpy.QtCore import Qt, QMargins - +from qtpy.QtWidgets import QLabel, QVBoxLayout, QHBoxLayout, QSizePolicy, QApplication +from qtpy.QtCore import Qt, QMargins, QPoint, QEvent, QRect +from qtpy.QtGui import QMouseEvent from ...widgets.slider import PyDMSlider, PyDMPrimitiveSlider from ...widgets.base import PyDMWidget +# Unit Tests for the PyDMPrimitiveSlider class -# -------------------- -# POSITIVE TEST CASES -# -------------------- + +@pytest.fixture(scope="module") +def app(): + """Fixture to create a QApplication instance.""" + app = QApplication.instance() + if app is None: + app = QApplication([]) + return app + + +@pytest.fixture +def test_slider(app): + """Fixture to create a PyDMPrimitiveSlider instance for each test.""" + test_slider = PyDMPrimitiveSlider(Qt.Horizontal) + test_slider.setMinimum(0) + test_slider.setMaximum(100) + test_slider.setValue(50) + test_slider.setSingleStep(1) + test_slider.resize(200, 30) + test_slider.show() + return test_slider + + +def test_mousePressEvent(test_slider, qtbot): + """Test mousePressEvent when clicking off and on the handle""" + handle_rect = test_slider.getHandleRect() + pos_off_handle = QPoint(handle_rect.right() + 10, handle_rect.center().y()) + qtbot.mouseClick(test_slider, Qt.LeftButton, pos=pos_off_handle) + assert not test_slider.isDraggingHandle + assert test_slider.value() == 51 + + pos_on_handle = handle_rect.center() + qtbot.mousePress(test_slider, Qt.LeftButton, pos=pos_on_handle) + assert test_slider.isDraggingHandle + assert test_slider.dragStartValue == 51 + + +def test_mouseMoveEvent(test_slider, qtbot): + """Test the mouseMoveEvent method by posting QMouseEvent instances.""" + handle_rect = test_slider.getHandleRect() + start_pos = handle_rect.center() + drag_distance = 100 + end_pos = QPoint(start_pos.x() + drag_distance, start_pos.y()) + + initial_value = test_slider.value() + + press_event = QMouseEvent(QEvent.MouseButtonPress, start_pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) + QApplication.postEvent(test_slider, press_event) + QApplication.processEvents() + + move_event = QMouseEvent(QEvent.MouseMove, end_pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) + QApplication.postEvent(test_slider, move_event) + QApplication.processEvents() + + release_event = QMouseEvent(QEvent.MouseButtonRelease, end_pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) + QApplication.postEvent(test_slider, release_event) + QApplication.processEvents() + + actual_value = test_slider.value() + + assert actual_value != initial_value + + +def test_mouseReleaseEvent(test_slider, qtbot): + """Test mouseReleaseEvent to stop dragging.""" + handle_rect = test_slider.getHandleRect() + pos_on_handle = handle_rect.center() + qtbot.mousePress(test_slider, Qt.LeftButton, pos=pos_on_handle) + assert test_slider.isDraggingHandle + + qtbot.mouseRelease(test_slider, Qt.LeftButton, pos=pos_on_handle) + + assert not test_slider.isDraggingHandle + assert test_slider.cursor().shape() == Qt.ArrowCursor + + +def test_getHandleRect(test_slider): + """Test getHandleRect method.""" + handle_rect = test_slider.getHandleRect() + assert isinstance(handle_rect, QRect) + assert test_slider.rect().contains(handle_rect) + + +def test_getPositions(test_slider): + """Test getPositions method.""" + event = QMouseEvent(QEvent.MouseButtonPress, QPoint(50, 10), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) + handle_pos, click_pos = test_slider.getPositions(event) + assert isinstance(handle_pos, float) + assert isinstance(click_pos, int) + assert 0 <= click_pos <= test_slider.width() + + +def test_shouldIncrement(test_slider): + """Test shouldIncrement method.""" + + # Click is to the right of the handle + assert test_slider.shouldIncrement(50, 70) is True + + # Click is to the left of the handle + assert test_slider.shouldIncrement(70, 50) is False + + +def test_getHandleSize(test_slider): + """Test getHandleSize method.""" + handle_size = test_slider.getHandleSize() + assert handle_size is not None + assert isinstance(handle_size.width(), int) + assert isinstance(handle_size.height(), int) + + +def test_getSliderLength(test_slider): + """Test getSliderLength method.""" + slider_length = test_slider.getSliderLength() + assert isinstance(slider_length, int) + assert slider_length < test_slider.width() + + +def test_getSliderPosition(test_slider): + """Test getSliderPosition method.""" + slider_position = test_slider.getSliderPosition() + assert isinstance(slider_position, float) + assert 0 <= slider_position <= test_slider.width() + + +# Unit Tests for the PyDMSlider Widget def test_construct(qtbot): From 9d846282f9ccee6f7d49b77fe671fca6ff11d34e Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 27 Sep 2024 15:47:22 -0700 Subject: [PATCH 4/7] switching ti interger division to address python 3.10 issue --- pydm/widgets/slider.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pydm/widgets/slider.py b/pydm/widgets/slider.py index d5779e625..4080717d7 100644 --- a/pydm/widgets/slider.py +++ b/pydm/widgets/slider.py @@ -185,9 +185,9 @@ def getHandleSize(self): """ handle_length = 20 # Fixed length for the handle if self.orientation() == Qt.Horizontal: - return QSize(handle_length, self.height() / 2) + return QSize(handle_length, self.height() // 2) else: - return QSize(self.width() / 2, handle_length) + return QSize(self.width() // 2, handle_length) def getSliderLength(self): """ From 72a83793bbb1d63a0c717fdedf44689e5c23262e Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 4 Oct 2024 15:57:11 -0700 Subject: [PATCH 5/7] changes to tests to include vertical sliders and added a couple comments to the slider. all changes are from requestsand notes which came up during code review --- pydm/tests/widgets/test_slider.py | 115 +++++++++++++++++++++--------- pydm/widgets/slider.py | 15 ++-- 2 files changed, 92 insertions(+), 38 deletions(-) diff --git a/pydm/tests/widgets/test_slider.py b/pydm/tests/widgets/test_slider.py index ab99a7d5c..5f321926e 100644 --- a/pydm/tests/widgets/test_slider.py +++ b/pydm/tests/widgets/test_slider.py @@ -3,14 +3,13 @@ import numpy as np from qtpy.QtWidgets import QLabel, QVBoxLayout, QHBoxLayout, QSizePolicy, QApplication -from qtpy.QtCore import Qt, QMargins, QPoint, QEvent, QRect +from qtpy.QtCore import Qt, QMargins, QPoint, QEvent, QRect, QSize from qtpy.QtGui import QMouseEvent from ...widgets.slider import PyDMSlider, PyDMPrimitiveSlider from ...widgets.base import PyDMWidget # Unit Tests for the PyDMPrimitiveSlider class - @pytest.fixture(scope="module") def app(): """Fixture to create a QApplication instance.""" @@ -21,7 +20,7 @@ def app(): @pytest.fixture -def test_slider(app): +def horizontal_slider(app): """Fixture to create a PyDMPrimitiveSlider instance for each test.""" test_slider = PyDMPrimitiveSlider(Qt.Horizontal) test_slider.setMinimum(0) @@ -32,28 +31,53 @@ def test_slider(app): test_slider.show() return test_slider +@pytest.fixture +def vertical_slider(app): + """Fixture to create a vertical PyDMPrimitiveSlider instance for each test.""" + test_slider = PyDMPrimitiveSlider(Qt.Vertical) + test_slider.setMinimum(0) + test_slider.setMaximum(100) + test_slider.setValue(50) + test_slider.setSingleStep(1) + test_slider.resize(30, 200) + test_slider.show() + return test_slider -def test_mousePressEvent(test_slider, qtbot): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_mousePressEvent(slider_fixture, qtbot, request): """Test mousePressEvent when clicking off and on the handle""" + test_slider = request.getfixturevalue(slider_fixture) handle_rect = test_slider.getHandleRect() - pos_off_handle = QPoint(handle_rect.right() + 10, handle_rect.center().y()) + + if test_slider.orientation() == Qt.Horizontal: + pos_off_handle = QPoint(handle_rect.right() + 10, handle_rect.center().y()) + increment = 1 + else: # Vertical + pos_off_handle = QPoint(handle_rect.center().x(), handle_rect.bottom() + 10) + increment = -1 + qtbot.mouseClick(test_slider, Qt.LeftButton, pos=pos_off_handle) assert not test_slider.isDraggingHandle - assert test_slider.value() == 51 + assert test_slider.value() == 50 + increment pos_on_handle = handle_rect.center() qtbot.mousePress(test_slider, Qt.LeftButton, pos=pos_on_handle) assert test_slider.isDraggingHandle - assert test_slider.dragStartValue == 51 + assert test_slider.dragStartValue == test_slider.value() - -def test_mouseMoveEvent(test_slider, qtbot): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_mouseMoveEvent(slider_fixture, qtbot, request): """Test the mouseMoveEvent method by posting QMouseEvent instances.""" + test_slider = request.getfixturevalue(slider_fixture) handle_rect = test_slider.getHandleRect() start_pos = handle_rect.center() drag_distance = 100 - end_pos = QPoint(start_pos.x() + drag_distance, start_pos.y()) + if test_slider.orientation() == Qt.Horizontal: + end_pos = QPoint(start_pos.x() + drag_distance, start_pos.y()) + else: + end_pos = QPoint(start_pos.x(), start_pos.y() - drag_distance) + initial_value = test_slider.value() press_event = QMouseEvent(QEvent.MouseButtonPress, start_pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) @@ -71,10 +95,12 @@ def test_mouseMoveEvent(test_slider, qtbot): actual_value = test_slider.value() assert actual_value != initial_value + assert actual_value == 100 - -def test_mouseReleaseEvent(test_slider, qtbot): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_mouseReleaseEvent(slider_fixture, qtbot, request): """Test mouseReleaseEvent to stop dragging.""" + test_slider = request.getfixturevalue(slider_fixture) handle_rect = test_slider.getHandleRect() pos_on_handle = handle_rect.center() qtbot.mousePress(test_slider, Qt.LeftButton, pos=pos_on_handle) @@ -85,54 +111,79 @@ def test_mouseReleaseEvent(test_slider, qtbot): assert not test_slider.isDraggingHandle assert test_slider.cursor().shape() == Qt.ArrowCursor - -def test_getHandleRect(test_slider): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_getHandleRect(slider_fixture, request): """Test getHandleRect method.""" + test_slider = request.getfixturevalue(slider_fixture) handle_rect = test_slider.getHandleRect() assert isinstance(handle_rect, QRect) assert test_slider.rect().contains(handle_rect) - -def test_getPositions(test_slider): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_getPositions(slider_fixture, request): """Test getPositions method.""" + test_slider = request.getfixturevalue(slider_fixture) event = QMouseEvent(QEvent.MouseButtonPress, QPoint(50, 10), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) handle_pos, click_pos = test_slider.getPositions(event) assert isinstance(handle_pos, float) assert isinstance(click_pos, int) - assert 0 <= click_pos <= test_slider.width() - + if test_slider.orientation() == Qt.Horizontal: + assert 0 <= click_pos <= test_slider.width() + else: + assert 0 <= click_pos <= test_slider.height() -def test_shouldIncrement(test_slider): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_shouldIncrement(slider_fixture, request): """Test shouldIncrement method.""" + test_slider = request.getfixturevalue(slider_fixture) - # Click is to the right of the handle - assert test_slider.shouldIncrement(50, 70) is True - - # Click is to the left of the handle - assert test_slider.shouldIncrement(70, 50) is False + if test_slider.orientation() == Qt.Horizontal: + # Click is to the right of the handle + assert test_slider.shouldIncrement(50, 70) is True + # Click is to the left of the handle + assert test_slider.shouldIncrement(70, 50) is False + else: + # Click is above the handle (smaller y) + assert test_slider.shouldIncrement(70, 50) is True + # Click is below the handle (larger y) + assert test_slider.shouldIncrement(50, 70) is False -def test_getHandleSize(test_slider): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_getHandleSize(slider_fixture, request): """Test getHandleSize method.""" + test_slider = request.getfixturevalue(slider_fixture) handle_size = test_slider.getHandleSize() assert handle_size is not None assert isinstance(handle_size.width(), int) assert isinstance(handle_size.height(), int) + + if test_slider.orientation() == Qt.Horizontal: + assert handle_size == QSize(20, test_slider.height() // 2) + else: + assert handle_size == QSize(test_slider.width() // 2, 20) - -def test_getSliderLength(test_slider): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_getSliderLength(slider_fixture, request): """Test getSliderLength method.""" + test_slider = request.getfixturevalue(slider_fixture) slider_length = test_slider.getSliderLength() assert isinstance(slider_length, int) - assert slider_length < test_slider.width() - + if test_slider.orientation() == Qt.Horizontal: + assert slider_length <= test_slider.width() + else: + assert slider_length <= test_slider.height() -def test_getSliderPosition(test_slider): +@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +def test_getSliderPosition(slider_fixture, request): """Test getSliderPosition method.""" + test_slider = request.getfixturevalue(slider_fixture) slider_position = test_slider.getSliderPosition() assert isinstance(slider_position, float) - assert 0 <= slider_position <= test_slider.width() - + if test_slider.orientation() == Qt.Horizontal: + assert 0 <= slider_position <= test_slider.width() + else: + assert 0 <= slider_position <= test_slider.height() # Unit Tests for the PyDMSlider Widget diff --git a/pydm/widgets/slider.py b/pydm/widgets/slider.py index 4080717d7..d56c97e9d 100644 --- a/pydm/widgets/slider.py +++ b/pydm/widgets/slider.py @@ -143,12 +143,11 @@ def getPositions(self, event): A tuple containing the handle position and the click position. """ slider_pos = self.getSliderPosition() - - if self.orientation() == Qt.Horizontal: - handle_pos = slider_pos + handle_pos = slider_pos + + if self.orientation() == Qt.Horizontal: click_pos = event.pos().x() else: - handle_pos = slider_pos click_pos = event.pos().y() return handle_pos, click_pos @@ -228,6 +227,7 @@ def getSliderPosition(self): return proportion * slider_length + self.getHandleSize().width() / 2 else: slider_length = self.getSliderLength() + # 1 - proportion, because the largest positional value is at the bottom of the slider. return (1 - proportion) * slider_length + self.getHandleSize().height() / 2 @@ -286,8 +286,11 @@ def __init__(self, parent=None, init_channel=None): self.high_lim_label.setSizePolicy(label_size_policy) self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = PyDMPrimitiveSlider(parent=self) + + # Pass PyDMPrimitiveWidget eventfilter to self._slider so it will have the tooltip display + # the address name from middle clicking on the widget self._slider.installEventFilter(self) - # self._slider.middleClicked.connect() + self._slider.setOrientation(Qt.Horizontal) self._orig_wheel_event = self._slider.wheelEvent @@ -297,7 +300,7 @@ def __init__(self, parent=None, init_channel=None): self._slider.sliderPressed.connect(self.internal_slider_pressed) self._slider.sliderReleased.connect(self.internal_slider_released) self._slider.valueChanged.connect(self.internal_slider_value_changed) - # self.vertical_layout.addWidget(self._slider) + # Other internal variables and final setup steps self._slider_position_to_value_map = None self._mute_internal_slider_changes = False From 9a0503e6cced5026bf2485e0f89a62121d9c8454 Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 4 Oct 2024 16:00:17 -0700 Subject: [PATCH 6/7] ran pre-commit to fix formatting --- pydm/tests/widgets/test_slider.py | 33 ++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/pydm/tests/widgets/test_slider.py b/pydm/tests/widgets/test_slider.py index 5f321926e..1cea688bd 100644 --- a/pydm/tests/widgets/test_slider.py +++ b/pydm/tests/widgets/test_slider.py @@ -10,6 +10,7 @@ # Unit Tests for the PyDMPrimitiveSlider class + @pytest.fixture(scope="module") def app(): """Fixture to create a QApplication instance.""" @@ -31,6 +32,7 @@ def horizontal_slider(app): test_slider.show() return test_slider + @pytest.fixture def vertical_slider(app): """Fixture to create a vertical PyDMPrimitiveSlider instance for each test.""" @@ -43,7 +45,8 @@ def vertical_slider(app): test_slider.show() return test_slider -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_mousePressEvent(slider_fixture, qtbot, request): """Test mousePressEvent when clicking off and on the handle""" test_slider = request.getfixturevalue(slider_fixture) @@ -65,7 +68,8 @@ def test_mousePressEvent(slider_fixture, qtbot, request): assert test_slider.isDraggingHandle assert test_slider.dragStartValue == test_slider.value() -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_mouseMoveEvent(slider_fixture, qtbot, request): """Test the mouseMoveEvent method by posting QMouseEvent instances.""" test_slider = request.getfixturevalue(slider_fixture) @@ -77,7 +81,7 @@ def test_mouseMoveEvent(slider_fixture, qtbot, request): end_pos = QPoint(start_pos.x() + drag_distance, start_pos.y()) else: end_pos = QPoint(start_pos.x(), start_pos.y() - drag_distance) - + initial_value = test_slider.value() press_event = QMouseEvent(QEvent.MouseButtonPress, start_pos, Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) @@ -97,7 +101,8 @@ def test_mouseMoveEvent(slider_fixture, qtbot, request): assert actual_value != initial_value assert actual_value == 100 -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_mouseReleaseEvent(slider_fixture, qtbot, request): """Test mouseReleaseEvent to stop dragging.""" test_slider = request.getfixturevalue(slider_fixture) @@ -111,7 +116,8 @@ def test_mouseReleaseEvent(slider_fixture, qtbot, request): assert not test_slider.isDraggingHandle assert test_slider.cursor().shape() == Qt.ArrowCursor -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_getHandleRect(slider_fixture, request): """Test getHandleRect method.""" test_slider = request.getfixturevalue(slider_fixture) @@ -119,7 +125,8 @@ def test_getHandleRect(slider_fixture, request): assert isinstance(handle_rect, QRect) assert test_slider.rect().contains(handle_rect) -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_getPositions(slider_fixture, request): """Test getPositions method.""" test_slider = request.getfixturevalue(slider_fixture) @@ -132,7 +139,8 @@ def test_getPositions(slider_fixture, request): else: assert 0 <= click_pos <= test_slider.height() -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_shouldIncrement(slider_fixture, request): """Test shouldIncrement method.""" test_slider = request.getfixturevalue(slider_fixture) @@ -149,7 +157,7 @@ def test_shouldIncrement(slider_fixture, request): assert test_slider.shouldIncrement(50, 70) is False -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_getHandleSize(slider_fixture, request): """Test getHandleSize method.""" test_slider = request.getfixturevalue(slider_fixture) @@ -157,13 +165,14 @@ def test_getHandleSize(slider_fixture, request): assert handle_size is not None assert isinstance(handle_size.width(), int) assert isinstance(handle_size.height(), int) - + if test_slider.orientation() == Qt.Horizontal: assert handle_size == QSize(20, test_slider.height() // 2) else: assert handle_size == QSize(test_slider.width() // 2, 20) -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_getSliderLength(slider_fixture, request): """Test getSliderLength method.""" test_slider = request.getfixturevalue(slider_fixture) @@ -174,7 +183,8 @@ def test_getSliderLength(slider_fixture, request): else: assert slider_length <= test_slider.height() -@pytest.mark.parametrize("slider_fixture", ['horizontal_slider', 'vertical_slider']) + +@pytest.mark.parametrize("slider_fixture", ["horizontal_slider", "vertical_slider"]) def test_getSliderPosition(slider_fixture, request): """Test getSliderPosition method.""" test_slider = request.getfixturevalue(slider_fixture) @@ -185,6 +195,7 @@ def test_getSliderPosition(slider_fixture, request): else: assert 0 <= slider_position <= test_slider.height() + # Unit Tests for the PyDMSlider Widget From ce67f9b56e4f25249ac3c907b33d4ee63c5f2341 Mon Sep 17 00:00:00 2001 From: YektaY Date: Fri, 4 Oct 2024 16:08:41 -0700 Subject: [PATCH 7/7] ran pre-commit again :--/ to fix formatting --- pydm/widgets/slider.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pydm/widgets/slider.py b/pydm/widgets/slider.py index d56c97e9d..041120bad 100644 --- a/pydm/widgets/slider.py +++ b/pydm/widgets/slider.py @@ -144,8 +144,8 @@ def getPositions(self, event): """ slider_pos = self.getSliderPosition() handle_pos = slider_pos - - if self.orientation() == Qt.Horizontal: + + if self.orientation() == Qt.Horizontal: click_pos = event.pos().x() else: click_pos = event.pos().y() @@ -227,7 +227,7 @@ def getSliderPosition(self): return proportion * slider_length + self.getHandleSize().width() / 2 else: slider_length = self.getSliderLength() - # 1 - proportion, because the largest positional value is at the bottom of the slider. + # 1 - proportion, because the largest positional value is at the bottom of the slider. return (1 - proportion) * slider_length + self.getHandleSize().height() / 2 @@ -287,10 +287,10 @@ def __init__(self, parent=None, init_channel=None): self.high_lim_label.setAlignment(Qt.AlignRight | Qt.AlignTrailing | Qt.AlignVCenter) self._slider = PyDMPrimitiveSlider(parent=self) - # Pass PyDMPrimitiveWidget eventfilter to self._slider so it will have the tooltip display + # Pass PyDMPrimitiveWidget eventfilter to self._slider so it will have the tooltip display # the address name from middle clicking on the widget self._slider.installEventFilter(self) - + self._slider.setOrientation(Qt.Horizontal) self._orig_wheel_event = self._slider.wheelEvent