source: src/mvc/delegate.cpp @ 81d87d7

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

Renamed favourite-on.png to favourite-strong.png and favourite-off.png to favourite-no.png.

  • Property mode set to 100644
File size: 13.0 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2015 Philipp Spitzer, gregor herrmann, Stefan Stahl
4 *
5 * This file is part of ConfClerk.
6 *
7 * ConfClerk is free software: you can redistribute it and/or modify it
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 *
12 * ConfClerk is distributed in the hope that it will be useful, but
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
18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19 */
20#include "delegate.h"
21#include "eventmodel.h"
22#include "track.h"
23
24#include <QDebug>
25#include <QPainter>
26
27#include "room.h"
28
29const int RADIUS = 10;
30const int SPACER = 10;
31
32const double scaleFactor1 = 0.4;
33const double scaleFactor2 = 0.8;
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
59    QColor textColor = option.palette.color(QPalette::Text);
60
61    if(hasParent(index))
62    {
63        Event *event = static_cast<Event*>(index.internalPointer());
64
65        // determine severity of conflict
66        Favourite eventTimeConflict = event->timeConflict(); // cache value as event->timeConflict is expensive
67        enum ConflictSeverity {csNone, csWeak, csStrong} conflictSeverity;
68        switch (event->favourite()) {
69            case Favourite_strong:
70                conflictSeverity = (eventTimeConflict == Favourite_strong) ? csStrong : csNone;
71                break;
72            case Favourite_weak:
73                conflictSeverity = (eventTimeConflict == Favourite_no) ? csNone : csWeak;
74                break;
75            case Favourite_no:
76                conflictSeverity = csNone;
77                break;
78        }
79
80        // entry horizontal layout:
81        // * spacer (aka y position of image)
82        // * image
83        // * rest is text, which is 1 line of title with big letters and 2 lines of Presenter and Track
84        int aux = option.rect.height() - SPACER - mControls[FavouriteControlStrong]->image()->height();
85
86        // font SMALL
87        QFont fontSmall = option.font;
88        fontSmall.setBold(false);
89        fontSmall.setPixelSize(aux*0.2);
90        QFontMetrics fmSmall(fontSmall);
91        // font SMALL bold
92        QFont fontSmallB = fontSmall;
93        fontSmallB.setBold(true);
94
95        // font BIG
96        QFont fontBig = option.font;
97        fontBig.setBold(false);
98        fontBig.setPixelSize(aux*0.33);
99        QFontMetrics fmBig(fontBig);
100
101        // font BIG bold
102        QFont fontBigB = fontBig;
103        fontBigB.setBold(true);
104        QFontMetrics fmBigB(fontBigB);
105
106        // background (in case of time conflicts)
107        if (conflictSeverity != csNone) {
108            painter->setBrush(conflictSeverity == csStrong ? Qt::yellow : QColor("lightyellow"));
109            painter->setPen(Qt::NoPen);
110            painter->drawRect(option.rect);
111        }
112
113        // background (without time conflicts)
114        else {
115            QStyleOption styleOption;
116            styleOption.rect = option.rect;
117            qApp->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &styleOption, painter, mViewPtr);
118        }
119
120        // draw Controls
121        foreach(Control* c, mControls.values()) {
122            c->setEnabled(false);
123        }
124        switch (event->favourite()) {
125        case Favourite_strong:
126            mControls[FavouriteControlStrong]->paint(painter, option.rect);
127            break;
128        case Favourite_weak:
129            mControls[FavouriteControlWeak]->paint(painter, option.rect);
130            break;
131        case Favourite_no:
132            mControls[FavouriteControlNo]->paint(painter, option.rect);
133            break;
134        }
135
136        if(event->hasAlarm())
137            mControls[AlarmControlOn]->paint(painter, option.rect);
138        else
139            mControls[AlarmControlOff]->paint(painter, option.rect);
140
141        if(eventTimeConflict != Favourite_no)
142            mControls[WarningControl]->paint(painter, option.rect);
143
144        // draw texts
145        // it starts just below the image
146        // ("position of text" is lower-left angle of the first letter,
147        //  so the first line is actually at the same height as the image)
148        painter->setPen(QPen(conflictSeverity != csNone ? Qt::black : textColor));
149        QPointF titlePointF(option.rect.x() + SPACER,
150                            option.rect.y() + SPACER + mControls[FavouriteControlStrong]->image()->height());
151        QTime start = event->start().time();
152        painter->setFont(fontBig);
153        painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
154
155        // title
156        titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
157        painter->setFont(fontBigB);
158        QString title = event->title();
159        if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
160        {
161            // chop words from the end
162            while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
163            {
164                title.chop(1);
165                // chop characters one-by-one from the end
166                while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
167                {
168                    title.chop(1);
169                }
170            }
171            title += "...";
172        }
173        painter->drawText(titlePointF,title);
174
175        // persons
176        titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
177        painter->setFont(fontSmall);
178        QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
179        painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
180
181        // track
182        titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
183        painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
184    }
185
186    else // doesn't have parent - time-groups' elements (top items)
187    {
188        int numFav = numberOfFavourities(index);
189        int numAlarm = numberOfAlarms(index);
190
191        QStyleOptionButton styleOptionButton;
192        styleOptionButton.rect = option.rect;
193        if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
194        // styleOptionButton.text = qVariantValue<QString>(index.data());
195        qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
196        // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
197        // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
198
199        QFont fontSmall = option.font;
200        fontSmall.setBold(true);
201        fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
202        QFontMetrics fmSmall(fontSmall);
203
204        QFont fontBig = option.font;
205        fontBig.setBold(true);
206        fontBig.setPixelSize(option.rect.height()*scaleFactor2);
207        QFontMetrics fmBig(fontBig);
208
209        int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
210
211        // draw icons
212        painter->setPen(QPen(textColor));
213        painter->setFont(fontSmall);
214        QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
215        QPoint drawPoint =
216            option.rect.topRight()
217            - QPoint(
218                    spacer + image->width(),
219                    - option.rect.height()/2 + image->height()/2);
220        painter->drawImage(drawPoint,*image);
221        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
222                QString::number(numFav));
223
224        drawPoint.setX(drawPoint.x() - spacer - image->width());
225        image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
226        painter->drawImage(drawPoint,*image);
227        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
228                QString::number(numAlarm));
229
230        // draw texts
231        QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
232        drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
233        drawPoint.setY(drawPoint.y()+image->height() - 2);
234        painter->drawText(drawPoint,numEvents);
235
236        QPointF titlePointF = QPoint(
237                option.rect.x()+SPACER,
238                option.rect.y()+option.rect.height()-fmBig.descent());
239        painter->setFont(fontBig);
240        painter->drawText(titlePointF,qVariantValue<QString>(index.data()));
241    }
242
243    painter->restore();
244}
245
246QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
247{
248    Q_UNUSED(option)
249
250    if (index.internalId() == 0) // time group
251    {
252        return QSize(40,40);
253    }
254    else // event
255    {
256        return QSize(100,100);
257    }
258}
259
260bool Delegate::hasParent( const QModelIndex &index ) const
261{
262    if( index.parent().isValid() )
263        return true;
264    else
265        return false;
266}
267 
268bool Delegate::isLast( const QModelIndex &index ) const
269{
270    if(!hasParent(index))
271        return false; // what should be returned here?
272
273    if(index.row() >= (index.model()->rowCount(index.parent())-1))
274        return true;
275    else
276        return false;
277}
278
279bool Delegate::isExpanded( const QModelIndex &index ) const
280{
281    if( !mViewPtr )
282        return false;
283    else
284        return mViewPtr->isExpanded( index );
285}
286
287Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
288{
289    if(!hasParent(aIndex)) // time-group item (root item)
290        return ControlNone;
291
292    QListIterator<ControlId> i(mControls.keys());
293    while (i.hasNext())
294    {
295        ControlId id = i.next();
296        Control *control = mControls[id];
297        if (control->enabled()
298            and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
299        {
300            return id;
301        }
302    }
303
304    return ControlNone;
305}
306
307Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
308    : mId(aControlId)
309    , mImage(new QImage(aImageName))
310    , mDrawPoint(QPoint(0,0))
311    , mEnabled(false)
312{
313    QPoint p;
314    if (prev_control == NULL) {
315        p = QPoint(0, SPACER);
316    } else {
317        p = prev_control->drawPoint();
318    }
319    p.setX(p.x()-image()->width()-SPACER);
320    setDrawPoint(p);
321}
322
323void Delegate::Control::paint(QPainter* painter, const QRect rect)
324{
325    painter->drawImage(drawPoint(rect),*image());
326    setEnabled(true);
327}
328
329void Delegate::defineControls()
330{
331    // FAVOURITE ICONs
332    // strong
333    mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-strong.png"), NULL));
334    // weak
335    mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
336    // no
337    mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-no.png"), NULL));
338
339    // ALARM ICONs
340    // on
341    mControls.insert(AlarmControlOn,
342                    new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
343    // off
344    mControls.insert(AlarmControlOff,
345                    new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
346    // WARNING ICON
347    mControls.insert(WarningControl,
348                    new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
349        }
350
351bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
352{
353    if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
354        return true;
355
356    return false;
357}
358
359int Delegate::numberOfFavourities(const QModelIndex &index) const
360{
361    if(index.parent().isValid()) // it's event, not time-group
362        return 0;
363
364    int nrofFavs = 0;
365    for(int i=0; i<index.model()->rowCount(index); i++)
366        if(static_cast<Event*>(index.child(i,0).internalPointer())->favourite() != Favourite_no)
367            nrofFavs++;
368
369    return nrofFavs;
370}
371
372int Delegate::numberOfAlarms(const QModelIndex &index) const
373{
374    if(index.parent().isValid()) // it's event, not time-group
375        return 0;
376
377    int nrofAlarms = 0;
378    for(int i=0; i<index.model()->rowCount(index); i++)
379        if(static_cast<Event*>(index.child(i,0).internalPointer())->hasAlarm())
380            nrofAlarms++;
381
382    return nrofAlarms;
383}
384
Note: See TracBrowser for help on using the repository browser.