• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kalarm

  • sources
  • kde-4.14
  • kdepim
  • kalarm
itemlistmodel.cpp
Go to the documentation of this file.
1 /*
2  * itemlistmodel.cpp - Akonadi item models
3  * Program: kalarm
4  * Copyright © 2007-2012 by David Jarvie <djarvie@kde.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "itemlistmodel.h"
22 #include "collectionmodel.h"
23 
24 #include <kalarmcal/kaevent.h>
25 
26 #include <kselectionproxymodel.h>
27 
28 using namespace Akonadi;
29 
30 
31 /*=============================================================================
32 = Class: ItemListModel
33 = Filter proxy model containing all items (alarms/templates) of specified mime
34 = types in enabled collections.
35 =============================================================================*/
36 ItemListModel::ItemListModel(CalEvent::Types allowed, QObject* parent)
37  : EntityMimeTypeFilterModel(parent),
38  mAllowedTypes(allowed),
39  mHaveEvents(false)
40 {
41  KSelectionProxyModel* selectionModel = new KSelectionProxyModel(CollectionControlModel::instance()->selectionModel(), this);
42  selectionModel->setSourceModel(AkonadiModel::instance());
43  selectionModel->setFilterBehavior(KSelectionProxyModel::ChildrenOfExactSelection);
44  setSourceModel(selectionModel);
45 
46  addMimeTypeExclusionFilter(Collection::mimeType());
47  setHeaderGroup(EntityTreeModel::ItemListHeaders);
48  if (allowed)
49  {
50  QStringList mimeTypes = CalEvent::mimeTypes(allowed);
51  foreach (const QString& mime, mimeTypes)
52  addMimeTypeInclusionFilter(mime);
53  }
54  setHeaderGroup(EntityTreeModel::ItemListHeaders);
55  setSortRole(AkonadiModel::SortRole);
56  setDynamicSortFilter(true);
57  connect(this, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(slotRowsInserted()));
58  connect(this, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(slotRowsRemoved()));
59  connect(AkonadiModel::instance(), SIGNAL(collectionStatusChanged(Akonadi::Collection,AkonadiModel::Change,QVariant,bool)),
60  SLOT(collectionStatusChanged(Akonadi::Collection,AkonadiModel::Change,QVariant,bool)));
61 }
62 
63 int ItemListModel::columnCount(const QModelIndex& /*parent*/) const
64 {
65  return AkonadiModel::ColumnCount;
66 }
67 
68 /******************************************************************************
69 * Called when rows have been inserted into the model.
70 */
71 void ItemListModel::slotRowsInserted()
72 {
73  if (!mHaveEvents && rowCount())
74  {
75  mHaveEvents = true;
76  emit haveEventsStatus(true);
77  }
78 }
79 
80 /******************************************************************************
81 * Called when rows have been deleted from the model.
82 */
83 void ItemListModel::slotRowsRemoved()
84 {
85  if (mHaveEvents && !rowCount())
86  {
87  mHaveEvents = false;
88  emit haveEventsStatus(false);
89  }
90 }
91 
92 /******************************************************************************
93 * Called when a collection parameter or status has changed.
94 * If the collection's enabled status has changed, re-filter the list to add or
95 * remove its alarms.
96 */
97 void ItemListModel::collectionStatusChanged(const Collection& collection, AkonadiModel::Change change, const QVariant&, bool inserted)
98 {
99  Q_UNUSED(inserted);
100  if (!collection.isValid())
101  return;
102  if (change == AkonadiModel::Enabled)
103  {
104  // Ensure that items for a newly enabled collection are always ordered
105  // correctly. Note that invalidateFilter() is not adequate for this.
106  invalidate();
107  }
108 }
109 
110 bool ItemListModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
111 {
112  if (!EntityMimeTypeFilterModel::filterAcceptsRow(sourceRow, sourceParent))
113  return false;
114  // Get the alarm type of the item
115  QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
116  CalEvent::Type type = static_cast<CalEvent::Type>(sourceModel()->data(sourceIndex, AkonadiModel::StatusRole).toInt());
117  Collection parent = sourceIndex.data(AkonadiModel::ParentCollectionRole).value<Collection>();
118  return CollectionControlModel::isEnabled(parent, type);
119 }
120 
121 #if 0
122 QModelIndex ItemListModel::index(int row, int column, const QModelIndex& parent) const
123 {
124  if (parent.isValid())
125  return QModelIndex();
126  return createIndex(row, column, mEvents[row]);
127 }
128 
129 bool ItemListModel::setData(const QModelIndex& ix, const QVariant&, int role)
130 {
131  if (ix.isValid() && role == Qt::EditRole)
132  {
133 //??? update event
134  int row = ix.row();
135  emit dataChanged(index(row, 0), index(row, AkonadiModel::ColumnCount - 1));
136  return true;
137  }
138  return false;
139 }
140 #endif
141 
142 Qt::ItemFlags ItemListModel::flags(const QModelIndex& index) const
143 {
144  if (!index.isValid())
145  return Qt::ItemIsEnabled;
146  return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
147 }
148 
149 /******************************************************************************
150 * Return the index to a specified event.
151 */
152 QModelIndex ItemListModel::eventIndex(Entity::Id itemId) const
153 {
154  QModelIndexList list = match(QModelIndex(), AkonadiModel::ItemIdRole, itemId, 1, Qt::MatchExactly | Qt::MatchRecursive);
155  if (list.isEmpty())
156  return QModelIndex();
157  return index(list[0].row(), 0, list[0].parent());
158 }
159 
160 /******************************************************************************
161 * Return the event in a specified row.
162 */
163 KAEvent ItemListModel::event(int row) const
164 {
165  return event(index(row, 0));
166 }
167 
168 /******************************************************************************
169 * Return the event referred to by an index.
170 */
171 KAEvent ItemListModel::event(const QModelIndex& index) const
172 {
173  return static_cast<AkonadiModel*>(sourceModel())->event(mapToSource(index));
174 }
175 
176 /******************************************************************************
177 * Check whether the model contains any events.
178 */
179 bool ItemListModel::haveEvents() const
180 {
181  return rowCount();
182 }
183 
184 
185 /*=============================================================================
186 = Class: AlarmListModel
187 = Filter proxy model containing all alarms (not templates) of specified mime
188 = types in enabled collections.
189 Equivalent to AlarmListFilterModel
190 =============================================================================*/
191 AlarmListModel* AlarmListModel::mAllInstance = 0;
192 
193 AlarmListModel::AlarmListModel(QObject* parent)
194  : ItemListModel(CalEvent::ACTIVE | CalEvent::ARCHIVED, parent),
195  mFilterTypes(CalEvent::ACTIVE | CalEvent::ARCHIVED)
196 {
197 }
198 
199 AlarmListModel::~AlarmListModel()
200 {
201  if (this == mAllInstance)
202  mAllInstance = 0;
203 }
204 
205 AlarmListModel* AlarmListModel::all()
206 {
207  if (!mAllInstance)
208  {
209  mAllInstance = new AlarmListModel(AkonadiModel::instance());
210  mAllInstance->sort(TimeColumn, Qt::AscendingOrder);
211  }
212  return mAllInstance;
213 }
214 
215 void AlarmListModel::setEventTypeFilter(CalEvent::Types types)
216 {
217  // Ensure that the filter isn't applied to the 'all' instance, and that
218  // 'types' doesn't include any disallowed alarm types
219  if (!types)
220  types = includedTypes();
221  if (this != mAllInstance
222  && types != mFilterTypes && (types & includedTypes()) == types)
223  {
224  mFilterTypes = types;
225  invalidateFilter();
226  }
227 }
228 
229 bool AlarmListModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
230 {
231  if (!ItemListModel::filterAcceptsRow(sourceRow, sourceParent))
232  return false;
233  if (mFilterTypes == CalEvent::EMPTY)
234  return false;
235  int type = sourceModel()->data(sourceModel()->index(sourceRow, 0, sourceParent), AkonadiModel::StatusRole).toInt();
236  return static_cast<CalEvent::Type>(type) & mFilterTypes;
237 }
238 
239 bool AlarmListModel::filterAcceptsColumn(int sourceCol, const QModelIndex&) const
240 {
241  return (sourceCol != AkonadiModel::TemplateNameColumn);
242 }
243 
244 QVariant AlarmListModel::headerData(int section, Qt::Orientation orientation, int role) const
245 {
246  if (orientation == Qt::Horizontal)
247  {
248  if (section < 0 || section >= ColumnCount)
249  return QVariant();
250  }
251  return ItemListModel::headerData(section, orientation, role);
252 }
253 
254 
255 /*=============================================================================
256 = Class: TemplateListModel
257 = Filter proxy model containing all alarm templates for specified alarm types
258 = in enabled collections.
259 Equivalent to TemplateListFilterModel
260 =============================================================================*/
261 TemplateListModel* TemplateListModel::mAllInstance = 0;
262 
263 TemplateListModel::TemplateListModel(QObject* parent)
264  : ItemListModel(CalEvent::TEMPLATE, parent),
265  mActionsEnabled(KAEvent::ACT_ALL),
266  mActionsFilter(KAEvent::ACT_ALL)
267 {
268 }
269 
270 TemplateListModel::~TemplateListModel()
271 {
272  if (this == mAllInstance)
273  mAllInstance = 0;
274 }
275 
276 TemplateListModel* TemplateListModel::all()
277 {
278  if (!mAllInstance)
279  {
280  mAllInstance = new TemplateListModel(AkonadiModel::instance());
281  mAllInstance->sort(TemplateNameColumn, Qt::AscendingOrder);
282  }
283  return mAllInstance;
284 }
285 
286 void TemplateListModel::setAlarmActionFilter(KAEvent::Actions types)
287 {
288  // Ensure that the filter isn't applied to the 'all' instance.
289  if (this != mAllInstance && types != mActionsFilter)
290  {
291  mActionsFilter = types;
292  filterChanged();
293  }
294 }
295 
296 void TemplateListModel::setAlarmActionsEnabled(KAEvent::Actions types)
297 {
298  // Ensure that the setting isn't applied to the 'all' instance.
299  if (this != mAllInstance && types != mActionsEnabled)
300  {
301  mActionsEnabled = types;
302  filterChanged();
303  }
304 }
305 
306 bool TemplateListModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
307 {
308  if (!ItemListModel::filterAcceptsRow(sourceRow, sourceParent))
309  return false;
310  if (mActionsFilter == KAEvent::ACT_ALL)
311  return true;
312  QModelIndex sourceIndex = sourceModel()->index(sourceRow, 0, sourceParent);
313  KAEvent::Actions actions = static_cast<KAEvent::Actions>(sourceModel()->data(sourceIndex, AkonadiModel::AlarmActionsRole).toInt());
314  return actions & mActionsFilter;
315 }
316 
317 bool TemplateListModel::filterAcceptsColumn(int sourceCol, const QModelIndex&) const
318 {
319  return sourceCol == AkonadiModel::TemplateNameColumn
320  || sourceCol == AkonadiModel::TypeColumn;
321 }
322 
323 QVariant TemplateListModel::headerData(int section, Qt::Orientation orientation, int role) const
324 {
325  if (orientation == Qt::Horizontal)
326  {
327  switch (section)
328  {
329  case TypeColumn:
330  section = AkonadiModel::TypeColumn;
331  break;
332  case TemplateNameColumn:
333  section = AkonadiModel::TemplateNameColumn;
334  break;
335  default:
336  return QVariant();
337  }
338  }
339  return ItemListModel::headerData(section, orientation, role);
340 }
341 
342 Qt::ItemFlags TemplateListModel::flags(const QModelIndex& index) const
343 {
344  Qt::ItemFlags f = sourceModel()->flags(mapToSource(index));
345  if (mActionsEnabled == KAEvent::ACT_ALL)
346  return f;
347  KAEvent::Actions actions = static_cast<KAEvent::Actions>(ItemListModel::data(index, AkonadiModel::AlarmActionsRole).toInt());
348  if (!(actions & mActionsEnabled))
349  f = static_cast<Qt::ItemFlags>(f & ~(Qt::ItemIsEnabled | Qt::ItemIsSelectable));
350  return f;
351 }
352 
353 // vim: et sw=4:
QModelIndex
AkonadiModel::StatusRole
Definition: akonadimodel.h:66
AlarmListModel::filterAcceptsColumn
virtual bool filterAcceptsColumn(int sourceCol, const QModelIndex &sourceParent) const
Definition: itemlistmodel.cpp:239
ItemListModel::columnCount
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: itemlistmodel.cpp:63
TemplateListModel::filterAcceptsRow
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Definition: itemlistmodel.cpp:306
AlarmListModel::filterAcceptsRow
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Definition: itemlistmodel.cpp:229
ItemListModel::event
KAEvent event(int row) const
Definition: itemlistmodel.cpp:163
AkonadiModel::Enabled
Definition: akonadimodel.h:50
QVariant::value
T value() const
ItemListModel::includedTypes
CalEvent::Types includedTypes() const
Definition: itemlistmodel.h:47
TemplateListModel
Definition: itemlistmodel.h:133
TemplateListModel::TemplateListModel
TemplateListModel(QObject *parent=0)
Definition: itemlistmodel.cpp:263
CollectionControlModel::isEnabled
static bool isEnabled(const Akonadi::Collection &, CalEvent::Type)
Return whether a collection is enabled (and valid).
Definition: collectionmodel.cpp:715
QModelIndex::isValid
bool isValid() const
TemplateListModel::~TemplateListModel
~TemplateListModel()
Definition: itemlistmodel.cpp:270
AkonadiModel::TemplateNameColumn
Definition: akonadimodel.h:54
ItemListModel::ItemListModel
ItemListModel(CalEvent::Types allowed, QObject *parent=0)
Constructor.
Definition: itemlistmodel.cpp:36
CollectionControlModel::instance
static CollectionControlModel * instance()
Definition: collectionmodel.cpp:653
QObject
TemplateListModel::setAlarmActionFilter
void setAlarmActionFilter(KAEvent::Actions)
Set which alarm action types should be included in the model.
Definition: itemlistmodel.cpp:286
itemlistmodel.h
AkonadiModel::instance
static AkonadiModel * instance()
Definition: akonadimodel.cpp:83
TemplateListModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &) const
Definition: itemlistmodel.cpp:342
QModelIndex::row
int row() const
AkonadiModel::SortRole
Definition: akonadimodel.h:70
TemplateListModel::headerData
virtual QVariant headerData(int section, Qt::Orientation, int role=Qt::DisplayRole) const
Definition: itemlistmodel.cpp:323
AkonadiModel::Change
Change
Definition: akonadimodel.h:50
AlarmListModel::setEventTypeFilter
void setEventTypeFilter(CalEvent::Types types)
Set a filter to restrict the event types to a subset of those specified in the constructor.
Definition: itemlistmodel.cpp:215
TemplateListModel::TemplateNameColumn
Definition: itemlistmodel.h:138
TemplateListModel::filterAcceptsColumn
virtual bool filterAcceptsColumn(int sourceCol, const QModelIndex &sourceParent) const
Definition: itemlistmodel.cpp:317
ItemListModel::eventIndex
QModelIndex eventIndex(Akonadi::Item::Id) const
Definition: itemlistmodel.cpp:152
QString
QStringList
ItemListModel::haveEventsStatus
void haveEventsStatus(bool have)
Signal emitted when either the first item is added to the model, or when the last item is deleted fro...
AlarmListModel::~AlarmListModel
~AlarmListModel()
Definition: itemlistmodel.cpp:199
AkonadiModel::ColumnCount
Definition: akonadimodel.h:55
ItemListModel::filterAcceptsRow
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
Definition: itemlistmodel.cpp:110
collectionmodel.h
QModelIndex::data
QVariant data(int role) const
TemplateListModel::TypeColumn
Definition: itemlistmodel.h:138
AkonadiModel::AlarmActionsRole
Definition: akonadimodel.h:67
TemplateListModel::setAlarmActionsEnabled
KAEvent::Actions setAlarmActionsEnabled() const
Set which alarm types should be shown as disabled in the model.
Definition: itemlistmodel.h:158
TemplateListModel::all
static TemplateListModel * all()
Return the model containing all alarm templates.
Definition: itemlistmodel.cpp:276
AlarmListModel::headerData
virtual QVariant headerData(int section, Qt::Orientation, int role=Qt::DisplayRole) const
Definition: itemlistmodel.cpp:244
ItemListModel::haveEvents
bool haveEvents() const
Determine whether the model contains any items.
Definition: itemlistmodel.cpp:179
AlarmListModel::ColumnCount
Definition: itemlistmodel.h:93
ItemListModel::flags
virtual Qt::ItemFlags flags(const QModelIndex &) const
Definition: itemlistmodel.cpp:142
AkonadiModel
Definition: akonadimodel.h:46
AlarmListModel::all
static AlarmListModel * all()
Return the model containing all active and archived alarms.
Definition: itemlistmodel.cpp:205
AlarmListModel
Definition: itemlistmodel.h:87
AlarmListModel::AlarmListModel
AlarmListModel(QObject *parent=0)
Definition: itemlistmodel.cpp:193
ItemListModel
Definition: itemlistmodel.h:38
AlarmListModel::TimeColumn
Definition: itemlistmodel.h:92
AkonadiModel::TypeColumn
Definition: akonadimodel.h:53
QVariant
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:34:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalarm

Skip menu "kalarm"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal