source: src/mvc/delegate.cpp @ 68b2df2

qt5
Last change on this file since 68b2df2 was 68b2df2, checked in by gregor herrmann <gregoa@…>, 10 years ago

Add copyright to source.

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