Make sure to remove src/bin/libqalarm.a on make clean.
[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         QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
188         painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
189         // track
190         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
191         painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
192     }
193     else // doesn't have parent - time-groups' elements (top items)
194     {
195         QFont fontSmall = option.font;
196         fontSmall.setBold(true);
197         fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
198         QFontMetrics fmSmall(fontSmall);
199
200         QFont fontBig = option.font;
201         fontBig.setBold(true);
202         fontBig.setPixelSize(option.rect.height()*scaleFactor2);
203         QFontMetrics fmBig(fontBig);
204
205         int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
206
207         QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
208         bkgrColor = option.palette.color(QPalette::Highlight);
209         textColor = option.palette.color(QPalette::HighlightedText);
210         titleGradient.setColorAt(0.0, bkgrColor);
211         titleGradient.setColorAt(1.0, bkgrColor);
212
213         QPainterPath titlePath;
214         if(isExpanded(index))
215         {
216             titlePath.moveTo(option.rect.bottomLeft());
217             titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
218             titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
219             titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
220             titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
221             titlePath.lineTo(option.rect.bottomRight());
222             titlePath.closeSubpath();
223         }
224         else
225         {
226             titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
227             titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
228             titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
229             titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
230             titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
231             titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
232             titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
233             titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);      
234             titlePath.closeSubpath();
235         }
236
237         painter->setBrush(titleGradient);
238         painter->setPen(borderPen);
239         painter->drawPath(titlePath);
240
241         // draw icons
242         borderPen.setColor(textColor);
243         painter->setPen(borderPen);
244         painter->setFont(fontSmall);
245         QImage *image = mControls[FavouriteControlOn]->image();
246         QPoint drawPoint =
247             option.rect.topRight()
248             - QPoint(
249                     spacer + image->width(),
250                     - option.rect.height()/2 + image->height()/2);
251         painter->drawImage(drawPoint,*image);
252         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
253                 QString::number(numberOfFavourities(index)));
254 #ifdef MAEMO
255         drawPoint.setX(drawPoint.x() - spacer - image->width());
256         painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
257         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
258                 QString::number(numberOfAlarms(index)));
259 #endif
260         // draw texts
261         QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
262         drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
263         drawPoint.setY(drawPoint.y()+image->height() - 2);
264         painter->drawText(drawPoint,numEvents);
265
266         QPointF titlePointF = QPoint(
267                 option.rect.x()+SPACER,
268                 option.rect.y()+option.rect.height()-fmBig.descent());
269         painter->setFont(fontBig);
270
271         painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
272     }
273
274     //// HIGHLIGHTING SELECTED ITEM
275     //if (option.state & QStyle::State_Selected)
276         //painter->fillRect(option.rect, option.palette.highlight());
277
278     painter->restore();
279 }
280
281 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
282 {
283     Q_UNUSED(option)
284
285     if (index.internalId() == 0) // time group
286     {
287         return QSize(40,40);
288     }
289     else // event
290     {
291         return QSize(100,100);
292     }
293 }
294
295 bool Delegate::hasParent( const QModelIndex &index ) const
296 {
297     if( index.parent().isValid() )
298         return true;
299     else
300         return false;
301 }
302   
303 bool Delegate::isLast( const QModelIndex &index ) const
304 {
305     if(!hasParent(index))
306         return false; // what should be returned here?
307
308     if(index.row() >= (index.model()->rowCount(index.parent())-1))
309         return true;
310     else
311         return false;
312 }
313
314 bool Delegate::isExpanded( const QModelIndex &index ) const
315 {
316     if( !mViewPtr )
317         return false;
318     else
319         return mViewPtr->isExpanded( index );
320 }
321
322 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
323 {
324     if(!hasParent(aIndex)) // time-group item (root item)
325         return ControlNone;
326
327     QListIterator<ControlId> i(mControls.keys());
328     while (i.hasNext())
329     {
330         ControlId id = i.next();
331         Control *control = mControls[id];
332         if (control->enabled()
333             and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
334         {
335             return id;
336         }
337     }
338
339     return ControlNone;
340 }
341
342 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
343     : mId(aControlId)
344     , mImage(new QImage(aImageName))
345     , mDrawPoint(QPoint(0,0))
346     , mEnabled(false)
347 {
348     QPoint p;
349     if (prev_control == NULL) {
350         p = QPoint(0, SPACER);
351     } else {
352         p = prev_control->drawPoint();
353     }
354     p.setX(p.x()-image()->width()-SPACER);
355     setDrawPoint(p);
356 }
357
358 void Delegate::Control::paint(QPainter* painter, const QRect rect)
359 {
360     painter->drawImage(drawPoint(rect),*image());
361     setEnabled(true);
362 }
363
364 void Delegate::defineControls()
365 {
366     // FAVOURITE ICONs
367     // on
368     mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/emblem-new.png"), NULL));
369     // off
370     mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/emblem-new-off.png"), NULL));
371
372 #ifdef MAEMO
373     // ALARM ICONs
374     // on
375     mControls.insert(AlarmControlOn,
376                     new Control(AlarmControlOn, QString(":icons/appointment-soon.png"), mControls[FavouriteControlOn]));
377     // off
378     mControls.insert(AlarmControlOff,
379                     new Control(AlarmControlOff, QString(":icons/appointment-soon-off.png"), mControls[FavouriteControlOff]));
380 #endif
381
382     // WARNING ICON
383     mControls.insert(WarningControl,
384                     new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[FavouriteControlOn]));
385 }
386
387 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
388 {
389     if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
390         return true;
391
392     return false;
393 }
394
395 int Delegate::numberOfFavourities(const QModelIndex &index) const
396 {
397     if(index.parent().isValid()) // it's event, not time-group
398         return 0;
399
400     int nrofFavs = 0;
401     for(int i=0; i<index.model()->rowCount(index); i++)
402         if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
403             nrofFavs++;
404
405     return nrofFavs;
406 }
407
408 int Delegate::numberOfAlarms(const QModelIndex &index) const
409 {
410     if(index.parent().isValid()) // it's event, not time-group
411         return 0;
412
413     int nrofAlarms = 0;
414     for(int i=0; i<index.model()->rowCount(index); i++)
415         if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())
416             nrofAlarms++;
417
418     return nrofAlarms;
419 }
420