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 "actionlistmodel.h"
26
#include <akonadi/item.h>
28
#include <KDE/KLocale>
30
#include <QtCore/QStringList>
32
#include "todocategoriesmodel.h"
33
#include "todoflatmodel.h"
34
#include "todotreemodel.h"
36
ActionListModel::ActionListModel(QObject *parent)
37
: QSortFilterProxyModel(parent), m_mode(StandardMode)
39
setDynamicSortFilter(true);
42
ActionListModel::~ActionListModel()
46
Akonadi::Item ActionListModel::itemForIndex(const QModelIndex &index) const
48
QAbstractItemModel *source = sourceModel();
50
if (TodoFlatModel *flat = qobject_cast<TodoFlatModel*>(source)) {
51
return flat->itemForIndex(mapToSource(index));
52
} else if (TodoTreeModel *tree = qobject_cast<TodoTreeModel*>(source)) {
53
return tree->itemForIndex(mapToSource(index));
54
} else if (TodoCategoriesModel *categories = qobject_cast<TodoCategoriesModel*>(source)) {
55
return categories->itemForIndex(mapToSource(index));
58
return Akonadi::Item();
61
Qt::ItemFlags ActionListModel::flags(const QModelIndex &index) const
63
QModelIndex sourceIndex = mapToSource(index);
64
QModelIndex rowTypeIndex = sourceIndex.sibling(sourceIndex.row(), TodoFlatModel::RowType);
65
int rowType = sourceModel()->data(rowTypeIndex).toInt();
67
if (rowType==TodoFlatModel::FolderTodo || !isInFocus(index)) {
70
return QSortFilterProxyModel::flags(index);
74
QVariant ActionListModel::data(const QModelIndex &index, int role) const
76
QModelIndex sourceIndex = mapToSource(index);
77
if (sourceIndex.column()==TodoFlatModel::Categories && role==Qt::DisplayRole) {
78
return sourceModel()->data(sourceIndex, Qt::EditRole);
81
return sourceModel()->data(sourceIndex, role);
84
QVariant ActionListModel::headerData(int section, Qt::Orientation orientation, int role) const
86
if (role==Qt::DisplayRole && orientation == Qt::Horizontal) {
87
QModelIndex sourceIndex = mapToSource(index(0, section));
89
switch(sourceIndex.column()) {
90
case TodoFlatModel::Categories:
91
return i18n("Contexts");
92
case TodoFlatModel::ParentSummary:
93
return i18n("Project");
99
return QSortFilterProxyModel::headerData(section, orientation, role);
102
void ActionListModel::setMode(Mode mode)
108
ActionListModel::Mode ActionListModel::mode() const
113
void ActionListModel::setSourceFocusIndex(const QModelIndex &sourceIndex)
115
m_sourceFocusIndex = sourceIndex;
119
QModelIndex ActionListModel::sourceFocusIndex() const
121
return m_sourceFocusIndex;
124
bool ActionListModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &/*sourceParent*/) const
126
return sourceColumn!=TodoFlatModel::RemoteId
127
&& sourceColumn!=TodoFlatModel::ParentRemoteId
128
&& sourceColumn!=TodoFlatModel::RowType
129
&& (sourceColumn!=TodoFlatModel::Categories || (m_mode!=ContextMode && m_mode!=NoContextMode))
130
&& (sourceColumn!=TodoFlatModel::ParentSummary || (m_mode!=ProjectMode && m_mode!=NoProjectMode));
133
bool ActionListModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
135
QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
141
sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::RowType, sourceParent);
142
if (!sourceParent.isValid() && sourceModel()->data(sourceIndex).toInt()==TodoFlatModel::StandardTodo) {
147
sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::RowType, sourceParent);
148
if (sourceModel()->data(sourceIndex).toInt()!=TodoFlatModel::StandardTodo) {
151
sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::ParentRemoteId, sourceParent);
152
if (!sourceModel()->data(sourceIndex).toString().isEmpty()) {
157
sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::RowType, sourceParent);
158
if (sourceModel()->data(sourceIndex).toInt()!=TodoFlatModel::StandardTodo) {
161
sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::Categories, sourceParent);
162
if (!sourceModel()->data(sourceIndex).toStringList().isEmpty()) {
168
sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
170
QModelIndex i = sourceIndex;
171
while (i.isValid()) {
172
if (m_sourceFocusIndex == i) {
178
i = m_sourceFocusIndex;
179
while (i.isValid()) {
180
if (sourceIndex == i) {
186
return !m_sourceFocusIndex.isValid();
189
bool ActionListModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
191
if (left.column()==0 && right.column()==0) {
192
QModelIndex leftRowType = left.sibling(left.row(), TodoFlatModel::RowType);
193
QModelIndex rightRowType = right.sibling(right.row(), TodoFlatModel::RowType);
195
if (sourceModel()->data(rightRowType).toInt()==TodoFlatModel::FolderTodo
196
&& sourceModel()->data(leftRowType).toInt()!=TodoFlatModel::FolderTodo) {
199
return QSortFilterProxyModel::lessThan(left, right);
203
return QSortFilterProxyModel::lessThan(left, right);
206
bool ActionListModel::isInFocus(const QModelIndex &index) const
208
if (!m_sourceFocusIndex.isValid()) {
212
QModelIndex sourceIndex = mapToSource(index);
213
sourceIndex = sourceIndex.sibling(sourceIndex.row(), 0);
215
while (sourceIndex.isValid()) {
216
if (m_sourceFocusIndex==sourceIndex) {
219
sourceIndex = sourceIndex.parent();