source: src/mvc/delegate.cpp @ 508de33

qt5
Last change on this file since 508de33 was 508de33, checked in by kirilma <kirilma@…>, 12 years ago

do not draw showmap button for event is there is no map for its room

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