8f5fdb85117b5d2260845c83c0afbdf0d621299d
[toast/confclerk.git] / src / mvc / delegate.cpp
1 #include "delegate.h"
2 #include "eventmodel.h"
3 #include <track.h>
4
5 #include <QDebug>
6 #include <QPainter>
7
8 const int RADIUS = 10;
9 const int SPACER = 10;
10
11 const double scaleFactor1 = 0.4;
12 const double scaleFactor2 = 0.8;
13
14 Delegate::Delegate(QTreeView *aParent)
15     : QItemDelegate(aParent)
16     , mViewPtr(aParent)
17 {
18     mControls.clear();
19     defineControls();
20 }
21
22 Delegate::~Delegate()
23 {
24     QListIterator<ControlId> i(mControls.keys());
25     while (i.hasNext())
26     {
27         delete mControls[i.next()]->image();
28     }
29 }
30
31 void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
32 {
33     if(!mViewPtr)
34         return;
35
36     painter->save();
37     QColor bkgrColor = Qt::cyan;
38
39     QPen borderPen(bkgrColor.darker());
40     //QColor bkgrColor = QColor(0,0,113);
41     //QPen borderPen(Qt::cyan);
42     if(hasParent(index))
43     {
44         int aux = option.rect.height() - mControls[FavouriteControlOn]->drawPoint().y() - mControls[FavouriteControlOn]->image()->height();
45         // font SMALL
46         QFont fontSmall = option.font;
47         fontSmall.setBold(false);
48         fontSmall.setPixelSize(aux*0.2);
49         QFontMetrics fmSmall(fontSmall);
50         // font SMALL bold
51         QFont fontSmallB = fontSmall;
52         fontSmallB.setBold(true);
53         QFontMetrics fmSmallB(fontSmallB);
54
55         // font BIG
56         QFont fontBig = option.font;
57         fontBig.setBold(false);
58         fontBig.setPixelSize(aux*0.33);
59         QFontMetrics fmBig(fontBig);
60         // font BIG bold
61         QFont fontBigB = fontBig;
62         fontBigB.setBold(true);
63         QFontMetrics fmBigB(fontBigB);
64
65         //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
66
67         //Time conflicts are colored differently
68         if(static_cast<Event*>(index.internalPointer())->hasTimeConflict())
69             bkgrColor = Qt::yellow;
70
71         if(isLast(index))
72         {
73             QLinearGradient lastGradient(option.rect.topLeft(), option.rect.bottomLeft());
74             lastGradient.setColorAt(0.0, Qt::white);
75             lastGradient.setColorAt(0.5, bkgrColor);
76             lastGradient.setColorAt(1.0, Qt::white);
77
78             QPainterPath endPath;
79             endPath.moveTo(option.rect.topLeft());
80             endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
81             endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90);
82             endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0));
83             endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
84             endPath.lineTo(option.rect.topRight());
85
86             painter->setBrush( bkgrColor );
87             //painter->setBrush(lastGradient);
88             painter->setPen(borderPen);
89             painter->drawPath(endPath);
90
91             painter->setFont(option.font);
92         }
93         else // middle elements
94         {
95             QLinearGradient middleGradient(option.rect.topLeft(), option.rect.bottomLeft());
96             middleGradient.setColorAt(0.0, Qt::white);
97             middleGradient.setColorAt(0.25, bkgrColor);
98             middleGradient.setColorAt(0.5, Qt::white);
99             middleGradient.setColorAt(0.75, bkgrColor);
100             middleGradient.setColorAt(1.0, Qt::white);
101
102             painter->setBrush( bkgrColor );
103             //painter->setBrush(middleGradient);
104             painter->setPen(Qt::NoPen);
105             painter->drawRect(option.rect);
106
107             painter->setPen(borderPen);
108             // vertical lines
109             painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
110             painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
111             // horizontal lines
112             painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
113
114             painter->setFont(option.font);
115         }
116
117         // draw Controls
118         // favourite
119         if(static_cast<Event*>(index.internalPointer())->isFavourite())
120             painter->drawImage(mControls[FavouriteControlOn]->drawPoint(option.rect),*mControls[FavouriteControlOn]->image());
121         else
122             painter->drawImage(mControls[FavouriteControlOff]->drawPoint(option.rect),*mControls[FavouriteControlOff]->image());
123 #ifdef MAEMO
124         // alarm
125         if(static_cast<Event*>(index.internalPointer())->hasAlarm())
126             painter->drawImage(mControls[AlarmControlOn]->drawPoint(option.rect),*mControls[AlarmControlOn]->image());
127         else
128             painter->drawImage(mControls[AlarmControlOff]->drawPoint(option.rect),*mControls[AlarmControlOff]->image());
129 #endif
130         // map
131         painter->drawImage(mControls[MapControl]->drawPoint(option.rect),*mControls[MapControl]->image());
132         // Time conflict
133         if(static_cast<Event*>(index.internalPointer())->hasTimeConflict())
134             painter->drawImage(mControls[WarningControl]->drawPoint(option.rect),*mControls[WarningControl]->image());
135
136         // draw texts
137         Event *event = static_cast<Event*>(index.internalPointer());
138         QPointF titlePointF(mControls[FavouriteControlOn]->drawPoint(option.rect));
139         titlePointF.setX(option.rect.x()+SPACER);
140         titlePointF.setY(titlePointF.y()+mControls[FavouriteControlOn]->image()->height());
141         QTime start = event->start().time();
142         painter->setFont(fontBig);
143         painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->room());
144         // title
145         titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
146         painter->setFont(fontBigB);
147         QString title = event->title();
148         if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
149         {
150             // chop words from the end
151             while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
152             {
153                 title.chop(1);
154                 // chop characters one-by-one from the end
155                 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
156                 {
157                     title.chop(1);
158                 }
159             }
160             title += "...";
161         }
162         painter->drawText(titlePointF,title);
163         // persons
164         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
165         painter->setFont(fontSmall);
166         painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and "));
167         // track
168         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
169         painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
170     }
171     else // doesn't have parent - time-groups' elements (top items)
172     {
173         QFont fontSmall = option.font;
174         fontSmall.setBold(true);
175         fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
176         QFontMetrics fmSmall(fontSmall);
177
178         QFont fontBig = option.font;
179         fontBig.setBold(true);
180         fontBig.setPixelSize(option.rect.height()*scaleFactor2);
181         QFontMetrics fmBig(fontBig);
182
183         int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
184
185         QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
186         //titleGradient.setColorAt(0.0, Qt::white);
187         titleGradient.setColorAt(0.0, bkgrColor);
188         titleGradient.setColorAt(0.5, Qt::white);
189         titleGradient.setColorAt(1.0, bkgrColor);
190
191         QPainterPath titlePath;
192         if(isExpanded(index))
193         {
194             titlePath.moveTo(option.rect.bottomLeft());
195             titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
196             titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
197             titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
198             titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
199             titlePath.lineTo(option.rect.bottomRight());
200             titlePath.closeSubpath();
201         }
202         else
203         {
204             titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
205             titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
206             titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
207             titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
208             titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
209             titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
210             titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
211             titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);      
212             titlePath.closeSubpath();
213         }
214
215         painter->setBrush(titleGradient);
216         painter->setPen(borderPen);
217         painter->drawPath(titlePath);
218
219         // draw icons 
220         painter->setFont(fontSmall);
221         QImage *image = mControls[FavouriteControlOn]->image();
222         QPoint drawPoint =
223             option.rect.topRight()
224             - QPoint(
225                     spacer + image->width(),
226                     - option.rect.height()/2 + image->height()/2);
227         painter->drawImage(drawPoint,*image);
228         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
229                 QString::number(numberOfFavourities(index)));
230 #ifdef MAEMO
231         drawPoint.setX(drawPoint.x() - spacer - image->width());
232         painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
233         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
234                 QString::number(numberOfAlarms(index)));
235 #endif
236         // draw texts
237         QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
238         drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
239         drawPoint.setY(drawPoint.y()+image->height() - 2);
240         painter->drawText(drawPoint,numEvents);
241
242         QPointF titlePointF = QPoint(
243                 option.rect.x()+SPACER,
244                 option.rect.y()+option.rect.height()-fmBig.descent());
245         painter->setFont(fontBig);
246
247         painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
248     }
249
250     //// HIGHLIGHTING SELECTED ITEM
251     //if (option.state & QStyle::State_Selected)
252         //painter->fillRect(option.rect, option.palette.highlight());
253
254     painter->restore();
255 }
256
257 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
258 {
259     Q_UNUSED(option)
260
261     if (index.internalId() == 0) // time group
262     {
263         return QSize(40,40);
264     }
265     else // event
266     {
267         return QSize(100,100);
268     }
269 }
270
271 bool Delegate::hasParent( const QModelIndex &index ) const
272 {
273     if( index.parent().isValid() )
274         return true;
275     else
276         return false;
277 }
278   
279 bool Delegate::isLast( const QModelIndex &index ) const
280 {
281     if(!hasParent(index))
282         return false; // what should be returned here?
283
284     if(index.row() >= (index.model()->rowCount(index.parent())-1))
285         return true;
286     else
287         return false;
288 }
289
290 bool Delegate::isExpanded( const QModelIndex &index ) const
291 {
292     if( !mViewPtr )
293         return false;
294     else
295         return mViewPtr->isExpanded( index );
296 }
297
298 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
299 {
300     if(!hasParent(aIndex)) // time-group item (root item)
301         return ControlNone;
302
303     QListIterator<ControlId> i(mControls.keys());
304     while (i.hasNext())
305     {
306         ControlId id = i.next();
307         if(mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
308         {
309             if(id == WarningControl)
310             {
311                 if(static_cast<Event*>(aIndex.internalPointer())->hasTimeConflict())
312                     return id;
313             }
314             else
315                 return id;
316         }
317     }
318
319     return ControlNone;
320 }
321
322 void Delegate::defineControls()
323 {
324     Control *control;
325     QPoint p(0,0);
326     // FAVOURITE ICONs
327     // on
328     control = new Control(FavouriteControlOn,QString(":icons/favourite-onBig.png"));
329     p = QPoint(0,SPACER);
330     p.setX(p.x()-control->image()->width()-SPACER);
331     control->setDrawPoint(p);
332     mControls.insert(FavouriteControlOn,control);
333     // off
334     control = new Control(FavouriteControlOff,QString(":icons/favourite-offBig.png"));
335     p = QPoint(0,SPACER);
336     p.setX(p.x()-control->image()->width()-SPACER);
337     control->setDrawPoint(p);
338     mControls.insert(FavouriteControlOff,control);
339
340 #ifdef MAEMO
341     // ALARM ICONs
342     // on
343     control = new Control(AlarmControlOn,QString(":icons/alarm-onBig.png"));
344     p = mControls[FavouriteControlOn]->drawPoint();
345     p.setX(p.x()-control->image()->width()-SPACER);
346     control->setDrawPoint(p);
347     mControls.insert(AlarmControlOn,control);
348     // off
349     control = new Control(AlarmControlOff,QString(":icons/alarm-offBig.png"));
350     p = mControls[FavouriteControlOff]->drawPoint();
351     p.setX(p.x()-control->image()->width()-SPACER);
352     control->setDrawPoint(p);
353     mControls.insert(AlarmControlOff,control);
354
355     // MAP ICON
356     control = new Control(MapControl,QString(":icons/compassBig.png"));
357     p = mControls[AlarmControlOn]->drawPoint();
358     p.setX(p.x()-control->image()->width()-SPACER);
359     control->setDrawPoint(p);
360     mControls.insert(MapControl,control);
361 #else
362     // MAP ICON
363     control = new Control(MapControl,QString(":icons/compassBig.png"));
364     p = mControls[FavouriteControlOn]->drawPoint();
365     p.setX(p.x()-control->image()->width()-SPACER);
366     control->setDrawPoint(p);
367     mControls.insert(MapControl,control);
368 #endif
369
370     // WARNING ICON
371     control = new Control(WarningControl,QString(":icons/exclamation.png"));
372     p = mControls[MapControl]->drawPoint();
373     p.setX(p.x()-control->image()->width()-SPACER);
374     control->setDrawPoint(p);
375     mControls.insert(WarningControl,control);
376 }
377
378 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
379 {
380     if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
381         return true;
382
383     return false;
384 }
385
386 int Delegate::numberOfFavourities(const QModelIndex &index) const
387 {
388     if(index.parent().isValid()) // it's event, not time-group
389         return 0;
390
391     int nrofFavs = 0;
392     for(int i=0; i<index.model()->rowCount(index); i++)
393         if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
394             nrofFavs++;
395
396     return nrofFavs;
397 }
398
399 int Delegate::numberOfAlarms(const QModelIndex &index) const
400 {
401     if(index.parent().isValid()) // it's event, not time-group
402         return 0;
403
404     int nrofAlarms = 0;
405     for(int i=0; i<index.model()->rowCount(index); i++)
406         if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())
407             nrofAlarms++;
408
409     return nrofAlarms;
410 }
411