pyqtUsrObj.py work...
This commit is contained in:
101
pyqtUsrObj.py
101
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)
|
||||
|
||||
Reference in New Issue
Block a user