summaryrefslogtreecommitdiff
path: root/tropical/qt/eventdrawer.py
blob: 1d52bb8467a848abbca718b96b519420370a1bcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QSizePolicy
from PyQt6 import uic, QtGui, QtCore
from PyQt6.QtCore import Qt, QRect
import math
#https://forum.qt.io/topic/93327/how-can-i-use-qpainter-to-paint-on-qgraphicsview/5
#https://www.pythonguis.com/tutorials/pyqt6-bitmap-graphics/#qpainter

# to solve the fit problem: https://stackoverflow.com/questions/61886358/qgraphicsview-fitinview-not-working-as-expected

class EvtQGraphicsView(QGraphicsView):
    def __init__(self, scene):
        super().__init__(None)
        self.setScene(scene)

    def resizeEvent(self, event):
        self.fitInView(self.sceneRect(), Qt.AspectRatioMode.IgnoreAspectRatio)

        
class EvtDrawerScene(QGraphicsScene):
    def __init__(self, env):
        self.gridWidth=2
        super().__init__(None)
        self.env=env
        self.eventsRect=list()
        self.eventsList=list()
        self.calStateChanged()

    def drawForeground(self, painter, rect):
        self.eventsRect.clear()
        origXF, origYF, widthF, heightF = rect.getRect()
        origXI, origYI, widthI, heightI = (int(origXF),int(origYF),int(widthF),int(heightF))
        self.drawEventsBG(painter,origXI, origYI, widthI, heightI)
        self.drawEventsText(painter,origXI, origYI, widthI, heightI)

    def drawEventsBG(self,painter,x,y,width,height):
        # Init Pen
        pen=QtGui.QPen()
        pen.setWidth(self.gridWidth)
        pen.setJoinStyle(Qt.PenJoinStyle.MiterJoin)
        po=int(self.gridWidth/2) # Pen offset
        painter.setPen(pen)
        # Init Brush
        brush=QtGui.QBrush()
        brush.setColor(QtGui.QColor("#e5e5e5"))
        brush.setStyle(Qt.BrushStyle.SolidPattern)
        painter.setBrush(brush)
        
        eventHeight=80
        colorWidth=20
        for e in self.eventsList:
            r=QRect(x+po,y+po,width-po*2,eventHeight-po*2)
            self.eventsRect.append(r)
            painter.drawRect(r)

    def drawEventsText(self,painter,x,y,width,height):
        font=painter.font()
        metric=QtGui.QFontMetrics(font);
        labelH=metric.boundingRect("Hello event").height()
        for i in range(0,len(self.eventsRect)):
            e=self.eventsList[i]
            r=self.eventsRect[i]
            painter.drawText(r.x(),r.y()+labelH,"Hello event")

    def calStateChanged(self):
        s=self.env.calState.selection
        self.eventsList=self.env.listEventsOn(s[0],s[1],s[2])
        self.update()
        
    
class EvtDrawer():
    
    def __init__(self, layout, env):
        self.gs=EvtDrawerScene(env)
        self.gv=EvtQGraphicsView(self.gs)
        # Setup propertion
        spLeft=QSizePolicy(QSizePolicy.Policy.Preferred,QSizePolicy.Policy.Preferred);
        spLeft.setHorizontalStretch(1);
        self.gv.setSizePolicy(spLeft);        

        # if jean:
        #     spLeft=QSizePolicy(QSizePolicy.Policy.Preferred,QSizePolicy.Policy.Preferred);
        #     spLeft.setHorizontalStretch(2);
        #     self.gv.setSizePolicy(spLeft);
        # else:
        #     spLeft=QSizePolicy(QSizePolicy.Policy.Preferred,QSizePolicy.Policy.Preferred);
        #     spLeft.setHorizontalStretch(1);
        #     self.gv.setSizePolicy(spLeft);
        layout.addWidget(self.gv)

    def calStateChanged(self):
        self.gs.calStateChanged()