source: src/mvc/delegate.cpp

Last change on this file was ff579df, checked in by Philipp Spitzer <philipp@…>, 5 months ago

Remaining fix of bug Debian BTS #992236 (line distance).

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=992236

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