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