source: src/mvc/delegate.cpp @ a5e1f50

Last change on this file since a5e1f50 was a5e1f50, checked in by gregor herrmann <gregor@…>, 11 months ago

Clean up #includes.

(qtcreator is helpful but only partially.)

  • Property mode set to 100644
File size: 13.1 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2021 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 <QDebug>
21#include <QPainter>
22
23#include "delegate.h"
24#include "eventmodel.h"
25#include "track.h"
26#include "conference.h"
27#include "room.h"
28#include "application.h"
29
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 = csNone;
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        Conference& conference = ((Application*) qApp)->activeConference();
152        QTime start = conference.shiftTime(event->start().time());
153        painter->setFont(fontBig);
154        painter->drawText(titlePointF,start.toString("hh:mm") + "-" + start.addSecs(event->duration()).toString("hh:mm") + ", " + event->roomName());
155
156        // title
157        titlePointF.setY(titlePointF.y()+fmBig.height()-fmBig.descent());
158        painter->setFont(fontBigB);
159        QString title = event->title();
160        if(fmBigB.boundingRect(title).width() > (option.rect.width()-2*SPACER)) // the title won't fit the screen
161        {
162            // chop words from the end
163            while( (fmBigB.boundingRect(title + "...").width() > (option.rect.width()-2*SPACER)) && !title.isEmpty())
164            {
165                title.chop(1);
166                // chop characters one-by-one from the end
167                while( (!title.at(title.length()-1).isSpace()) && !title.isEmpty())
168                {
169                    title.chop(1);
170                }
171            }
172            title += "...";
173        }
174        painter->drawText(titlePointF,title);
175
176        // persons
177        titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
178        painter->setFont(fontSmall);
179        QString presenterPrefix = event->persons().count() < 2 ? "Presenter" : "Presenters";
180        painter->drawText(titlePointF,presenterPrefix + ": " + event->persons().join(" and "));
181
182        // track
183        titlePointF.setY(titlePointF.y()+fmSmall.height()-fmSmall.descent());
184        painter->drawText(titlePointF,"Track: " + Track::retrieveTrackName(event->trackId()));
185    }
186
187    else // doesn't have parent - time-groups' elements (top items)
188    {
189        int numFav = numberOfFavourites(index);
190        int numAlarm = numberOfAlarms(index);
191
192        QStyleOptionButton styleOptionButton;
193        styleOptionButton.rect = option.rect;
194        if (isExpanded(index)) styleOptionButton.state = QStyle::State_Sunken;
195        // styleOptionButton.text = qVariantValue<QString>(index.data());
196        qApp->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &styleOptionButton, painter, mViewPtr);
197        // qApp->style()->drawControl(QStyle::CE_PushButtonLabel, &styleOptionButton, painter, mViewPtr);
198        // qApp->style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &styleOptionButton, painter, mViewPtr);
199
200        QFont fontSmall = option.font;
201        fontSmall.setBold(true);
202        fontSmall.setPixelSize(option.rect.height()*scaleFactor1);
203        QFontMetrics fmSmall(fontSmall);
204
205        QFont fontBig = option.font;
206        fontBig.setBold(true);
207        fontBig.setPixelSize(option.rect.height()*scaleFactor2);
208        QFontMetrics fmBig(fontBig);
209
210        int spacer = (fmSmall.boundingRect("999").width() < SPACER) ? SPACER : fmSmall.boundingRect("999").width();
211
212        // draw icons
213        painter->setPen(QPen(textColor));
214        painter->setFont(fontSmall);
215        QImage *image = mControls[numFav ? FavouriteControlStrong : FavouriteControlNo]->image();
216        QPoint drawPoint =
217            option.rect.topRight()
218            - QPoint(
219                    spacer + image->width(),
220                    - option.rect.height()/2 + image->height()/2);
221        painter->drawImage(drawPoint,*image);
222        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
223                QString::number(numFav));
224
225        drawPoint.setX(drawPoint.x() - spacer - image->width());
226        image = mControls[numAlarm ? AlarmControlOn : AlarmControlOff]->image();
227        painter->drawImage(drawPoint,*image);
228        painter->drawText(drawPoint+QPoint(image->width()+2, image->height() - 2),
229                QString::number(numAlarm));
230
231        // draw texts
232        QString numEvents = QString::number(index.model()->rowCount(index)).append("/");
233        drawPoint.setX(drawPoint.x() - spacer - fmSmall.boundingRect(numEvents).width());
234        drawPoint.setY(drawPoint.y()+image->height() - 2);
235        painter->drawText(drawPoint,numEvents);
236
237        QPointF titlePointF = QPoint(
238                option.rect.x()+SPACER,
239                option.rect.y()+option.rect.height()-fmBig.descent());
240        painter->setFont(fontBig);
241        painter->drawText(titlePointF,index.data().value<QString>());
242    }
243
244    painter->restore();
245}
246
247QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
248{
249    Q_UNUSED(option)
250
251    if (index.internalId() == 0) // time group
252    {
253        return QSize(40,40);
254    }
255    else // event
256    {
257        return QSize(100,100);
258    }
259}
260
261bool Delegate::hasParent( const QModelIndex &index ) const
262{
263    if( index.parent().isValid() )
264        return true;
265    else
266        return false;
267}
268 
269bool Delegate::isLast( const QModelIndex &index ) const
270{
271    if(!hasParent(index))
272        return false; // what should be returned here?
273
274    if(index.row() >= (index.model()->rowCount(index.parent())-1))
275        return true;
276    else
277        return false;
278}
279
280bool Delegate::isExpanded( const QModelIndex &index ) const
281{
282    if( !mViewPtr )
283        return false;
284    else
285        return mViewPtr->isExpanded( index );
286}
287
288Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
289{
290    if(!hasParent(aIndex)) // time-group item (root item)
291        return ControlNone;
292
293    QListIterator<ControlId> i(mControls.keys());
294    while (i.hasNext())
295    {
296        ControlId id = i.next();
297        Control *control = mControls[id];
298        if (control->enabled()
299            and control->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
300        {
301            return id;
302        }
303    }
304
305    return ControlNone;
306}
307
308Delegate::Control::Control(ControlId aControlId, const QString &aImageName, const Control* prev_control)
309    : mId(aControlId)
310    , mImage(new QImage(aImageName))
311    , mDrawPoint(QPoint(0,0))
312    , mEnabled(false)
313{
314    QPoint p;
315    if (prev_control == NULL) {
316        p = QPoint(0, SPACER);
317    } else {
318        p = prev_control->drawPoint();
319    }
320    p.setX(p.x()-image()->width()-SPACER);
321    setDrawPoint(p);
322}
323
324void Delegate::Control::paint(QPainter* painter, const QRect rect)
325{
326    painter->drawImage(drawPoint(rect),*image());
327    setEnabled(true);
328}
329
330void Delegate::defineControls()
331{
332    // FAVOURITE ICONs
333    // strong
334    mControls.insert(FavouriteControlStrong, new Control(FavouriteControlStrong, QString(":icons/favourite-strong.png"), NULL));
335    // weak
336    mControls.insert(FavouriteControlWeak, new Control(FavouriteControlWeak, QString(":icons/favourite-weak.png"), NULL));
337    // no
338    mControls.insert(FavouriteControlNo, new Control(FavouriteControlNo, QString(":icons/favourite-no.png"), NULL));
339
340    // ALARM ICONs
341    // on
342    mControls.insert(AlarmControlOn,
343                    new Control(AlarmControlOn, QString(":icons/alarm-on.png"), mControls[FavouriteControlStrong]));
344    // off
345    mControls.insert(AlarmControlOff,
346                    new Control(AlarmControlOff, QString(":icons/alarm-off.png"), mControls[FavouriteControlNo]));
347    // WARNING ICON
348    mControls.insert(WarningControl,
349                    new Control(WarningControl, QString(":icons/dialog-warning.png"), mControls[AlarmControlOff]));
350        }
351
352bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
353{
354    if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
355        return true;
356
357    return false;
358}
359
360int Delegate::numberOfFavourites(const QModelIndex &index) const
361{
362    if(index.parent().isValid()) // it's event, not time-group
363        return 0;
364
365    int nrofFavs = 0;
366    for(int i=0; i<index.model()->rowCount(index); i++)
367        if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->favourite() != Favourite_no)
368            nrofFavs++;
369
370    return nrofFavs;
371}
372
373int Delegate::numberOfAlarms(const QModelIndex &index) const
374{
375    if(index.parent().isValid()) // it's event, not time-group
376        return 0;
377
378    int nrofAlarms = 0;
379    for(int i=0; i<index.model()->rowCount(index); i++)
380        if(static_cast<Event*>(index.model()->index(i, 0, index).internalPointer())->hasAlarm())
381            nrofAlarms++;
382
383    return nrofAlarms;
384}
385
Note: See TracBrowser for help on using the repository browser.