Fix typo "favourities" -> "favourites".
[toast/confclerk.git] / src / mvc / delegate.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
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 SPACER = 10;
30
31 const double scaleFactor1 = 0.4;
32 const double scaleFactor2 = 0.8;
33
34 Delegate::Delegate(QTreeView *aParent)
35     : QItemDelegate(aParent)
36     , mViewPtr(aParent)
37 {
38     mControls.clear();
39     defineControls();
40 }
41
42 Delegate::~Delegate()
43 {
44     QListIterator<ControlId> i(mControls.keys());
45     while (i.hasNext())
46     {
47         delete mControls[i.next()]->image();
48     }
49 }
50
51 void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
52 {
53     if (!mViewPtr)
54         return;
55
56     painter->save();
57
58     QColor textColor = option.palette.color(QPalette::Text);
59
60     if(hasParent(index))
61     {
62         Event *event = static_cast<Event*>(index.internalPointer());
63
64         // determine severity of conflict
65         Favourite eventTimeConflict = event->timeConflict(); // cache value as event->timeConflict is expensive
66         enum ConflictSeverity {csNone, csWeak, csStrong} conflictSeverity = csNone;
67         switch (event->favourite()) {
68             case Favourite_strong:
69                 conflictSeverity = (eventTimeConflict == Favourite_strong) ? csStrong : csNone;
70                 break;
71             case Favourite_weak:
72                 conflictSeverity = (eventTimeConflict == Favourite_no) ? csNone : csWeak;
73                 break;
74             case Favourite_no:
75                 conflictSeverity = csNone;
76                 break;
77         }
78
79         // entry horizontal layout:
80         // * spacer (aka y position of image)
81         // * image
82         // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
83         int aux = option.rect.height() - SPACER - mControls[FavouriteControlStrong]->image()->height();
84
85         // font SMALL
86         QFont fontSmall = option.font;
87         fontSmall.setBold(false);
88         fontSmall.setPixelSize(aux*0.2);
89         QFontMetrics fmSmall(fontSmall);
90         // font SMALL bold
91         QFont fontSmallB = fontSmall;
92         fontSmallB.setBold(true);
93
94         // font BIG
95         QFont fontBig = option.font;
96         fontBig.setBold(false);
97         fontBig.setPixelSize(aux*0.33);
98         QFontMetrics fmBig(fontBig);
99
100         // font BIG bold
101         QFont fontBigB = fontBig;
102         fontBigB.setBold(true);
103         QFontMetrics fmBigB(fontBigB);
104
105         // background (in case of time conflicts)
106         if (conflictSeverity != csNone) {
107             painter->setBrush(conflictSeverity == csStrong ? Qt::yellow : QColor("lightyellow"));
108             painter->setPen(Qt::NoPen);
109             painter->drawRect(option.rect);
110         }
111
112         // background (without time conflicts)
113         else {
114             QStyleOption styleOption;
115             styleOption.rect = option.rect;
116             qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
117         }
118
119         // draw Controls
120         foreach(Control* c, mControls.values()) {
121             c->setEnabled(false);
122         }
123         switch (event->favourite()) {
124         case Favourite_strong:
125             mControls[FavouriteControlStrong]->paint(painter, option.rect);
126             break;
127         case Favourite_weak:
128             mControls[FavouriteControlWeak]->paint(painter, option.rect);
129             break;
130         case Favourite_no:
131             mControls[FavouriteControlNo]->paint(painter, option.rect);
132             break;
133         }
134
135         if(event->hasAlarm())
136             mControls[AlarmControlOn]->paint(painter, option.rect);
137         else
138             mControls[AlarmControlOff]->paint(painter, option.rect);
139
140         if(eventTimeConflict != Favourite_no)
141             mControls[WarningControl]->paint(painter, option.rect);
142
143         // draw texts
144         // it starts just below the image
145         // ("position of text" is lower-left angle of the first letter,
146         //  so the first line is actually at the same height as the image)
147         painter->setPen(QPen(conflictSeverity != csNone ? Qt::black : textColor));
148         QPointF titlePointF(option.rect.x() + SPACER,
149                             option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height());
150         QTime start = event->start().time();
151         painter->setFont(fontBig);
152         painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
153
154         // title
155         titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
156         painter->setFont(fontBigB);
157         QString title = event->title();
158         if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
159         {
160             // chop words from the end
161             while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
162             {
163                 title.chop(1);
164                 // chop characters one-by-one from the end
165                 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
166                 {
167                     title.chop(1);
168                 }
169             }
170             title += "...";
171         }
172         painter->drawText(titlePointF,title);
173
174         // persons
175         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
176         painter->setFont(fontSmall);
177         QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
178         painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
179
180         // track
181         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
182         painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
183     }
184
185     else // doesn't have parent - time-groups' elements (top items)
186     {
187         int numFav = numberOfFavourites(index);
188         int numAlarm = numberOfAlarms(index);
189
190         QStyleOptionButton styleOptionButton;
191         styleOptionButton.rect = option.rect;
192         if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
193         // styleOptionButton.text = qVariantValue<QString>(index.data());
194         qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
195         // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
196         // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
197
198         QFont fontSmall = option.font;
199         fontSmall.setBold(true);
200         fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
201         QFontMetrics fmSmall(fontSmall);
202
203         QFont fontBig = option.font;
204         fontBig.setBold(true);
205         fontBig.setPixelSize(option.rect.height()*scaleFactor2);
206         QFontMetrics fmBig(fontBig);
207
208         int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
209
210         // draw icons
211         painter->setPen(QPen(textColor));
212         painter->setFont(fontSmall);
213         QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
214         QPoint drawPoint =
215             option.rect.topRight()
216             - QPoint(
217                     spacer + image->width(),
218                     - option.rect.height()/2 + image->height()/2);
219         painter->drawImage(drawPoint,*image);
220         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
221                 QString::number(numFav));
222
223         drawPoint.setX(drawPoint.x() - spacer - image->width());
224         image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
225         painter->drawImage(drawPoint,*image);
226         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
227                 QString::number(numAlarm));
228
229         // draw texts
230         QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
231         drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
232         drawPoint.setY(drawPoint.y()+image->height() - 2);
233         painter->drawText(drawPoint,numEvents);
234
235         QPointF titlePointF = QPoint(
236                 option.rect.x()+SPACER,
237                 option.rect.y()+option.rect.height()-fmBig.descent());
238         painter->setFont(fontBig);
239         painter->drawText(titlePointF,index.data().value<QString>());
240     }
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         Control *control = mControls[id];
296         if (control->enabled()
297             and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
298         {
299             return id;
300         }
301     }
302
303     return ControlNone;
304 }
305
306 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
307     : mId(aControlId)
308     , mImage(new QImage(aImageName))
309     , mDrawPoint(QPoint(0,0))
310     , mEnabled(false)
311 {
312     QPoint p;
313     if (prev_control == NULL) {
314         p = QPoint(0, SPACER);
315     } else {
316         p = prev_control->drawPoint();
317     }
318     p.setX(p.x()-image()->width()-SPACER);
319     setDrawPoint(p);
320 }
321
322 void Delegate::Control::paint(QPainter* painter, const QRect rect)
323 {
324     painter->drawImage(drawPoint(rect),*image());
325     setEnabled(true);
326 }
327
328 void Delegate::defineControls()
329 {
330     // FAVOURITE ICONs
331     // strong
332     mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-strong.png"), NULL));
333     // weak
334     mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
335     // no
336     mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-no.png"), NULL));
337
338     // ALARM ICONs
339     // on
340     mControls.insert(AlarmControlOn,
341                     new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
342     // off
343     mControls.insert(AlarmControlOff,
344                     new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
345     // WARNING ICON
346     mControls.insert(WarningControl,
347                     new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
348         }
349
350 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
351 {
352     if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
353         return true;
354
355     return false;
356 }
357
358 int Delegate::numberOfFavourites(const QModelIndex &index) const
359 {
360     if(index.parent().isValid()) // it's event, not time-group
361         return 0;
362
363     int nrofFavs = 0;
364     for(int i=0; i<index.model()->rowCount(index); i++)
365         if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->favourite() != Favourite_no)
366             nrofFavs++;
367
368     return nrofFavs;
369 }
370
371 int Delegate::numberOfAlarms(const QModelIndex &index) const
372 {
373     if(index.parent().isValid()) // it's event, not time-group
374         return 0;
375
376     int nrofAlarms = 0;
377     for(int i=0; i<index.model()->rowCount(index); i++)
378         if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->hasAlarm())
379             nrofAlarms++;
380
381     return nrofAlarms;
382 }
383