<rect>
<x>0</x>
<y>0</y>
- <width>800</width>
- <height>600</height>
+ <width>856</width>
+ <height>558</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget" >
- <widget class="QTabWidget" name="tabWidget" >
- <property name="geometry" >
- <rect>
- <x>0</x>
- <y>0</y>
- <width>791</width>
- <height>551</height>
- </rect>
- </property>
- <property name="currentIndex" >
- <number>0</number>
- </property>
- <widget class="QWidget" name="tab" >
- <attribute name="title" >
- <string>Tab 1</string>
- </attribute>
- <widget class="QTreeView" name="treeView" >
- <property name="geometry" >
- <rect>
- <x>0</x>
- <y>0</y>
- <width>781</width>
- <height>521</height>
- </rect>
+ <layout class="QGridLayout" name="gridLayout" >
+ <item row="0" column="0" >
+ <widget class="QTabWidget" name="tabWidget" >
+ <property name="currentIndex" >
+ <number>0</number>
</property>
+ <widget class="QWidget" name="tab" >
+ <attribute name="title" >
+ <string>Tab 1</string>
+ </attribute>
+ <layout class="QGridLayout" name="gridLayout_2" >
+ <item row="0" column="0" >
+ <layout class="QVBoxLayout" name="verticalLayout" >
+ <item>
+ <widget class="TreeView" name="treeView" >
+ <property name="maximumSize" >
+ <size>
+ <width>16777215</width>
+ <height>16777215</height>
+ </size>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </item>
+ </layout>
+ </widget>
+ <widget class="QWidget" name="tab_2" >
+ <attribute name="title" >
+ <string>Tab 2</string>
+ </attribute>
+ </widget>
</widget>
- </widget>
- <widget class="QWidget" name="tab_2" >
- <attribute name="title" >
- <string>Tab 2</string>
- </attribute>
- </widget>
- </widget>
+ </item>
+ </layout>
</widget>
<widget class="QMenuBar" name="menubar" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
- <width>800</width>
- <height>25</height>
+ <width>856</width>
+ <height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar" />
</widget>
+ <customwidgets>
+ <customwidget>
+ <class>TreeView</class>
+ <extends>QTreeView</extends>
+ <header>../model/treeview.h</header>
+ </customwidget>
+ </customwidgets>
<resources/>
<connections/>
</ui>
--- /dev/null
+#include "delegate.h"
+#include "eventmodel.h"
+
+#include <QDebug>
+#include <QPainter>
+
+const int RADIUS = 10;
+const int SPACER = RADIUS;
+
+Delegate::Delegate(QTreeView *aParent)
+ : QItemDelegate(aParent)
+ , mViewPtr(aParent)
+{
+ mControls.clear();
+ defineControls();
+}
+
+Delegate::~Delegate()
+{
+ QListIterator<ControlId> i(mControls.keys());
+ while (i.hasNext())
+ {
+ delete mControls[i.next()]->image();
+ }
+}
+
+void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+ if(!mViewPtr)
+ return;
+
+ painter->save();
+
+ QColor bkgrColor = Qt::cyan;
+ QPen borderPen(bkgrColor.darker());
+ if(hasParent(index))
+ {
+ if(isLast(index))
+ {
+ QLinearGradient lastGradient(option.rect.topLeft(), option.rect.bottomLeft());
+ lastGradient.setColorAt(0.0, Qt::white);
+ lastGradient.setColorAt(0.5, bkgrColor);
+ lastGradient.setColorAt(1.0, Qt::white);
+
+ QPainterPath endPath;
+ endPath.moveTo(option.rect.topLeft());
+ endPath.lineTo(option.rect.bottomLeft()-QPoint(0, RADIUS));
+ endPath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 180, 90);
+ endPath.lineTo(option.rect.bottomRight()-QPoint(RADIUS, 0));
+ endPath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, 90);
+ endPath.lineTo(option.rect.topRight());
+
+ //painter->setBrush( bkgrColor );
+ painter->setBrush(lastGradient);
+ painter->setPen(borderPen);
+ painter->drawPath(endPath);
+
+ painter->setFont(option.font);
+ }
+ else // middle elements
+ {
+
+ QLinearGradient middleGradient(option.rect.topLeft(), option.rect.bottomLeft());
+ middleGradient.setColorAt(0.0, Qt::white);
+ middleGradient.setColorAt(0.25, bkgrColor);
+ middleGradient.setColorAt(0.5, Qt::white);
+ middleGradient.setColorAt(0.75, bkgrColor);
+ middleGradient.setColorAt(1.0, Qt::white);
+
+ //painter->setBrush( bkgrColor );
+ painter->setBrush(middleGradient);
+ painter->setPen(Qt::NoPen);
+ painter->drawRect(option.rect);
+
+ painter->setPen(borderPen);
+ // vertical lines
+ painter->drawLine(option.rect.topLeft(), option.rect.bottomLeft());
+ painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
+ // horizontal lines
+ painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
+
+ painter->setFont(option.font);
+ }
+
+ // draw Controls
+ painter->drawImage(mControls[FavouriteControl]->drawPoint(option.rect),*mControls[FavouriteControl]->image());
+ painter->drawImage(mControls[AlarmControl]->drawPoint(option.rect),*mControls[AlarmControl]->image());
+ }
+ else // doesn't have parent - time-groups' elements (top items)
+ {
+ QLinearGradient titleGradient(option.rect.topLeft(), option.rect.topRight());
+ //titleGradient.setColorAt(0.0, Qt::white);
+ titleGradient.setColorAt(0.0, bkgrColor);
+ titleGradient.setColorAt(0.5, Qt::white);
+ titleGradient.setColorAt(1.0, bkgrColor);
+
+ QPainterPath titlePath;
+ if(isExpanded(index))
+ {
+ titlePath.moveTo(option.rect.bottomLeft());
+ titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
+ titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
+ titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
+ titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
+ titlePath.lineTo(option.rect.bottomRight());
+ titlePath.closeSubpath();
+ }
+ else
+ {
+ titlePath.lineTo(option.rect.topLeft()+QPoint(0, RADIUS));
+ titlePath.arcTo(option.rect.left(), option.rect.top(), 2*RADIUS, 2*RADIUS, 180, -90);
+ titlePath.lineTo(option.rect.topRight()-QPoint(RADIUS, 0));
+ titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.top(), 2*RADIUS, 2*RADIUS, 90, -90);
+ titlePath.lineTo(option.rect.bottomRight()-QPoint(0, RADIUS));
+ titlePath.arcTo(option.rect.right()-2*RADIUS, option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 0, -90);
+ titlePath.lineTo(option.rect.bottomLeft()+QPoint(RADIUS, 0));
+ titlePath.arcTo(option.rect.left(), option.rect.bottom()-2*RADIUS, 2*RADIUS, 2*RADIUS, 270, -90);
+ titlePath.closeSubpath();
+ }
+
+ painter->setBrush(titleGradient);
+ painter->setPen(borderPen);
+ painter->drawPath(titlePath);
+
+ QFont font = option.font;
+ font.setBold(true);
+ painter->setFont(font);
+ }
+
+ //// HIGHLIGHTING SELECTED ITEM
+ //if (option.state & QStyle::State_Selected)
+ //painter->fillRect(option.rect, option.palette.highlight());
+
+ // draw title
+ QPointF titlePointF(option.rect.x(),option.rect.y()+option.rect.height()-10);
+ QString text = qVariantValue<QString>(index.data());
+ painter->drawText(titlePointF,text);
+
+ painter->restore();
+}
+
+QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+ Q_UNUSED(option)
+
+ if (index.internalId() == 0) // time group
+ {
+ return QSize(30,30);
+ }
+ else // event
+ {
+ return QSize(100,100);
+ }
+}
+
+bool Delegate::hasParent( const QModelIndex &index ) const
+{
+ if( index.parent().isValid() )
+ return true;
+ else
+ return false;
+}
+
+bool Delegate::isLast( const QModelIndex &index ) const
+{
+ if(!hasParent(index))
+ return false; // what should be returned here?
+
+ if(index.row() >= (index.model()->rowCount(index.parent())-1))
+ return true;
+ else
+ return false;
+}
+
+bool Delegate::isExpanded( const QModelIndex &index ) const
+{
+ if( !mViewPtr )
+ return false;
+ else
+ return mViewPtr->isExpanded( index );
+}
+
+Delegate::ControlId Delegate::whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const
+{
+ if(!hasParent(aIndex)) // time-group item (root item)
+ return ControlNone;
+
+ QListIterator<ControlId> i(mControls.keys());
+ while (i.hasNext())
+ {
+ ControlId id = i.next();
+ if(mControls[id]->drawRect(static_cast<QTreeView*>(parent())->visualRect(aIndex)).contains(aPoint))
+ return id;
+ }
+
+ return ControlNone;
+}
+
+void Delegate::defineControls()
+{
+ Control *control;
+ // FAVOURITE ICON
+ control = new Control(FavouriteControl,QString(":icons/favourite-on.png"));
+ control->setDrawPoint(QPoint(-control->image()->width()-SPACER,SPACER));
+ mControls.insert(FavouriteControl,control);
+
+ // ALARM ICON
+ control = new Control(AlarmControl,QString(":icons/alarm-on.png"));
+ control->setDrawPoint(QPoint(-mControls[FavouriteControl]->image()->width()-control->image()->width()-2*SPACER,SPACER));
+ mControls.insert(AlarmControl,control);
+}
+
+bool Delegate::isPointFromRect(const QPoint &aPoint, const QRect &aRect) const
+{
+ if( (aPoint.x()>=aRect.left() && aPoint.x()<=aRect.right()) && (aPoint.y()>=aRect.top() && aPoint.y()<=aRect.bottom()) )
+ return true;
+
+ return false;
+}
+
--- /dev/null
+#ifndef DELEGATE_H
+#define DELEGATE_H
+
+#include <QItemDelegate>
+#include <QTreeView>
+#include <QPointer>
+
+class Delegate : public QItemDelegate
+{
+ Q_OBJECT
+
+ public:
+
+ enum ControlId
+ {
+ ControlNone = 0,
+ FavouriteControl,
+ AlarmControl
+ };
+
+ class Control
+ {
+ public:
+ Control(ControlId aControlId, const QString &aImageName)
+ : mId(aControlId)
+ , mImage(new QImage(aImageName))
+ , mDrawPoint(QPoint(0,0))
+ { }
+ inline QImage *image() const { return mImage; }
+ inline void setDrawPoint(const QPoint &aPoint) { mDrawPoint = aPoint; }
+ inline QPoint drawPoint(const QRect &aRect) const // for painter to draw Control
+ {
+ return QPoint(aRect.x()+aRect.width(),aRect.y()) + mDrawPoint;
+ }
+ inline QRect drawRect(const QRect &aRect) const // helper for determining if Control was clicked
+ {
+ return QRect(drawPoint(aRect), drawPoint(aRect)+QPoint(mImage->size().width(),mImage->size().height()));
+ }
+ private:
+ ControlId mId;
+ QImage *mImage;
+ QPoint mDrawPoint; // relative 'start-drawing' position (may hold negative values)
+ };
+
+ Delegate(QTreeView *aParent); // the delegate 'owner' has to be specified in the constructor - it's used to obtain visualRect of selected item/index
+ ~Delegate();
+
+ void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
+ QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
+ //
+ Delegate::ControlId whichControlClicked(const QModelIndex &aIndex, const QPoint &aPoint) const;
+ bool isPointFromRect(const QPoint &aPoint, const QRect &aRect) const;
+
+ private:
+ bool hasParent( const QModelIndex &index ) const;
+ bool isLast( const QModelIndex &index ) const;
+ bool isExpanded( const QModelIndex &index ) const;
+ void defineControls();
+
+ private:
+ QPointer<QTreeView> mViewPtr;
+ QMap<ControlId,Control*> mControls;
+};
+
+#endif /* DELEGATE_H */
+