source: src/mvc/delegate.cpp @ 6df32f2

qt5
Last change on this file since 6df32f2 was 6df32f2, checked in by gregor herrmann <gregoa@…>, 10 years ago

Update GPL blurb in source files.

  • Property mode set to 100644
File size: 14.8 KB
RevLine 
[ca90cb1]1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 *
[6df32f2]4 * This file is part of ConfClerk.
[ca90cb1]5 *
[6df32f2]6 * ConfClerk is free software: you can redistribute it and/or modify it
[ca90cb1]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 *
[6df32f2]11 * ConfClerk is distributed in the hope that it will be useful, but
[ca90cb1]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
[6df32f2]17 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
[ca90cb1]18 */
[66327a0]19#include "delegate.h"
20#include "eventmodel.h"
[4693fa6]21#include <track.h>
[66327a0]22
23#include <QDebug>
24#include <QPainter>
25
[508de33]26#include "room.h"
27
[66327a0]28const int RADIUS = 10;
[b1a201a]29const int SPACER = 10;
[5d7c9e5]30
31const double scaleFactor1 = 0.4;
32const double scaleFactor2 = 0.8;
[66327a0]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;
[d7986a6]58    //QColor bkgrColor = QColor(0xAA,0xAA,0xAA);
59    QColor conflictColor = Qt::yellow;
[699d5b8]60
[66327a0]61    QPen borderPen(bkgrColor.darker());
62    if(hasParent(index))
63    {
[525448c]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();
[508de33]69        Event *event = static_cast<Event*>(index.internalPointer());
[b1a201a]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
[4693fa6]90        //int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
[b1a201a]91
[699d5b8]92        //Time conflicts are colored differently
[508de33]93        if(event->hasTimeConflict())
[d7986a6]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);
[699d5b8]102
[66327a0]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
[d7986a6]113            //painter->setBrush( bkgrColor );
114            painter->setBrush(itemGradient);
[66327a0]115            painter->setPen(borderPen);
116            painter->drawPath(endPath);
117
118            painter->setFont(option.font);
119        }
120        else // middle elements
121        {
[d7986a6]122            //painter->setBrush( bkgrColor );
123            painter->setBrush(itemGradient);
[66327a0]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
[a6a9e0b]138        foreach(Control* c, mControls.values()) {
139            c->setEnabled(false);
140        }
[508de33]141        if(event->isFavourite())
[525448c]142            mControls[FavouriteControlOn]->paint(painter, option.rect);
[680a4da]143        else
[525448c]144            mControls[FavouriteControlOff]->paint(painter, option.rect);
[a5c1179]145#ifdef MAEMO
[508de33]146        if(event->hasAlarm())
[525448c]147            mControls[AlarmControlOn]->paint(painter, option.rect);
[b6cd05c]148        else
[525448c]149            mControls[AlarmControlOff]->paint(painter, option.rect);
[a5c1179]150#endif
[525448c]151        if (event->room()->hasMap())
152            mControls[MapControl]->paint(painter, option.rect);
[508de33]153        if(event->hasTimeConflict())
[525448c]154            mControls[WarningControl]->paint(painter, option.rect);
[5d7c9e5]155
156        // draw texts
[525448c]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());
[b1a201a]162        QTime start = event->start().time();
163        painter->setFont(fontBig);
[0d995ed]164        painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
[b1a201a]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());
[005e2b7]190        painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
[66327a0]191    }
192    else // doesn't have parent - time-groups' elements (top items)
193    {
[b1a201a]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
[66327a0]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
[4be95b8]240        // draw icons
[5d7c9e5]241        painter->setFont(fontSmall);
[b0a3bc7]242        QImage *image = mControls[FavouriteControlOn]->image();
[4be95b8]243        QPoint drawPoint =
244            option.rect.topRight()
245            - QPoint(
[b0a3bc7]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),
[4be95b8]250                QString::number(numberOfFavourities(index)));
[a5c1179]251#ifdef MAEMO
[b0a3bc7]252        drawPoint.setX(drawPoint.x() - spacer - image->width());
[4be95b8]253        painter->drawImage(drawPoint,*mControls[AlarmControlOn]->image());
[b0a3bc7]254        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
[4be95b8]255                QString::number(numberOfAlarms(index)));
[a5c1179]256#endif
[4be95b8]257        // draw texts
[5d7c9e5]258        QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
259        drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
[b0a3bc7]260        drawPoint.setY(drawPoint.y()+image->height() - 2);
[5d7c9e5]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()));
[66327a0]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    {
[a3f6b00]284        return QSize(40,40);
[66327a0]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();
[900bcf1]328        Control *control = mControls[id];
329        if (control->enabled()
330            and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
[e7340e1]331        {
[900bcf1]332            return id;
[3f3e22d]333        }
[66327a0]334    }
335
336    return ControlNone;
337}
338
[525448c]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))
[a6a9e0b]343    , mEnabled(false)
[525448c]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());
[a6a9e0b]358    setEnabled(true);
[525448c]359}
360
[66327a0]361void Delegate::defineControls()
362{
[680a4da]363    // FAVOURITE ICONs
364    // on
[525448c]365    mControls.insert(FavouriteControlOn, new Control(FavouriteControlOn, QString(":icons/favourite-onBig.png"), NULL));
[680a4da]366    // off
[525448c]367    mControls.insert(FavouriteControlOff, new Control(FavouriteControlOff, QString(":icons/favourite-offBig.png"), NULL));
[680a4da]368
[a5c1179]369#ifdef MAEMO
[680a4da]370    // ALARM ICONs
371    // on
[525448c]372    mControls.insert(AlarmControlOn,
373                    new Control(AlarmControlOn, QString(":icons/alarm-onBig.png"), mControls[FavouriteControlOn]));
[680a4da]374    // off
[525448c]375    mControls.insert(AlarmControlOff,
376                    new Control(AlarmControlOff, QString(":icons/alarm-offBig.png"), mControls[FavouriteControlOff]));
[03ff157]377
378    // MAP ICON
[525448c]379    mControls.insert(MapControl,
380                    new Control(MapControl, QString(":icons/compassBig.png"), mControls[AlarmControlOn]));
[a5c1179]381#else
382    // MAP ICON
[525448c]383    mControls.insert(MapControl,
384                    new Control(MapControl, QString(":icons/compassBig.png"), mControls[FavouriteControlOn]));
[a5c1179]385#endif
[f2ef735]386
[336fa33]387    // WARNING ICON
[525448c]388    mControls.insert(WarningControl,
389                    new Control(WarningControl, QString(":icons/exclamation.png"), mControls[MapControl]));
[66327a0]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
[4be95b8]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.