fereria
7/20/2019 - 12:40 PM

sample_create_node.py

# -*- coding: utf-8 -*-

import sys
import os
import os.path

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtUiTools import QUiLoader

ZOOM_MIN = -0.5
ZOOM_MAX = 2
GRID_STEP = 20


class UISample(QtWidgets.QDialog):

    def __init__(self, parent=None):

        super(UISample, self).__init__((parent))

        self.view = NodeView()
        layout  = QtWidgets.QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.view)

        self.scene = NodeScene()
        self.view.setScene(self.scene)
        
        # Item追加
        item = BaseNodeItem()
        self.scene.addItem(item)


class NodeView(QtWidgets.QGraphicsView):

    def __init__(self, parent=None):
        super(NodeView, self).__init__(parent)

    def wheelEvent(self, e):
        delta = e.delta()
        adjust = (delta / 120) * 0.1
        self.set_zoom(adjust)

    def set_zoom(self, value):

        scale = 0.0
        # 今のズーム率 指定外にはならないようにする
        zoom = self.get_zoom()
        if ZOOM_MIN >= zoom:
            if value < 0.0:
                return
        if ZOOM_MAX <= zoom:
            if value > 0.0:
                return
        scale = 0.9 if value < 0.0 else 1.1
        self.scale(scale, scale)

    def get_zoom(self):
        transform = self.transform()
        cur_scale = (transform.m11(), transform.m22())
        return float('{:0.2f}'.format(cur_scale[0] - 1.0))


class NodeScene(QtWidgets.QGraphicsScene):

    sel_item = None

    def __init__(self, parent=None):
        super(NodeScene, self).__init__(parent)

    def mousePressEvent(self, e):
        if e.button() == QtCore.Qt.LeftButton:
            self.sel_item = self.itemAt(e.scenePos(), QtGui.QTransform())
            self.mouse_pos = e.scenePos()

    def mouseMoveEvent(self, e):
        cur = e.scenePos()
        val = cur - self.mouse_pos
        if self.sel_item is not None:
            self.sel_item.moveBy(val.x(), val.y())
            self.mouse_pos = cur
            self.update()

    def viewer(self):
        return self.views()[0] if self.views() else None

    def mouseReleaseEvent(self, e):
        self.mouse_pos = None
        self.sel_item = None

    def drawBackground(self, painter, rect):
        # Grid表示をする
        painter.setPen(QtGui.QColor(200, 200, 255, 125))

        # 横線を作る
        st = rect.top() + GRID_STEP
        scene_height = rect.bottom()
        if rect.top() < -1 * GRID_STEP:
            st = rect.top()
            scene_height -= rect.top()

        for i in range(int(scene_height / GRID_STEP) + 1):
            # start_x,y end_xy
            line_val = st + (i * GRID_STEP)
            painter.drawLine(rect.left(), line_val, rect.right(), line_val)

         # 縦線を作る
        st = rect.left()
        scene_width = rect.right()
        if st < GRID_STEP * -1:
            st = rect.left()
            scene_width -= rect.left()

        for i in range(int(scene_width / GRID_STEP) + 1):
            line_val = st + (i * GRID_STEP)
            painter.drawLine(line_val, rect.top(), line_val, rect.bottom())


class BaseNodeItem(QtWidgets.QGraphicsItem):

    def __init__(self, parent=None):

        super(BaseNodeItem, self).__init__(parent)

        self._width = 200
        self._height = 100
        
        self.color = [255,0,0]
        

    def boundingRect(self):

        return QtCore.QRectF(0.0, 0.0, self._width, self._height)

    def painter(self, painter, option, widget):
        
        margin = 20
        rect = self.boundingRect()        

        dis_rect = QtCore.QRectF(rect.left() - (margin / 2),
                                 rect.top() - (margin / 2),
                                 rect.width() + margin,
                                 rect.height() + margin)
                                 
        pen = QtGui.QPen(QtGui.QColor(*self.color), 8)
        bg_color = QtGui.QColor(*self.color)
        painter.setPen(pen)
        painter.setBrush(bg_color)
        
        painter.drawRoundedRect(dis_rect, 5, 5)
        


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    a = UISample()
    a.show()
    sys.exit(app.exec_())