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