1
/* This file is part of Zanshin Todo.
3
Copyright 2008 Kevin Ottens <ervin@kde.org>
5
This program is free software; you can redistribute it and/or
6
modify it under the terms of the GNU General Public License as
7
published by the Free Software Foundation; either version 2 of
8
the License or (at your option) version 3 or any later version
9
accepted by the membership of KDE e.V. (or its successor approved
10
by the membership of KDE e.V.), which shall act as a proxy
11
defined in Section 14 of version 3 of the license.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
#include "actionlistdelegate.h"
26
#include <akonadi/item.h>
27
#include <boost/shared_ptr.hpp>
28
#include <kcal/todo.h>
30
#include "actionlistmodel.h"
32
typedef boost::shared_ptr<KCal::Incidence> IncidencePtr;
34
ActionListDelegate::ActionListDelegate(QObject *parent)
35
: QStyledItemDelegate(parent), m_dragModeCount(0)
40
ActionListDelegate::~ActionListDelegate()
45
QSize ActionListDelegate::sizeHint(const QStyleOptionViewItem &option,
46
const QModelIndex &index) const
48
if (m_dragModeCount>0) {
49
if (index.column()==0) {
50
return QStyledItemDelegate::sizeHint(option, index);
56
QSize res = QStyledItemDelegate::sizeHint(option, index);
58
TodoFlatModel::ItemType type = rowType(index);
60
if (type==TodoFlatModel::FolderTodo || !isInFocus(index)) {
63
} else if (type!=TodoFlatModel::StandardTodo) {
70
void ActionListDelegate::paint(QPainter *painter,
71
const QStyleOptionViewItem &option,
72
const QModelIndex &index) const
74
if (m_dragModeCount>0) {
75
QStyleOptionViewItemV4 opt = option;
76
opt.rect.setHeight(sizeHint(option, index).height());
78
if (index.column()==0) {
79
return QStyledItemDelegate::paint(painter, opt, index);
85
TodoFlatModel::ItemType type = rowType(index);
87
QStyleOptionViewItemV4 opt = option;
89
if (type==TodoFlatModel::FolderTodo || !isInFocus(index)) {
90
opt.decorationSize = QSize(1, 1);
91
opt.displayAlignment = Qt::AlignHCenter|Qt::AlignBottom;
92
opt.font.setItalic(true);
93
opt.font.setPointSizeF(opt.font.pointSizeF()*0.75);
95
} else if (type!=TodoFlatModel::StandardTodo) {
96
opt.decorationSize = QSize(22, 22);
97
opt.font.setWeight(QFont::Bold);
99
} else if (index.parent().isValid()) {
100
if (index.column()==0) {
101
opt.rect.adjust(40, 0, 0, 0);
103
if (index.row()%2==0) {
104
opt.features|= QStyleOptionViewItemV4::Alternate;
108
if (isCompleted(index)) {
109
opt.font.setStrikeOut(true);
110
} else if (isOverdue(index)) {
111
opt.palette.setColor(QPalette::Text, QColor(Qt::red));
112
opt.palette.setColor(QPalette::HighlightedText, QColor(Qt::red));
115
QStyledItemDelegate::paint(painter, opt, index);
118
void ActionListDelegate::updateEditorGeometry(QWidget *editor,
119
const QStyleOptionViewItem &option,
120
const QModelIndex &index) const
122
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
124
TodoFlatModel::ItemType type = rowType(index);
126
if (type==TodoFlatModel::StandardTodo
128
&& index.parent().isValid()) {
129
QRect r = editor->geometry();
130
r.adjust(40, 0, 0, 0);
131
editor->setGeometry(r);
135
bool ActionListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
136
const QStyleOptionViewItem &option,
137
const QModelIndex &index)
139
QStyleOptionViewItemV4 opt = option;
141
TodoFlatModel::ItemType type = rowType(index);
143
if (type==TodoFlatModel::StandardTodo
145
&& index.parent().isValid()) {
146
opt.rect.adjust(40, 0, 0, 0);
149
return QStyledItemDelegate::editorEvent(event, model, opt, index);
152
TodoFlatModel::ItemType ActionListDelegate::rowType(const QModelIndex &index)
154
const ActionListModel *model = qobject_cast<const ActionListModel*>(index.model());
157
return TodoFlatModel::StandardTodo;
160
QModelIndex sourceIndex = model->mapToSource(index);
161
QModelIndex rowTypeIndex = sourceIndex.sibling(sourceIndex.row(), TodoFlatModel::RowType);
163
QVariant value = model->sourceModel()->data(rowTypeIndex);
164
if (!value.isValid()) {
165
return TodoFlatModel::StandardTodo;
168
return (TodoFlatModel::ItemType)value.toInt();
171
bool ActionListDelegate::isInFocus(const QModelIndex &index) const
173
const ActionListModel *model = qobject_cast<const ActionListModel*>(index.model());
179
return model->isInFocus(index);
182
bool ActionListDelegate::isCompleted(const QModelIndex &index) const
184
return index.model()->data(index.sibling(index.row(), 0), Qt::CheckStateRole).toInt()==Qt::Checked;
187
bool ActionListDelegate::isOverdue(const QModelIndex &index) const
189
const ActionListModel *model = qobject_cast<const ActionListModel*>(index.model());
195
TodoFlatModel::ItemType type = rowType(index);
197
if (type==TodoFlatModel::Category) {
201
Akonadi::Item item = model->itemForIndex(index);
202
const IncidencePtr incidence = item.payload<IncidencePtr>();
203
KCal::Todo *todo = dynamic_cast<KCal::Todo*>(incidence.get());
205
return todo->isOverdue();
208
void ActionListDelegate::setDragModeCount(int count)
210
m_dragModeCount = count;