Skip to content

Commit

Permalink
From QDialog to Toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
mmouchous-ledger committed Nov 26, 2024
1 parent d2cfe7e commit a5f89d4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 44 deletions.
19 changes: 14 additions & 5 deletions laserstudio/laserstudio.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
CameraToolbar,
MainToolbar,
MarkersToolbar,
MarkersListToolbar,
PDMToolbar,
LaserDriverToolbar,
CameraNITToolBar,
Expand Down Expand Up @@ -86,6 +87,10 @@ def __init__(self, config: Optional[dict]):
toolbar = MarkersToolbar(self.viewer)
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, toolbar)

# Toolbar: Markers' List
toolbar = MarkersListToolbar(self.viewer)
self.addToolBar(Qt.ToolBarArea.LeftToolBarArea, toolbar)

# Toolbar: Stage positioning
if self.instruments.stage is not None:
toolbar = StageToolbar(self)
Expand Down Expand Up @@ -185,11 +190,11 @@ def __init__(self, config: Optional[dict]):
if window_state is not None:
self.restoreState(window_state)

def closeEvent(self, event):
def closeEvent(self, a0):
"""Saves user settings before closing the application."""
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("window-state", self.saveState())
super().closeEvent(event)
super().closeEvent(a0)

def handle_go_next(self) -> dict:
"""Go Next operation.
Expand Down Expand Up @@ -248,13 +253,17 @@ def handle_position(self, pos: Optional[list[float]]) -> dict:

def handle_markers(self) -> list[dict]:
"""Handle a Markers API request to get the list of markers."""

return [
{
"id": marker.id if isinstance(marker, IdMarker) else -1,
"pos": [marker.pos().x(), marker.pos().y()],
"color":
[marker.qfillcolor.redF(), marker.qfillcolor.greenF(), marker.qfillcolor.blueF(), marker.qfillcolor.alphaF()]
"color": [
marker.qfillcolor.redF(),
marker.qfillcolor.greenF(),
marker.qfillcolor.blueF(),
marker.qfillcolor.alphaF(),
],
}
for marker in self.viewer.markers
]
Expand Down
2 changes: 2 additions & 0 deletions laserstudio/widgets/toolbars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .laserdrivertoolbar import LaserDriverToolbar
from .maintoolbar import MainToolbar
from .markerstoolbar import MarkersToolbar
from .markerslisttoolbar import MarkersListToolbar
from .scantoolbar import ScanToolbar
from .picturetoolbar import PictureToolbar
from .stagetoolbar import StageToolbar
Expand All @@ -21,4 +22,5 @@
"LaserDriverToolbar",
"PDMToolbar",
"MarkersToolbar",
"MarkersListToolbar",
]
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor
from PyQt6.QtWidgets import QPushButton, QDialog, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QHBoxLayout
from PyQt6.QtWidgets import (
QPushButton,
QToolBar,
QTreeWidget,
QTreeWidgetItem,
QVBoxLayout,
QHBoxLayout,
QWidget,
)
from ..viewer import Viewer
from ..marker import Marker

class MarkerGroupListItem(QTreeWidgetItem):

class MarkersGroupListItem(QTreeWidgetItem):
def __init__(self, parent: QTreeWidget):
super().__init__(parent)
self.number_of_checked = 0
Expand All @@ -22,25 +31,23 @@ def update_checked_state(self):
self.setCheckState(0, Qt.CheckState.PartiallyChecked)


class MarkerListItem(QTreeWidgetItem):
def __init__(self, group: MarkerGroupListItem, marker: Marker):
class MarkersListItem(QTreeWidgetItem):
def __init__(self, group: MarkersGroupListItem, marker: Marker):
super().__init__(group)
self.group = group
x, y = marker.pos().x(), marker.pos().y()
x, y = marker.pos().x(), marker.pos().y()
self.marker = marker
visible = marker.isVisible()
self.setCheckState(0, Qt.CheckState.Checked if visible else Qt.CheckState.Unchecked)
self.setText(0, f"{x:.02f}\xA0µm, {y:.02f}\xA0µm")
self.setCheckState(
0, Qt.CheckState.Checked if visible else Qt.CheckState.Unchecked
)
self.setText(0, f"{x:.02f}\xa0µm, {y:.02f}\xa0µm")
self.setForeground(0, marker.fillcolor)
if visible:
group.number_of_checked += 1

class MarkerListDialog(QDialog):
def open(self):
super().open()
self.refresh_list()
self.activateWindow()

class MarkersListToolbar(QToolBar):
def show_selected(self):
for item in self.list.selectedItems():
item.setCheckState(0, Qt.CheckState.Checked)
Expand All @@ -63,28 +70,37 @@ def refresh_list(self):
markers_by_colors[name].append(marker)
for color in sorted(markers_by_colors.keys()):
markers: list[Marker] = markers_by_colors[color]
group = MarkerGroupListItem(self.list)
group = MarkersGroupListItem(self.list)
group.setForeground(0, markers[0].fillcolor)
group.setText(0, f"{len(markers)} marker" + ("" if len(markers) == 1 else "s"))
group.setText(
0, f"{len(markers)} marker" + ("" if len(markers) == 1 else "s")
)

self.list.itemChanged.disconnect(self.item_changed)
for marker in markers:
MarkerListItem(group, marker)
MarkersListItem(group, marker)
group.update_checked_state()
self.list.itemChanged.connect(self.item_changed)


def __init__(self, viewer: Viewer):
super().__init__()
super().__init__("Markers List")
self.setObjectName("toolbar-markers-list") # For settings save and restore
self.setAllowedAreas(
Qt.ToolBarArea.LeftToolBarArea | Qt.ToolBarArea.RightToolBarArea
)
self.setFloatable(True)

self.viewer = viewer
self.list = QTreeWidget()
self.list.setHeaderHidden(True)
self.list.setSelectionMode(QTreeWidget.SelectionMode.ExtendedSelection)
self.list.itemChanged.connect(self.item_changed)

w = QWidget()
self.addWidget(w)
vbox = QVBoxLayout()
self.setLayout(vbox)
w.setLayout(vbox)

w = QPushButton("Refresh")
w.clicked.connect(self.refresh_list)
vbox.addWidget(w)
Expand All @@ -96,17 +112,19 @@ def __init__(self, viewer: Viewer):
w = QPushButton("Hide")
w.clicked.connect(self.hide_selected)
hbox.addWidget(w)

vbox.addLayout(hbox)
vbox.addWidget(self.list)

self.list.itemDoubleClicked.connect(self.show_marker)

def show_marker(self, item: QTreeWidgetItem):
if isinstance(item, MarkerListItem):
if isinstance(item, MarkersListItem):
self.viewer.follow_stage_sight = False
self.viewer.cam_pos_zoom = item.marker.pos(), self.viewer.cam_pos_zoom[1]

def item_changed(self, item: QTreeWidgetItem):
if isinstance(item, MarkerListItem):
if isinstance(item, MarkersListItem):
visible = item.checkState(0) == Qt.CheckState.Checked
was_visible = item.marker.isVisible()
if not was_visible and visible:
Expand All @@ -115,11 +133,12 @@ def item_changed(self, item: QTreeWidgetItem):
item.group.number_of_checked -= 1
item.marker.setVisible(visible)
item.group.update_checked_state()
if isinstance(item, MarkerGroupListItem):
if isinstance(item, MarkersGroupListItem):
new_state = item.checkState(0)
if new_state == Qt.CheckState.PartiallyChecked: return
if new_state == Qt.CheckState.PartiallyChecked:
return
for i in range(item.childCount()):
child = item.child(i)
if child is None: continue
if child is None:
continue
child.setCheckState(0, new_state)

16 changes: 3 additions & 13 deletions laserstudio/widgets/toolbars/markerstoolbar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import TYPE_CHECKING
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QToolBar, QPushButton
from ..return_line_edit import ReturnSpinBox
from ...utils.util import colored_image
from ..viewer import Viewer
from .markerslistdialog import MarkerListDialog
from .markerslisttoolbar import MarkersListToolbar


class MarkersToolbar(QToolBar):
def __init__(self, viewer: Viewer):
Expand All @@ -18,7 +18,7 @@ def __init__(self, viewer: Viewer):
w = QPushButton(self)
w.setIcon(QIcon(colored_image(":/icons/location-pin-plus.svg")))
w.setIconSize(QSize(24, 24))
w.setToolTip("Add markers")
w.setToolTip("Add marker")
w.clicked.connect(lambda: viewer.add_marker())
self.addWidget(w)

Expand All @@ -43,13 +43,3 @@ def __init__(self, viewer: Viewer):
lambda: viewer.marker_size(float(self.marker_size_sp.value()))
)
self.addWidget(self.marker_size_sp)

# Markers categories
self.makers_list_dialog = MarkerListDialog(viewer)
self.makers_list_dialog.setModal(False)
self.makers_list_dialog.setWindowTitle("Markers list")

self.show_list = w = QPushButton("Show List")
w.clicked.connect(lambda: self.makers_list_dialog.open())

self.addWidget(w)

0 comments on commit a5f89d4

Please sign in to comment.