Skip to content

Commit c0a541b

Browse files
committed
Added c2c plugin and key handling
1 parent 651b53d commit c0a541b

5 files changed

Lines changed: 88 additions & 72 deletions

File tree

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
from qmicroscope.container import Container
2424
from qmicroscope.settings import Settings
2525
from qmicroscope.plugins.record_plugin import RecordPlugin
26-
26+
from qmicroscope.plugins.c2c_plugin import C2CPlugin
2727

2828
class Form(QMainWindow):
2929
def __init__(self, parent=None):
3030
super(Form, self).__init__(parent)
3131
# Create widgets
3232
self.setWindowTitle("NSLS-II Microscope Widget")
33-
self.container = Container(self, plugins=[RecordPlugin])
33+
self.container = Container(self, plugins=[RecordPlugin, C2CPlugin])
3434
self.container.count = 3
3535
self.container.size = [2, 2]
3636
self.microscope = self.container.microscope(0)

monitor.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
import sys
22

3-
43
from qtpy.QtCore import QPoint, QSettings, QSize, Qt
5-
64
from qtpy.QtWidgets import (
5+
QApplication,
76
QCheckBox,
7+
QFormLayout,
8+
QHBoxLayout,
89
QLineEdit,
10+
QMainWindow,
911
QPushButton,
10-
QApplication,
11-
QVBoxLayout,
12-
QHBoxLayout,
13-
QFormLayout,
1412
QSpinBox,
15-
QMainWindow,
16-
QWidget,
1713
QSplitter,
14+
QVBoxLayout,
15+
QWidget,
1816
)
19-
from qmicroscope.microscope import Microscope
17+
2018
from qmicroscope.container import Container
21-
from qmicroscope.settings import Settings
19+
from qmicroscope.microscope import Microscope
2220
from qmicroscope.plugins import (
23-
ZoomPlugin,
21+
C2CPlugin,
22+
CrossHairPlugin,
2423
GridPlugin,
24+
MouseWheelCameraZoomPlugin,
2525
PresetPlugin,
26-
ScalePlugin,
27-
TogglePlugin,
28-
CrossHairPlugin,
2926
RecordPlugin,
27+
ScalePlugin,
3028
SquareGridPlugin,
29+
TogglePlugin,
30+
ZoomPlugin,
3131
)
32+
from qmicroscope.settings import Settings
3233

3334

3435
class Form(QMainWindow):
@@ -49,6 +50,8 @@ def __init__(self, parent=None):
4950
ScalePlugin,
5051
RecordPlugin,
5152
SquareGridPlugin,
53+
MouseWheelCameraZoomPlugin,
54+
C2CPlugin,
5255
]
5356
self.main_microscope = Microscope(self, viewport=False, plugins=plugins)
5457
self.main_microscope.scale = [0, 500]

qmicroscope/microscope.py

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
from collections.abc import Iterable
2-
from qtpy.QtCore import Signal, QByteArray, QPoint, QSize, QSettings, QEvent
3-
from qtpy.QtGui import QImage, QPainter, QContextMenuEvent, QMouseEvent, QPixmap, QKeyEvent
2+
from typing import Any, Dict, List, Optional
3+
4+
from PyQt5 import QtGui
5+
from qtpy.QtCore import QByteArray, QEvent, QPoint, QSettings, QSize, Qt, Signal
6+
from qtpy.QtGui import (
7+
QContextMenuEvent,
8+
QImage,
9+
QKeyEvent,
10+
QMouseEvent,
11+
QPainter,
12+
QPixmap,
13+
)
414
from qtpy.QtWidgets import (
5-
QWidget,
6-
QMenu,
715
QAction,
8-
QGraphicsView,
9-
QGraphicsScene,
1016
QGraphicsPixmapItem,
17+
QGraphicsScene,
18+
QGraphicsView,
19+
QMenu,
1120
QVBoxLayout,
21+
QWidget,
1222
)
13-
from typing import List, Any, Dict, Optional
14-
from .widgets.downloader import VideoThread
15-
from .plugins.base_plugin import BasePlugin, SupportsBasePlugin
23+
1624
from .plugin_settings import PluginSettingsDialog
25+
from .plugins.base_plugin import BasePlugin, SupportsBasePlugin
26+
from .widgets.downloader import VideoThread
1727

1828

1929
class Microscope(QWidget):
@@ -25,7 +35,7 @@ class Microscope(QWidget):
2535
mouse_wheel_signal: Signal = Signal(object)
2636
key_press_signal: Signal = Signal(object)
2737
key_release_signal: Signal = Signal(object)
28-
38+
2939
def __init__(
3040
self,
3141
parent: Optional[QWidget] = None,
@@ -41,6 +51,7 @@ def __init__(
4151
self.pixmap = QGraphicsPixmapItem(None)
4252
self.scene = QGraphicsScene(self)
4353
self.view = QGraphicsView(self.scene)
54+
self.view.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
4455
self.view.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
4556
self.scene.addItem(self.pixmap)
4657
self.layout = QVBoxLayout()
@@ -77,6 +88,7 @@ def __init__(
7788
self.key_release_signal.connect(plugin.key_release_event)
7889

7990
self.view.viewport().installEventFilter(self)
91+
self.view.installEventFilter(self)
8092

8193
def updatedImageSize(self) -> None:
8294
if self.image.size() != self.minimumSize():
@@ -113,54 +125,38 @@ def eventFilter(self, obj, event):
113125
self.mouse_move_event(event)
114126
if event.type() == QEvent.Wheel:
115127
self.mouse_wheel_event(event)
128+
if obj is self.view:
129+
if event.type() == QEvent.Type.Enter:
130+
self.view.setFocus()
131+
print("Entered widget")
132+
if event.type() == QEvent.Type.Leave:
133+
self.view.clearFocus()
134+
print("Leaving Widget")
116135
if event.type() == QEvent.KeyPress:
117136
self.key_press_event(event)
118137
if event.type() == QEvent.KeyRelease:
119138
self.key_release_event(event)
139+
120140
return QWidget.eventFilter(self, obj, event)
121141

122142
def key_press_event(self, event: QKeyEvent):
123143
self.key_press_signal.emit(event)
124144

125145
def key_release_event(self, event: QKeyEvent):
126-
self.key_release_event.emit(event)
146+
self.key_release_signal.emit(event)
127147

128148
def mouse_wheel_event(self, event):
129-
self.mouse_wheel_signal.emit(event.angleDelta())
130-
"""
131-
# Zoom Factor
132-
zoomInFactor = 1.05
133-
zoomOutFactor = 1 / zoomInFactor
134-
135-
# Set Anchors
136-
self.view.setTransformationAnchor(QGraphicsView.NoAnchor)
137-
self.view.setResizeAnchor(QGraphicsView.NoAnchor)
138-
139-
# Save the scene pos
140-
oldPos = self.view.mapToScene(event.pos())
141-
142-
# Zoom
143-
if event.angleDelta().y() > 0:
144-
zoomFactor = zoomInFactor
145-
else:
146-
zoomFactor = zoomOutFactor
147-
self.view.scale(zoomFactor, zoomFactor)
148-
149-
# Get the new position
150-
newPos = self.view.mapToScene(event.pos())
149+
print(event.angleDelta())
150+
self.mouse_wheel_signal.emit(event.angleDelta())
151151

152-
# Move scene to old position
153-
delta = newPos - oldPos
154-
self.view.translate(delta.x(), delta.y())
155-
"""
156-
157152
def mouse_press_event(self, a0: QMouseEvent):
158153
if self.viewport:
159154
self.clicked_url.emit(self.settings_group)
160155

161156
self.mouse_press_signal.emit(a0)
162157

163158
def mouse_move_event(self, a0: QMouseEvent):
159+
self.setFocus()
164160
self.mouse_move_signal.emit(a0)
165161

166162
def mouse_release_event(self, a0: QMouseEvent) -> None:
@@ -194,7 +190,9 @@ def addMenuItem(self, item):
194190
self.menu.addMenu(item)
195191

196192
def _config_plugins(self):
197-
plugin_settings_dialog = PluginSettingsDialog(parent=self, plugins=self.plugins.values())
193+
plugin_settings_dialog = PluginSettingsDialog(
194+
parent=self, plugins=self.plugins.values()
195+
)
198196

199197
def sizeHint(self) -> QSize:
200198
return QSize(400, 400)

qmicroscope/plugins/c2c_plugin.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import typing
2-
from qtpy.QtCore import QObject, Signal
2+
3+
from qtpy.QtCore import QObject, QPoint, Qt, Signal
4+
from qtpy.QtGui import QKeyEvent
5+
36
from qmicroscope.plugins.base_plugin import BasePlugin
7+
48
if typing.TYPE_CHECKING:
59
from qmicroscope.microscope import Microscope
610

11+
712
class ClickedSignal(QObject):
813
clicked = Signal(object)
9-
14+
15+
1016
class C2CPlugin(BasePlugin):
11-
1217
def __init__(self, parent: "Optional[Microscope]" = None):
1318
"""Initializes the C2CPlugin instance.
1419
@@ -20,15 +25,24 @@ def __init__(self, parent: "Optional[Microscope]" = None):
2025
self.parent = parent
2126
self.c2c_active = False
2227
self.clicked_signal = ClickedSignal()
23-
2428

2529
def mouse_press_event(self, event):
2630
if self.parent:
2731
self.parent: "Microscope"
2832
if self.c2c_active:
29-
delta = event.pos() - self.parent.center
33+
delta = event.pos() - QPoint(
34+
int(self.parent.view.width() / 2),
35+
int(self.parent.view.height() / 2),
36+
)
3037
print(delta)
3138
self.clicked_signal.clicked.emit(delta)
3239

3340
def key_press_event(self, event):
34-
print(event)
41+
if event.key() == Qt.Key.Key_Control:
42+
self.c2c_active = True
43+
self.parent.setCursor(Qt.CursorShape.CrossCursor)
44+
45+
def key_release_event(self, event: QKeyEvent):
46+
if event.key() == Qt.Key.Key_Control:
47+
self.c2c_active = False
48+
self.parent.unsetCursor()

qmicroscope/plugins/square_grid.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
from typing import Optional, Dict, Any, TYPE_CHECKING
1+
from collections import defaultdict
2+
from typing import TYPE_CHECKING, Any, Dict, Optional
3+
4+
from qtpy.QtCore import QObject, QPoint, QRect, QRectF, Qt, Signal
5+
from qtpy.QtGui import QColor, QMouseEvent, QPen
26
from qtpy.QtWidgets import (
37
QAction,
48
QColorDialog,
5-
QGraphicsScene,
6-
QGroupBox,
79
QFormLayout,
8-
QSpinBox,
910
QGraphicsRectItem,
11+
QGraphicsScene,
1012
QGraphicsSceneMouseEvent,
13+
QGroupBox,
14+
QSpinBox,
1115
)
12-
from qtpy.QtCore import QPoint, Qt, QRect, QRectF, Signal, QObject
13-
from qtpy.QtGui import QColor, QPen
14-
from qmicroscope.widgets.rubberband import ResizableRubberBand
15-
from qmicroscope.widgets.color_button import ColorButton
16+
1617
from qmicroscope.plugins.base_plugin import BasePlugin
17-
from qtpy.QtGui import QMouseEvent
18-
from collections import defaultdict
18+
from qmicroscope.widgets.color_button import ColorButton
19+
from qmicroscope.widgets.rubberband import ResizableRubberBand
1920

2021
if TYPE_CHECKING:
2122
from qmicroscope.microscope import Microscope
@@ -342,7 +343,7 @@ def paintBoxes(self, scene: QGraphicsScene) -> None:
342343
num_cols = int(abs(x2 - x1) / self._cell_size)
343344
self._grid_cells: "list[list[CellGraphicsItem]]" = []
344345
center_x = (x2 - x1) / 2
345-
center_y = (y2 / y1) / 2
346+
center_y = (y2 - y1) / 2
346347

347348
for c in range(num_cols):
348349
row = []

0 commit comments

Comments
 (0)