Minor changes

This commit is contained in:
Loïc Guégan 2024-09-18 18:32:24 +02:00
parent 171fbd28a3
commit 76ddf4fe10
6 changed files with 73 additions and 33 deletions

View file

@ -1,6 +1,6 @@
import calendar
from datetime import date, timedelta
from datetime import date, timedelta, datetime
class CalState:
@ -20,6 +20,10 @@ class CalState:
today = date.today()
self.goto(today.year, today.month, today.day)
def today(self):
t=datetime.today()
return (t.year,t.month,t.day,int(t.strftime("%w"))-1)
def gotoNextWeek(self):
day=date(self.year,self.month,self.day) + timedelta(weeks=1)
self.goto(day.year,day.month, day.day)

View file

@ -1,6 +1,7 @@
from pathlib import Path
from db import CalDB
from calstate import CalState
import configparser
from enum import Enum
from datetime import date, timedelta
@ -40,6 +41,7 @@ class Env:
# Database
self.dbFile=self.confdir / "sqlite3.db"
self.db=CalDB(str(self.dbFile))
self.calState=CalState()
def initConfig(self):
self.config["global"]={"new":True}
@ -57,4 +59,3 @@ class Env:
events.append(e)
# TODO: Account for repeat
return events

View file

@ -21,7 +21,7 @@ class CalQGraphicsView(QGraphicsView):
class CalDrawerScene(QGraphicsScene):
def __init__(self, parent, calState):
def __init__(self, parent, env):
self.parent=parent
self.gridWidth=2
self.daysLabelBG="#dddddd"
@ -31,9 +31,11 @@ class CalDrawerScene(QGraphicsScene):
self.daysRect=list()
self.eventsRect=list()
self.mouseOver=-1
self.calState=calState
self.env=env
self.calState=env.calState
self.daysNames=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
self.pullState()
self.selection=self.today
def drawForeground(self, painter, rect):
origXF, origYF, widthF, heightF = rect.getRect()
@ -141,11 +143,12 @@ class CalDrawerScene(QGraphicsScene):
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])
dayLabel="["+str(d[2])+"]" if d == today else str(d[2])
labelW=metric.boundingRect(dayLabel).width()
offsetX=int(r.width()/2-labelW/2)
painter.setPen(Qt.PenStyle.NoPen)
@ -175,14 +178,16 @@ class CalDrawerScene(QGraphicsScene):
colMark=5
colMarkPadding=2
offsetY=int(labelH/2)+int(labelH/4)+margin
offsetX=colMark+colMarkPadding
# Draw
po=int(self.gridWidth/2) # Pen offset
for r in self.eventsRect:
offsetX=colMark+colMarkPadding
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)
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):
"""
@ -203,14 +208,23 @@ class CalDrawerScene(QGraphicsScene):
b=event.button()
if self.mouseOver>=0 and b==Qt.MouseButton.LeftButton:
event.accept()
self.selection=self.calState.getMonthDays()[self.mouseOver]
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, calState):
self.gs=CalDrawerScene(parent, calState)
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);
@ -227,3 +241,7 @@ class CalDrawer():
# self.gv.setSizePolicy(spLeft);
layout.addWidget(self.gv)
def getSelectionEvents(self):
(yy,mm,dd,ww)=self.gs.selection
return self.env.listEventsOn(yy,mm,dd)

View file

@ -18,20 +18,22 @@ class EvtQGraphicsView(QGraphicsView):
class EvtDrawerScene(QGraphicsScene):
def __init__(self, calState, env):
def __init__(self, env):
self.gridWidth=2
super().__init__(None)
self.calState=calState
self.env=env
self.eventsRect=list()
self.eventsList=list()
def drawForeground(self, painter, rect):
self.eventsList=self.env.listEventsOn(2024,10,1)
self.eventsRect.clear()
origXF, origYF, widthF, heightF = rect.getRect()
origXI, origYI, widthI, heightI = (int(origXF),int(origYF),int(widthF),int(heightF))
self.drawEvents(painter,origXI, origYI, widthI, heightI)
self.drawEventsBG(painter,origXI, origYI, widthI, heightI)
self.drawEventsText(painter,origXI, origYI, widthI, heightI)
def drawEvents(self,painter,x,y,width,height):
def drawEventsBG(self,painter,x,y,width,height):
# Init Pen
pen=QtGui.QPen()
pen.setWidth(self.gridWidth)
@ -46,14 +48,28 @@ class EvtDrawerScene(QGraphicsScene):
eventHeight=80
colorWidth=20
for e in self.env.listEventsOn(2024,10,1):
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 setEvents(self, events):
print(events)
pass
class EvtDrawer():
def __init__(self, layout, calState, env):
self.gs=EvtDrawerScene(calState, env)
def __init__(self, layout, env):
self.gs=EvtDrawerScene(env)
self.gv=EvtQGraphicsView(self.gs)
# Setup propertion
spLeft=QSizePolicy(QSizePolicy.Policy.Preferred,QSizePolicy.Policy.Preferred);
@ -70,3 +86,5 @@ class EvtDrawer():
# self.gv.setSizePolicy(spLeft);
layout.addWidget(self.gv)
def setEvents(self,events):
self.gs.setEvents(events)

View file

@ -14,24 +14,24 @@ import sys, os
class MainWindow(QMainWindow):
def __init__(self,uipath, calState, env):
def __init__(self,uipath, env):
super(MainWindow,self).__init__()
uic.loadUi(uipath+"/MainWindow.ui",self)
self.calDrawer=CalDrawer(self, self.calContainer.layout(),calState)
self.evtDrawer=EvtDrawer(self.calContainer.layout(),calState, env)
self.calState=calState
self.calDrawer=CalDrawer(self, self.calContainer.layout(), env)
self.evtDrawer=EvtDrawer(self.calContainer.layout(), env)
self.show()
print(self.calDrawer.getSelectionEvents())
def setVersion(self,version):
self.statusbar.showMessage("TropiCal v"+version,0)
def event(self, event):
if event.type() == DaySelectedEvent:
print("Ho")
self.evtDrawer.setEvents(self.calDrawer.getSelectionEvents())
return QWidget.event(self,event) #super().event(event)
def StartApplication(env,calState):
def StartApplication(env):
path = os.path.dirname(os.path.abspath(__file__))+"/designer"
# You need one (and only one) QApplication instance per application.
# Pass in sys.argv to allow command line arguments for your app.
@ -39,7 +39,7 @@ def StartApplication(env,calState):
app = QApplication(sys.argv)
# Create a Qt widget, which will be our window.
window = MainWindow(path, calState, env)
window = MainWindow(path, env)
window.setVersion(env.__VERSION__)
window.show() # IMPORTANT!!!!! Windows are hidden by default.

View file

@ -14,7 +14,6 @@ from env import Env
if __name__ == '__main__':
env=Env()
env.listEventsOn(2024,10,1)
calState=CalState()
QtCalanus.StartApplication(env,calState)
# env.listEventsOn(2024,10,1)
QtCalanus.StartApplication(env)