Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shift key enables size/angle snapping #12

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gmc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.5.3"
__version__ = "1.5.4"
13 changes: 3 additions & 10 deletions gmc/markup_objects/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ..views.image_view import ImageView
from ..settings import settings
from .polygon import MarkupPolygon, UndoPolygonCreate
from .polygon import MarkupPolygon, EditableMarkupPolygon, UndoPolygonCreate

Qt = QtCore.Qt

Expand Down Expand Up @@ -52,11 +52,7 @@ def mouse_press(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
view.scene().addItem(self)
return True

def mouse_move(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
self._polygon[1] = view.mapToScene(event.pos())
self.update()
self.on_change_polygon(self._polygon)
return True
mouse_move = EditableMarkupPolygon.mouse_move

def mouse_release(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
self.mouse_move(event, view)
Expand All @@ -73,7 +69,4 @@ def mouse_release(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
view.set_mouse_press(self.prevent_event)
return True

def cancel(self, view: ImageView):
view.unset_all_events()
view.scene().removeItem(self)
view.set_mouse_press(self.mouse_press) # start over
cancel = EditableMarkupPolygon.cancel
6 changes: 4 additions & 2 deletions gmc/markup_objects/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ def on_change_point(self, _):
pass # for overriding

def mouse_press(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
self._create_pos = view.mapToScene(event.pos())
pos = view.mapToScene(event.pos())
if QtGui.QGuiApplication.keyboardModifiers() == Qt.Modifier.SHIFT:
pos = QPointF(round(pos.x(), round(pos.y())))
scene = view.scene()
scene.undo_stack.push(UndoPointCreate(scene, self, self._create_pos))
scene.undo_stack.push(UndoPointCreate(scene, self, pos))
view.set_mouse_move(self.mouse_move)
view.set_mouse_release(self.mouse_release)
view.set_mouse_press(None)
Expand Down
13 changes: 11 additions & 2 deletions gmc/markup_objects/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..views.image_view import ImageView
from . import MarkupObjectMeta
from .moveable_diamond import MoveableDiamond
from math import hypot
from math import hypot, atan2, degrees, radians, sin, cos
from typing import Any, Callable
from PyQt5.QtCore import Qt, QPointF, QCoreApplication, QRectF

Expand Down Expand Up @@ -239,7 +239,16 @@ def mouse_doubleclick(
return True

def mouse_move(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
self._polygon[-1] = view.mapToScene(event.pos())
pt = view.mapToScene(event.pos())
if QtGui.QGuiApplication.keyboardModifiers() == Qt.ShiftModifier:
step_deg = 15
a: QPointF = self._polygon[-2]
dx, dy = pt.x() - a.x(), pt.y() - a.y()
angle = degrees(atan2(dx, dy)) + step_deg * 0.5
d = hypot(dx, dy)
fixed_angle = radians(angle - angle % step_deg)
pt = a + QPointF(d * sin(fixed_angle), d * cos(fixed_angle))
self._polygon[-1] = pt
self.update()
self.on_change_polygon(self._polygon)
return True
Expand Down
20 changes: 12 additions & 8 deletions gmc/markup_objects/quadrangle.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import copysign
from PyQt5 import QtGui, QtCore, QtWidgets
from ..markup_objects.moveable_diamond import MoveableDiamond
from ..views.image_view import ImageView
Expand Down Expand Up @@ -72,14 +73,17 @@ def mouse_move_sequential(
def mouse_move(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
p0 = self._polygon.at(0)
p2 = view.mapToScene(event.pos())
polygon = QtGui.QPolygonF(
[
p0,
QtCore.QPointF(p2.x(), p0.y()),
p2,
QtCore.QPointF(p0.x(), p2.y()),
]
)
if QtGui.QGuiApplication.keyboardModifiers() != Qt.ShiftModifier:
p1 = QtCore.QPointF(p2.x(), p0.y())
p3 = QtCore.QPointF(p0.x(), p2.y())
else:
w, h = p2.x() - p0.x(), p2.y() - p0.y()
size = max(abs(w), abs(h))
w, h = copysign(size, w), copysign(size, h)
p1 = QtCore.QPointF(p0.x() + w, p0.y())
p2 = QtCore.QPointF(p0.x() + w, p0.y() + h)
p3 = QtCore.QPointF(p0.x(), p0.y() + h)
polygon = QtGui.QPolygonF((p0, p1, p2, p3))
self._polygon = polygon
self.update()
self.on_change_polygon(polygon)
Expand Down
12 changes: 11 additions & 1 deletion gmc/markup_objects/rect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
from math import copysign
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt, QRectF, QPointF, QSizeF, QElapsedTimer

Expand Down Expand Up @@ -158,7 +159,16 @@ def mouse_press(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
return True # if not 'return True', back objects will be selected

def mouse_move(self, event: QtGui.QMouseEvent, view: ImageView) -> bool:
self._rect.setBottomRight(view.mapToScene(event.pos()))
if QtGui.QGuiApplication.keyboardModifiers() != Qt.ShiftModifier:
self._rect.setBottomRight(view.mapToScene(event.pos()))
else:
rc = self._rect
rc.setBottomRight(view.mapToScene(event.pos()))
w, h = rc.width(), rc.height()
size = max(abs(w), abs(h))
rc.setWidth(copysign(size, w))
rc.setHeight(copysign(size, h))
self._rect = rc
self.update()
self.on_change_rect(self._rect)
return True
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "run-gmc"
version = "1.5.3"
version = "1.5.4"
authors = [
{ name="Arseniy Terekhin", email="[email protected]" },
]
Expand Down