GridItem: Add custom line width based on pen option.

This commit is contained in:
Anthony De Bortoli 2021-04-11 11:40:11 +02:00
parent 5a08650853
commit 0e7b8a4828
2 changed files with 21 additions and 15 deletions

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
## Add path to library (just for examples; you do not need this) ## Add path to library (just for examples; you do not need this)
from PySide2.QtGui import QPen, QColor
import initExample import initExample
from pyqtgraph.Qt import QtCore, QtGui from pyqtgraph.Qt import QtCore, QtGui
@ -15,27 +17,27 @@ class Obj(QtGui.QGraphicsObject):
def __init__(self): def __init__(self):
QtGui.QGraphicsObject.__init__(self) QtGui.QGraphicsObject.__init__(self)
GraphicsScene.registerObject(self) GraphicsScene.registerObject(self)
def paint(self, p, *args): def paint(self, p, *args):
p.setPen(pg.mkPen(200,200,200)) p.setPen(pg.mkPen(200,200,200))
p.drawRect(self.boundingRect()) p.drawRect(self.boundingRect())
def boundingRect(self): def boundingRect(self):
return QtCore.QRectF(0, 0, 20, 20) return QtCore.QRectF(0, 0, 20, 20)
def mouseClickEvent(self, ev): def mouseClickEvent(self, ev):
if ev.double(): if ev.double():
print("double click") print("double click")
else: else:
print("click") print("click")
ev.accept() ev.accept()
#def mouseDragEvent(self, ev): #def mouseDragEvent(self, ev):
#print "drag" #print "drag"
#ev.accept() #ev.accept()
#self.setPos(self.pos() + ev.pos()-ev.lastPos()) #self.setPos(self.pos() + ev.pos()-ev.lastPos())
vb = pg.ViewBox() vb = pg.ViewBox()
win.setCentralItem(vb) win.setCentralItem(vb)
@ -56,6 +58,9 @@ prox.setPos(100,0)
vb.addItem(prox) vb.addItem(prox)
g = pg.GridItem() g = pg.GridItem()
p = QPen(QColor(0, 255, 255))
p.setWidthF(1)
g.opts["pen"] = p
vb.addItem(g) vb.addItem(g)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -9,7 +9,7 @@ __all__ = ['GridItem']
class GridItem(UIGraphicsItem): class GridItem(UIGraphicsItem):
""" """
**Bases:** :class:`UIGraphicsItem <pyqtgraph.UIGraphicsItem>` **Bases:** :class:`UIGraphicsItem <pyqtgraph.UIGraphicsItem>`
Displays a rectangular grid of lines indicating major divisions within a coordinate system. Displays a rectangular grid of lines indicating major divisions within a coordinate system.
Automatically determines what divisions to use. Automatically determines what divisions to use.
""" """
@ -87,7 +87,7 @@ class GridItem(UIGraphicsItem):
self.picture = None self.picture = None
#UIGraphicsItem.viewRangeChanged(self) #UIGraphicsItem.viewRangeChanged(self)
#self.update() #self.update()
def paint(self, p, opt, widget): def paint(self, p, opt, widget):
#p.setPen(QtGui.QPen(QtGui.QColor(100, 100, 100))) #p.setPen(QtGui.QPen(QtGui.QColor(100, 100, 100)))
#p.drawRect(self.boundingRect()) #p.drawRect(self.boundingRect())
@ -101,22 +101,22 @@ class GridItem(UIGraphicsItem):
#p.drawLine(0, -100, 0, 100) #p.drawLine(0, -100, 0, 100)
#p.drawLine(-100, 0, 100, 0) #p.drawLine(-100, 0, 100, 0)
#print "drawing Grid." #print "drawing Grid."
def generatePicture(self): def generatePicture(self):
self.picture = QtGui.QPicture() self.picture = QtGui.QPicture()
p = QtGui.QPainter() p = QtGui.QPainter()
p.begin(self.picture) p.begin(self.picture)
vr = self.getViewWidget().rect() vr = self.getViewWidget().rect()
unit = self.pixelWidth(), self.pixelHeight() unit = self.pixelWidth(), self.pixelHeight()
dim = [vr.width(), vr.height()] dim = [vr.width(), vr.height()]
lvr = self.boundingRect() lvr = self.boundingRect()
ul = np.array([lvr.left(), lvr.top()]) ul = np.array([lvr.left(), lvr.top()])
br = np.array([lvr.right(), lvr.bottom()]) br = np.array([lvr.right(), lvr.bottom()])
texts = [] texts = []
if ul[1] > br[1]: if ul[1] > br[1]:
x = ul[1] x = ul[1]
ul[1] = br[1] ul[1] = br[1]
@ -157,6 +157,7 @@ class GridItem(UIGraphicsItem):
linePen = self.opts['pen'] linePen = self.opts['pen']
lineColor = self.opts['pen'].color() lineColor = self.opts['pen'].color()
lineWidth = self.opts['pen'].width()
lineColor.setAlpha(c) lineColor.setAlpha(c)
linePen.setColor(lineColor) linePen.setColor(lineColor)
@ -170,9 +171,9 @@ class GridItem(UIGraphicsItem):
for x in range(0, int(nl[ax])): for x in range(0, int(nl[ax])):
linePen.setCosmetic(False) linePen.setCosmetic(False)
if ax == 0: if ax == 0:
linePen.setWidthF(self.pixelWidth()) linePen.setWidthF(lineWidth * self.pixelWidth())
else: else:
linePen.setWidthF(self.pixelHeight()) linePen.setWidthF(lineWidth * self.pixelHeight())
p.setPen(linePen) p.setPen(linePen)
p1 = np.array([0.,0.]) p1 = np.array([0.,0.])
p2 = np.array([0.,0.]) p2 = np.array([0.,0.])