feat: hide preview controls with esc
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
"""Offscreen tests for image canvas interaction controls."""
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
class TestImageCanvasControls(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = Path(tempfile.mkdtemp())
|
||||
self.app = self._app()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(str(self.tmp), ignore_errors=True)
|
||||
|
||||
def _app(self):
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
app = QApplication.instance()
|
||||
if app is None:
|
||||
app = QApplication([])
|
||||
return app
|
||||
|
||||
def _write_image(self, name, size, color):
|
||||
path = self.tmp / name
|
||||
Image.new("RGBA", size, color).save(str(path), "PNG")
|
||||
return path
|
||||
|
||||
def _canvas(self):
|
||||
from app.widgets.image_canvas import ImageCanvas
|
||||
|
||||
garment = self._write_image("garment.png", (320, 240), (240, 240, 240, 255))
|
||||
print_path = self._write_image("print.png", (80, 40), (0, 120, 255, 180))
|
||||
canvas = ImageCanvas()
|
||||
canvas.resize(640, 480)
|
||||
canvas.show()
|
||||
canvas.load_garment(garment)
|
||||
canvas.load_print(print_path)
|
||||
self.app.processEvents()
|
||||
return canvas
|
||||
|
||||
def _controls_visible(self, canvas):
|
||||
return all(item.isVisible() for item in canvas._control_items())
|
||||
|
||||
def _state_tuple(self, canvas):
|
||||
state = canvas.get_transform()
|
||||
return (
|
||||
state.x,
|
||||
state.y,
|
||||
state.width,
|
||||
state.height,
|
||||
state.rotation,
|
||||
state.keep_aspect_ratio,
|
||||
)
|
||||
|
||||
def test_escape_hides_controls_without_changing_transform(self):
|
||||
from PySide6.QtCore import Qt
|
||||
from PySide6.QtTest import QTest
|
||||
|
||||
canvas = self._canvas()
|
||||
before = self._state_tuple(canvas)
|
||||
|
||||
self.assertEqual(canvas._view.focusPolicy(), Qt.StrongFocus)
|
||||
self.assertTrue(self._controls_visible(canvas))
|
||||
canvas._view.setFocus()
|
||||
QTest.keyClick(canvas._view, Qt.Key_Escape)
|
||||
self.app.processEvents()
|
||||
|
||||
self.assertFalse(canvas._controls_visible)
|
||||
self.assertFalse(self._controls_visible(canvas))
|
||||
self.assertTrue(canvas._print_item.isVisible())
|
||||
self.assertEqual(self._state_tuple(canvas), before)
|
||||
|
||||
def test_clicking_print_restores_controls_and_keeps_drag_behavior(self):
|
||||
from PySide6.QtCore import QPointF
|
||||
|
||||
canvas = self._canvas()
|
||||
canvas._hide_controls()
|
||||
state = canvas.get_transform()
|
||||
|
||||
canvas._on_press(QPointF(state.x + state.width / 2.0, state.y + state.height / 2.0))
|
||||
|
||||
self.assertTrue(canvas._controls_visible)
|
||||
self.assertTrue(self._controls_visible(canvas))
|
||||
self.assertEqual(canvas._drag_tag, "print")
|
||||
|
||||
def test_loading_print_restores_controls_visible_by_default(self):
|
||||
canvas = self._canvas()
|
||||
canvas._hide_controls()
|
||||
self.assertFalse(canvas._controls_visible)
|
||||
|
||||
next_print = self._write_image("print2.png", (48, 48), (255, 80, 0, 200))
|
||||
canvas.load_print(next_print)
|
||||
|
||||
self.assertTrue(canvas._controls_visible)
|
||||
self.assertTrue(self._controls_visible(canvas))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user