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