from __future__ import annotations import os os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") import pytest from PySide6.QtCore import QPoint, Qt from PySide6.QtTest import QTest from PySide6.QtWidgets import QApplication, QLabel from doctor_workstation.ui.diagnosis_index_widgets import DiagnosisTableHost @pytest.fixture(scope="module") def application() -> QApplication: return QApplication.instance() or QApplication([]) @pytest.mark.parametrize("tech_blue", [True, False]) def test_patient_name_can_be_drag_selected_and_copied_without_opening_or_editing( application: QApplication, tech_blue: bool, ) -> None: host = DiagnosisTableHost(tech_blue=tech_blue) host.resize(1400, 400) rows = [{"id": 1, "patient_name": "测试甲"}, {"id": 2, "patient_name": "测试乙"}] host.set_rows(rows) opened = [] host.main.itemDoubleClicked.connect(opened.append) host.show() for _ in range(10): application.processEvents() label = host.main.indexWidget(host.model.index(1, 2)) assert isinstance(label, QLabel) start = QPoint(label.contentsMargins().left(), label.height() // 2) end = QPoint(start.x() + label.fontMetrics().horizontalAdvance(label.text()) + 2, start.y()) QTest.mousePress(label, Qt.MouseButton.LeftButton, pos=start) QTest.mouseMove(label, end, 30) QTest.mouseRelease(label, Qt.MouseButton.LeftButton, pos=end) assert label.selectedText() == "测试乙" assert host.main.currentRow() == 1 QTest.keyClick(label, Qt.Key.Key_C, Qt.KeyboardModifier.ControlModifier) assert application.clipboard().text() == "测试乙" QTest.keyClicks(label, "renamed") assert label.text() == "测试乙" assert host.model.rows == rows assert host.selected_records() == [] # Double-clicking text must stay in text selection, not open the diagnosis. QTest.mouseDClick(label, Qt.MouseButton.LeftButton, pos=QPoint(start.x() + 7, start.y())) assert label.hasSelectedText() assert opened == [] # An unchanged poll preserves the live text selection and label instance. selected = label.selectedText() host.set_rows([dict(row) for row in rows]) assert host.main.indexWidget(host.model.index(1, 2)) is label assert label.selectedText() == selected host.close() host.deleteLater() application.processEvents()