Skip to content

Commit

Permalink
Create a dialog to show the list of markers, and handle their visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mmouchous-ledger committed Nov 25, 2024
1 parent be737e0 commit 6f23aa9
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
93 changes: 93 additions & 0 deletions laserstudio/widgets/toolbars/markerslistdialog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QColor
from PyQt6.QtWidgets import QPushButton, QDialog, QTreeWidget, QTreeWidgetItem, QVBoxLayout, QHBoxLayout
from ..viewer import Viewer
from ..marker import Marker

class MarkerListItem(QTreeWidgetItem):
def __init__(self, group: QTreeWidgetItem, marker: Marker):
super().__init__(group)
x, y = marker.pos().x(), marker.pos().y()
self.marker = marker
self.setCheckState(0, Qt.CheckState.Checked if marker.isVisible() else Qt.CheckState.Unchecked)
self.setText(0, f"{x:.02f}µm {y:.02f}µm")
self.setForeground(0, marker.fillcolor)

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

def show_selected(self):
for item in self.list.selectedItems():
item.setCheckState(0, Qt.CheckState.Checked)

def hide_selected(self):
for item in self.list.selectedItems():
item.setCheckState(0, Qt.CheckState.Unchecked)

def refresh_list(self):
self.list.clear()
markers_by_colors: dict[str, list[Marker]] = {}
for marker in self.viewer.markers:
if type(marker.fillcolor) is QColor:
name: str = f"{marker.fillcolor.hue():02x}{marker.fillcolor.saturation():02x}{marker.fillcolor.lightness():02x}{marker.fillcolor.alpha():02x}"
else:
name = str(marker.color)
if name not in markers_by_colors:
markers_by_colors[name] = [marker]
else:
markers_by_colors[name].append(marker)
for color in sorted(markers_by_colors.keys()):
markers = markers_by_colors[color]
group = QTreeWidgetItem(self.list)
group.setForeground(0, markers[0].fillcolor)
group.setText(0, f"{len(markers)} marker(s)")
group.setCheckState(0, Qt.CheckState.Checked)

for marker in markers:
MarkerListItem(group, marker)

def __init__(self, viewer: Viewer):
super().__init__()
self.viewer = viewer
self.list = QTreeWidget()
self.list.setHeaderHidden(True)
self.list.setSelectionMode(QTreeWidget.SelectionMode.ExtendedSelection)
self.list.itemChanged.connect(self.item_changed)

vbox = QVBoxLayout()
self.setLayout(vbox)

w = QPushButton("Refresh")
w.clicked.connect(self.refresh_list)
vbox.addWidget(w)

hbox = QHBoxLayout()
w = QPushButton("Show")
w.clicked.connect(self.show_selected)
hbox.addWidget(w)
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):
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):
item.marker.setVisible(item.checkState(0) == Qt.CheckState.Checked)
else:
for i in range(item.childCount()):
child = item.child(i)
if child is None: continue
child.setCheckState(0, item.checkState(0))

12 changes: 11 additions & 1 deletion laserstudio/widgets/toolbars/markerstoolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..return_line_edit import ReturnSpinBox
from ...utils.util import colored_image
from ..viewer import Viewer

from .markerslistdialog import MarkerListDialog

class MarkersToolbar(QToolBar):
def __init__(self, viewer: Viewer):
Expand Down Expand Up @@ -43,3 +43,13 @@ 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 6f23aa9

Please sign in to comment.