scieditor2 3 version 1

2019/07/31 23:22 by yamasyuh68
  :追加された部分   :削除された部分
(差分が大きい場合、文字単位では表示しません)
hogehoge
```
import sys , os
from PyQt5.QtWidgets import (QApplication, QGraphicsItem, QGraphicsScene, QGraphicsView,QMenu,QAction,
        QGraphicsDropShadowEffect ,QGraphicsPathItem )
# from PyQt5.QtSvg import QGraphicsSvgItem
from PyQt5.QtGui import  QPixmap , QColor , QPainterPath ,QPen,QCursor,QImage,QPainter,QTransform
from PyQt5.QtCore import Qt,QSizeF ,QPoint,QPointF ,Qt,QDataStream,QIODevice,QDataStream,QFile,QRect,QRectF


# Qt.SizeVerCursor
# Qt.SizeHorCursor
# Qt.SizeBDiagCursor
# Qt.SizeFDiagCursor
# Qt.SizeAllCursor

class pathitem(QGraphicsPathItem):
    def __init__(self,filename):
        super().__init__(filename)
        # self.setCursor(Qt.OpenHandCursor)
        self.setAcceptedMouseButtons(Qt.LeftButton)
        self.setFlag(QGraphicsItem.ItemIsSelectable,1)
        self.setFlag(QGraphicsItem.ItemIsMovable,1)
        self.setGraphicsEffect(QGraphicsDropShadowEffect())
        self.setAcceptHoverEvents(1)

        self.angle=0
        self.scale=1.0
        self.distance=5.0
        self.radius=1.0
        self.x=1
        self.y=1
        self.original = self.path() # originalpath
        self.pos=None # key press position --use drag
        self.dragflag=0
        self.drawflag=0

        self.setTransformOriginPoint(50,50)
        self.makecrect()

    def makecrect(self):
        a=8 # size of drag rect
        br=self.boundingRect()
        crect=[ QRectF(0,0,a,a) , QRectF(br.width()/2 -a/2 , br.height()-a , a,a ) ,
                QRectF(br.width() -a , br.height()-a , a,a ) ,
                QRectF(br.width() -a , br.height()/2-a/2 , a,a )    ]

        self.crect=[]
        for i,l in enumerate(crect) :
            self.crect.append(QPainterPath())
            self.crect[i].addRect( l )

    def hoverEnterEvent(self,e):
        # print('kita enter',e.pos()  )
        # print(self.boundingRect())
        self.drawflag=1
    def hoverLeaveEvent(self,e):
        self.drawflag=0
    def hoverMoveEvent(self,event):
        self.dragflag=0
        for i,l in enumerate(self.crect):
            if l.contains(event.pos()):
                self.dragflag=i+1
                break
        if self.dragflag==4:
            self.setCursor(Qt.SizeHorCursor)
        elif self.dragflag==2:
            self.setCursor(Qt.SizeVerCursor)
        elif self.dragflag==3:
            self.setCursor(Qt.SizeFDiagCursor)
        else:
            self.setCursor(Qt.SizeAllCursor)
        # print('kita Move',e.pos() )

    def mousePressEvent(self, event):
        # self.dragflag=0
        if self.dragflag:
            self.pos=event.pos()
        else :
            self.setCursor(Qt.ClosedHandCursor)
            super().mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        # self.setCursor(Qt.OpenHandCursor)
        self.dragflag=0
        super().mouseReleaseEvent(event)

    def mouseMoveEvent(self, event):
        if self.dragflag==4 :
            dx= (event.pos().x()-self.pos.x())
            self.x = (dx/self.original.boundingRect().width())+1
        elif self.dragflag==2 :
            dy= (event.pos().y()-self.pos.y())
            self.y=(dy/self.original.boundingRect().height())+1
        elif self.dragflag==3 :
            dy= (event.pos().y()-self.pos.y())
            self.y=(dy/self.original.boundingRect().height())+1
            dx= (event.pos().x()-self.pos.x())
            self.x=(dx/self.original.boundingRect().width())+1
        else:
            super().mouseMoveEvent(event)
            return

        qt = QTransform()
        qt.scale( self.x, self.y )
        # self.setPath( qt.map( self.original ) )
        self.setTransform(qt,0)
        self.makecrect()


    def keyPressEvent(self, event):

        if (event.modifiers() & Qt.ControlModifier): # ctrl
            if (event.key() == Qt.Key_Up):
                self.y += 0.1
            elif (event.key() == Qt.Key_Down):
                self.y -= 0.1
            elif (event.key() == Qt.Key_Q):
                self.setPath( self.original )
                self.y=1
                return
            elif (event.key() == Qt.Key_Right):
                self.x += 0.1
            elif (event.key() == Qt.Key_Left):
                self.x -= 0.1

            qt = QTransform()
            qt.scale( self.x, self.y )
            self.setPath( qt.map( self.original ) )
            # self.setTransform(qt,0)
            # print(self.boundingRect())
            self.makecrect()


            return

        if (event.modifiers() & Qt.ShiftModifier): # shift
            if (event.key() == Qt.Key_Up):
                self.distance += 0.5
            elif (event.key() == Qt.Key_Down):
                self.distance -= 0.5
            elif (event.key() == Qt.Key_Right):
                self.radius += 0.5
            elif (event.key() == Qt.Key_Left):
                self.radius -= 0.5
            self.graphicsEffect().setOffset(self.distance)
            self.graphicsEffect().setBlurRadius(self.distance)
            return

        if (event.key() == Qt.Key_Right):
            self.angle += 1
        elif (event.key() == Qt.Key_Left):
            self.angle -= 1
        elif (event.key() == Qt.Key_Up):
            self.scale += 0.1
        elif (event.key() == Qt.Key_Down):
            self.scale -= 0.1
        # elif (event.key() == Qt.Key_Escape):
        #     return

        self.setTransformOriginPoint(50,50)

        self.setScale(self.scale)
        self.setRotation(self.angle)
        self.makecrect()
        # print(self.boundingRect())

    def wheelEvent (self, e):#  mousewheel 引いたら-yが128
        if (e.modifiers() & Qt.ShiftModifier): # shift
            if e.delta() < 0 : # increase
                self.scale += 0.1
            else:
                self.scale -= 0.1
            self.setScale(self.scale)
        else:
            if e.delta() < 0 : # increase
                self.angle += 1
            else:
                self.angle -= 1
            self.setRotation(self.angle)

    def paint(self,painter,option,n):
        if self.drawflag:
            painter.setPen( QPen( Qt.red , 2, style=Qt.SolidLine ) )
            # painter.setBrush(  Qt.red )
            painter.drawPath(self.path())
    
            painter.setPen( QPen( Qt.black , 2, style=Qt.SolidLine ) )
            for l in self.crect :
                painter.drawPath(l )
 




class SvgView(QGraphicsView):
    def __init__(self, parent=None):
        super(SvgView, self).__init__(parent)
        self.pixmap = QPixmap(r'e:\Programs\python\qt\svg\cat.png')

        fname=os.path.join(os.path.dirname(__file__) , 'qtpath.qtdata')
        f = QFile(fname)
        f.open( QIODevice.ReadOnly)
        ds = QDataStream(f)
        path=QPainterPath()
        ds.__rshift__(path)
        f.close() 
        self.path=QGraphicsPathItem(path)

        p=QPainterPath()
        p.addRect(0,0,100,100)
        pa=pathitem(p)

        self.setScene(QGraphicsScene(self))
        self.pix = self.scene().addPixmap(self.pixmap)
        self.scene().addItem(self.path)
        self.scene().addItem(pa)
        self.path.setParentItem(pa)

        self.scene().selectionChanged.connect(self.selectionChanged)
        self.selected=None

        # self.setMouseTracking (True)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect( self.custommenu )

    def custommenu(self):
        menu=QMenu(self)
        action = QAction('SavePicture', self)
        action.triggered.connect(self.savepicture)
        menu.addAction(action)
        menu.exec_(QCursor.pos ())   

    def savepicture(self):
        w,h=self.pixmap.width(),self.pixmap.height()
        vw,vh = self.viewport().rect().width(),self.viewport().rect().height()
        vsx,vsy=self.mapToScene( QPoint(0,0) ).x(),self.mapToScene( QPoint(0,0) ).y() 

        if w > vw :
            x= -1*self.horizontalScrollBar().value()
        else:
            x= -vsx
        if h > vh :
            y= -1*self.verticalScrollBar().value()
        else:
            y=-vsy

        qi=QImage(w,h ,QImage.Format_RGB32)
        pa=QPainter()
        pa.begin(qi)
        self.render(pa , source=QRect(x , y , w,h ) ) 
        qi.save(r'e:\Programs\python\qt\GraphicsScene\savescene.png')
        pa.end()

    def selectionChanged(self):
        if self.selected :
            self.selected.ungrabKeyboard()
        p=self.scene().selectedItems()
        if len(p) >= 1 :
            p=p[0]
            p.grabKeyboard()
            self.selected=p
    def keyPressEvent(self, event): # jikken
        if event.key() == Qt.Key_A :
            print( self.mapToScene( QPoint(0,0) ).x() )



        



app = QApplication(sys.argv)
window = SvgView()
window.show()
sys.exit(app.exec_())
```      
import sys , os
from PyQt5.QtWidgets import (QApplication, QGraphicsItem, QGraphicsScene, QGraphicsView,QMenu,QAction,
        QGraphicsDropShadowEffect ,QGraphicsPathItem )
# from PyQt5.QtSvg import QGraphicsSvgItem
from PyQt5.QtGui import  QPixmap , QColor , QPainterPath ,QPen,QCursor,QImage,QPainter,QTransform
from PyQt5.QtCore import Qt,QSizeF ,QPoint,QPointF ,Qt,QDataStream,QIODevice,QDataStream,QFile,QRect,QRectF


# Qt.SizeVerCursor
# Qt.SizeHorCursor
# Qt.SizeBDiagCursor
# Qt.SizeFDiagCursor
# Qt.SizeAllCursor

class pathitem(QGraphicsPathItem):
    def __init__(self,filename):
        super().__init__(filename)
        # self.setCursor(Qt.OpenHandCursor)
        self.setAcceptedMouseButtons(Qt.LeftButton)
        self.setFlag(QGraphicsItem.ItemIsSelectable,1)
        self.setFlag(QGraphicsItem.ItemIsMovable,1)
        self.setGraphicsEffect(QGraphicsDropShadowEffect())
        self.setAcceptHoverEvents(1)

        self.angle=0
        self.scale=1.0
        self.distance=5.0
        self.radius=1.0
        self.x=1
        self.y=1
        self.original = self.path() # originalpath
        self.pos=None # key press position --use drag
        self.dragflag=0
        self.drawflag=0

        self.setTransformOriginPoint(50,50)
        self.makecrect()

    def makecrect(self):
        a=8 # size of drag rect
        br=self.boundingRect()
        crect=[ QRectF(0,0,a,a) , QRectF(br.width()/2 -a/2 , br.height()-a , a,a ) ,
                QRectF(br.width() -a , br.height()-a , a,a ) ,
                QRectF(br.width() -a , br.height()/2-a/2 , a,a )    ]

        self.crect=[]
        for i,l in enumerate(crect) :
            self.crect.append(QPainterPath())
            self.crect[i].addRect( l )

    def hoverEnterEvent(self,e):
        # print('kita enter',e.pos()  )
        # print(self.boundingRect())
        self.drawflag=1
    def hoverLeaveEvent(self,e):
        self.drawflag=0
    def hoverMoveEvent(self,event):
        self.dragflag=0
        for i,l in enumerate(self.crect):
            if l.contains(event.pos()):
                self.dragflag=i+1
                break
        if self.dragflag==4:
            self.setCursor(Qt.SizeHorCursor)
        elif self.dragflag==2:
            self.setCursor(Qt.SizeVerCursor)
        elif self.dragflag==3:
            self.setCursor(Qt.SizeFDiagCursor)
        else:
            self.setCursor(Qt.SizeAllCursor)
        # print('kita Move',e.pos() )

    def mousePressEvent(self, event):
        # self.dragflag=0
        if self.dragflag:
            self.pos=event.pos()
        else :
            self.setCursor(Qt.ClosedHandCursor)
            super().mousePressEvent(event)

    def mouseReleaseEvent(self, event):
        # self.setCursor(Qt.OpenHandCursor)
        self.dragflag=0
        super().mouseReleaseEvent(event)

    def mouseMoveEvent(self, event):
        if self.dragflag==4 :
            dx= (event.pos().x()-self.pos.x())
            self.x = (dx/self.original.boundingRect().width())+1
        elif self.dragflag==2 :
            dy= (event.pos().y()-self.pos.y())
            self.y=(dy/self.original.boundingRect().height())+1
        elif self.dragflag==3 :
            dy= (event.pos().y()-self.pos.y())
            self.y=(dy/self.original.boundingRect().height())+1
            dx= (event.pos().x()-self.pos.x())
            self.x=(dx/self.original.boundingRect().width())+1
        else:
            super().mouseMoveEvent(event)
            return

        qt = QTransform()
        qt.scale( self.x, self.y )
        # self.setPath( qt.map( self.original ) )
        self.setTransform(qt,0)
        self.makecrect()


    def keyPressEvent(self, event):

        if (event.modifiers() & Qt.ControlModifier): # ctrl
            if (event.key() == Qt.Key_Up):
                self.y += 0.1
            elif (event.key() == Qt.Key_Down):
                self.y -= 0.1
            elif (event.key() == Qt.Key_Q):
                self.setPath( self.original )
                self.y=1
                return
            elif (event.key() == Qt.Key_Right):
                self.x += 0.1
            elif (event.key() == Qt.Key_Left):
                self.x -= 0.1

            qt = QTransform()
            qt.scale( self.x, self.y )
            self.setPath( qt.map( self.original ) )
            # self.setTransform(qt,0)
            # print(self.boundingRect())
            self.makecrect()


            return

        if (event.modifiers() & Qt.ShiftModifier): # shift
            if (event.key() == Qt.Key_Up):
                self.distance += 0.5
            elif (event.key() == Qt.Key_Down):
                self.distance -= 0.5
            elif (event.key() == Qt.Key_Right):
                self.radius += 0.5
            elif (event.key() == Qt.Key_Left):
                self.radius -= 0.5
            self.graphicsEffect().setOffset(self.distance)
            self.graphicsEffect().setBlurRadius(self.distance)
            return

        if (event.key() == Qt.Key_Right):
            self.angle += 1
        elif (event.key() == Qt.Key_Left):
            self.angle -= 1
        elif (event.key() == Qt.Key_Up):
            self.scale += 0.1
        elif (event.key() == Qt.Key_Down):
            self.scale -= 0.1
        # elif (event.key() == Qt.Key_Escape):
        #     return

        self.setTransformOriginPoint(50,50)

        self.setScale(self.scale)
        self.setRotation(self.angle)
        self.makecrect()
        # print(self.boundingRect())

    def wheelEvent (self, e):#  mousewheel 引いたら-yが128
        if (e.modifiers() & Qt.ShiftModifier): # shift
            if e.delta() < 0 : # increase
                self.scale += 0.1
            else:
                self.scale -= 0.1
            self.setScale(self.scale)
        else:
            if e.delta() < 0 : # increase
                self.angle += 1
            else:
                self.angle -= 1
            self.setRotation(self.angle)

    def paint(self,painter,option,n):
        if self.drawflag:
            painter.setPen( QPen( Qt.red , 2, style=Qt.SolidLine ) )
            # painter.setBrush(  Qt.red )
            painter.drawPath(self.path())
    
            painter.setPen( QPen( Qt.black , 2, style=Qt.SolidLine ) )
            for l in self.crect :
                painter.drawPath(l )
 




class SvgView(QGraphicsView):
    def __init__(self, parent=None):
        super(SvgView, self).__init__(parent)
        self.pixmap = QPixmap(r'e:\Programs\python\qt\svg\cat.png')

        fname=os.path.join(os.path.dirname(__file__) , 'qtpath.qtdata')
        f = QFile(fname)
        f.open( QIODevice.ReadOnly)
        ds = QDataStream(f)
        path=QPainterPath()
        ds.__rshift__(path)
        f.close() 
        self.path=QGraphicsPathItem(path)

        p=QPainterPath()
        p.addRect(0,0,100,100)
        pa=pathitem(p)

        self.setScene(QGraphicsScene(self))
        self.pix = self.scene().addPixmap(self.pixmap)
        self.scene().addItem(self.path)
        self.scene().addItem(pa)
        self.path.setParentItem(pa)

        self.scene().selectionChanged.connect(self.selectionChanged)
        self.selected=None

        # self.setMouseTracking (True)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.customContextMenuRequested.connect( self.custommenu )

    def custommenu(self):
        menu=QMenu(self)
        action = QAction('SavePicture', self)
        action.triggered.connect(self.savepicture)
        menu.addAction(action)
        menu.exec_(QCursor.pos ())   

    def savepicture(self):
        w,h=self.pixmap.width(),self.pixmap.height()
        vw,vh = self.viewport().rect().width(),self.viewport().rect().height()
        vsx,vsy=self.mapToScene( QPoint(0,0) ).x(),self.mapToScene( QPoint(0,0) ).y() 

        if w > vw :
            x= -1*self.horizontalScrollBar().value()
        else:
            x= -vsx
        if h > vh :
            y= -1*self.verticalScrollBar().value()
        else:
            y=-vsy

        qi=QImage(w,h ,QImage.Format_RGB32)
        pa=QPainter()
        pa.begin(qi)
        self.render(pa , source=QRect(x , y , w,h ) ) 
        qi.save(r'e:\Programs\python\qt\GraphicsScene\savescene.png')
        pa.end()

    def selectionChanged(self):
        if self.selected :
            self.selected.ungrabKeyboard()
        p=self.scene().selectedItems()
        if len(p) >= 1 :
            p=p[0]
            p.grabKeyboard()
            self.selected=p
    def keyPressEvent(self, event): # jikken
        if event.key() == Qt.Key_A :
            print( self.mapToScene( QPoint(0,0) ).x() )



        



app = QApplication(sys.argv)
window = SvgView()
window.show()
sys.exit(app.exec_())