def shape(self): if self._rect is not None: p = QPainterPath() p.addRect(self.boundingRect()) return p else: return super().shape()
def test_shapeFromPath(self): path = QPainterPath() path.addRect(10, 10, 20, 20) pen = QPen(QColor("#FFF"), 2.0) path = shapeFromPath(path, pen) self.assertGreaterEqual(area(path.controlPointRect()), (20 + 2.0)**2)
def shape(self): """ Returns the shape of this item as a QPainterPath in local coordinates. """ path = QPainterPath() path.addRect(self.rect()) if self.isSelected(): for shape in self.handles.values(): path.addEllipse(shape) return path
def paintEvent(self, QPaintEvent): back = (numpy.ones((100, 200, 300)) * 0).astype(numpy.uint8) back[0:60, 0:60, 0:60] = 255 ol = (numpy.zeros((100, 200, 300))).astype(numpy.uint8) ol[0:99, 0:99, 0:99] = 120 ol[0:99, 120:140, 0:99] = 255 path = QPainterPath() path.addRect(20, 20, 60, 60) path.moveTo(0, 0) path.cubicTo(99, 0, 50, 50, 99, 99) path.cubicTo(0, 99, 50, 50, 0, 0) painter = QPainter(self) painter.fillRect(0, 0, 100, 100, Qt.Qt.red)
def Path_toQtPath(geom): p = QPainterPath() anchor, points = geom if len(points) > 1: p.moveTo(*points[0]) for (x, y) in points[1:]: p.lineTo(x, y) elif len(points) == 1: r = QRectF(0, 0, 1e-0, 1e-9) r.moveCenter(*points[0]) p.addRect(r) elif len(points) == 0: r = QRectF(0, 0, 1e-16, 1e-16) r.moveCenter(QPointF(*anchor)) p.addRect(r) return p
def path_from_graphics(graphics): """ Return a constructed `QPainterPath` for a KEGG pathway graphics element. """ path = QPainterPath() x, y, w, h = [int(graphics.get(c, 0)) for c in ["x", "y", "width", "height"]] type = graphics.get("type", "rectangle") if type == "rectangle": path.addRect(QRectF(x - w / 2, y - h / 2, w, h)) elif type == "roundrectangle": path.addRoundedRect(QRectF(x - w / 2, y - h / 2, w, h), 10, 10) elif type == "circle": path.addEllipse(QRectF(x - w / 2, y - h / 2, w, h)) else: ValueError("Unknown graphics type %r." % type) return path
def updateSelectionRect(self, event): pos = event.scenePos() buttonDownPos = event.buttonDownScenePos(Qt.LeftButton) rect = QRectF(pos, buttonDownPos).normalized() rect = rect.intersected(self.sceneRect()) if not self.selectionRect: self.selectionRect = QGraphicsRectItem() self.selectionRect.setBrush(QColor(10, 10, 10, 20)) self.selectionRect.setPen(QPen(QColor(200, 200, 200, 200))) self.addItem(self.selectionRect) self.selectionRect.setRect(rect) if event.modifiers() & Qt.ControlModifier or \ event.modifiers() & Qt.ShiftModifier: path = self.selectionArea() else: path = QPainterPath() path.addRect(rect) self.setSelectionArea(path) self.selectionRectPointChanged.emit(pos)
def keyPressEvent(self, event): if event.matches(QKeySequence.Delete): if self.mode == TrackingScene.RemoveCell: c_ids = [] for item in self.selectedCells(): c_ids = item.cell_id if c_ids: self.planRemoveCells(c_ids) elif self.mode != TrackingScene.AddCell: pt_ids = [] for item in self.selectedPoints(): pt_ids.append(item.pt_id) if pt_ids: self.planRemovePoints(pt_ids) elif event.key() == Qt.Key_Delete and event.modifiers() | Qt.ShiftModifier: self.delete_act.trigger() elif event.matches(QKeySequence.ZoomIn): self.zoomIn.emit() elif event.matches(QKeySequence.ZoomOut): self.zoomOut.emit() elif event.matches(QKeySequence.SelectAll): path = QPainterPath() path.addRect(self.sceneRect()) self.setSelectionArea(path)
def __updateFrame(self): rect = self.geometry() rect.moveTo(0, 0) path = QPainterPath() path.addRect(rect) self.__framePathItem.setPath(path)
def _updatePP(self): """Create the neck geometry updating this item's QPainterPath. Return None. """ self.prepareGeometryChange() pp = QPainterPath() stringSpan = (self.nStrings - 1) * self.stringSpacing nutWidth = stringSpan + self.stringEdgeOffset * 2 # frets scaleLen = 25.5 offset = 0.0 # previous fret x coordinate self.fretXs = [0.0] for n in range(self.nFrets): pos = offset + (scaleLen - offset) / 17.817 self.fretXs.append(pos) pp.moveTo(pos, nutWidth) pp.lineTo(pos, 0.0) offset = pos # marker dots y = nutWidth / 2.0 for n in [3, 5, 7, 9, 12, 15, 17, 19, 21, 24]: if n > self.nFrets: break fretX1 = self.fretXs[n-1] fretX2 = self.fretXs[n] x = fretX1 + (fretX2 - fretX1) / 2.0 d = self.markerDia r = d / 2.0 dy = nutWidth / 4.0 if n % 12 == 0: pp.addEllipse(x-r, y-r-dy, d, d) pp.addEllipse(x-r, y-r+dy, d, d) else: pp.addEllipse(x-r, y-r, d, d) # strings self.stringYs = [] endX = self.fretXs[-1] for n in range(self.nStrings): y = self.stringEdgeOffset + self.stringSpacing * n self.stringYs.append(y) pp.moveTo(-self.nutThickness - self.fretXs[1] / 2.0, y) pp.lineTo(endX, y) # outline pp.addRect(0, 0, self.fretXs[-1], nutWidth) # nut pp.addRect(-self.nutThickness, 0, self.nutThickness, nutWidth) # partial headstock, to allow room for open note display # upper curve r = 2.0 d = self.fretXs[1] / 2.0 rectL = -self.nutThickness - r rect = QRectF(rectL, -r*2.0, r*2.0, r*2.0) ra = asin(d / r) da = degrees(ra) pp.arcMoveTo(rect, 270.0) pp.arcTo(rect, 270.0, -da) # lower curve rect = QRectF(rectL, nutWidth, r*2.0, r*2.0) pp.arcMoveTo(rect, 90.0) pp.arcTo(rect, 90.0, da) # x coordinate of open string note markers self.openX = (-self.nutThickness - sin(ra) * r) / 2.0 self.setPath(pp)
def getSymbolFromCmnd(self, symbol): ''' Returns a SymbolPath of the specified symbol. Recognized symbols are: '.' (period): filled circle 'o' (lowercase oh): unfilled circle '+': plus mark 'x' (lowercase ex): x mark '*': asterisk '^': triangle "#": square The path is drawn for a 100 x 100 unit square where the origin is in the center of the square. ''' # check if this symbol has already been created try: sympath = self.__symbolpaths[symbol] return sympath except KeyError: pass # new symbol - create a SymbolPath for it if symbol == '.': path = QPainterPath() path.addEllipse(-10.0, -10.0, 20.0, 20.0) sympath = SymbolPath(path, True) elif symbol == 'o': path = QPainterPath() path.addEllipse(-40.0, -40.0, 80.0, 80.0) sympath = SymbolPath(path, False) elif symbol == 'x': path = QPainterPath(QPointF(-30.0, -30.0)) path.lineTo(30.0, 30.0) path.moveTo(-30.0, 30.0) path.lineTo(30.0, -30.0) sympath = SymbolPath(path, False) elif symbol == '+': path = QPainterPath(QPointF(0.0, -40.0)) path.lineTo(0.0, 40.0) path.moveTo(-40.0, 0.0) path.lineTo(40.0, 0.0) sympath = SymbolPath(path, False) elif symbol == '*': path = QPainterPath(QPointF(0.0, -40.0)) path.lineTo(0.0, 40.0) path.moveTo(-34.641, -20.0) path.lineTo(34.641, 20.0) path.moveTo(-34.641, 20.0) path.lineTo(34.641, -20.0) sympath = SymbolPath(path, False) elif symbol == '^': path = QPainterPath(QPointF(-40.0, 30.0)) path.lineTo(0.0, -39.282) path.lineTo(40.0, 30.0) path.closeSubpath() sympath = SymbolPath(path, False) elif symbol == '#': path = QPainterPath() path.addRect(-35.0, -35.0, 70.0, 70.0) sympath = SymbolPath(path, False) else: raise ValueError("Unrecognized symbol '%s'" % str(symbol)) # save and return the SymbolPath self.__symbolpaths[symbol] = sympath return sympath
def shape(self): path = QPainterPath() path.addRect(self.boundingRect()) return path
# from lazyflow.operators import OpCompressedCache from PyQt4 import QtCore, uic from PyQt4.QtGui import QApplication, QPainter, QPainterPath from PyQt4 import QtGui from PyQt4 import Qt app = QtGui.QApplication([]) scene = QtGui.QGraphicsScene() scene.addText("Hello World!") back = (numpy.ones((100, 200, 300)) * 0).astype(numpy.uint8) back[0:60, 0:60, 0:60] = 255 ol = (numpy.zeros((100, 200, 300))).astype(numpy.uint8) ol[0:99, 0:99, 0:99] = 120 ol[0:99, 120:140, 0:99] = 255 path = QPainterPath() path.addRect(20, 20, 60, 60) path.moveTo(0, 0) path.cubicTo(99, 0, 50, 50, 99, 99) path.cubicTo(0, 99, 50, 50, 0, 0) scene.addPath(path) view = QtGui.QGraphicsView(scene) view.show() app.exec_()
class PlotSelectionTool(PlotTool): """ A tool for selecting a region on a plot. """ #: Selection modes Rect, Lasso = 1, 2 #: Selection was started by the user. selectionStarted = Signal(QPainterPath) #: The current selection has been updated selectionUpdated = Signal(QPainterPath) #: The selection has finished (user has released the mouse button) selectionFinished = Signal(QPainterPath) def __init__(self, parent=None, selectionMode=Rect, **kwargs): super().__init__(parent, **kwargs) self.__mode = selectionMode self.__path = None self.__item = None def setSelectionMode(self, mode): """ Set the selection mode (rectangular or lasso selection). Parameters ---------- mode : int PlotSelectionTool.Rect or PlotSelectionTool.Lasso """ assert mode in {PlotSelectionTool.Rect, PlotSelectionTool.Lasso} if self.__mode != mode: if self.__path is not None: self.selectionFinished.emit() self.__mode = mode self.__path = None def selectionMode(self): """ Return the current selection mode. """ return self.__mode def selectionShape(self): """ Return the current selection shape. This is the area selected/drawn by the user. Returns ------- shape : QPainterPath The selection shape in view coordinates. """ if self.__path is not None: shape = QPainterPath(self.__path) shape.closeSubpath() else: shape = QPainterPath() viewbox = self.viewBox() if viewbox is None: return QPainterPath() return viewbox.childGroup.mapFromParent(shape) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: pos = event.pos() if self.__mode == PlotSelectionTool.Rect: rect = QRectF(pos, pos) self.__path = QPainterPath() self.__path.addRect(rect) else: self.__path = QPainterPath() self.__path.moveTo(event.pos()) self.selectionStarted.emit(self.selectionShape()) self.__updategraphics() event.accept() return True else: return False def mouseMoveEvent(self, event): if event.buttons() & Qt.LeftButton: if self.__mode == PlotSelectionTool.Rect: rect = QRectF(event.buttonDownPos(Qt.LeftButton), event.pos()) self.__path = QPainterPath() self.__path.addRect(rect) else: self.__path.lineTo(event.pos()) self.selectionUpdated.emit(self.selectionShape()) self.__updategraphics() event.accept() return True else: return False def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton: if self.__mode == PlotSelectionTool.Rect: rect = QRectF(event.buttonDownPos(Qt.LeftButton), event.pos()) self.__path = QPainterPath() self.__path.addRect(rect) else: self.__path.lineTo(event.pos()) self.selectionFinished.emit(self.selectionShape()) self.__path = QPainterPath() self.__updategraphics() event.accept() return True else: return False def __updategraphics(self): viewbox = self.viewBox() if viewbox is None: return if self.__path.isEmpty(): if self.__item is not None: self.__item.setParentItem(None) viewbox.removeItem(self.__item) if self.__item.scene(): self.__item.scene().removeItem(self.__item) self.__item = None else: if self.__item is None: item = QtGui.QGraphicsPathItem() color = QtGui.QColor(Qt.yellow) item.setPen(QtGui.QPen(color, 0)) color.setAlpha(50) item.setBrush(QtGui.QBrush(color)) self.__item = item viewbox.addItem(item) self.__item.setPath(self.selectionShape())