]> ToastFreeware Gitweb - toast/confclerk.git/blob - src/mvc/delegate.cpp
Event favourite is now tristate in the code now and the corresponding buttons are...
[toast/confclerk.git] / src / mvc / delegate.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2015 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 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
59     QColor textColor = option.palette.color(QPalette::Text);
60
61     if(hasParent(index))
62     {
63         // entry horizontal layout:
64         // * spacer (aka y position of image)
65         // * image
66         // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
67         int aux = option.rect.height() - SPACER - mControls[FavouriteControlStrong]->image()->height();
68         Event *event = static_cast<Event*>(index.internalPointer());
69
70         // font SMALL
71         QFont fontSmall = option.font;
72         fontSmall.setBold(false);
73         fontSmall.setPixelSize(aux*0.2);
74         QFontMetrics fmSmall(fontSmall);
75         // font SMALL bold
76         QFont fontSmallB = fontSmall;
77         fontSmallB.setBold(true);
78
79         // font BIG
80         QFont fontBig = option.font;
81         fontBig.setBold(false);
82         fontBig.setPixelSize(aux*0.33);
83         QFontMetrics fmBig(fontBig);
84
85         // font BIG bold
86         QFont fontBigB = fontBig;
87         fontBigB.setBold(true);
88         QFontMetrics fmBigB(fontBigB);
89
90         // background (in case of time conflicts)
91         if(event->hasTimeConflict()) {
92             painter->setBrush(Qt::yellow);
93             painter->setPen(Qt::NoPen);
94             painter->drawRect(option.rect);
95         }
96
97         // background (without time conflicts)
98         else {
99             QStyleOption styleOption;
100             styleOption.rect = option.rect;
101             qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
102         }
103
104         // draw Controls
105         foreach(Control* c, mControls.values()) {
106             c->setEnabled(false);
107         }
108         switch (event->favourite()) {
109         case Favourite_strong:
110             mControls[FavouriteControlStrong]->paint(painter, option.rect);
111             break;
112         case Favourite_weak:
113             mControls[FavouriteControlWeak]->paint(painter, option.rect);
114             break;
115         case Favourite_no:
116             mControls[FavouriteControlNo]->paint(painter, option.rect);
117             break;
118         }
119
120         if(event->hasAlarm())
121             mControls[AlarmControlOn]->paint(painter, option.rect);
122         else
123             mControls[AlarmControlOff]->paint(painter, option.rect);
124
125         if(event->hasTimeConflict())
126             mControls[WarningControl]->paint(painter, option.rect);
127
128         // draw texts
129         // it starts just below the image
130         // ("position of text" is lower-left angle of the first letter,
131         //  so the first line is actually at the same height as the image)
132         painter->setPen(QPen(event->hasTimeConflict() ? Qt::black : textColor));
133         QPointF titlePointF(option.rect.x() + SPACER,
134                             option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height());
135         QTime start = event->start().time();
136         painter->setFont(fontBig);
137         painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
138
139         // title
140         titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
141         painter->setFont(fontBigB);
142         QString title = event->title();
143         if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
144         {
145             // chop words from the end
146             while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
147             {
148                 title.chop(1);
149                 // chop characters one-by-one from the end
150                 while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
151                 {
152                     title.chop(1);
153                 }
154             }
155             title += "...";
156         }
157         painter->drawText(titlePointF,title);
158
159         // persons
160         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
161         painter->setFont(fontSmall);
162         QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
163         painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
164
165         // track
166         titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
167         painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
168     }
169
170     else // doesn't have parent - time-groups' elements (top items)
171     {
172         int numFav = numberOfFavourities(index);
173         int numAlarm = numberOfAlarms(index);
174
175         QStyleOptionButton styleOptionButton;
176         styleOptionButton.rect = option.rect;
177         if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
178         // styleOptionButton.text = qVariantValue<QString>(index.data());
179         qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
180         // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
181         // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
182
183         QFont fontSmall = option.font;
184         fontSmall.setBold(true);
185         fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
186         QFontMetrics fmSmall(fontSmall);
187
188         QFont fontBig = option.font;
189         fontBig.setBold(true);
190         fontBig.setPixelSize(option.rect.height()*scaleFactor2);
191         QFontMetrics fmBig(fontBig);
192
193         int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
194
195         // draw icons
196         painter->setPen(QPen(textColor));
197         painter->setFont(fontSmall);
198         QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
199         QPoint drawPoint =
200             option.rect.topRight()
201             - QPoint(
202                     spacer + image->width(),
203                     - option.rect.height()/2 + image->height()/2);
204         painter->drawImage(drawPoint,*image);
205         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
206                 QString::number(numFav));
207
208         drawPoint.setX(drawPoint.x() - spacer - image->width());
209         image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
210         painter->drawImage(drawPoint,*image);
211         painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
212                 QString::number(numAlarm));
213
214         // draw texts
215         QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
216         drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
217         drawPoint.setY(drawPoint.y()+image->height() - 2);
218         painter->drawText(drawPoint,numEvents);
219
220         QPointF titlePointF = QPoint(
221                 option.rect.x()+SPACER,
222                 option.rect.y()+option.rect.height()-fmBig.descent());
223         painter->setFont(fontBig);
224         painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
225     }
226
227     painter->restore();
228 }
229
230 QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
231 {
232     Q_UNUSED(option)
233
234     if (index.internalId() == 0) // time group
235     {
236         return QSize(40,40);
237     }
238     else // event
239     {
240         return QSize(100,100);
241     }
242 }
243
244 bool Delegate::hasParent( const QModelIndex &index ) const
245 {
246     if( index.parent().isValid() )
247         return true;
248     else
249         return false;
250 }
251   
252 bool Delegate::isLast( const QModelIndex &index ) const
253 {
254     if(!hasParent(index))
255         return false; // what should be returned here?
256
257     if(index.row() >= (index.model()->rowCount(index.parent())-1))
258         return true;
259     else
260         return false;
261 }
262
263 bool Delegate::isExpanded( const QModelIndex &index ) const
264 {
265     if( !mViewPtr )
266         return false;
267     else
268         return mViewPtr->isExpanded( index );
269 }
270
271 Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
272 {
273     if(!hasParent(aIndex)) // time-group item (root item)
274         return ControlNone;
275
276     QListIterator<ControlId> i(mControls.keys());
277     while (i.hasNext())
278     {
279         ControlId id = i.next();
280         Control *control = mControls[id];
281         if (control->enabled()
282             and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
283         {
284             return id;
285         }
286     }
287
288     return ControlNone;
289 }
290
291 Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
292     : mId(aControlId)
293     , mImage(new QImage(aImageName))
294     , mDrawPoint(QPoint(0,0))
295     , mEnabled(false)
296 {
297     QPoint p;
298     if (prev_control == NULL) {
299         p = QPoint(0, SPACER);
300     } else {
301         p = prev_control->drawPoint();
302     }
303     p.setX(p.x()-image()->width()-SPACER);
304     setDrawPoint(p);
305 }
306
307 void Delegate::Control::paint(QPainter* painter, const QRect rect)
308 {
309     painter->drawImage(drawPoint(rect),*image());
310     setEnabled(true);
311 }
312
313 void Delegate::defineControls()
314 {
315     // FAVOURITE ICONs
316     // strong
317     mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-on.png"), NULL));
318     // weak
319     mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
320     // no
321     mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-off.png"), NULL));
322
323     // ALARM ICONs
324     // on
325     mControls.insert(AlarmControlOn,
326                     new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
327     // off
328     mControls.insert(AlarmControlOff,
329                     new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
330     // WARNING ICON
331     mControls.insert(WarningControl,
332                     new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
333         }
334
335 bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
336 {
337     if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
338         return true;
339
340     return false;
341 }
342
343 int Delegate::numberOfFavourities(const QModelIndex &index) const
344 {
345     if(index.parent().isValid()) // it's event, not time-group
346         return 0;
347
348     int nrofFavs = 0;
349     for(int i=0; i<index.model()->rowCount(index); i++)
350         if(static_cast<Event*>(index.child(i,0).internalPointer())->favourite() != Favourite_no)
351             nrofFavs++;
352
353     return nrofFavs;
354 }
355
356 int Delegate::numberOfAlarms(const QModelIndex &index) const
357 {
358     if(index.parent().isValid()) // it's event, not time-group
359         return 0;
360
361     int nrofAlarms = 0;
362     for(int i=0; i<index.model()->rowCount(index); i++)
363         if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())
364             nrofAlarms++;
365
366     return nrofAlarms;
367 }
368