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

« back to all changes in this revision

Viewing changes to src/actionlistmodel.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 "actionlistmodel.h"
 
25
 
 
26
#include <akonadi/item.h>
 
27
 
 
28
#include <KDE/KLocale>
 
29
 
 
30
#include <QtCore/QStringList>
 
31
 
 
32
#include "todocategoriesmodel.h"
 
33
#include "todoflatmodel.h"
 
34
#include "todotreemodel.h"
 
35
 
 
36
ActionListModel::ActionListModel(QObject *parent)
 
37
    : QSortFilterProxyModel(parent), m_mode(StandardMode)
 
38
{
 
39
    setDynamicSortFilter(true);
 
40
}
 
41
 
 
42
ActionListModel::~ActionListModel()
 
43
{
 
44
}
 
45
 
 
46
Akonadi::Item ActionListModel::itemForIndex(const QModelIndex &index) const
 
47
{
 
48
    QAbstractItemModel *source = sourceModel();
 
49
 
 
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));
 
56
    }
 
57
 
 
58
    return Akonadi::Item();
 
59
}
 
60
 
 
61
Qt::ItemFlags ActionListModel::flags(const QModelIndex &index) const
 
62
{
 
63
    QModelIndex sourceIndex = mapToSource(index);
 
64
    QModelIndex rowTypeIndex = sourceIndex.sibling(sourceIndex.row(), TodoFlatModel::RowType);
 
65
    int rowType = sourceModel()->data(rowTypeIndex).toInt();
 
66
 
 
67
    if (rowType==TodoFlatModel::FolderTodo || !isInFocus(index)) {
 
68
        return 0;
 
69
    } else {
 
70
        return QSortFilterProxyModel::flags(index);
 
71
    }
 
72
}
 
73
 
 
74
QVariant ActionListModel::data(const QModelIndex &index, int role) const
 
75
{
 
76
    QModelIndex sourceIndex = mapToSource(index);
 
77
    if (sourceIndex.column()==TodoFlatModel::Categories && role==Qt::DisplayRole) {
 
78
        return sourceModel()->data(sourceIndex, Qt::EditRole);
 
79
    }
 
80
 
 
81
    return sourceModel()->data(sourceIndex, role);
 
82
}
 
83
 
 
84
QVariant ActionListModel::headerData(int section, Qt::Orientation orientation, int role) const
 
85
{
 
86
    if (role==Qt::DisplayRole && orientation == Qt::Horizontal) {
 
87
        QModelIndex sourceIndex = mapToSource(index(0, section));
 
88
 
 
89
        switch(sourceIndex.column()) {
 
90
        case TodoFlatModel::Categories:
 
91
            return i18n("Contexts");
 
92
        case TodoFlatModel::ParentSummary:
 
93
            return i18n("Project");
 
94
        default:
 
95
            break;
 
96
        }
 
97
    }
 
98
 
 
99
    return QSortFilterProxyModel::headerData(section, orientation, role);
 
100
}
 
101
 
 
102
void ActionListModel::setMode(Mode mode)
 
103
{
 
104
    m_mode = mode;
 
105
    invalidate();
 
106
}
 
107
 
 
108
ActionListModel::Mode ActionListModel::mode() const
 
109
{
 
110
    return m_mode;
 
111
}
 
112
 
 
113
void ActionListModel::setSourceFocusIndex(const QModelIndex &sourceIndex)
 
114
{
 
115
    m_sourceFocusIndex = sourceIndex;
 
116
    invalidate();
 
117
}
 
118
 
 
119
QModelIndex ActionListModel::sourceFocusIndex() const
 
120
{
 
121
    return m_sourceFocusIndex;
 
122
}
 
123
 
 
124
bool ActionListModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &/*sourceParent*/) const
 
125
{
 
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));
 
131
}
 
132
 
 
133
bool ActionListModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
 
134
{
 
135
    QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
 
136
 
 
137
    switch (m_mode) {
 
138
    case StandardMode:
 
139
    case ContextMode:
 
140
    case ProjectMode:
 
141
        sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::RowType, sourceParent);
 
142
        if (!sourceParent.isValid() && sourceModel()->data(sourceIndex).toInt()==TodoFlatModel::StandardTodo) {
 
143
            return false;
 
144
        }
 
145
        break;
 
146
    case NoProjectMode:
 
147
        sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::RowType, sourceParent);
 
148
        if (sourceModel()->data(sourceIndex).toInt()!=TodoFlatModel::StandardTodo) {
 
149
            return false;
 
150
        }
 
151
        sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::ParentRemoteId, sourceParent);
 
152
        if (!sourceModel()->data(sourceIndex).toString().isEmpty()) {
 
153
            return false;
 
154
        }
 
155
        break;
 
156
    case NoContextMode:
 
157
        sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::RowType, sourceParent);
 
158
        if (sourceModel()->data(sourceIndex).toInt()!=TodoFlatModel::StandardTodo) {
 
159
            return false;
 
160
        }
 
161
        sourceIndex = sourceModel()->index(sourceRow, TodoFlatModel::Categories, sourceParent);
 
162
        if (!sourceModel()->data(sourceIndex).toStringList().isEmpty()) {
 
163
            return false;
 
164
        }
 
165
        break;
 
166
    }
 
167
 
 
168
    sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
 
169
 
 
170
    QModelIndex i = sourceIndex;
 
171
    while (i.isValid()) {
 
172
        if (m_sourceFocusIndex == i) {
 
173
            return true;
 
174
        }
 
175
        i = i.parent();
 
176
    }
 
177
 
 
178
    i = m_sourceFocusIndex;
 
179
    while (i.isValid()) {
 
180
        if (sourceIndex == i) {
 
181
            return true;
 
182
        }
 
183
        i = i.parent();
 
184
    }
 
185
 
 
186
    return !m_sourceFocusIndex.isValid();
 
187
}
 
188
 
 
189
bool ActionListModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
 
190
{
 
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);
 
194
 
 
195
        if (sourceModel()->data(rightRowType).toInt()==TodoFlatModel::FolderTodo
 
196
         && sourceModel()->data(leftRowType).toInt()!=TodoFlatModel::FolderTodo) {
 
197
            return true;
 
198
        } else {
 
199
            return QSortFilterProxyModel::lessThan(left, right);
 
200
        }
 
201
    }
 
202
 
 
203
    return QSortFilterProxyModel::lessThan(left, right);
 
204
}
 
205
 
 
206
bool ActionListModel::isInFocus(const QModelIndex &index) const
 
207
{
 
208
    if (!m_sourceFocusIndex.isValid()) {
 
209
        return true;
 
210
    }
 
211
 
 
212
    QModelIndex sourceIndex = mapToSource(index);
 
213
    sourceIndex = sourceIndex.sibling(sourceIndex.row(), 0);
 
214
 
 
215
    while (sourceIndex.isValid()) {
 
216
        if (m_sourceFocusIndex==sourceIndex) {
 
217
            return true;
 
218
        }
 
219
        sourceIndex = sourceIndex.parent();
 
220
    }
 
221
 
 
222
    return false;
 
223
}