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