1 /*
2 * Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include "CategorizedItemDelegate.h"
20
21 #include <QPainter>
22
23 #include <KCategoryDrawer>
24 #include <KCategorizedSortFilterProxyModel>
25
26 struct CategorizedItemDelegate::Private {
27 QAbstractItemDelegate* fallback;
28 KCategoryDrawer* categoryDrawer;
29 bool isFirstOfCategory(const QModelIndex& index);
30 };
31
isFirstOfCategory(const QModelIndex & index)32 bool CategorizedItemDelegate::Private::isFirstOfCategory(const QModelIndex& index)
33 {
34 if(index.row() == 0) return true;
35 QModelIndex idx = index.model()->index(index.row() - 1, index.column(), index.parent());
36 const QString category1 = index.model()->data(index, KCategorizedSortFilterProxyModel::CategorySortRole).toString();
37 const QString category2 = index.model()->data(idx, KCategorizedSortFilterProxyModel::CategorySortRole).toString();
38 return category1 != category2;
39 }
40
CategorizedItemDelegate(QAbstractItemDelegate * _fallback,QObject * parent)41 CategorizedItemDelegate::CategorizedItemDelegate(QAbstractItemDelegate* _fallback, QObject* parent) : QAbstractItemDelegate(parent), d(new Private)
42 {
43 _fallback->setParent(this);
44 d->fallback = _fallback;
45 // QT5TODO: Pass correct param to KCategoryDrawer
46 d->categoryDrawer = new KCategoryDrawer(0);
47 }
~CategorizedItemDelegate()48 CategorizedItemDelegate::~CategorizedItemDelegate()
49 {
50 delete d;
51 }
52
createEditor(QWidget * parent,const QStyleOptionViewItem & option,const QModelIndex & index) const53 QWidget * CategorizedItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
54 {
55 return d->fallback->createEditor(parent, option, index);
56 }
57
editorEvent(QEvent * event,QAbstractItemModel * model,const QStyleOptionViewItem & option,const QModelIndex & index)58 bool CategorizedItemDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index)
59 {
60 return d->fallback->editorEvent(event, model, option, index);
61 }
62
paint(QPainter * painter,const QStyleOptionViewItem & _option,const QModelIndex & index) const63 void CategorizedItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & _option, const QModelIndex & index) const
64 {
65 // We will need to edit the option to make sure the header isn't drawned as selected
66 QStyleOptionViewItem* option = 0;
67 if(const QStyleOptionViewItem *v4 = qstyleoption_cast<const QStyleOptionViewItem*>(&_option)) {
68 option = new QStyleOptionViewItem(*v4);
69 } else if(const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3*>(&_option)) {
70 option = new QStyleOptionViewItemV3(*v3);
71 } else if(const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2*>(&_option)) {
72 option = new QStyleOptionViewItemV2(*v2);
73 } else {
74 option = new QStyleOptionViewItem(_option);
75 }
76 Q_ASSERT(option);
77 // If it's a first category then we need to draw it
78 if(d->isFirstOfCategory(index)) {
79 // Prepare the rectangle for drawing the category
80 int h = d->categoryDrawer->categoryHeight(index, *option);
81 QRect rect = option->rect;
82
83 // Make sure the category isn't drown as selected
84 option->state &= (~QStyle::State_Selected);
85 Q_ASSERT(!(option->state & QStyle::State_Selected));
86 option->state &= (~QStyle::State_HasFocus);
87 Q_ASSERT(!(option->state & QStyle::State_HasFocus));
88 option->state &= (~QStyle::State_MouseOver);
89 Q_ASSERT(!(option->state & QStyle::State_MouseOver));
90 option->rect.setHeight(h);
91
92 // draw the category
93 d->categoryDrawer->drawCategory(index, 0, *option, painter);
94
95 // Prepare the rectangle for the item
96 option->rect = rect;
97 option->rect.setY(rect.y() + h);
98 option->rect.setHeight(rect.height() - h);
99 option->state = _option.state;
100 }
101 d->fallback->paint(painter, *option, index);
102 delete option;
103 }
104
setEditorData(QWidget * editor,const QModelIndex & index) const105 void CategorizedItemDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
106 {
107 d->fallback->setEditorData(editor, index);
108 }
109
setModelData(QWidget * editor,QAbstractItemModel * model,const QModelIndex & index) const110 void CategorizedItemDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
111 {
112 d->fallback->setModelData(editor, model, index);
113 }
114
sizeHint(const QStyleOptionViewItem & option,const QModelIndex & index) const115 QSize CategorizedItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
116 {
117 QSize size = d->fallback->sizeHint(option, index);
118 // If is first of a category, then add the space needed to paint the category
119 if(d->isFirstOfCategory(index)) {
120 size.setHeight(d->categoryDrawer->categoryHeight(index, option) + size.height());
121 }
122 return size;
123 }
124
updateEditorGeometry(QWidget * editor,const QStyleOptionViewItem & option,const QModelIndex & index) const125 void CategorizedItemDelegate::updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const
126 {
127 d->fallback->updateEditorGeometry(editor, option, index);
128
129 // If it's the first category, then the editor need to be moved
130 if(d->isFirstOfCategory(index)) {
131 int h = d->categoryDrawer->categoryHeight(index, option);
132 editor->move(editor->x(), editor->y() + h);
133 editor->resize(editor->width(), editor->height() - h);
134 }
135 }
136