【PyQt4实例12】Graphics Item 的各种变形

#-*- coding:utf8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import math

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf-8"))

class PixItem(QGraphicsItem):
    def __init__(self,QPixmap):
        super(PixItem,self).__init__()
        self.pix = QPixmap
    
    def boundingRect(self):
        return QRectF(-2 - self.pix.width()/2,-2 - self.pix.height()/2,self.pix.width() + 4,self.pix.height() + 4)
    
    def paint(self,painter,option,widget):
        painter.drawPixmap(-self.pix.width()/2,-self.pix.height()/2,self.pix)

class MainWidget(QWidget):
    def __init__(self):
        super(MainWidget,self).__init__()

        self.angle = 0
        self.scale = 5
        self.shear = 5
        self.translate =50
        
        self.scene = QGraphicsScene()
        self.scene.setSceneRect(-200,-200,400,400)
        self.pixmap = QPixmap("image/butterfly.png")
        self.item = PixItem(self.pixmap)
        
        self.scene.addItem(self.item)
        self.item.setPos(0,0)
        
        self.view = QGraphicsView()
        self.view.setScene(self.scene)
        self.view.setMinimumSize(400,400)
        
        self.ctrlFrame = QFrame()
        self.createControllFrame()
        
        self.rightlayout = QVBoxLayout()
        self.rightlayout.addWidget(self.rotateGroup)
        self.rightlayout.addWidget(self.scaleGroup)
        self.rightlayout.addWidget(self.shearGroup)
        self.rightlayout.addWidget(self.translateGroup)

        
        self.leftlayout = QHBoxLayout()
        self.leftlayout.addWidget(self.view)
        self.leftlayout.addWidget(self.ctrlFrame)        
        
        self.mainlayout = QHBoxLayout()
        self.mainlayout.addLayout(self.leftlayout)
        self.mainlayout.addLayout(self.rightlayout)
        self.setLayout(self.mainlayout)
        
        self.setWindowTitle(self.tr("Graphics Item 的各种变形")) 
    
    def createControllFrame(self):
        self.rotateGroup = QGroupBox(self.tr("旋转"))
        rotateSlider = QSlider()
        rotateSlider.setOrientation(Qt.Horizontal)
        rotateSlider.setRange(0,360)
        self.connect(rotateSlider,SIGNAL("valueChanged(int)"),self.slotRotate)
        rotateLayout = QHBoxLayout()
        rotateLayout.addWidget(rotateSlider)
        self.rotateGroup.setLayout(rotateLayout)
        
        self.scaleGroup = QGroupBox(self.tr("缩放"))
        scaleSlider = QSlider()
        scaleSlider.setOrientation(Qt.Horizontal)
        self.connect(scaleSlider,SIGNAL("valueChanged(int)"),self.slotScale)
        scaleLayout = QHBoxLayout()
        scaleLayout.addWidget(scaleSlider)
        self.scaleGroup.setLayout(scaleLayout)
        
        self.shearGroup = QGroupBox(self.tr("切变"))
        shearSlider = QSlider()
        shearSlider.setOrientation(Qt.Horizontal)
        self.connect(shearSlider,SIGNAL("valueChanged(int)"),self.slotShear)
        shearLayout = QHBoxLayout()
        shearLayout.addWidget(shearSlider)
        self.shearGroup.setLayout(shearLayout)
        
        
        self.translateGroup = QGroupBox(self.tr("位移"))
        translateSlider = QSlider()
        translateSlider.setOrientation(Qt.Horizontal)
        self.connect(translateSlider,SIGNAL("valueChanged(int)"),self.slotTranslate)
        translateLayout = QHBoxLayout()
        translateLayout.addWidget(translateSlider)
        self.translateGroup.setLayout(translateLayout)
        
    def slotRotate(self,value):
        self.item.rotate(value - self.angle)
        self.angle = value
    
    def slotScale(self,value):
        if value  > self.scale:
            s = math.pow(1.1,(value - self.scale))
        else:
            s = math.pow(1/1.1,(self.scale - value))
        self.item.scale(s,s)
        self.scale = value
    
    def slotShear(self,value):
        self.item.shear((value - self.shear)/10.0,0)
        self.shear = value
    
    def slotTranslate(self,value):
        self.item.translate(value - self.translate,value - self.translate)
        self.translate = value
    
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    mainwindow = MainWidget()
    mainwindow.show()
    sys.exit(app.exec_())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值