source: src/mvc/delegate.cpp @ b6cd05c

qt5
Last change on this file since b6cd05c was b6cd05c, checked in by pavelpa <pavelpa@…>, 13 years ago

added 'alarm' columnt to the 'EVENT' table to signalize
that the event has/hasn't alarm set

  • Property mode set to 100644
File size: 8.9 KB
Line 
1#include "delegate.h"
2#include "eventmodel.h"
3
4#include <QDebug>
5#include <QPainter>
6
7const int RADIUS = 10;
8const int SPACER = RADIUS;
9
10Delegate::Delegate(QTreeView *aParent)
11    : QItemDelegate(aParent)
12    , mViewPtr(aParent)
13{
14    mControls.clear();
15    defineControls();
16}
17
18Delegate::~Delegate()
19{
20    QListIterator<ControlId> i(mControls.keys());
21    while (i.hasNext())
22    {
23        delete mControls[i.next()]->image();
24    }
25}
26
27void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
28{
29    if(!mViewPtr)
30        return;
31
32    painter->save();
33
34    QColor bkgrColor = Qt::cyan;
35    QPen borderPen(bkgrColor.darker());
36    if(hasParent(index))
37    {
38        if(isLast(index))
39        {
40            QLinearGradient lastGradient(option.rect.topLeft(), option.rect.bottomLeft());
41            lastGradient.setColorAt(0.0, Qt::white);
42            lastGradient.setColorAt(0.5, bkgrColor);
43            lastGradient.setColorAt(1.0, Qt::white);
44
45            QPainterPath endPath;
46            endPath.moveTo(option.rect.topLeft());
47            endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
48            endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90);
49            endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0));
50            endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
51            endPath.lineTo(option.rect.topRight());
52
53            //painter->setBrush( bkgrColor );
54            painter->setBrush(lastGradient);
55            painter->setPen(borderPen);
56            painter->drawPath(endPath);
57
58            painter->setFont(option.font);
59        }
60        else // middle elements
61        {
62
63            QLinearGradient middleGradient(option.rect.topLeft(), option.rect.bottomLeft());
64            middleGradient.setColorAt(0.0, Qt::white);
65            middleGradient.setColorAt(0.25, bkgrColor);
66            middleGradient.setColorAt(0.5, Qt::white);
67            middleGradient.setColorAt(0.75, bkgrColor);
68            middleGradient.setColorAt(1.0, Qt::white);
69
70            //painter->setBrush( bkgrColor );
71            painter->setBrush(middleGradient);
72            painter->setPen(Qt::NoPen);
73            painter->drawRect(option.rect);
74
75            painter->setPen(borderPen);
76            // vertical lines
77            painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
78            painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
79            // horizontal lines
80            painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
81
82            painter->setFont(option.font);
83        }
84
85        // draw Controls
86        // favourite
87        if(static_cast<Event*>(index.internalPointer())->isFavourite())
88            painter->drawImage(mControls[FavouriteControlOn]->drawPoint(option.rect),*mControls[FavouriteControlOn]->image());
89        else
90            painter->drawImage(mControls[FavouriteControlOff]->drawPoint(option.rect),*mControls[FavouriteControlOff]->image());
91        // alarm
92        if(static_cast<Event*>(index.internalPointer())->hasAlarm())
93            painter->drawImage(mControls[AlarmControlOn]->drawPoint(option.rect),*mControls[AlarmControlOn]->image());
94        else
95            painter->drawImage(mControls[AlarmControlOff]->drawPoint(option.rect),*mControls[AlarmControlOff]->image());
96        // map
97        painter->drawImage(mControls[MapControl]->drawPoint(option.rect),*mControls[MapControl]->image());
98    }
99    else // doesn't have parent - time-groups' elements (top items)
100    {
101        QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
102        //titleGradient.setColorAt(0.0, Qt::white);
103        titleGradient.setColorAt(0.0, bkgrColor);
104        titleGradient.setColorAt(0.5, Qt::white);
105        titleGradient.setColorAt(1.0, bkgrColor);
106
107        QPainterPath titlePath;
108        if(isExpanded(index))
109        {
110            titlePath.moveTo(option.rect.bottomLeft());
111            titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
112            titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
113            titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
114            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
115            titlePath.lineTo(option.rect.bottomRight());
116            titlePath.closeSubpath();
117        }
118        else
119        {
120            titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
121            titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
122            titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
123            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
124            titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
125            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
126            titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
127            titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);     
128            titlePath.closeSubpath();
129        }
130
131        painter->setBrush(titleGradient);
132        painter->setPen(borderPen);
133        painter->drawPath(titlePath);
134
135        QFont font = option.font;
136        font.setBold(true);
137        painter->setFont(font);
138    }
139
140    //// HIGHLIGHTING SELECTED ITEM
141    //if (option.state & QStyle::State_Selected)
142        //painter->fillRect(option.rect, option.palette.highlight());
143
144    // draw title
145    QPointF titlePointF(option.rect.x()+SPACER,option.rect.y()+option.rect.height()-10);
146    QString text;
147    if(index.parent().isValid()) // event
148        text = qVariantValue<QString>(index.data()) + ": " + static_cast<Event*>(index.internalPointer())->title();
149    else // group
150        text = qVariantValue<QString>(index.data());
151    painter->drawText(titlePointF,text);
152
153    painter->restore();
154}
155
156QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
157{
158    Q_UNUSED(option)
159
160    if (index.internalId() == 0) // time group
161    {
162        return QSize(30,30);
163    }
164    else // event
165    {
166        return QSize(100,100);
167    }
168}
169
170bool Delegate::hasParent( const QModelIndex &index ) const
171{
172    if( index.parent().isValid() )
173        return true;
174    else
175        return false;
176}
177 
178bool Delegate::isLast( const QModelIndex &index ) const
179{
180    if(!hasParent(index))
181        return false; // what should be returned here?
182
183    if(index.row() >= (index.model()->rowCount(index.parent())-1))
184        return true;
185    else
186        return false;
187}
188
189bool Delegate::isExpanded( const QModelIndex &index ) const
190{
191    if( !mViewPtr )
192        return false;
193    else
194        return mViewPtr->isExpanded( index );
195}
196
197Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
198{
199    if(!hasParent(aIndex)) // time-group item (root item)
200        return ControlNone;
201
202    QListIterator<ControlId> i(mControls.keys());
203    while (i.hasNext())
204    {
205        ControlId id = i.next();
206        if(mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
207            return id;
208    }
209
210    return ControlNone;
211}
212
213void Delegate::defineControls()
214{
215    Control *control;
216    QPoint p(0,0);
217    // FAVOURITE ICONs
218    // on
219    control = new Control(FavouriteControlOn,QString(":icons/favourite-on.png"));
220    p = QPoint(0,0);
221    p.setX(p.x()-control->image()->width()-SPACER);
222    control->setDrawPoint(p);
223    mControls.insert(FavouriteControlOn,control);
224    // off
225    control = new Control(FavouriteControlOff,QString(":icons/favourite-off.png"));
226    p = QPoint(0,0);
227    p.setX(p.x()-control->image()->width()-SPACER);
228    control->setDrawPoint(p);
229    mControls.insert(FavouriteControlOff,control);
230
231    // ALARM ICONs
232    // on
233    control = new Control(AlarmControlOn,QString(":icons/alarm-on.png"));
234    p = mControls[FavouriteControlOn]->drawPoint();
235    p.setX(p.x()-control->image()->width()-SPACER);
236    control->setDrawPoint(p);
237    mControls.insert(AlarmControlOn,control);
238    // off
239    control = new Control(AlarmControlOff,QString(":icons/alarm-off.png"));
240    p = mControls[FavouriteControlOff]->drawPoint();
241    p.setX(p.x()-control->image()->width()-SPACER);
242    control->setDrawPoint(p);
243    mControls.insert(AlarmControlOff,control);
244
245    // MAP ICON
246    control = new Control(MapControl,QString(":icons/compass.png"));
247    p = mControls[AlarmControlOn]->drawPoint();
248    p.setX(p.x()-control->image()->width()-SPACER);
249    control->setDrawPoint(p);
250    mControls.insert(MapControl,control);
251}
252
253bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
254{
255    if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
256        return true;
257
258    return false;
259}
260
Note: See TracBrowser for help on using the repository browser.