Skip to content

Commit

Permalink
added scalebar for spheroid and pyTFM
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Dec 29, 2023
1 parent 80ec431 commit 36d5559
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 60 deletions.
62 changes: 62 additions & 0 deletions saenopy/gui/common/ModuleScaleBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import numpy as np
from qtpy import QtCore, QtWidgets, QtGui


class ModuleScaleBar(QtWidgets.QGroupBox):
pixtomu = None

def __init__(self, parent, view):
QtWidgets.QWidget.__init__(self)
self.parent = parent

self.font = QtGui.QFont()
self.font.setPointSize(16)

self.scale = 1

self.scalebar = QtWidgets.QGraphicsRectItem(0, 0, 1, 1, view.hud_lowerRight)
self.scalebar.setBrush(QtGui.QBrush(QtGui.QColor("white")))
self.scalebar.setPen(QtGui.QPen(QtGui.QColor("white")))
self.scalebar.setPos(-20, -20)
self.scalebar_text = QtWidgets.QGraphicsTextItem("", view.hud_lowerRight)
self.scalebar_text.setFont(self.font)
self.scalebar_text.setDefaultTextColor(QtGui.QColor("white"))

self.time_text = QtWidgets.QGraphicsTextItem("", view.hud_upperRight)
self.time_text.setFont(self.font)
self.time_text.setDefaultTextColor(QtGui.QColor("white"))

view.signal_zoom.connect(self.zoomEvent)

self.updateStatus()

def updateStatus(self):
self.updateBar()

def zoomEvent(self, scale, pos):
self.scale = scale
self.updateBar()

def setScale(self, voxel_size):
self.pixtomu = voxel_size[0]
self.updateBar()

def updateBar(self):
if self.scale == 0 or self.pixtomu is None:
return
mu = 100*self.pixtomu/self.scale
values = [1, 5, 10, 25, 50, 75, 100, 150, 200, 250, 500, 1000, 1500, 2000, 2500, 5000, 10000]
old_v = mu
for v in values:
if mu < v:
mu = old_v
break
old_v = v
if np.abs(self.pixtomu) < 1e-10:
pixel = 0
else:
pixel = mu/(self.pixtomu)*self.scale
self.scalebar.setRect(0, 0, -pixel, 5)
self.scalebar_text.setPos(-pixel-20-25, -20-30)
self.scalebar_text.setTextWidth(pixel+50)
self.scalebar_text.setHtml(u"<center>%d&thinsp;µm</center>" % mu)
61 changes: 1 addition & 60 deletions saenopy/gui/solver/modules/StackDisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from saenopy.gui.common.PipelineModule import PipelineModule
from saenopy.gui.common.QTimeSlider import QTimeSlider
from saenopy.gui.common.code_export import get_code
from saenopy.gui.common.ModuleScaleBar import ModuleScaleBar


class StackDisplay(PipelineModule):
Expand Down Expand Up @@ -353,63 +354,3 @@ def code(filename, output1, voxel_size1, time_delta1, result_file, crop1): # pr

code = get_code(code, data)
return import_code, code


class ModuleScaleBar(QtWidgets.QGroupBox):
pixtomu = None

def __init__(self, parent, view):
QtWidgets.QWidget.__init__(self)
self.parent = parent

self.font = QtGui.QFont()
self.font.setPointSize(16)

self.scale = 1

self.scalebar = QtWidgets.QGraphicsRectItem(0, 0, 1, 1, view.hud_lowerRight)
self.scalebar.setBrush(QtGui.QBrush(QtGui.QColor("white")))
self.scalebar.setPen(QtGui.QPen(QtGui.QColor("white")))
self.scalebar.setPos(-20, -20)
self.scalebar_text = QtWidgets.QGraphicsTextItem("", view.hud_lowerRight)
self.scalebar_text.setFont(self.font)
self.scalebar_text.setDefaultTextColor(QtGui.QColor("white"))

self.time_text = QtWidgets.QGraphicsTextItem("", view.hud_upperRight)
self.time_text.setFont(self.font)
self.time_text.setDefaultTextColor(QtGui.QColor("white"))

view.signal_zoom.connect(self.zoomEvent)

self.updateStatus()

def updateStatus(self):
self.updateBar()

def zoomEvent(self, scale, pos):
self.scale = scale
self.updateBar()

def setScale(self, voxel_size):
self.pixtomu = voxel_size[0]
self.updateBar()

def updateBar(self):
if self.scale == 0 or self.pixtomu is None:
return
mu = 100*self.pixtomu/self.scale
values = [1, 5, 10, 25, 50, 75, 100, 150, 200, 250, 500, 1000, 1500, 2000, 2500, 5000, 10000]
old_v = mu
for v in values:
if mu < v:
mu = old_v
break
old_v = v
if np.abs(self.pixtomu) < 1e-10:
pixel = 0
else:
pixel = mu/(self.pixtomu)*self.scale
self.scalebar.setRect(0, 0, -pixel, 5)
self.scalebar_text.setPos(-pixel-20-25, -20-30)
self.scalebar_text.setTextWidth(pixel+50)
self.scalebar_text.setHtml(u"<center>%d&thinsp;µm</center>" % mu)
3 changes: 3 additions & 0 deletions saenopy/gui/spheroid/modules/DeformationDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from saenopy.gui.common.QTimeSlider import QTimeSlider
from saenopy.gui.common.resources import resource_icon
from saenopy.gui.common.code_export import get_code
from saenopy.gui.common.ModuleScaleBar import ModuleScaleBar


class DeformationDetector(PipelineModule):
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(self, parent: "BatchEvaluate", layout):
#self.plotter.set_background("black")
#layout.addWidget(self.plotter.interactor)
self.label = QExtendedGraphicsView.QExtendedGraphicsView().addToLayout()
self.scale1 = ModuleScaleBar(self, self.label)
#self.label.setMinimumWidth(300)
self.pixmap = QtWidgets.QGraphicsPixmapItem(self.label.origin)
self.contour = QtWidgets.QGraphicsPathItem(self.label.origin)
Expand Down Expand Up @@ -222,6 +224,7 @@ def update_display(self, *, plotter=None):
im = np.asarray(pil_image)
self.pixmap.setPixmap(QtGui.QPixmap(array2qimage(im)))
self.label.setExtend(im.shape[1], im.shape[0])
self.scale1.setScale([self.result.pixel_size])

if self.show_seg.value():
thresh_segmentation = self.thresh_segmentation.value()
Expand Down
3 changes: 3 additions & 0 deletions saenopy/gui/tfm2d/modules/BatchEvaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from saenopy.gui.common.BatchEvaluate import BatchEvaluate
from ...solver.modules.exporter.Exporter import ExportViewer
from saenopy.gui.common.ModuleScaleBar import ModuleScaleBar


class BatchEvaluate(BatchEvaluate):
Expand Down Expand Up @@ -75,6 +76,7 @@ def tab_changed(x):
pass
self.draw = DrawWindow(self, QtShortCuts.currentLayout())
self.draw.signal_mask_drawn.connect(self.on_mask_drawn)
self.scale1 = ModuleScaleBar(self, self.draw.view1)

def generate_data(self):
new_path = QtWidgets.QFileDialog.getSaveFileName(None, "Save Data CSV", os.getcwd(),
Expand Down Expand Up @@ -237,6 +239,7 @@ def listSelected(self):
self.draw.setMask(np.zeros(pipe.shape, dtype=np.uint8))
else:
self.draw.setMask(pipe.mask.astype(np.uint8))
self.scale1.setScale([pipe.pixel_size])
self.set_current_result.emit(pipe)
tab = self.tabs.currentWidget()
self.tab_changed.emit(tab)

0 comments on commit 36d5559

Please sign in to comment.