~ubuntu-branches/ubuntu/wily/zanshin/wily-proposed

« back to all changes in this revision

Viewing changes to src/actionlistdelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2009-08-13 23:19:32 UTC
  • Revision ID: james.westby@ubuntu.com-20090813231932-lewqphiry1qs7w80
Tags: upstream-0.1+svn1006410
ImportĀ upstreamĀ versionĀ 0.1+svn1006410

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Zanshin Todo.
 
2
 
 
3
   Copyright 2008 Kevin Ottens <ervin@kde.org>
 
4
 
 
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.
 
12
 
 
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.
 
17
 
 
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,
 
21
   USA.
 
22
*/
 
23
 
 
24
#include "actionlistdelegate.h"
 
25
 
 
26
#include <akonadi/item.h>
 
27
#include <boost/shared_ptr.hpp>
 
28
#include <kcal/todo.h>
 
29
 
 
30
#include "actionlistmodel.h"
 
31
 
 
32
typedef boost::shared_ptr<KCal::Incidence> IncidencePtr;
 
33
 
 
34
ActionListDelegate::ActionListDelegate(QObject *parent)
 
35
    : QStyledItemDelegate(parent), m_dragModeCount(0)
 
36
{
 
37
 
 
38
}
 
39
 
 
40
ActionListDelegate::~ActionListDelegate()
 
41
{
 
42
 
 
43
}
 
44
 
 
45
QSize ActionListDelegate::sizeHint(const QStyleOptionViewItem &option,
 
46
                                   const QModelIndex &index) const
 
47
{
 
48
    if (m_dragModeCount>0) {
 
49
        if (index.column()==0) {
 
50
            return QStyledItemDelegate::sizeHint(option, index);
 
51
        } else {
 
52
            return QSize();
 
53
        }
 
54
    }
 
55
 
 
56
    QSize res = QStyledItemDelegate::sizeHint(option, index);
 
57
 
 
58
    TodoFlatModel::ItemType type = rowType(index);
 
59
 
 
60
    if (type==TodoFlatModel::FolderTodo || !isInFocus(index)) {
 
61
        res.setHeight(32);
 
62
 
 
63
    } else if (type!=TodoFlatModel::StandardTodo) {
 
64
        res.setHeight(24);
 
65
    }
 
66
 
 
67
    return res;
 
68
}
 
69
 
 
70
void ActionListDelegate::paint(QPainter *painter,
 
71
                               const QStyleOptionViewItem &option,
 
72
                               const QModelIndex &index) const
 
73
{
 
74
    if (m_dragModeCount>0) {
 
75
        QStyleOptionViewItemV4 opt = option;
 
76
        opt.rect.setHeight(sizeHint(option, index).height());
 
77
        m_dragModeCount--;
 
78
        if (index.column()==0) {
 
79
            return QStyledItemDelegate::paint(painter, opt, index);
 
80
        } else {
 
81
            return;
 
82
        }
 
83
    }
 
84
 
 
85
    TodoFlatModel::ItemType type = rowType(index);
 
86
 
 
87
    QStyleOptionViewItemV4 opt = option;
 
88
 
 
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);
 
94
 
 
95
    } else if (type!=TodoFlatModel::StandardTodo) {
 
96
        opt.decorationSize = QSize(22, 22);
 
97
        opt.font.setWeight(QFont::Bold);
 
98
 
 
99
    } else if (index.parent().isValid()) {
 
100
        if (index.column()==0) {
 
101
            opt.rect.adjust(40, 0, 0, 0);
 
102
        }
 
103
        if (index.row()%2==0) {
 
104
            opt.features|= QStyleOptionViewItemV4::Alternate;
 
105
        }
 
106
    }
 
107
 
 
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));
 
113
    }
 
114
 
 
115
    QStyledItemDelegate::paint(painter, opt, index);
 
116
}
 
117
 
 
118
void ActionListDelegate::updateEditorGeometry(QWidget *editor,
 
119
                                              const QStyleOptionViewItem &option,
 
120
                                              const QModelIndex &index) const
 
121
{
 
122
    QStyledItemDelegate::updateEditorGeometry(editor, option, index);
 
123
 
 
124
    TodoFlatModel::ItemType type = rowType(index);
 
125
 
 
126
    if (type==TodoFlatModel::StandardTodo
 
127
     && index.column()==0
 
128
     && index.parent().isValid()) {
 
129
        QRect r = editor->geometry();
 
130
        r.adjust(40, 0, 0, 0);
 
131
        editor->setGeometry(r);
 
132
    }
 
133
}
 
134
 
 
135
bool ActionListDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
 
136
                                     const QStyleOptionViewItem &option,
 
137
                                     const QModelIndex &index)
 
138
{
 
139
    QStyleOptionViewItemV4 opt = option;
 
140
 
 
141
    TodoFlatModel::ItemType type = rowType(index);
 
142
 
 
143
    if (type==TodoFlatModel::StandardTodo
 
144
     && index.column()==0
 
145
     && index.parent().isValid()) {
 
146
        opt.rect.adjust(40, 0, 0, 0);
 
147
    }
 
148
 
 
149
    return QStyledItemDelegate::editorEvent(event, model, opt, index);
 
150
}
 
151
 
 
152
TodoFlatModel::ItemType ActionListDelegate::rowType(const QModelIndex &index)
 
153
{
 
154
    const ActionListModel *model = qobject_cast<const ActionListModel*>(index.model());
 
155
 
 
156
    if (model==0) {
 
157
        return TodoFlatModel::StandardTodo;
 
158
    }
 
159
 
 
160
    QModelIndex sourceIndex = model->mapToSource(index);
 
161
    QModelIndex rowTypeIndex = sourceIndex.sibling(sourceIndex.row(), TodoFlatModel::RowType);
 
162
 
 
163
    QVariant value = model->sourceModel()->data(rowTypeIndex);
 
164
    if (!value.isValid()) {
 
165
        return TodoFlatModel::StandardTodo;
 
166
    }
 
167
 
 
168
    return (TodoFlatModel::ItemType)value.toInt();
 
169
}
 
170
 
 
171
bool ActionListDelegate::isInFocus(const QModelIndex &index) const
 
172
{
 
173
    const ActionListModel *model = qobject_cast<const ActionListModel*>(index.model());
 
174
 
 
175
    if (model==0) {
 
176
        return true;
 
177
    }
 
178
 
 
179
    return model->isInFocus(index);
 
180
}
 
181
 
 
182
bool ActionListDelegate::isCompleted(const QModelIndex &index) const
 
183
{
 
184
    return index.model()->data(index.sibling(index.row(), 0), Qt::CheckStateRole).toInt()==Qt::Checked;
 
185
}
 
186
 
 
187
bool ActionListDelegate::isOverdue(const QModelIndex &index) const
 
188
{
 
189
    const ActionListModel *model = qobject_cast<const ActionListModel*>(index.model());
 
190
 
 
191
    if (model==0) {
 
192
        return false;
 
193
    }
 
194
 
 
195
    TodoFlatModel::ItemType type = rowType(index);
 
196
 
 
197
    if (type==TodoFlatModel::Category) {
 
198
        return false;
 
199
    }
 
200
 
 
201
    Akonadi::Item item = model->itemForIndex(index);
 
202
    const IncidencePtr incidence = item.payload<IncidencePtr>();
 
203
    KCal::Todo *todo = dynamic_cast<KCal::Todo*>(incidence.get());
 
204
 
 
205
    return todo->isOverdue();
 
206
}
 
207
 
 
208
void ActionListDelegate::setDragModeCount(int count)
 
209
{
 
210
    m_dragModeCount = count;
 
211
}