source: src/mvc/delegate.cpp @ 92995da

qt5
Last change on this file since 92995da was 92995da, checked in by pavelpa <pavelpa@…>, 12 years ago

just some directory renaming

  • renamed 'model' to 'mvc' (Model-View-Controller), since it contains also 'delegate' and 'view'
  • Property mode set to 100644
File size: 7.3 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        painter->drawImage(mControls[FavouriteControl]->drawPoint(option.rect),*mControls[FavouriteControl]->image());
87        painter->drawImage(mControls[AlarmControl]->drawPoint(option.rect),*mControls[AlarmControl]->image());
88    }
89    else // doesn't have parent - time-groups' elements (top items)
90    {
91        QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
92        //titleGradient.setColorAt(0.0, Qt::white);
93        titleGradient.setColorAt(0.0, bkgrColor);
94        titleGradient.setColorAt(0.5, Qt::white);
95        titleGradient.setColorAt(1.0, bkgrColor);
96
97        QPainterPath titlePath;
98        if(isExpanded(index))
99        {
100            titlePath.moveTo(option.rect.bottomLeft());
101            titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
102            titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
103            titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
104            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
105            titlePath.lineTo(option.rect.bottomRight());
106            titlePath.closeSubpath();
107        }
108        else
109        {
110            titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
111            titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
112            titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
113            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
114            titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
115            titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
116            titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
117            titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);     
118            titlePath.closeSubpath();
119        }
120
121        painter->setBrush(titleGradient);
122        painter->setPen(borderPen);
123        painter->drawPath(titlePath);
124
125        QFont font = option.font;
126        font.setBold(true);
127        painter->setFont(font);
128    }
129
130    //// HIGHLIGHTING SELECTED ITEM
131    //if (option.state & QStyle::State_Selected)
132        //painter->fillRect(option.rect, option.palette.highlight());
133
134    // draw title
135    QPointF titlePointF(option.rect.x(),option.rect.y()+option.rect.height()-10);
136    QString text = qVariantValue<QString>(index.data());
137    painter->drawText(titlePointF,text);
138
139    painter->restore();
140}
141
142QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
143{
144    Q_UNUSED(option)
145
146    if (index.internalId() == 0) // time group
147    {
148        return QSize(30,30);
149    }
150    else // event
151    {
152        return QSize(100,100);
153    }
154}
155
156bool Delegate::hasParent( const QModelIndex &index ) const
157{
158    if( index.parent().isValid() )
159        return true;
160    else
161        return false;
162}
163 
164bool Delegate::isLast( const QModelIndex &index ) const
165{
166    if(!hasParent(index))
167        return false; // what should be returned here?
168
169    if(index.row() >= (index.model()->rowCount(index.parent())-1))
170        return true;
171    else
172        return false;
173}
174
175bool Delegate::isExpanded( const QModelIndex &index ) const
176{
177    if( !mViewPtr )
178        return false;
179    else
180        return mViewPtr->isExpanded( index );
181}
182
183Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
184{
185    if(!hasParent(aIndex)) // time-group item (root item)
186        return ControlNone;
187
188    QListIterator<ControlId> i(mControls.keys());
189    while (i.hasNext())
190    {
191        ControlId id = i.next();
192        if(mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
193            return id;
194    }
195
196    return ControlNone;
197}
198
199void Delegate::defineControls()
200{
201    Control *control;
202    // FAVOURITE ICON
203    control = new Control(FavouriteControl,QString(":icons/favourite-on.png"));
204    control->setDrawPoint(QPoint(-control->image()->width()-SPACER,SPACER));
205    mControls.insert(FavouriteControl,control);
206
207    // ALARM ICON
208    control = new Control(AlarmControl,QString(":icons/alarm-on.png"));
209    control->setDrawPoint(QPoint(-mControls[FavouriteControl]->image()->width()-control->image()->width()-2*SPACER,SPACER));
210    mControls.insert(AlarmControl,control);
211}
212
213bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
214{
215    if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
216        return true;
217
218    return false;
219}
220
Note: See TracBrowser for help on using the repository browser.