source: src/mvc/delegate.cpp @ 83f0ba7

qt5
Last change on this file since 83f0ba7 was 900bcf1, checked in by kirilma <kirilma@…>, 12 years ago

use enabled flag instead of repeated criateria check

  • Property mode set to 100644
File size: 14.8 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        // entry horisontal layout:
65        // * spacer (aka y position of image)
66        // * image
67        // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
68        int aux = option.rect.height() - SPACER - mControls[FavouriteControlOn]->image()->height();
69        Event *event = static_cast<Event*>(index.internalPointer());
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        QFontMetrics fmSmallB(fontSmallB);
79
80        // font BIG
81        QFont fontBig = option.font;
82        fontBig.setBold(false);
83        fontBig.setPixelSize(aux*0.33);
84        QFontMetrics fmBig(fontBig);
85        // font BIG bold
86        QFont fontBigB = fontBig;
87        fontBigB.setBold(true);
88        QFontMetrics fmBigB(fontBigB);
89
90        //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
91
92        //Time conflicts are colored differently
93        if(event->hasTimeConflict())
94            bkgrColor = conflictColor;
95
96        QLinearGradient itemGradient(option.rect.topLeft(), option.rect.bottomLeft());
97        itemGradient.setColorAt(0.0, Qt::white);
98        itemGradient.setColorAt(0.25, bkgrColor);
99        itemGradient.setColorAt(0.5, bkgrColor);
100        itemGradient.setColorAt(0.75, bkgrColor);
101        itemGradient.setColorAt(1.0, Qt::white);
102
103        if(isLast(index))
104        {
105            QPainterPath endPath;
106            endPath.moveTo(option.rect.topLeft());
107            endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
108            endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90);
109            endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0));
110            endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
111            endPath.lineTo(option.rect.topRight());
112
113            //painter->setBrush( bkgrColor );
114            painter->setBrush(itemGradient);
115            painter->setPen(borderPen);
116            painter->drawPath(endPath);
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            // vertical lines
129            painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
130            painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
131            // horizontal lines
132            painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
133
134            painter->setFont(option.font);
135        }
136
137        // draw Controls
138        foreach(Control* c, mControls.values()) {
139            c->setEnabled(false);
140        }
141        if(event->isFavourite())
142            mControls[FavouriteControlOn]->paint(painter, option.rect);
143        else
144            mControls[FavouriteControlOff]->paint(painter, option.rect);
145#ifdef MAEMO
146        if(event->hasAlarm())
147            mControls[AlarmControlOn]->paint(painter, option.rect);
148        else
149            mControls[AlarmControlOff]->paint(painter, option.rect);
150#endif
151        if (event->room()->hasMap())
152            mControls[MapControl]->paint(painter, option.rect);
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        painter->drawText(titlePointF,"Presenter(s): " + event->persons().join(" and "));
188        // track
189        titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
190        painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
191    }
192    else // doesn't have parent - time-groups' elements (top items)
193    {
194        QFont fontSmall = option.font;
195        fontSmall.setBold(true);
196        fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
197        QFontMetrics fmSmall(fontSmall);
198
199        QFont fontBig = option.font;
200        fontBig.setBold(true);
201        fontBig.setPixelSize(option.rect.height()*scaleFactor2);
202        QFontMetrics fmBig(fontBig);
203
204        int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
205
206        QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
207        //titleGradient.setColorAt(0.0, Qt::white);
208        titleGradient.setColorAt(0.0, bkgrColor);
209        titleGradient.setColorAt(0.5, Qt::white);
210        titleGradient.setColorAt(1.0, bkgrColor);
211
212        QPainterPath titlePath;
213        if(isExpanded(index))
214        {
215            titlePath.moveTo(option.rect.bottomLeft());
216            titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
217            titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
218            titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
219            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
220            titlePath.lineTo(option.rect.bottomRight());
221            titlePath.closeSubpath();
222        }
223        else
224        {
225            titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
226            titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
227            titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
228            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
229            titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
230            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
231            titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
232            titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);     
233            titlePath.closeSubpath();
234        }
235
236        painter->setBrush(titleGradient);
237        painter->setPen(borderPen);
238        painter->drawPath(titlePath);
239
240        // draw icons
241        painter->setFont(fontSmall);
242        QImage *image = mControls[FavouriteControlOn]->image();
243        QPoint drawPoint =
244            option.rect.topRight()
245            - QPoint(
246                    spacer + image->width(),
247                    - option.rect.height()/2 + image->height()/2);
248        painter->drawImage(drawPoint,*image);
249        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
250                QString::number(numberOfFavourities(index)));
251#ifdef MAEMO
252        drawPoint.setX(drawPoint.x() - spacer - image->width());
253        painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
254        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
255                QString::number(numberOfAlarms(index)));
256#endif
257        // draw texts
258        QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
259        drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
260        drawPoint.setY(drawPoint.y()+image->height() - 2);
261        painter->drawText(drawPoint,numEvents);
262
263        QPointF titlePointF = QPoint(
264                option.rect.x()+SPACER,
265                option.rect.y()+option.rect.height()-fmBig.descent());
266        painter->setFont(fontBig);
267
268        painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
269    }
270
271    //// HIGHLIGHTING SELECTED ITEM
272    //if (option.state & QStyle::State_Selected)
273        //painter->fillRect(option.rect, option.palette.highlight());
274
275    painter->restore();
276}
277
278QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
279{
280    Q_UNUSED(option)
281
282    if (index.internalId() == 0) // time group
283    {
284        return QSize(40,40);
285    }
286    else // event
287    {
288        return QSize(100,100);
289    }
290}
291
292bool Delegate::hasParent( const QModelIndex &index ) const
293{
294    if( index.parent().isValid() )
295        return true;
296    else
297        return false;
298}
299 
300bool Delegate::isLast( const QModelIndex &index ) const
301{
302    if(!hasParent(index))
303        return false; // what should be returned here?
304
305    if(index.row() >= (index.model()->rowCount(index.parent())-1))
306        return true;
307    else
308        return false;
309}
310
311bool Delegate::isExpanded( const QModelIndex &index ) const
312{
313    if( !mViewPtr )
314        return false;
315    else
316        return mViewPtr->isExpanded( index );
317}
318
319Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
320{
321    if(!hasParent(aIndex)) // time-group item (root item)
322        return ControlNone;
323
324    QListIterator<ControlId> i(mControls.keys());
325    while (i.hasNext())
326    {
327        ControlId id = i.next();
328        Control *control = mControls[id];
329        if (control->enabled()
330            and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
331        {
332            return id;
333        }
334    }
335
336    return ControlNone;
337}
338
339Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
340    : mId(aControlId)
341    , mImage(new QImage(aImageName))
342    , mDrawPoint(QPoint(0,0))
343    , mEnabled(false)
344{
345    QPoint p;
346    if (prev_control == NULL) {
347        p = QPoint(0, SPACER);
348    } else {
349        p = prev_control->drawPoint();
350    }
351    p.setX(p.x()-image()->width()-SPACER);
352    setDrawPoint(p);
353}
354
355void Delegate::Control::paint(QPainter* painter, const QRect rect)
356{
357    painter->drawImage(drawPoint(rect),*image());
358    setEnabled(true);
359}
360
361void Delegate::defineControls()
362{
363    // FAVOURITE ICONs
364    // on
365    mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/favourite-onBig.png"), NULL));
366    // off
367    mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/favourite-offBig.png"), NULL));
368
369#ifdef MAEMO
370    // ALARM ICONs
371    // on
372    mControls.insert(AlarmControlOn,
373                    new Control(AlarmControlOn, QString(":icons/alarm-onBig.png"), mControls[FavouriteControlOn]));
374    // off
375    mControls.insert(AlarmControlOff,
376                    new Control(AlarmControlOff, QString(":icons/alarm-offBig.png"), mControls[FavouriteControlOff]));
377
378    // MAP ICON
379    mControls.insert(MapControl,
380                    new Control(MapControl, QString(":icons/compassBig.png"), mControls[AlarmControlOn]));
381#else
382    // MAP ICON
383    mControls.insert(MapControl,
384                    new Control(MapControl, QString(":icons/compassBig.png"), mControls[FavouriteControlOn]));
385#endif
386
387    // WARNING ICON
388    mControls.insert(WarningControl,
389                    new Control(WarningControl, QString(":icons/exclamation.png"), mControls[MapControl]));
390}
391
392bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
393{
394    if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
395        return true;
396
397    return false;
398}
399
400int Delegate::numberOfFavourities(const QModelIndex &index) const
401{
402    if(index.parent().isValid()) // it's event, not time-group
403        return 0;
404
405    int nrofFavs = 0;
406    for(int i=0; i<index.model()->rowCount(index); i++)
407        if(static_cast<Event*>(index.child(i,0).internalPointer())->isFavourite())
408            nrofFavs++;
409
410    return nrofFavs;
411}
412
413int Delegate::numberOfAlarms(const QModelIndex &index) const
414{
415    if(index.parent().isValid()) // it's event, not time-group
416        return 0;
417
418    int nrofAlarms = 0;
419    for(int i=0; i<index.model()->rowCount(index); i++)
420        if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())
421            nrofAlarms++;
422
423    return nrofAlarms;
424}
425
Note: See TracBrowser for help on using the repository browser.