source: src/mvc/delegate.cpp @ dcefa71

qt5
Last change on this file since dcefa71 was 68b2df2, checked in by gregor herrmann <gregoa@…>, 11 years ago

Add copyright to source.

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