Commit d5fcd499 authored by Aurélien's avatar Aurélien
Browse files

dessin de fleches pleines

parent f1bfbc28
Loading
Loading
Loading
Loading
+182 −70
Original line number Diff line number Diff line
from math import sqrt, cos, pi, sin

from PyQt5.QtGui import QFont, QPen, QColor, QPainterPath
from PyQt5.QtGui import QFont, QPen, QColor, QPainterPath, QLinearGradient
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QPointF

@@ -60,50 +60,76 @@ class MenuView(QGraphicsView):
            self.scene().removeItem(arrow)
        self.arrows = []
        self.itemsGroup = QGraphicsItemGroup()
        h = self.height()
        w = self.width()
        circleSize = 0.52 * min(h, w)
        self.drawCircles(w, h, circleSize)
        self.drawBinaryTree(circleSize, w, h)
        self.drawPlanarTree(circleSize, w, h)
        self.drawDyckWord(circleSize, w, h)
        self.drawArrow(0.5 * w - circleSize, 0.25 * h - circleSize / 2, 0.25 * w - circleSize / 2, 0.65 * h - circleSize
                       , GameEnum.PLANAR_TREE_TO_BINARY_TREE)
        self.drawArrow(0.25 * w, 0.65 * h - circleSize / 2, 0.5 * w - 0.5 * circleSize, 0.25 * h,
                       GameEnum.BINARY_TREE_TO_PLANAR_TREE)
        self.drawArrow(0.75 * w - circleSize, 0.65 * h - circleSize / 2, 0.5 * w - 0.5 * circleSize, 0.25 * h,
                       GameEnum.DYCK_WORD_TO_BINARY_TREE)
        self.drawArrow(0.5 * w, 0.25 * h - circleSize / 2, 0.75 * w - circleSize / 2, 0.65 * h - circleSize,
                       GameEnum.BINARY_TREE_TO_DYCK_WORD)
        self.drawArrow(0.25 * w - circleSize / 2 + (sqrt(2) / 2) * 0.5 * circleSize, 0.65 * h - 0.5 * circleSize +
                       (sqrt(2) / 2) * 0.5 * circleSize, 0.75 * w - 0.5 * circleSize - (sqrt(2) / 2) * 0.5 * circleSize,
                       0.65 * h - 0.5 * circleSize + (sqrt(2) / 2) * 0.5 * circleSize, GameEnum.DYCK_WORD_TO_PLANAR_TREE)
        self.drawArrow(0.25 * w - circleSize / 2 + cos(pi / 8) * 0.5 * circleSize, 0.65 * h - 0.5 * circleSize +
                       sin(pi / 8) * 0.5 * circleSize, 0.75 * w - 0.5 * circleSize - cos(pi / 8) * 0.5 * circleSize,
                       0.65 * h - 0.5 * circleSize + sin(pi / 8) * 0.5 * circleSize, GameEnum.PLANAR_TREE_TO_DYCK_WORD)
        self.h = self.height()
        self.w = self.width()
        self.c1x = 0.5 * self.w
        self.c1y = 0.25 * self.h
        self.c2x = 0.18 * self.w
        self.c2y = 0.65 * self.h
        self.c3x = 0.82 * self.w
        self.c3y = 0.65 * self.h
        self.circleSize = 0.52 * min(self.h, self.w)
        self.drawCircles()
        self.drawBinaryTree()
        self.drawPlanarTree()
        self.drawDyckWord()
        beginX = self.c2x + 0.5 * self.circleSize
        beginY = self.c2y
        endX = self.c1x
        endY = self.c1y + 0.5 * self.circleSize
        xLength = endX - beginX
        yLength = beginY - endY
        p1x = beginX + 0.1 * xLength
        p1y = beginY - 0.8 * yLength
        p2x = beginX + 0.6 * xLength
        p2y = beginY - 0.95 * yLength
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.PLANAR_TREE_TO_BINARY_TREE)
        beginX = self.c1x + self.circleSize
        beginY = self.c1y + 0.5 * self.circleSize
        endX = self.c3x + 0.5 * self.circleSize
        endY = self.c3y
        xLength = endX - beginX
        yLength = endY - beginY
        p1x = beginX + 0.6 * xLength
        p1y = beginY + 0.05 * yLength
        p2x = beginX + 0.95 * xLength
        p2y = beginY + 0.1 * yLength
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.BINARY_TREE_TO_DYCK_WORD)
        beginX = self.c3x + 0.5 * self.circleSize - (sin(pi / 3) * 0.5 * self.circleSize)
        beginY = self.c3y + 0.5 * self.circleSize + (cos(pi / 3) * 0.5 * self.circleSize)
        endX = self.c2x + 0.5 * self.circleSize + (sin(pi / 3) * 0.5 * self.circleSize)
        endY = self.c2y + 0.5 * self.circleSize + (cos(pi / 3) * 0.5 * self.circleSize)
        xLength = beginX - endX
        p1x = beginX - 0.25 * xLength
        p1y = beginY + 0.2 * self.circleSize
        p2x = beginX - 0.75 * xLength
        p2y = beginY + 0.2 * self.circleSize
        self.drawArrow(beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, GameEnum.DYCK_WORD_TO_PLANAR_TREE)


        self.scene().addItem(self.itemsGroup)
        self.scene().setSceneRect(self.itemsGroup.sceneBoundingRect())

    def drawArrow(self, beginX: float, beginY: float, endX: float, endY: float, game: GameEnum):
        arrow = Arrow(game, beginX, beginY, endX, endY, self.controller)
    def drawArrow(self, beginX: float, beginY: float, endX: float, endY: float, p1x: float, p1y: float, p2x: float,
                  p2y: float, game: GameEnum):
        arrow = Arrow(game, beginX, beginY, endX, endY, p1x, p1y, p2x, p2y, self.circleSize, self.controller)
        self.arrows.append(arrow)
        self.scene().addItem(arrow)

    def drawCircles(self, w: float, h: float, circleSize: float):
    def drawCircles(self):
        pen = QPen(QColor(23, 32, 42))
        pen.setWidth(5)
        circle1 = QGraphicsEllipseItem(0.5 * w - circleSize, 0.25 * h - circleSize, circleSize, circleSize)
        circle1 = QGraphicsEllipseItem(self.c1x, self.c1y, self.circleSize, self.circleSize)
        circle1.setPen(pen)
        circle2 = QGraphicsEllipseItem(0.25 * w - circleSize, 0.65 * h - circleSize, circleSize, circleSize)
        circle2 = QGraphicsEllipseItem(self.c2x, self.c2y, self.circleSize, self.circleSize)
        circle2.setPen(pen)
        circle3 = QGraphicsEllipseItem(0.75 * w - circleSize, 0.65 * h - circleSize, circleSize, circleSize)
        circle3 = QGraphicsEllipseItem(self.c3x, self.c3y, self.circleSize, self.circleSize)
        circle3.setPen(pen)
        self.itemsGroup.addToGroup(circle1)
        self.itemsGroup.addToGroup(circle2)
        self.itemsGroup.addToGroup(circle3)

    def drawDyckWord(self, circleSize: float, w: float, h: float):
    def drawDyckWord(self):
        word = get_model_tree().dyckWord
        newWord = ""
        for i in range(len(word)):
@@ -111,33 +137,35 @@ class MenuView(QGraphicsView):
            if i != len(word) - 1:
                newWord += " "
        dyckWord = QGraphicsTextItem(newWord)
        dyckWord.setTextWidth(circleSize)
        dyckWord.setX(0.75 * w - 0.95 * circleSize)
        dyckWord.setY(0.33 * h)
        size = int(circleSize / 8)
        dyckWord.setTextWidth(self.circleSize)
        dyckWord.setX(self.c3x + 0.05 * self.circleSize)
        dyckWord.setY(self.c3y + 0.35 * self.circleSize)
        """dyckWord.setX(0.75 * self.w - 0.95 * self.circleSize)
        dyckWord.setY(0.33 * self.h)"""
        size = int(self.circleSize / 8)
        dyckWord.setFont(QFont("Arial", size))
        self.itemsGroup.addToGroup(dyckWord)

    def drawPlanarTree(self, circleSize: float, w: float, h: float):
    def drawPlanarTree(self):
        nodePen = QPen(QColor(21, 67, 96))
        branchPen = QPen(QColor(21, 67, 96))
        nodeColor = QColor(245, 183, 177)
        lineWidth = 0.015 * circleSize
        nodeWidth = 0.12 * circleSize
        lineWidth = 0.015 * self.circleSize
        nodeWidth = 0.12 * self.circleSize
        branchPen.setWidth(lineWidth)
        branchPen.setCapStyle(Qt.RoundCap)
        nodePen.setWidth(0.1 * nodeWidth)

        x1 = 0.25 * w - circleSize / 2
        y1 = 0.65 * h - 0.9 * circleSize
        x2 = x1 - 0.25 * circleSize
        y2 = y1 + 0.35 * circleSize
        x1 = self.c2x + self.circleSize / 2
        y1 = self.c2y + 0.1 * self.circleSize
        x2 = x1 - 0.25 * self.circleSize
        y2 = y1 + 0.35 * self.circleSize
        x3 = x1
        y3 = y2
        x4 = x1 + 0.25 * circleSize
        x4 = x1 + 0.25 * self.circleSize
        y4 = y2
        x5 = x2
        y5 = y4 + 0.35 * circleSize
        y5 = y4 + 0.35 * self.circleSize

        line1 = QGraphicsLineItem(x1, y1, x2, y2)
        line1.setPen(branchPen)
@@ -172,33 +200,33 @@ class MenuView(QGraphicsView):
        self.itemsGroup.addToGroup(node4)
        self.itemsGroup.addToGroup(node5)

    def drawBinaryTree(self, circleSize: float, w: float, h: float):
    def drawBinaryTree(self):
        nodePen = QPen(QColor(21, 67, 96))
        branchPen = QPen(QColor(21, 67, 96))
        nodeColor = QColor(169, 204, 227)
        lineWidth = 0.015 * circleSize
        nodeWidth = 0.12 * circleSize
        lineWidth = 0.015 * self.circleSize
        nodeWidth = 0.12 * self.circleSize
        branchPen.setWidth(lineWidth)
        branchPen.setCapStyle(Qt.RoundCap)
        nodePen.setWidth(0.1 * nodeWidth)

        x1 = 0.5 * w - circleSize / 1.7
        y1 = 0.1 * h - 0.61 * circleSize
        x2 = x1 - 0.19 * circleSize
        y2 = y1 + 0.23 * circleSize
        x3 = x1 + 0.19 * circleSize
        x1 = self.c1x + self.circleSize / 2.35
        y1 = 0.1 * self.h + 0.38 * self.circleSize
        x2 = x1 - 0.19 * self.circleSize
        y2 = y1 + 0.23 * self.circleSize
        x3 = x1 + 0.19 * self.circleSize
        y3 = y2
        x4 = x2 - 0.08 * circleSize
        y4 = y2 + 0.23 * circleSize
        x5 = x2 + 0.08 * circleSize
        x4 = x2 - 0.08 * self.circleSize
        y4 = y2 + 0.23 * self.circleSize
        x5 = x2 + 0.08 * self.circleSize
        y5 = y4
        x6 = x3 - 0.08 * circleSize
        x6 = x3 - 0.08 * self.circleSize
        y6 = y4
        x7 = x3 + 0.08 * circleSize
        x7 = x3 + 0.08 * self.circleSize
        y7 = y4
        x8 = x7 - 0.08 * circleSize
        y8 = y7 + 0.23 * circleSize
        x9 = x7 + 0.08 * circleSize
        x8 = x7 - 0.08 * self.circleSize
        y8 = y7 + 0.23 * self.circleSize
        x9 = x7 + 0.08 * self.circleSize
        y9 = y8

        leftLine1 = QGraphicsLineItem(x1, y1, x2, y2)
@@ -263,24 +291,108 @@ class MenuView(QGraphicsView):


class Arrow(QGraphicsItemGroup):
    def __init__(self, game, beginX: float, beginY: float, endX: float, endY: float, controller):
    def __init__(self, game, beginX: float, beginY: float, endX: float, endY: float, p1x: float, p1y: float, p2x: float,
                  p2y: float, circleSize: float, controller):
        super(QGraphicsItemGroup, self).__init__()
        self.clickableObject = Clickable()
        self.game = game
        self.controller = controller
        #self.pen = QPen(QColor(21, 67, 96))
        offset = 0.03 * circleSize
        pen = QPen()
        pen.setWidth(3)
        """line = QGraphicsLineItem(beginX, beginY, endX, endY)
        if game == GameEnum.PLANAR_TREE_TO_BINARY_TREE:
            endLineX = endX - 0.15 * (endX - beginX)

            line = QGraphicsPathItem()
            line.setPen(pen)
            path = QPainterPath()
            path.moveTo(beginX - offset, beginY)
            path.cubicTo(p1x - offset, p1y - offset, p2x - offset, p2y - offset, endLineX, endY - offset)
            line.setPath(path)

            line2 = QGraphicsPathItem()
            line2.setPen(pen)
            path = QPainterPath()
            path.moveTo(beginX + offset, beginY)
            path.cubicTo(p1x + offset, p1y + offset, p2x + offset, p2y + offset, endLineX, endY + offset)
            line2.setPath(path)

            line3 = QGraphicsLineItem(endLineX, endY - offset, endLineX, endY - 2 * offset)
            line3.setPen(pen)

            line4 = QGraphicsLineItem(endLineX, endY + offset, endLineX, endY + 2 * offset)
            line4.setPen(pen)

            line5 = QGraphicsLineItem(endLineX, endY - 2 * offset, endX, endY)
            line5.setPen(pen)

            line6 = QGraphicsLineItem(endLineX, endY + 2 * offset, endX, endY)
            line6.setPen(pen)

        elif game == GameEnum.BINARY_TREE_TO_DYCK_WORD:
            endLineY = endY - 0.3 * (endY - beginY)

            line = QGraphicsPathItem()
            line.setPen(pen)
            path = QPainterPath()
            path.moveTo(beginX, beginY - offset)
            path.cubicTo(p1x - offset, p1y - offset, p2x + offset, p2y - offset, endX + offset, endLineY)
            line.setPath(path)

            line2 = QGraphicsPathItem()
            line2.setPen(pen)
            path = QPainterPath()
            path.moveTo(beginX, beginY + offset)
            path.cubicTo(p1x - offset, p1y + offset, p2x - offset, p2y + offset, endX - offset, endLineY)
            line2.setPath(path)

            line3 = QGraphicsLineItem(endX - offset, endLineY, endX - 2 * offset, endLineY)
            line3.setPen(pen)

            line4 = QGraphicsLineItem(endX + offset, endLineY, endX + 2 * offset, endLineY)
            line4.setPen(pen)

            line5 = QGraphicsLineItem(endX - 2 * offset, endLineY, endX, endY)
            line5.setPen(pen)

            line6 = QGraphicsLineItem(endX + 2 * offset, endLineY, endX, endY)
            line6.setPen(pen)

        elif game == GameEnum.DYCK_WORD_TO_PLANAR_TREE:
            endLineX = endX + 0.1 * circleSize

            line = QGraphicsPathItem()
            line.setPen(pen)
        self.addToGroup(line)"""
        path = QGraphicsPathItem()
        painterPath = QPainterPath(QPointF(beginX, beginY))
        painterPath.arcTo(beginX, beginY, endX, endY, 0, 0)
        path.setPath(painterPath)
        path.setPen(pen)

        self.addToGroup(path)
            path = QPainterPath()
            path.moveTo(beginX, beginY - offset)
            path.cubicTo(p1x - offset, p1y - offset, p2x + offset, p2y - offset, endLineX, endY - offset)
            line.setPath(path)

            line2 = QGraphicsPathItem()
            line2.setPen(pen)
            path = QPainterPath()
            path.moveTo(beginX, beginY + offset)
            path.cubicTo(p1x - offset, p1y + offset, p2x - offset, p2y + offset, endLineX, endY + offset)
            line2.setPath(path)

            """line3 = QGraphicsLineItem(endX - offset, endLineY, endX - 2 * offset, endLineY)
            line3.setPen(pen)

            line4 = QGraphicsLineItem(endX + offset, endLineY, endX + 2 * offset, endLineY)
            line4.setPen(pen)

            line5 = QGraphicsLineItem(endX - 2 * offset, endLineY, endX, endY)
            line5.setPen(pen)

            line6 = QGraphicsLineItem(endX + 2 * offset, endLineY, endX, endY)
            line6.setPen(pen)"""

        self.addToGroup(line)
        self.addToGroup(line2)
        """self.addToGroup(line3)
        self.addToGroup(line4)
        self.addToGroup(line5)
        self.addToGroup(line6)"""
        self.connect()

    def connect(self):