summaryrefslogtreecommitdiff
path: root/tropical/qt/caldrawer.py
blob: f2e7558490deb9b3e194f23e349e3cf5663ac789 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QSizePolicy
from PyQt6 import uic, QtGui, QtCore
from PyQt6.QtCore import Qt, QRect, QEvent, QCoreApplication
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

DaySelectedEvent = QEvent.registerEventType()

class CalQGraphicsView(QGraphicsView):
    def __init__(self, scene):
        super().__init__(None)
        self.setScene(scene)
        self.setMouseTracking(True) # Allow to keep track of mouse location in the scene
        
    def resizeEvent(self, event):
        self.fitInView(self.sceneRect(), Qt.AspectRatioMode.IgnoreAspectRatio)

        
class CalDrawerScene(QGraphicsScene):
    def __init__(self, parent, env):
        self.parent=parent
        self.gridWidth=2
        self.daysLabelBG="#dddddd"
        self.eventsLabelBG="#36e364"
        super().__init__(None)
        self.showWeekends=True
        self.daysRect=list()
        self.eventsRect=list()
        self.mouseOver=-1
        self.env=env
        self.calState=env.calState
        self.daysNames=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
        self.monthNames=["January", "February", "March", "April", "May", "June","July","August", "September","October", "November", "December"]
        self.pullState()
        self.selection=self.today

    def drawForeground(self, painter, rect):
        origXF, origYF, widthF, heightF = rect.getRect()
        origXI, origYI, widthI, heightI = (int(origXF),int(origYF),int(widthF),int(heightF))
        self.defaultBrush=painter.brush()
        self.daysRect.clear()
        self.eventsRect.clear()
        offsetX=0
        offsetX+=self.drawHeader(painter, origXI, origYI, widthI, heightI)
        offsetX+=self.drawDaysName(painter, origXI, origYI+offsetX, widthI, heightI-offsetX)
        self.drawGrid(painter, origXI, origYI+offsetX, widthI, heightI-offsetX)
        self.drawDaysLabel(painter)
        self.drawEvents(painter)

    def drawHeader(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
        painter.setBrush(Qt.BrushStyle.NoBrush)
        # Init dimensions
        font=painter.font()
        metric=QtGui.QFontMetrics(font);
        labelH=metric.boundingRect("".join(self.monthNames)).height()
        # Draw
        rect=QRect(x+po,y+po,width-po*2,height-po*2) # Draw grid
        painter.drawRect(rect)
        painter.drawText(x,y,width,height,Qt.AlignmentFlag.AlignHCenter,self.monthNames[self.calState.month])
        return labelH
        
    def drawDaysName(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
        painter.setBrush(Qt.BrushStyle.NoBrush)
        # Setup dimensions
        weekLength=7
        font=painter.font()
        metric=QtGui.QFontMetrics(font);
        labelH=metric.boundingRect("".join(self.daysNames)).height()
        margin=0
        offsetY=int(labelH/2)+int(labelH/4)+margin
        w=int((width-po*2)/weekLength)
        h=int(labelH)
        for i in range(0, 7):
            labelW=metric.boundingRect(self.daysNames[i]).width()
            offsetX=int(w/2-labelW/2)
#            painter.drawText(x+offsetX+w*i,y+offsetY,self.daysNames[i])
            painter.drawText(x+w*i,y,w,h,Qt.AlignmentFlag.AlignHCenter,self.daysNames[i])
            rect=QRect(x+po+w*i,y+po,w,h) # Draw grid
            painter.drawRect(rect)
        return labelH
        
    def drawGrid(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
        brushHL=QtGui.QBrush()
        brushHL.setColor(QtGui.QColor("#e5e5e5"))
        brushHL.setStyle(Qt.BrushStyle.SolidPattern)
        painter.setBrush(Qt.BrushStyle.NoBrush)
        # Setup dimensions
        weekLength=7
        if not self.showWeekends:
            weekLength=5
        daysCount=weekLength*6


        # brush=QtGui.QBrush()
        # brush.setColor(QtGui.QColor("#0000FF"))
        # brush.setStyle(Qt.BrushStyle.SolidPattern)

        
        w=int((width-po*2)/weekLength)
        h=int((height-po*2)/6)
        row=0
        col=0
        self.daysRect.clear()
        while row*weekLength+col<daysCount:
            rect=QRect(x+po+w*col,y+po+row*h,w,h) # Draw grid
            rectInside=QRect(rect.x()+po,rect.y()+po,rect.width()-po*2,rect.height()-po*2) # Area within grid (within stroke)
            self.daysRect.append(rectInside) # Store this region for later drawing
            # if col==0 and row == 0:
            #     painter.setBrush(brush)
            #     painter.setPen(Qt.PenStyle.NoPen)
            #     painter.drawRect(QRect(b.x(),b.y(),b.width(),b.height()))
            #     painter.setPen(self.gridPen)
            #     painter.setBrush(self.defaultBrush)
            if row*weekLength+col == self.mouseOver:
                painter.setBrush(brushHL)
                painter.drawRect(rect)
                painter.setBrush(Qt.BrushStyle.NoBrush)
            else:
                painter.drawRect(rect)
            col+=1
            if col==weekLength:
                col=0
                row+=1

    def drawDaysLabel(self,painter):
        # Init Pen
        pen=QtGui.QPen()
        pen.setWidth(self.gridWidth)
        pen.setJoinStyle(Qt.PenJoinStyle.MiterJoin)
        po=int(self.gridWidth/2) # Pen offset
        # Init Brush
        brush=QtGui.QBrush()
        brush.setColor(QtGui.QColor(self.daysLabelBG))
        brush.setStyle(Qt.BrushStyle.SolidPattern)
        painter.setBrush(brush)
        # Brush for today
        brushToday=QtGui.QBrush()
        brushToday.setColor(QtGui.QColor("#23a1fc"))
        brushToday.setStyle(Qt.BrushStyle.SolidPattern)
        # Init various things
        days=self.calState.getMonthDays()
        font=painter.font()
        metric=QtGui.QFontMetrics(font);
        labelH=metric.boundingRect("1234567890").height()
        margin=0
        offsetY=int(labelH/2)+int(labelH/4)+margin
        today=self.calState.today()
        # Draw labels
        for i in range(0,len(self.daysRect)):
            r=self.daysRect[i]
            d=days[i]
            dayLabel=str(d[2])
            labelW=metric.boundingRect(dayLabel).width()
            offsetX=int(r.width()/2-labelW/2)
            painter.setPen(Qt.PenStyle.NoPen)
            painter.drawRect(r.x(),r.y(),r.width(),labelH) # Remember r is within grid stroke
            painter.setPen(pen)
#            painter.drawText(r.x()+offsetX,r.y()+offsetY,dayLabel)
            if d == today:
                painter.setBrush(brushToday)
                painter.setPen(Qt.PenStyle.NoPen)
                painter.drawRect(r.x()+int(r.width()/4),r.y(),int(r.width()/2),labelH)
                painter.setBrush(brush)
                painter.setPen(pen)
            painter.drawText(r,Qt.AlignmentFlag.AlignHCenter,dayLabel)
            self.eventsRect.append(QRect(r.x(),r.y()+labelH+margin,r.width(),r.height()-(labelH+margin)))

    def drawEvents(self,painter):
        # Init Pen
        pen=QtGui.QPen()
        pen.setWidth(self.gridWidth)
        pen.setJoinStyle(Qt.PenJoinStyle.MiterJoin)
        po=int(self.gridWidth/2) # Pen offset
        # Init Brush
        brush=QtGui.QBrush()
        brush.setColor(QtGui.QColor(self.eventsLabelBG))
        brush.setStyle(Qt.BrushStyle.SolidPattern)
        painter.setBrush(brush)
        # Init various things
        days=self.calState.getMonthDays()
        font=painter.font()
        metric=QtGui.QFontMetrics(font);
        labelH=metric.height()
        margin=0
        colMark=5
        colMarkPadding=2
        offsetY=int(labelH/2)+int(labelH/4)+margin
        offsetX=colMark+colMarkPadding
        # Draw
        po=int(self.gridWidth/2) # Pen offset
        for i in range(0,len(self.eventsRect)):
            r=self.eventsRect[i]
            for e in self.events[i]:
                painter.drawText(r.x()+po+offsetX,r.y(),r.width()-offsetX-po*2,r.height(),0,"event testddddddddddddd")
                painter.setPen(Qt.PenStyle.NoPen)
                painter.drawRect(r.x(),r.y(),colMark,labelH) # Remember r is within grid stroke
                painter.setPen(pen)
            
    def mouseMoveEvent(self, event):
        """
        This is possible because of self.setMouseTracking(True) inside the view
        """
        pos=event.scenePos()
        x, y=(int(pos.x()),int(pos.y()))
        self.mouseOver=-1
        for i in range(0,len(self.daysRect)):
            r=self.daysRect[i]
            if r.contains(x,y):
                self.mouseOver=i
                self.update()
                event.accept()
                break

    def mousePressEvent(self, event):
        b=event.button()
        if self.mouseOver>=0 and b==Qt.MouseButton.LeftButton:
            event.accept()
            s=self.calState.getMonthDays()[self.mouseOver]
            self.calState.setSelection(s[0],s[1],s[2])
            event = QEvent(DaySelectedEvent)
            QCoreApplication.postEvent(self.parent, event)

    def pullState(self):
        self.monthDays=self.calState.getMonthDays()
        self.today=self.calState.today()
        self.events=list()
        for yy, mm, dd, ww  in self.monthDays:
            self.events.append(self.env.listEventsOn(yy,mm,dd))

     
class CalDrawer():
    
    def __init__(self, parent, layout, env):
        self.env=env
        self.gs=CalDrawerScene(parent, env)
        self.gv=CalQGraphicsView(self.gs)
        # Setup propertion
        spLeft=QSizePolicy(QSizePolicy.Policy.Preferred,QSizePolicy.Policy.Preferred);
        spLeft.setHorizontalStretch(3);
        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 getSelection(self):
        return self.gs.selection