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