wip
This commit is contained in:
176
pyqtUsrObj.py
176
pyqtUsrObj.py
@@ -17,7 +17,7 @@ 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,QLineF
|
||||
from PyQt5.QtCore import Qt,QPointF,QLineF
|
||||
|
||||
def obj_tree(obj,p=''):
|
||||
obj_info(obj,p)
|
||||
@@ -28,7 +28,12 @@ def obj_info(obj,p=''):
|
||||
print(f"{p}obj_info:{obj}")
|
||||
try:
|
||||
pos=obj.pos()
|
||||
print(f"{p}Pos:({pos.x():.6g},{pos.y():.6g})") # in coordinate value on the scene (no change by zooming)
|
||||
print(f"{p}pos:({pos.x():.6g},{pos.y():.6g})") # in coordinate value on the scene (no change by zooming)
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
sz=obj.size()
|
||||
print(f"{p}size:({sz.x():.6g},{sz.y():.6g})") # in coordinate value on the scene (no change by zooming)
|
||||
except AttributeError:
|
||||
pass
|
||||
try:
|
||||
@@ -53,11 +58,18 @@ def obj_info(obj,p=''):
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
class BeamMark(pg.ROI):
|
||||
class Marker(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, **kargs):
|
||||
def __init__(self, pos, size, mode, **kargs):
|
||||
pg.ROI.__init__(self, pos, size, **kargs)
|
||||
self._mode=mode
|
||||
#widget.signal.connect(slot_function)
|
||||
#self.sigRegionChangeFinished.connect(self.OnRgnChanged)
|
||||
|
||||
#def OnRgnChanged(self, event):
|
||||
# print(event)
|
||||
# obj_info(self)
|
||||
|
||||
def paint(self, p, *args):
|
||||
#pg.ROI.paint(self, p, *args)
|
||||
@@ -65,24 +77,66 @@ class BeamMark(pg.ROI):
|
||||
p.setRenderHint(QtGui.QPainter.Antialiasing)
|
||||
p.setPen(self.currentPen)
|
||||
p.translate(r.left(), r.top())
|
||||
p.scale(r.width(), r.height())
|
||||
p.drawEllipse(0, 0, 1, 1)
|
||||
p.drawRect(0, 0, 1, 1)
|
||||
p.setPen(pg.mkPen(width=3, color=[200, 100, 100]))
|
||||
p.drawLine(pg.Point(.5, 0), pg.Point(.5, 1))
|
||||
p.drawLine(pg.Point( 0,.5), pg.Point( 1,.5))
|
||||
#w, h = self.getState()["size"]
|
||||
#v1, v2 = -(h * 0.8) / 2, (h * 0.8) / 2
|
||||
#h1, h2 = -(w * 0.8) / 2, (w * 0.8) / 2
|
||||
#p.setRenderHint(QtGui.QPainter.Antialiasing)
|
||||
#p.setPen(pg.mkPen(width=3, color=[200, 100, 100]))
|
||||
#p.drawLine(pg.Point(0, v1), pg.Point(0, v2))
|
||||
#p.drawLine(pg.Point(h1, 0), pg.Point(h2, 0))
|
||||
#p.setPen(self.currentPen)
|
||||
##p.setPen(pg.mkPen(width=3, color=[200, 200, 100]))
|
||||
#p.drawRect(-w / 2, -h / 2, w, h)
|
||||
## p.drawText(-w, -h, '{:.0f}x{:.0f}'.format(*self._size))
|
||||
#p.scale(r.width(), r.height()) # -> values x,y 0 to 1
|
||||
#f=p.font();
|
||||
#f.setPixelSize(50)
|
||||
#p.setFont(f)
|
||||
# p.drawText(0, 0, '{:.0f}x{:.0f}'.format(*tuple(self.size())))
|
||||
#p.drawText(0, 0, 'Thierry')
|
||||
|
||||
p.scale(.01*r.width(), .01*r.height()) # -> values x,y 0 to 100
|
||||
m=self._mode
|
||||
if m==0:
|
||||
p.drawEllipse(0, 0, 100, 100)
|
||||
p.drawRect(0, 0, 100, 100)
|
||||
p.setPen(pg.mkPen(width=3, color=[200, 100, 100]))
|
||||
p.drawLine(pg.Point(50, 0), pg.Point(50, 100))
|
||||
p.drawLine(pg.Point( 0,50), pg.Point(100, 50))
|
||||
tr=p.transform()
|
||||
tr.setMatrix(tr.m11(), tr.m12(), tr.m13(), tr.m21(), -tr.m22(), tr.m23(), tr.m31(), tr.m32(), tr.m33())
|
||||
p.setTransform(tr)
|
||||
f=p.font()
|
||||
f.setPixelSize(10)
|
||||
p.setFont(f)
|
||||
p.drawText(24, -80, 'beam marker')
|
||||
ctr=tuple(self.pos()+self.size()/2)
|
||||
sz=tuple(self.size())
|
||||
p.drawText(5, -55, '{:.1f}x{:.1f}'.format(*sz))
|
||||
#p.drawText(5, -35, '{:.1f}'.format(ctr[0]))
|
||||
p.drawText(5, -45,42,30,Qt.AlignRight, '{:.1f}'.format(ctr[0]))
|
||||
p.drawText(55, -35, '{:.1f}'.format(ctr[1]))
|
||||
|
||||
elif m==1:
|
||||
p.drawEllipse(20,20,60,60)
|
||||
p.drawRect(0, 0, 100, 100)
|
||||
p.setPen(pg.mkPen(width=2, color=[10, 255, 0]))
|
||||
p.drawLine(pg.Point(50, 0), pg.Point( 50,100))
|
||||
p.drawLine(pg.Point( 0,50), pg.Point(100, 50))
|
||||
tr=p.transform()
|
||||
tr.setMatrix(tr.m11(), tr.m12(), tr.m13(), tr.m21(), -tr.m22(), tr.m23(), tr.m31(), tr.m32(), tr.m33())
|
||||
p.setTransform(tr)
|
||||
f=p.font();
|
||||
f.setPixelSize(10)
|
||||
p.setFont(f)
|
||||
px=tuple(self.pos()+self.size()/2)
|
||||
p.drawText(5, -90, 'optical')
|
||||
p.drawText(55,-90, 'center')
|
||||
#p.drawText(5, -10, '{:.1f}/{:.1f}'.format(*px))
|
||||
p.drawText(5, -10, '{:.1f}'.format(px[0]))
|
||||
p.drawText(55, -10, '{:.1f}'.format(px[1]))
|
||||
#p.drawText(0, 0, 'Thierry')
|
||||
|
||||
# w, h = self.getState()["size"]
|
||||
# v1, v2 = -(h * 0.8) / 2, (h * 0.8) / 2
|
||||
# h1, h2 = -(w * 0.8) / 2, (w * 0.8) / 2
|
||||
# p.setRenderHint(QtGui.QPainter.Antialiasing)
|
||||
# p.setPen(pg.mkPen(width=3, color=[200, 100, 100]))
|
||||
# p.drawLine(pg.Point(0, v1), pg.Point(0, v2))
|
||||
# p.drawLine(pg.Point(h1, 0), pg.Point(h2, 0))
|
||||
# p.setPen(self.currentPen)
|
||||
# #p.setPen(pg.mkPen(width=3, color=[200, 200, 100]))
|
||||
# p.drawRect(-w / 2, -h / 2, w, h)
|
||||
# # p.drawText(-w, -h, '{:.0f}x{:.0f}'.format(*self._size))
|
||||
|
||||
class Grid(pg.ROI):
|
||||
'''a grid'''
|
||||
@@ -182,20 +236,43 @@ class Path(pg.ROI):
|
||||
|
||||
fid=self._fiducial
|
||||
for i in range(fid.shape[0]):
|
||||
x,y=fid[i,:];print(x,y)
|
||||
x,y=fid[i,:]#;print(x,y)
|
||||
lh=QLineF(x-5*rx,y,x+5*rx,y)
|
||||
lv=QLineF(x, y-5*ry, x, y+5*ry)
|
||||
p.drawLines(lh,lv)
|
||||
|
||||
|
||||
|
||||
class TxtROI(pg.ROI):
|
||||
def __init__(self, pos, size, **kargs):
|
||||
pg.ROI.__init__(self, pos, size, **kargs)
|
||||
|
||||
def paint(self, p, *args):
|
||||
#pg.ROI.paint(self, p, *args)
|
||||
r=QtCore.QRectF(0, 0, self.state['size'][0], self.state['size'][1]).normalized()
|
||||
|
||||
p.setRenderHint(QtGui.QPainter.Antialiasing)
|
||||
p.setPen(self.currentPen)
|
||||
#p.translate(r.left(), r.top())
|
||||
#p.scale(r.width(), r.height())
|
||||
#p.drawRect(0, 0, 1, 1)
|
||||
p.drawRect(r)
|
||||
tr=p.worldTransform()
|
||||
obj_info(tr)
|
||||
tr.setMatrix(tr.m11(),tr.m12(),tr.m13(),tr.m21(),-tr.m22(),tr.m23(),tr.m31(),tr.m32(),tr.m33())
|
||||
p.setWorldTransform(tr)
|
||||
obj_info(tr)
|
||||
obj_info(p.transform())
|
||||
obj_info(p.worldTransform())
|
||||
f=p.font();
|
||||
f.setPixelSize(15)
|
||||
p.setFont(f)
|
||||
p.drawText(0, 5, 'Thierry')
|
||||
|
||||
## Start Qt event loop unless running in interactive mode or using pyside.
|
||||
if __name__=='__main__':
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
#(h, t)=os.path.split(sys.argv[0]);cmd='\n '+(t if len(h)>20 else sys.argv[0])+' '
|
||||
#exampleCmd=('', '-m0xf -v0')
|
||||
epilog=__doc__ #+'\nExamples:'+''.join(map(lambda s:cmd+s, exampleCmd))+'\n'
|
||||
@@ -234,13 +311,36 @@ if __name__=='__main__':
|
||||
|
||||
|
||||
def mouse_click_event(event):
|
||||
#event.pos(): return Point(self.currentItem.mapFromScene(self._scenePos))
|
||||
pos=event.pos()
|
||||
scn=event.scenePos() # there is a small black border, that makes the difference
|
||||
img=viImg.mapFromScene(scn)
|
||||
roi=viUsrRoi.mapFromScene(scn)
|
||||
print(f'mouse:{pt2str(pos)} {pt2str(scn)} img:{pt2str(img)} roi:{pt2str(roi)}')
|
||||
print(f'mouse-> scene pos:{pt2str(scn)} currentItem pos:{pt2str(pos)}')
|
||||
#img=viImg.mapFromScene(scn)
|
||||
#roi=viUsrRoi.mapFromScene(scn)
|
||||
#print(f'mouse-> img:{pt2str(img)} roi:{pt2str(roi)}')
|
||||
#childTree(vb)
|
||||
obj_info(viUsrRoi)
|
||||
#obj_info(viImg)
|
||||
m=int(event.modifiers())
|
||||
o=event.currentItem
|
||||
if m&Qt.ShiftModifier:
|
||||
o.setPos((100,200))
|
||||
o.setSize((200,100))
|
||||
o.rotate(10)
|
||||
pass
|
||||
elif m&Qt.ControlModifier:
|
||||
tr=o.transform()
|
||||
obj_info(tr)
|
||||
#tr.setMatrix(tr.m11(),tr.m12(),tr.m13()+100,tr.m21(),tr.m22(),tr.m23(),tr.m31(),tr.m32(),tr.m33())
|
||||
#tr.setMatrix(tr.m11(),tr.m12(),tr.m13(),tr.m21(),tr.m22(),tr.m23(),tr.m31(),tr.m32()+20,tr.m33())
|
||||
tr.setMatrix(tr.m11(),tr.m12(),tr.m13(),tr.m21(),-tr.m22(),tr.m23(),tr.m31(),tr.m32(),tr.m33())
|
||||
obj_info(tr)
|
||||
o.setTransform(tr)
|
||||
elif m&Qt.AltModifier:
|
||||
pass
|
||||
else:
|
||||
obj_info(o)
|
||||
print(o.state)
|
||||
|
||||
|
||||
## Create image to display
|
||||
arr=np.ones((100, 100), dtype=float)
|
||||
@@ -264,17 +364,23 @@ if __name__=='__main__':
|
||||
w=pg.GraphicsLayoutWidget(show=True, size=(1000, 800), border=True)
|
||||
w.setWindowTitle('pyqtgraph example: ROI Examples')
|
||||
|
||||
vb=w.addViewBox(row=1, col=0, lockAspect=True)
|
||||
g=pg.GridItem()
|
||||
vb=w.addViewBox(row=1, col=0, lockAspect=True,invertY=False)
|
||||
try:
|
||||
g=pg.GridItem(pen=(0, 255, 0), textPen=(0, 255, 0)) # green grid and labels
|
||||
except:
|
||||
g=pg.GridItem()
|
||||
vb.addItem(g)
|
||||
|
||||
viImg=pg.ImageItem(arr, border='y')
|
||||
vb.addItem(viImg)
|
||||
# Custom ROI for selecting an image region
|
||||
viRoi=pg.ROI([20, -50], [60, 40])
|
||||
#viRoi=pg.ROI([20, -50], [60, 40])
|
||||
viRoi=TxtROI([20, -50], [60, 40])
|
||||
vb.addItem(viRoi)
|
||||
viUsrRoi=BeamMark([50, 120], [30, 20])
|
||||
viUsrRoi=Marker([50, 120], [30, 20],mode=0)
|
||||
vb.addItem(viUsrRoi)
|
||||
obj=Marker([250, 220], [30, 20],mode=1)
|
||||
vb.addItem(obj)
|
||||
vi=Grid( (120,-100), (200,150), (30,20),2)
|
||||
#vi=Grid( (50,10), (200,150), (6,4))
|
||||
vb.addItem(vi) #vi= visual item
|
||||
@@ -283,11 +389,19 @@ if __name__=='__main__':
|
||||
fiducial=np.array(((18, 7), (25, 16), (70, 20)))
|
||||
path=gen_swissmx_points(ofs=(10, 5), width=200)
|
||||
vi=Path((120,100),path,fiducial,fidScl)
|
||||
#tr=QtGui.QTransform() # prepare ImageItem transformation:
|
||||
#tr.setMatrix(1, 0, 0,
|
||||
# 0, 1, 0,
|
||||
# 10, 10, 1)
|
||||
#vi.setTransform(tr) # assign transform
|
||||
vb.addItem(vi)
|
||||
|
||||
childTree(vb)
|
||||
|
||||
w.scene().sigMouseClicked.connect(mouse_click_event)
|
||||
#viImg.sigImageChanged
|
||||
#print(vb.pos())
|
||||
#vb.setPos(50,50)
|
||||
|
||||
if (sys.flags.interactive!=1) or not hasattr(QtCore, 'PYQT_VERSION'):
|
||||
QtGui.QApplication.instance().exec_()
|
||||
|
||||
Reference in New Issue
Block a user