source: src/mvc/delegate.cpp @ ec813bb

qt5
Last change on this file since ec813bb was ec813bb, checked in by Philipp Spitzer <philipp@…>, 11 years ago

This should close ticket #35 ([maemo] conflict icon overlaps alarm icon).

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