1

I'm trying to create small GUI application, where user will be able to draw points and curves. I implemented scale option.

I would like to scale only paths, not graphic representations of the points (RBNode class). Is there way to scale QGraphicsView with exception of RBNode class?

My current approach is to pass factor attribute created in wheelEvent method inside RBGraphicView class (which inherits from QGraphicsView) into the instances of RBNode and use it to redraw RBNode.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import math
from PySide.QtGui import *
from PySide.QtCore import *


class RBNode(QGraphicsItem):
    def __init__(self, factorView = 1):
        super(RBNode, self).__init__()
        self.factor = factorView
        self.pressed = False
        self.x = self.pos().x()
        self.y = self.pos().y()
        self.setFlag(QGraphicsItem.ItemIsMovable)

    def boundingRect(self):
        self.update()
        return QRectF(-5*self.factor,-5*self.factor,10*self.factor,10*self.factor)


    def paint(self, painter, option, widget):
        self.update()
        rect = QRectF(-5*self.factor,-5*self.factor,10*self.factor,10*self.factor)

        if self.pressed:
            painter.setBrush(Qt.red)
        else:
            painter.setBrush(Qt.darkGray)

        painter.drawEllipse(rect)

    def mousePressEvent(self, event):
        self.pressed = True
        self.update()
        QGraphicsItem.mousePressEvent(self,event)

    def mouseReleaseEvent(self, event):
        self.pressed = False
        self.update()
        QGraphicsItem.mouseReleaseEvent(self,event)


class RBGraphicView(QGraphicsView):
    def __init__(self):
        super(RBGraphicView, self).__init__()
        self.factorView = 1
        self.initScene()
        self.initGui()



    def initGui(self):
        self.setWindowTitle("A Simple Animation")
        self.show()

    def initScene(self):
        self.rbScene = QGraphicsScene(self)
        self.rbAddItem(self.rbScene)
        self.setScene(self.rbScene)

    def wheelEvent(self, event):
        factor = math.pow(2.0, -event.delta() / 240.0)
        self.scaleView(factor)

    def scaleView(self, scaleFactor):
        factor = self.matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0,0,1,1)).width()
        if factor < 0.001 or factor > 1000:
            return
        self.scale(scaleFactor, scaleFactor)
        self.factorView = factor


    def rbAddItem(self, scene):

        rbNode1 = RBNode(self.factorView)
        rbNode1.setPos(100,100)

        rbNode2 = RBNode()
        rbNode2.setPos(100,100)

        rbP2 = QPointF(20.0, 10.0)
        rbP3 = QPointF(80.0, 30.0)
        rbP4 = QPointF(90.0, 70.0)
        bezierPath = QPainterPath()
        bezierPath.moveTo(rbNode1.x, rbNode1.y)
        bezierPath.cubicTo(rbP2.x(),rbP2.y(),rbP3.x(),rbP3.y(),rbP4.x(),rbP4.y())
        myItem = QGraphicsPathItem(bezierPath)

        scene.addItem(rbNode1)
        scene.addItem(rbNode2)
        scene.addItem(myItem)

if __name__ == '__main__':
    try:
        myApp = QApplication(sys.argv)
        myView = RBGraphicView()
        myApp.exec_()
        sys.exit(0)

    except NameError:
        print("Name Error:", sys.exc_info()[1])

    except SystemExit:
        print("Closing Window...")

    except Exception:
        print(sys.exc_info()[1])
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Rafal
  • 83
  • 5

0 Answers0