158 lines
4.7 KiB
Python
158 lines
4.7 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
import pytest
|
|
from qtpy.QtCore import QPoint, QPointF, Qt
|
|
from qtpy.QtWidgets import QApplication
|
|
|
|
from csaxs_bec.bec_widgets.widgets.saxs_widget.models import (
|
|
CameraCenter,
|
|
Correction,
|
|
RawROI,
|
|
SAXSRow,
|
|
corrected_geometry,
|
|
)
|
|
from csaxs_bec.bec_widgets.widgets.saxs_widget.saxs_widget import SAXSWidget
|
|
from csaxs_bec.bec_widgets.widgets.saxs_widget import table as table_module
|
|
from csaxs_bec.bec_widgets.widgets.saxs_widget.table import SAXSTableWidget
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def app():
|
|
return QApplication.instance() or QApplication([])
|
|
|
|
|
|
def _row(name: str, index: int) -> SAXSRow:
|
|
raw_roi = RawROI(x=10 + index, y=20 + index, width=30, height=40)
|
|
correction = Correction(
|
|
camera_scale_pixel_x=0.1,
|
|
offset_cam_xray_x=1.0,
|
|
direction_x=1,
|
|
camera_scale_pixel_y=0.2,
|
|
offset_cam_xray_y=-1.0,
|
|
direction_y=1,
|
|
)
|
|
camera_center = CameraCenter(x=100 + index, y=200 + index)
|
|
geometry = corrected_geometry(raw_roi, correction, camera_center=camera_center)
|
|
return SAXSRow(
|
|
sample_name=name,
|
|
sample_id=f"sample-{index}",
|
|
raw_roi=raw_roi,
|
|
correction=correction,
|
|
geometry=geometry,
|
|
exposure_time=0.05,
|
|
camera_center=camera_center,
|
|
x_positioner="samx",
|
|
y_positioner="samy",
|
|
)
|
|
|
|
|
|
def test_saxs_row_payload_has_no_schema_version():
|
|
row = _row("alpha", 0)
|
|
|
|
assert "schema_version" not in row.to_dict()
|
|
assert "schema_version" not in SAXSRow.help()
|
|
|
|
|
|
class _DropEvent:
|
|
def __init__(self, point: QPoint):
|
|
self._point = point
|
|
self.accepted = False
|
|
self.ignored = False
|
|
|
|
def position(self) -> QPointF:
|
|
return QPointF(self._point)
|
|
|
|
def pos(self) -> QPoint:
|
|
return self._point
|
|
|
|
def acceptProposedAction(self) -> None:
|
|
self.accepted = True
|
|
|
|
def ignore(self) -> None:
|
|
self.ignored = True
|
|
|
|
|
|
class _Drag:
|
|
instances = []
|
|
|
|
def __init__(self, source):
|
|
self.source = source
|
|
self.mime_data = None
|
|
self.executed_with = None
|
|
self.instances.append(self)
|
|
|
|
def setMimeData(self, mime_data) -> None:
|
|
self.mime_data = mime_data
|
|
|
|
def exec(self, supported_actions):
|
|
self.executed_with = supported_actions
|
|
return Qt.DropAction.MoveAction
|
|
|
|
|
|
def test_table_displays_rows_and_updates_selection_actions(app):
|
|
widget = SAXSTableWidget()
|
|
rows = [_row("alpha", 0), _row("beta", 1)]
|
|
|
|
widget.set_rows(rows)
|
|
widget.table.selectRow(1)
|
|
app.processEvents()
|
|
|
|
assert widget.table.rowCount() == 2
|
|
assert widget.table.item(0, 0).text() == "alpha"
|
|
assert widget.table.item(0, 0).flags() & Qt.ItemFlag.ItemIsDragEnabled
|
|
assert widget.selected_rows() == [1]
|
|
assert widget.selected_model_rows() == [rows[1]]
|
|
assert widget.move_to_selected_button.isEnabled()
|
|
|
|
|
|
def test_table_start_drag_keeps_source_items_visible(app, monkeypatch):
|
|
widget = SAXSTableWidget()
|
|
rows = [_row("alpha", 0), _row("beta", 1), _row("gamma", 2)]
|
|
widget.set_rows(rows)
|
|
widget.table.selectRow(1)
|
|
app.processEvents()
|
|
_Drag.instances.clear()
|
|
monkeypatch.setattr(table_module, "QDrag", _Drag)
|
|
|
|
widget.table.startDrag(Qt.DropAction.MoveAction)
|
|
|
|
assert len(_Drag.instances) == 1
|
|
assert _Drag.instances[0].source is widget.table
|
|
assert _Drag.instances[0].mime_data is not None
|
|
assert _Drag.instances[0].executed_with == Qt.DropAction.MoveAction
|
|
assert [widget.table.item(row, 0).text() for row in range(3)] == ["alpha", "beta", "gamma"]
|
|
|
|
|
|
def test_drag_drop_reorders_rows_and_persists_in_saxs_widget(app):
|
|
widget = SAXSTableWidget()
|
|
rows = [_row("alpha", 0), _row("beta", 1), _row("gamma", 2)]
|
|
widget.set_rows(rows)
|
|
widget.resize(900, 240)
|
|
widget.show()
|
|
app.processEvents()
|
|
|
|
saxs_widget = SAXSWidget.__new__(SAXSWidget)
|
|
saxs_widget._rows = list(rows)
|
|
saxs_widget._session_dirty = False
|
|
saxs_widget.table_widget = widget
|
|
saxs_widget.status_label = widget.status_label
|
|
saxs_widget._update_session_ui_state = lambda: None
|
|
widget.rows_reordered.connect(lambda order: SAXSWidget.reorder_rows(saxs_widget, order))
|
|
|
|
widget.table.selectRow(0)
|
|
app.processEvents()
|
|
last_row = widget.table.rowCount() - 1
|
|
drop_y = widget.table.rowViewportPosition(last_row) + widget.table.rowHeight(last_row) + 4
|
|
event = _DropEvent(QPoint(4, drop_y))
|
|
widget.table.dropEvent(event)
|
|
|
|
assert event.accepted
|
|
assert not event.ignored
|
|
assert [row.sample_name for row in saxs_widget._rows] == ["beta", "gamma", "alpha"]
|
|
assert [widget.table.item(row, 0).text() for row in range(3)] == ["beta", "gamma", "alpha"]
|
|
assert saxs_widget._session_dirty
|