2 * itemlistmodel.cpp - Akonadi item models
4 * Copyright © 2007-2011 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "itemlistmodel.h"
22 #include "collectionmodel.h"
25 #include <kselectionproxymodel.h>
27 using namespace Akonadi
;
30 /*=============================================================================
31 = Class: ItemListModel
32 = Filter proxy model containing all items (alarms/templates) of specified mime
33 = types in enabled collections.
34 =============================================================================*/
35 ItemListModel::ItemListModel(KAlarm::CalEvent::Types allowed
, QObject
* parent
)
36 : EntityMimeTypeFilterModel(parent
),
37 mAllowedTypes(allowed
),
40 KSelectionProxyModel
* selectionModel
= new KSelectionProxyModel(CollectionControlModel::instance()->selectionModel(), this);
41 selectionModel
->setSourceModel(AkonadiModel::instance());
42 selectionModel
->setFilterBehavior(KSelectionProxyModel::ChildrenOfExactSelection
);
43 setSourceModel(selectionModel
);
45 addMimeTypeExclusionFilter(Collection::mimeType());
46 setHeaderGroup(EntityTreeModel::ItemListHeaders
);
49 QStringList mimeTypes
= KAlarm::CalEvent::mimeTypes(allowed
);
50 foreach (const QString
& mime
, mimeTypes
)
51 addMimeTypeInclusionFilter(mime
);
53 setHeaderGroup(EntityTreeModel::ItemListHeaders
);
54 setSortRole(AkonadiModel::SortRole
);
55 setDynamicSortFilter(true);
56 connect(this, SIGNAL(rowsInserted(const QModelIndex
&, int, int)), SLOT(slotRowsInserted()));
57 connect(this, SIGNAL(rowsRemoved(const QModelIndex
&, int, int)), SLOT(slotRowsRemoved()));
60 int ItemListModel::columnCount(const QModelIndex
& /*parent*/) const
62 return AkonadiModel::ColumnCount
;
65 /******************************************************************************
66 * Called when rows have been inserted into the model.
68 void ItemListModel::slotRowsInserted()
70 if (!mHaveEvents
&& rowCount())
73 emit
haveEventsStatus(true);
77 /******************************************************************************
78 * Called when rows have been deleted from the model.
80 void ItemListModel::slotRowsRemoved()
82 if (mHaveEvents
&& !rowCount())
85 emit
haveEventsStatus(false);
90 QModelIndex
ItemListModel::index(int row
, int column
, const QModelIndex
& parent
) const
94 return createIndex(row
, column
, mEvents
[row
]);
97 bool ItemListModel::setData(const QModelIndex
& ix
, const QVariant
&, int role
)
99 if (ix
.isValid() && role
== Qt::EditRole
)
103 emit
dataChanged(index(row
, 0), index(row
, AkonadiModel::ColumnCount
- 1));
110 Qt::ItemFlags
ItemListModel::flags(const QModelIndex
& index
) const
112 if (!index
.isValid())
113 return Qt::ItemIsEnabled
;
114 return Qt::ItemIsEnabled
| Qt::ItemIsSelectable
| Qt::ItemIsEditable
| Qt::ItemIsDragEnabled
;
117 /******************************************************************************
118 * Return the index to a specified event.
120 QModelIndex
ItemListModel::eventIndex(Entity::Id itemId
) const
122 QModelIndexList list
= match(QModelIndex(), AkonadiModel::ItemIdRole
, itemId
, 1, Qt::MatchExactly
| Qt::MatchRecursive
);
124 return QModelIndex();
125 return index(list
[0].row(), 0, list
[0].parent());
128 /******************************************************************************
129 * Return the event in a specified row.
131 KAEvent
ItemListModel::event(int row
) const
133 return event(index(row
, 0));
136 /******************************************************************************
137 * Return the event referred to by an index.
139 KAEvent
ItemListModel::event(const QModelIndex
& index
) const
141 return static_cast<AkonadiModel
*>(sourceModel())->event(mapToSource(index
));
144 /******************************************************************************
145 * Check whether the model contains any events.
147 bool ItemListModel::haveEvents() const
153 /*=============================================================================
154 = Class: AlarmListModel
155 = Filter proxy model containing all alarms of specified mime types in enabled
157 Equivalent to AlarmListFilterModel
158 =============================================================================*/
159 AlarmListModel
* AlarmListModel::mAllInstance
= 0;
161 AlarmListModel::AlarmListModel(QObject
* parent
)
162 : ItemListModel(KAlarm::CalEvent::ACTIVE
| KAlarm::CalEvent::ARCHIVED
, parent
),
163 mFilterTypes(KAlarm::CalEvent::ACTIVE
| KAlarm::CalEvent::ARCHIVED
)
167 AlarmListModel::~AlarmListModel()
169 if (this == mAllInstance
)
173 AlarmListModel
* AlarmListModel::all()
177 mAllInstance
= new AlarmListModel(AkonadiModel::instance());
178 mAllInstance
->sort(TimeColumn
, Qt::AscendingOrder
);
183 void AlarmListModel::setEventTypeFilter(KAlarm::CalEvent::Types types
)
185 // Ensure that the filter isn't applied to the 'all' instance, and that
186 // 'types' doesn't include any disallowed alarm types
188 types
= includedTypes();
189 if (this != mAllInstance
190 && types
!= mFilterTypes
&& (types
& includedTypes()) == types
)
192 mFilterTypes
= types
;
197 bool AlarmListModel::filterAcceptsRow(int sourceRow
, const QModelIndex
& sourceParent
) const
199 if (!ItemListModel::filterAcceptsRow(sourceRow
, sourceParent
))
201 if (mFilterTypes
== KAlarm::CalEvent::EMPTY
)
203 int type
= sourceModel()->data(sourceModel()->index(sourceRow
, 0, sourceParent
), AkonadiModel::StatusRole
).toInt();
204 return static_cast<KAlarm::CalEvent::Type
>(type
) & mFilterTypes
;
207 bool AlarmListModel::filterAcceptsColumn(int sourceCol
, const QModelIndex
&) const
209 return (sourceCol
!= AkonadiModel::TemplateNameColumn
);
212 QVariant
AlarmListModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
214 if (orientation
== Qt::Horizontal
)
216 if (section
< 0 || section
>= ColumnCount
)
219 return ItemListModel::headerData(section
, orientation
, role
);
223 /*=============================================================================
224 = Class: TemplateListModel
225 = Filter proxy model containing all alarm templates for specified alarm types
226 = in enabled collections.
227 Equivalent to TemplateListFilterModel
228 =============================================================================*/
229 TemplateListModel
* TemplateListModel::mAllInstance
= 0;
231 TemplateListModel::TemplateListModel(QObject
* parent
)
232 : ItemListModel(KAlarm::CalEvent::TEMPLATE
, parent
),
233 mActionsEnabled(KAEvent::ACT_ALL
),
234 mActionsFilter(KAEvent::ACT_ALL
)
238 TemplateListModel::~TemplateListModel()
240 if (this == mAllInstance
)
244 TemplateListModel
* TemplateListModel::all()
248 mAllInstance
= new TemplateListModel(AkonadiModel::instance());
249 mAllInstance
->sort(TemplateNameColumn
, Qt::AscendingOrder
);
254 void TemplateListModel::setAlarmActionFilter(KAEvent::Actions types
)
256 // Ensure that the filter isn't applied to the 'all' instance.
257 if (this != mAllInstance
&& types
!= mActionsFilter
)
259 mActionsFilter
= types
;
264 void TemplateListModel::setAlarmActionsEnabled(KAEvent::Actions types
)
266 // Ensure that the setting isn't applied to the 'all' instance.
267 if (this != mAllInstance
&& types
!= mActionsEnabled
)
269 mActionsEnabled
= types
;
274 bool TemplateListModel::filterAcceptsRow(int sourceRow
, const QModelIndex
& sourceParent
) const
276 if (!ItemListModel::filterAcceptsRow(sourceRow
, sourceParent
))
278 if (mActionsFilter
== KAEvent::ACT_ALL
)
280 QModelIndex sourceIndex
= sourceModel()->index(sourceRow
, 0, sourceParent
);
281 KAEvent::Actions actions
= static_cast<KAEvent::Actions
>(sourceModel()->data(sourceIndex
, AkonadiModel::AlarmActionsRole
).toInt());
282 return actions
& mActionsFilter
;
285 bool TemplateListModel::filterAcceptsColumn(int sourceCol
, const QModelIndex
&) const
287 return sourceCol
== AkonadiModel::TemplateNameColumn
288 || sourceCol
== AkonadiModel::TypeColumn
;
291 QVariant
TemplateListModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
293 if (orientation
== Qt::Horizontal
)
298 section
= AkonadiModel::TypeColumn
;
300 case TemplateNameColumn
:
301 section
= AkonadiModel::TemplateNameColumn
;
307 return ItemListModel::headerData(section
, orientation
, role
);
310 Qt::ItemFlags
TemplateListModel::flags(const QModelIndex
& index
) const
312 Qt::ItemFlags f
= sourceModel()->flags(mapToSource(index
));
313 if (mActionsEnabled
== KAEvent::ACT_ALL
)
315 KAEvent::Actions actions
= static_cast<KAEvent::Actions
>(ItemListModel::data(index
, AkonadiModel::AlarmActionsRole
).toInt());
316 if (!(actions
& mActionsEnabled
))
317 f
= static_cast<Qt::ItemFlags
>(f
& ~(Qt::ItemIsEnabled
| Qt::ItemIsSelectable
));