mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-14 03:31:50 +02:00
fix: differentiate click and drag for DeviceItem, adapt tests accordingly
This fixes the blocking "QDrag.exec_()" on Linux, indeed before the drag'n'drop operation was started with a simple click and it was waiting for drop forever. Now there are 2 different cases, click or drag'n'drop - the drag'n'drop test actually moves the mouse and releases the button.
This commit is contained in:
@ -5,7 +5,7 @@ from typing import TYPE_CHECKING
|
|||||||
from bec_lib.logger import bec_logger
|
from bec_lib.logger import bec_logger
|
||||||
from qtpy.QtCore import QMimeData, Qt
|
from qtpy.QtCore import QMimeData, Qt
|
||||||
from qtpy.QtGui import QDrag
|
from qtpy.QtGui import QDrag
|
||||||
from qtpy.QtWidgets import QHBoxLayout, QLabel, QWidget
|
from qtpy.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from qtpy.QtGui import QMouseEvent
|
from qtpy.QtGui import QMouseEvent
|
||||||
@ -16,6 +16,9 @@ logger = bec_logger.logger
|
|||||||
class DeviceItem(QWidget):
|
class DeviceItem(QWidget):
|
||||||
def __init__(self, device: str) -> None:
|
def __init__(self, device: str) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
|
self._drag_pos = None
|
||||||
|
|
||||||
self.device = device
|
self.device = device
|
||||||
layout = QHBoxLayout()
|
layout = QHBoxLayout()
|
||||||
layout.setContentsMargins(10, 2, 10, 2)
|
layout.setContentsMargins(10, 2, 10, 2)
|
||||||
@ -32,12 +35,21 @@ class DeviceItem(QWidget):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def mousePressEvent(self, event: QMouseEvent) -> None:
|
def mousePressEvent(self, event: QMouseEvent) -> None:
|
||||||
|
super().mousePressEvent(event)
|
||||||
if event.button() == Qt.LeftButton:
|
if event.button() == Qt.LeftButton:
|
||||||
drag = QDrag(self)
|
self._drag_pos = event.pos()
|
||||||
mime_data = QMimeData()
|
|
||||||
mime_data.setText(self.device)
|
def mouseMoveEvent(self, event: QMouseEvent) -> None:
|
||||||
drag.setMimeData(mime_data)
|
if not (event.buttons() and Qt.LeftButton):
|
||||||
drag.exec_(Qt.MoveAction)
|
return
|
||||||
|
if (event.pos() - self._drag_pos).manhattanLength() < QApplication.startDragDistance():
|
||||||
|
return
|
||||||
|
|
||||||
|
drag = QDrag(self)
|
||||||
|
mime_data = QMimeData()
|
||||||
|
mime_data.setText(self.device)
|
||||||
|
drag.setMimeData(mime_data)
|
||||||
|
drag.exec_(Qt.MoveAction)
|
||||||
|
|
||||||
def mouseDoubleClickEvent(self, event: QMouseEvent) -> None:
|
def mouseDoubleClickEvent(self, event: QMouseEvent) -> None:
|
||||||
logger.debug("Double Clicked")
|
logger.debug("Double Clicked")
|
||||||
|
@ -2,7 +2,7 @@ from typing import TYPE_CHECKING
|
|||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from qtpy.QtCore import Qt
|
from qtpy.QtCore import QPoint, Qt
|
||||||
|
|
||||||
from bec_widgets.widgets.services.device_browser.device_browser import DeviceBrowser
|
from bec_widgets.widgets.services.device_browser.device_browser import DeviceBrowser
|
||||||
|
|
||||||
@ -55,10 +55,10 @@ def test_device_item_mouse_press_event(device_browser, qtbot):
|
|||||||
# Simulate a left mouse press event on the device item
|
# Simulate a left mouse press event on the device item
|
||||||
device_item: QListWidgetItem = device_browser.ui.device_list.itemAt(0, 0)
|
device_item: QListWidgetItem = device_browser.ui.device_list.itemAt(0, 0)
|
||||||
widget: DeviceItem = device_browser.ui.device_list.itemWidget(device_item)
|
widget: DeviceItem = device_browser.ui.device_list.itemWidget(device_item)
|
||||||
qtbot.mousePress(widget.label, Qt.MouseButton.LeftButton)
|
qtbot.mouseClick(widget.label, Qt.MouseButton.LeftButton)
|
||||||
|
|
||||||
|
|
||||||
def test_device_item_mouse_press_event_creates_drag(device_browser, qtbot):
|
def test_device_item_mouse_press_and_move_events_creates_drag(device_browser, qtbot):
|
||||||
"""
|
"""
|
||||||
Test that the mousePressEvent is triggered correctly and initiates a drag.
|
Test that the mousePressEvent is triggered correctly and initiates a drag.
|
||||||
"""
|
"""
|
||||||
@ -67,7 +67,9 @@ def test_device_item_mouse_press_event_creates_drag(device_browser, qtbot):
|
|||||||
device_name = widget.device
|
device_name = widget.device
|
||||||
with mock.patch("qtpy.QtGui.QDrag.exec_") as mock_exec:
|
with mock.patch("qtpy.QtGui.QDrag.exec_") as mock_exec:
|
||||||
with mock.patch("qtpy.QtGui.QDrag.setMimeData") as mock_set_mimedata:
|
with mock.patch("qtpy.QtGui.QDrag.setMimeData") as mock_set_mimedata:
|
||||||
qtbot.mousePress(widget.label, Qt.MouseButton.LeftButton)
|
qtbot.mousePress(widget.label, Qt.MouseButton.LeftButton, pos=QPoint(0, 0))
|
||||||
|
qtbot.mouseMove(widget, pos=QPoint(10, 10))
|
||||||
|
qtbot.mouseRelease(widget, Qt.MouseButton.LeftButton)
|
||||||
mock_set_mimedata.assert_called_once()
|
mock_set_mimedata.assert_called_once()
|
||||||
mock_exec.assert_called_once()
|
mock_exec.assert_called_once()
|
||||||
assert mock_set_mimedata.call_args[0][0].text() == device_name
|
assert mock_set_mimedata.call_args[0][0].text() == device_name
|
||||||
|
Reference in New Issue
Block a user