source: src/mvc/delegate.cpp @ 268d58f

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

First try to improve the colors (ticket #13).

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