From b512ac6aa510ee3bcf479c9c6af4a245336e3aae Mon Sep 17 00:00:00 2001 From: Thierry Zamofing Date: Thu, 21 Jul 2022 21:18:51 +0200 Subject: [PATCH] pyqtUsrObj.py work... --- pyqtUsrObj.py | 101 ++++++++++++++++++++++++++++++++++++++++++++------ swissmx.py | 11 +++--- 2 files changed, 96 insertions(+), 16 deletions(-) diff --git a/pyqtUsrObj.py b/pyqtUsrObj.py index 2d47d79..b5f19bd 100644 --- a/pyqtUsrObj.py +++ b/pyqtUsrObj.py @@ -1,25 +1,28 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -Demonstrates a variety of uses for ROI. This class provides a user-adjustable -region of interest marker. It is possible to customize the layout and -function of the scale/rotate handles in very flexible ways. -""" +# *-----------------------------------------------------------------------* +# | | +# | Copyright (c) 2022 by Paul Scherrer Institute (http://www.psi.ch) | +# | Based on Zac great first implementation | +# | Author Thierry Zamofing (thierry.zamofing@psi.ch) | +# *-----------------------------------------------------------------------* +''' +User pyqtgraph objects and functions. + +TODO: Dimension of pyqtgraph is in um not pixel (as now) +''' #import initExample ## Add path to library (just for examples; you do not need this) import pyqtgraph as pg from pyqtgraph.Qt import QtCore, QtGui import numpy as np - +from PyQt5.QtGui import QPolygon,QPolygonF +from PyQt5.QtCore import QPointF class BeamMark(pg.ROI): """A crosshair ROI whose position is at the center of the crosshairs. By default, it is scalable, rotatable and translatable.""" - def __init__(self, pos=None, size=None, parent=None, **kargs): - #self._size=size - #self._pos=pos - #self._shape=None + def __init__(self, pos=None, size=None, **kargs): pg.ROI.__init__(self, pos, size, **kargs) def paint(self, p, *args): @@ -47,6 +50,75 @@ class BeamMark(pg.ROI): ## p.drawText(-w, -h, '{:.0f}x{:.0f}'.format(*self._size)) +class Grid(pg.ROI): + '''a grid''' + + def __init__( self, pos=(0,0), size=(30,20), cnt=(6,4), **kargs): + pg.ROI.__init__(self, pos, size, **kargs) + self._cnt=cnt + self.addScaleHandle([1, 1], [0, 0]) + self.addScaleHandle([0, 0], [1, 1]) + self.addScaleRotateHandle([1, 0], [0, 0]) + + def paint(self, p, *args): + #pg.ROI.paint(self, p, *args) + sz=self.state['size'] + nx, ny=self._cnt + px, py=sz[0]/(nx-1), sz[1]/(ny-1) + r=QtCore.QRectF(0, 0, sz[0], sz[1]).normalized() + p.setRenderHint(QtGui.QPainter.Antialiasing) + p.setPen(self.currentPen) + for i in range(nx): + x=i*px + p.drawLine(pg.Point(x, 0), pg.Point(x, sz[1])) + + for i in range(ny): + y=i*py + p.drawLine(pg.Point(0, y), pg.Point(sz[0] ,y )) + + #p.translate(r.left(), r.top()) + #p.scale(r.width(), r.height()) + #p.drawEllipse(0, 0, 1, 1) + #p.drawRect(0, 0, 1, 1) + + +class Path(pg.ROI): + '''a grid''' + + def __init__( self, pos=(0,0), mark=np.array(((3,4),)), path=np.array(((6,4),(16,24),(-5,7),(3,12))), **kargs): + all=np.vstack((mark,path)) + mn=all.min(0) + mx=all.max(0) + size=mx-mn + + pg.ROI.__init__(self, pos, size, **kargs) + self._mark=mark + self._path=path + self.addScaleHandle([1, 1], [0, 0]) + self.addScaleHandle([0, 0], [1, 1]) + self.addScaleRotateHandle([1, 0], [0, 0]) + + def paint(self, p, *args): + #pg.ROI.paint(self, p, *args) + pos=self.state['pos'] + sz=self.state['size'] + mark=self._mark + path=self._path + + p.setRenderHint(QtGui.QPainter.Antialiasing) + p.setPen(self.currentPen) + #p.translate(pos[0], pos[1]) + p.drawEllipse(0, 0, int(sz[0]), int(sz[1])) + p.drawRect(0, 0, int(sz[0]), int(sz[1])) + + qPlg=QPolygonF() + for pt in path: + qPlg.append(QPointF(*pt)) + #pp =QPolygonF(path) + p.drawPolyline(qPlg) + + + ## Start Qt event loop unless running in interactive mode or using pyside. if __name__=='__main__': @@ -126,6 +198,13 @@ if __name__=='__main__': vb.addItem(viRoi) viUsrRoi=BeamMark([10, 20], [30, 20]) vb.addItem(viUsrRoi) + vi=Grid( (50,10), (200,150), (30,20)) + #vi=Grid( (50,10), (200,150), (6,4)) + vb.addItem(vi) #vi= visual item + + vi=Path() + vb.addItem(vi) + childTree(vb) w.scene().sigMouseClicked.connect(mouse_click_event) diff --git a/swissmx.py b/swissmx.py index a104ac8..5bf2b2f 100755 --- a/swissmx.py +++ b/swissmx.py @@ -243,15 +243,16 @@ class Main(QMainWindow, Ui_MainWindow): self.microscope_page.layout().addWidget(self.glw) self.glw.show() - self.vb = self.glw.addViewBox(invertY=True)#,enableMenu=False) + self.vb=vb=self.glw.addViewBox(invertY=True)#,enableMenu=False) self.img = pg.ImageItem() - self.img._swissmx_name = "microscope_image_item" # self.graphicsView.setCentralItem(self.vb) self.glw.scene().sigMouseMoved.connect(self.mouse_move_event) self.glw.scene().sigMouseClicked.connect(self.mouse_click_event) - self.vb.setAspectLocked(True) - self.vb.setBackgroundColor((120, 90, 90)) - self.vb.addItem(self.img) + vb.setAspectLocked(True) + vb.setBackgroundColor((120, 90, 90)) + vb.addItem(self.img) + grid=pg.GridItem() + vb.addItem(grid) self._escape_current_state = "Maintenance" self._pin_mounting_offset = 0.0