4 # Copyright (C) 2010 Stefanos Harhalakis
6 # This file is part of maegirls.
8 # maegirls is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # maegirls is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with maegirls. If not, see <http://www.gnu.org/licenses/>.
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
25 from PyQt4.QtGui import *
26 from PyQt4.QtCore import *
32 class DaysGraph(QWidget):
33 def __init__(self, algo_, *args, **kwargs):
34 QWidget.__init__(self, *args, **kwargs)
41 self.first_day=algo.today()
46 def setAlgo(self, algo_):
50 self.first_day=algo.today()
53 def paintEvent(self, event):
54 palette=self.palette()
61 days=int(w0/self.day_width)
62 days=int((days)/2)*2 + 3
65 # Now calculate the x0
68 painter=QPainter(self)
70 # This makes it sloooooooooooow
71 # painter.setRenderHints(painter.Antialiasing \
72 # | painter.TextAntialiasing \
73 # | painter.HighQualityAntialiasing
77 colol=QColor(0x66,0x66,0x66)
80 # outline of the small block
82 penblockol=QPen(colblockol)
86 pencur=QPen(colcur, 3)
90 coltxt=palette.color(palette.WindowText)
95 fontdate.setPointSizeF(fontdate.pointSizeF()*0.5)
96 fontdate_m=QFontMetrics(fontdate)
98 # Last bounding rectagle of the text
99 last_br=QRect(0,0,4,4)
103 for d in xrange(days):
104 # This will cause the first_day to be at the center
105 curday=self.first_day+d - int(days/2)
106 x=x0+d*self.day_width
111 st=self.algo.status(curday)
113 if st['status']=='ok':
114 colbg=QColor(gray, gray, gray)
117 painter.setPen(penol)
118 painter.fillRect(x, y, self.day_width-1, h2-1, brbg)
119 elif st['status']=='red' or \
120 st['status']=='blue' or \
121 st['status']=='green':
122 if st['status']=='red':
123 perc=1.0*st['day'] / (st['len'])
125 0xff - int((0xff - gray - gray) * perc),
129 elif st['status']=='blue':
130 perc=1.0*(st['day']+1) / (st['len'])
134 gray + ((0xff-gray) * perc))
136 elif st['status']=='green':
137 perc=1.0*(st['day']+1) / (st['len'])
140 gray + ((0xff-gray) * perc),
144 painter.fillRect(x, y, self.day_width-1, h2-1, brbg)
146 # Show a nice small box for interesting days
150 # Ensure that boxw is sane - e.g. not larger than
151 # the current day_width
152 boxw2=self.day_width-10
153 boxw=min(boxw, boxw2)
155 x1=x+int((self.day_width-boxw)/2)
157 if st['status']=='red':
158 y1=y + int(perc * h2*2/3) + 20
159 elif st['status']=='blue' or st['status']=='green':
160 y1=y + int(h2*2/3) - int(perc * h2*2/3) + 20
162 brbg=QBrush(colblock)
163 painter.fillRect(x1, y1, boxw, boxh, brbg)
164 painter.setPen(colblockol)
165 painter.drawRect(x1, y1, boxw, boxh)
167 painter.setPen(colol)
168 painter.drawRect(x, y, self.day_width, h2-1)
172 painter.setPen(pencur)
173 painter.drawRect(x+1,y+3,self.day_width-4, h2-7)
175 date=time.localtime(curday*86400+30000)
179 day=QLocale().dayName(date.tm_wday+1, QLocale.ShortFormat)
180 txt="%s\n%d.%02d\n%d" % \
181 (day, date.tm_mday, date.tm_mon, date.tm_year)
183 # br is used to determine overlaps
184 br=fontdate_m.boundingRect(txt)
188 while colnum*self.day_width < br.width():
191 # If this intersects with the previous day then it is ok
192 if br.intersects(last_br):
196 # br2 is the true bounding rectangle
197 # The width calculation helps with alignment
198 br2=QRect(x-((colnum-1)/2 * self.day_width), y2,
199 self.day_width*colnum, fontdate_m.height()*3)
202 painter.setFont(fontdate)
203 painter.setPen(pentxt)
204 painter.drawText(br2, Qt.AlignCenter | Qt.AlignTop, txt)
209 # for x in xrange(5, -1, -1):
211 # colobg=QColor(c, c, c)
212 # penobg=QPen(colobg, 1)
213 # painter.setPen(penobg)
214 # painter.drawRect(x, x, self.width()-(2*x)-1, h2-(2*x)-1)
219 # penobg=QPen(colobg, w)
220 # painter.setPen(penobg)
221 # painter.drawRect(w/2, w/2, self.width()-w, h2-w)
223 # Handle both horizontal and vertical movement
224 def mouseMoveEvent(self, event):
231 if self.last_x==None:
235 # Vertical movement - that much pixels account for one zoom
238 # First look horizontal movement
239 # If there was movement the reset vertical movement
242 days=int(dx/self.day_width)
244 days=-int(abs(dx)/self.day_width)
248 self.last_x+=(days*self.day_width)
249 self.last_y=y # Reset vertical movement
252 # Now look for vertical movement
257 zoom=-int(abs(dy)/y_step)
260 t=self.day_width+zoom
261 if t<self.min_day_width:
263 elif t>self.max_day_width:
265 #self.day_width-=zoom
268 self.last_y+=zoom*y_step
269 self.last_x=x # Reset horizontal movement
275 def mousePressEvent(self, event):
281 return(QSize(100,50))
283 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent: