• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

Akonadi::EntityTreeModel

Akonadi::EntityTreeModel Class Reference

A model for collections and items together. More...

#include <entitytreemodel.h>

Inherits QAbstractItemModel.

List of all members.

Public Types

enum  CollectionFetchStrategy { FetchNoCollections, FetchFirstLevelChildCollections, FetchCollectionsRecursive }
enum  HeaderGroup {
  EntityTreeHeaders, CollectionTreeHeaders, ItemListHeaders, UserHeaders = 10,
  EndHeaderGroup = 32
}
enum  ItemPopulationStrategy { NoItemPopulation, ImmediatePopulation, LazyPopulation }
enum  Roles {
  ItemIdRole = Qt::UserRole + 1, ItemRole = Qt::UserRole + 2, MimeTypeRole = Qt::UserRole + 3, CollectionIdRole = Qt::UserRole + 10,
  CollectionRole = Qt::UserRole + 11, RemoteIdRole, CollectionChildOrderRole, AmazingCompletionRole,
  ParentCollectionRole, ColumnCountRole, LoadedPartsRole, AvailablePartsRole,
  SessionRole, CollectionRefRole, CollectionDerefRole, PendingCutRole,
  EntityUrlRole, UserRole = Qt::UserRole + 500, TerminalUserRole = 2000, EndRole = 65535
}

Public Member Functions

 EntityTreeModel (ChangeRecorder *monitor, QObject *parent=0)
virtual ~EntityTreeModel ()
virtual bool canFetchMore (const QModelIndex &parent) const
CollectionFetchStrategy collectionFetchStrategy () const
virtual int columnCount (const QModelIndex &parent=QModelIndex()) const
virtual QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const
virtual bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
virtual void fetchMore (const QModelIndex &parent)
virtual Qt::ItemFlags flags (const QModelIndex &index) const
virtual bool hasChildren (const QModelIndex &parent=QModelIndex()) const
virtual QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
bool includeRootCollection () const
virtual QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const
ItemPopulationStrategy itemPopulationStrategy () const
virtual QModelIndexList match (const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
virtual QMimeData * mimeData (const QModelIndexList &indexes) const
virtual QStringList mimeTypes () const
virtual QModelIndex parent (const QModelIndex &index) const
QString rootCollectionDisplayName () const
virtual int rowCount (const QModelIndex &parent=QModelIndex()) const
void setCollectionFetchStrategy (CollectionFetchStrategy strategy)
virtual bool setData (const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
void setIncludeRootCollection (bool include)
void setItemPopulationStrategy (ItemPopulationStrategy strategy)
void setRootCollectionDisplayName (const QString &name)
void setShowSystemEntities (bool show)
virtual Qt::DropActions supportedDropActions () const
bool systemEntitiesShown () const

Protected Member Functions

void clearAndReset ()
virtual int entityColumnCount (HeaderGroup headerGroup) const
virtual QVariant entityData (const Collection &collection, int column, int role=Qt::DisplayRole) const
virtual QVariant entityData (const Item &item, int column, int role=Qt::DisplayRole) const
virtual QVariant entityHeaderData (int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
virtual bool entityMatch (const Collection &collection, const QVariant &value, Qt::MatchFlags flags) const
virtual bool entityMatch (const Item &item, const QVariant &value, Qt::MatchFlags flags) const

Detailed Description

A model for collections and items together.

Akonadi models and views provide a high level way to interact with the akonadi server. Most applications will use these classes.

Models provide an interface for viewing, deleting and moving Items and Collections. Additionally, the models are updated automatically if another application changes the data or inserts of deletes items etc.

Note:
The EntityTreeModel should be used with the EntityTreeView or the EntityListView class either directly or indirectly via proxy models.

Retrieving Collections and Items from the model

If you want to retrieve and Item or Collection from the model, and already have a valid QModelIndex for the correct row, the Collection can be retrieved like this:

 Collection col = index.data( EntityTreeModel::CollectionRole ).value<Collection>();

And similarly for Items. This works even if there is a proxy model between the calling code and the EntityTreeModel.

If you want to retrieve a Collection for a particular Collection::Id and you do not yet have a valid QModelIndex, use match:

 QModelIndexList list = model->match( QModelIndex(), CollectionIdRole, id );
 if ( list.isEmpty() )
   return; // A Collection with that Id is not in the model.
 Q_ASSERT( list.size() == 1 ); // Otherwise there must be only one instance of it.
 Collection collection = list.at( 0 ).data( EntityTreeModel::CollectionRole ).value<Collection>();

Not that a single Item may appear multiple times in a model, so the list size may not be 1 if it is not empty in that case, so the Q_ASSERT should not be used.

See also:
virtual-collections

Using EntityTreeModel in your application

The responsibilities which fall to the application developer are

  • Configuring the ChangeRecorder and EntityTreeModel
  • Making use of this class via proxy models
  • Subclassing for type specific display information

Creating and configuring the EntityTreeModel

This class is a wrapper around a Akonadi::ChangeRecorder object. The model represents a part of the collection and item tree configured in the ChangeRecorder. The structure of the model mirrors the structure of Collections and Items on the Akonadi server.

The following code creates a model which fetches items and collections relevant to addressees (contacts), and automatically manages keeping the items up to date.

   ChangeRecorder *changeRecorder = new ChangeRecorder( this );
   changeRecorder->setCollectionMonitored( Collection::root() );
   changeRecorder->setMimeTypeMonitored( KABC::addresseeMimeType() );
   changeRecorder->setSession( session );

   EntityTreeModel *model = new EntityTreeModel( changeRecorder, this );

   EntityTreeView *view = new EntityTreeView( this );
   view->setModel( model );

The EntityTreeModel will show items of a different type by changing the line

 changeRecorder->setMimeTypeMonitored( KABC::addresseeMimeType() );

to a different mimetype. KABC::addresseeMimeType() is an alias for "text/directory". If changed to KMime::Message::mimeType() (an alias for "message/rfc822") the model would instead contain emails. The model can be configured to contain items of any mimetype known to Akonadi.

Note:
The EntityTreeModel does some extra configuration on the Monitor, such as setting itemFetchScope() and collectionFetchScope() to retrieve all ancestors. This is necessary for proper function of the model.
See also:
Akonadi::ItemFetchScope::AncestorRetrieval.
akonadi-mimetypes.

The EntityTreeModel can be further configured for certain behaviours such as fetching of collections and items.

The model can be configured to not fetch items into the model (ie, fetch collections only) by setting

 entityTreeModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );

The items may be fetched lazily, i.e. not inserted into the model until request by the user for performance reasons.

The Collection tree is always built immediately if Collections are to be fetched.

 entityTreeModel->setItemPopulationStrategy( EntityTreeModel::LazyPopulation );

This will typically be used with a EntityMimeTypeFilterModel in a configuration such as KMail4.5 or AkonadiConsole.

The CollectionFetchStrategy determines how the model will be populated with Collections. That is, if FetchNoCollections is set, no collections beyond the root of the model will be fetched. This can be used in combination with setting a particular Collection to monitor.

 // Get an collection id from a config file.
 Collection::Id id;
 monitor->setCollectionMonitored( Collection( id ) );
 // ... Other initialization code.
 entityTree->setCollectionFetchStrategy( FetchNoCollections );

This has the effect of creating a model of only a list of Items, and not collections. This is similar in behaviour and aims to the ItemModel. By using FetchFirstLevelCollections instead, a mixed list of entities can be created.

Note:
It is important that you set only one Collection to be monitored in the monitor object. This one collection will be the root of the tree. If you need a model with a more complex structure, consider monitoring a common ancestor and using a SelectionProxyModel.
See also:
lazy-model-population

It is also possible to show the root Collection as part of the selectable model:

 entityTree->setIncludeRootCollection( true );

By default the displayed name of the root collection is '[*]', because it doesn't require i18n, and is generic. It can be changed too.

 entityTree->setIncludeRootCollection( true );
 entityTree->setRootCollectionDisplayName( i18nc( "Name of top level for all addressbooks in the application", "[All AddressBooks]" ) )

This feature is used in KAddressBook.

Using EntityTreeModel with Proxy models

An Akonadi::SelectionProxyModel can be used to simplify managing selection in one view through multiple proxy models to a representation in another view. The selectionModel of the initial view is used to create a proxied model which filters out anything not related to the current selection.

 // ... create an EntityTreeModel

 collectionTree = new EntityMimeTypeFilterModel( this );
 collectionTree->setSourceModel( entityTreeModel );

 // Include only collections in this proxy model.
 collectionTree->addMimeTypeInclusionFilter( Collection::mimeType() );
 collectionTree->setHeaderGroup( EntityTreeModel::CollectionTreeHeaders );

 treeview->setModel(collectionTree);

 // SelectionProxyModel can handle complex selections:
 treeview->setSelectionMode( QAbstractItemView::ExtendedSelection );

 SelectionProxyModel *selProxy = new SelectionProxyModel( treeview->selectionModel(), this );
 selProxy->setSourceModel( entityTreeModel );

 itemList = new EntityMimeTypeFilterModel( this );
 itemList->setSourceModel( selProxy );

 // Filter out collections. Show only items.
 itemList->addMimeTypeExclusionFilter( Collection::mimeType() );
 itemList->setHeaderGroup( EntityTreeModel::ItemListHeaders );

 EntityTreeView *itemView = new EntityTreeView( splitter );
 itemView->setModel( itemList );

The SelectionProxyModel can handle complex selections.

See the KSelectionProxyModel documentation for the valid configurations of a Akonadi::SelectionProxyModel.

Obviously, the SelectionProxyModel may be used in a view, or further processed with other proxy models. Typically, the result from this model will be further filtered to remove collections from the item list as in the above example.

There are several advantages of using EntityTreeModel with the SelectionProxyModel, namely the items can be fetched and cached instead of being fetched many times, and the chain of proxies from the core model to the view is automatically handled. There is no need to manage all the mapToSource and mapFromSource calls manually.

A KDescendantsProxyModel can be used to represent all descendants of a model as a flat list. For example, to show all descendant items in a selected Collection in a list:

 collectionTree = new EntityMimeTypeFilterModel( this );
 collectionTree->setSourceModel( entityTreeModel );

 // Include only collections in this proxy model.
 collectionTree->addMimeTypeInclusionFilter( Collection::mimeType() );
 collectionTree->setHeaderGroup( EntityTreeModel::CollectionTreeHeaders );

 treeview->setModel( collectionTree );

 SelectionProxyModel *selProxy = new SelectionProxyModel( treeview->selectionModel(), this );
 selProxy->setSourceModel( entityTreeModel );

 descendedList = new DescendantEntitiesProxyModel( this );
 descendedList->setSourceModel( selProxy );

 itemList = new EntityMimeTypeFilterModel( this );
 itemList->setSourceModel( descendedList );

 // Exclude collections from the list view.
 itemList->addMimeTypeExclusionFilter( Collection::mimeType() );
 itemList->setHeaderGroup( EntityTreeModel::ItemListHeaders );

 listView = new EntityTreeView( this );
 listView->setModel( itemList );

Note that it is important in this case to use the DescendantEntitesProxyModel before the EntityMimeTypeFilterModel. Otherwise, by filtering out the collections first, you would also be filtering out their child items.

This pattern is used in KAddressBook.

It would not make sense to use a KDescendantsProxyModel with LazyPopulation.

Subclassing EntityTreeModel

Usually an application will create a subclass of an EntityTreeModel and use that in several views via proxy models.

The subclassing is necessary in order for the data in the model to have type-specific representation in applications

For example, the headerData for an EntityTreeModel will be different depending on whether it is in a view showing only Collections in which case the header data should be "AddressBooks" for example, or only Items, in which case the headerData would be for example "Family Name", "Given Name" and "Email addres" for contacts or "Subject", "Sender", "Date" in the case of emails.

Additionally, the actual data shown in the rows of the model should be type specific.

In summary, it must be possible to have different numbers of columns, different data in hte rows of those columns, and different titles for each column depending on the contents of the view.

The way this is accomplished is by using the EntityMimeTypeFilterModel for splitting the model into a "CollectionTree" and an "Item List" as in the above example, and using a type-specific EntityTreeModel subclass to return the type-specific data, typically for only one type (for example, contacts or emails).

The following protected virtual methods should be implemented in the subclass:

  • int entityColumnCount( HeaderGroup headerGroup ) const; -- Implement to return the number of columns for a HeaderGroup. If the HeaderGroup is CollectionTreeHeaders, return the number of columns to display for the Collection tree, and if it is ItemListHeaders, return the number of columns to display for the item. In the case of addressee, this could be for example, two (for given name and family name) or for emails it could be three (for subject, sender, date). This is a decision of the subclass implementor.
  • QVariant entityHeaderData( int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup ) const; -- Implement to return the data for each section for a HeaderGroup. For example, if the header group is CollectionTreeHeaders in a contacts model, the string "Address books" might be returned for column 0, whereas if the headerGroup is ItemListHeaders, the strings "Given Name", "Family Name", "Email Address" might be returned for the columns 0, 1, and 2.
  • QVariant entityData( const Collection &collection, int column, int role = Qt::DisplayRole ) const; -- Implement to return data for a particular Collection. Typically this will be the name of the collection or the EntityDisplayAttribute.
  • QVariant entityData( const Item &item, int column, int role = Qt::DisplayRole ) const; -- Implement to return the data for a particular item and column. In the case of email for example, this would be the actual subject, sender and date of the email.
Note:
The entityData methods are just for convenience. the QAbstractItemMOdel::data method can be overridden if required.

The application writer must then properly configure proxy models for the views, so that the correct data is shown in the correct view. That is the purpose of these lines in the above example

 collectionTree->setHeaderGroup( EntityTreeModel::CollectionTreeHeaders );
 itemList->setHeaderGroup( EntityTreeModel::ItemListHeaders );
Author:
Stephen Kelly <steveire@gmail.com>
Since:
4.4

Definition at line 310 of file entitytreemodel.h.


Member Enumeration Documentation

enum Akonadi::EntityTreeModel::CollectionFetchStrategy

Describes what collections shall be fetched by and represent in the model.

Enumerator:
FetchNoCollections 

Fetches nothing. This creates an empty model.

FetchFirstLevelChildCollections 

Fetches first level collections in the root collection.

FetchCollectionsRecursive 

Fetches collections in the root collection recursively. This is the default.

Definition at line 436 of file entitytreemodel.h.

enum Akonadi::EntityTreeModel::HeaderGroup

Describes what header information the model shall return.

Enumerator:
EntityTreeHeaders 

Header information for a tree with collections and items.

CollectionTreeHeaders 

Header information for a collection-only tree.

ItemListHeaders 

Header information for a list of items.

UserHeaders 

Last header information for submodel extensions.

EndHeaderGroup 

Last headergroup role. Don't use a role beyond this or headerData will break.

Definition at line 351 of file entitytreemodel.h.

enum Akonadi::EntityTreeModel::ItemPopulationStrategy

Describes how the model should populated its items.

Enumerator:
NoItemPopulation 

Do not include items in the model.

ImmediatePopulation 

Retrieve items immediately when their parent is in the model. This is the default.

LazyPopulation 

Fetch items only when requested (using canFetchMore/fetchMore).

Definition at line 376 of file entitytreemodel.h.

enum Akonadi::EntityTreeModel::Roles

Describes the roles for items.

Roles for collections are defined by the superclass.

Enumerator:
ItemIdRole 

The item id.

ItemRole 

The Item.

MimeTypeRole 

The mimetype of the entity.

CollectionIdRole 

The collection id.

CollectionRole 

The collection.

RemoteIdRole 

The remoteId of the entity.

CollectionChildOrderRole 

Ordered list of child items if available.

AmazingCompletionRole 

Role used to implement amazing completion.

ParentCollectionRole 

The parent collection of the entity.

ColumnCountRole 

For internal use only.

Used by proxies to determine the number of columns for a header group.

LoadedPartsRole 

Parts available in the model for the item.

AvailablePartsRole 

Parts available in the Akonadi server for the item.

SessionRole 

For internal use only.

The Session used by this model

CollectionRefRole 

For internal use only.

Used to increase the reference count on a Collection

CollectionDerefRole 

For internal use only.

Used to decrease the reference count on a Collection

PendingCutRole 

For internal use only.

Used to indicate items which are to be cut

EntityUrlRole 

The akonadi:/ Url of the entity as a string. Item urls will contain the mimetype.

UserRole 

First role for user extensions.

TerminalUserRole 

Last role for user extensions. Don't use a role beyond this or headerData will break.

Definition at line 318 of file entitytreemodel.h.


Constructor & Destructor Documentation

EntityTreeModel::EntityTreeModel ( ChangeRecorder *  monitor,
QObject *  parent = 0 
) [explicit]

Creates a new entity tree model.

Parameters:
monitor The ChangeRecorder whose entities should be represented in the model.
parent The parent object.

Definition at line 56 of file entitytreemodel.cpp.

EntityTreeModel::~EntityTreeModel (  )  [virtual]

Destroys the entity tree model.

Definition at line 75 of file entitytreemodel.cpp.


Member Function Documentation

void EntityTreeModel::clearAndReset (  )  [protected]

Clears and resets the model.

Always call this instead of the reset method in the superclass. Using the reset method will not reliably clear or refill the model.

Definition at line 98 of file entitytreemodel.cpp.

EntityTreeModel::CollectionFetchStrategy EntityTreeModel::collectionFetchStrategy (  )  const

Returns the collection fetch strategy of the model.

Definition at line 1042 of file entitytreemodel.cpp.

QVariant EntityTreeModel::entityData ( const Collection &  collection,
int  column,
int  role = Qt::DisplayRole 
) const [protected, virtual]

Provided for convenience of subclasses.

Definition at line 141 of file entitytreemodel.cpp.

QVariant EntityTreeModel::entityData ( const Item &  item,
int  column,
int  role = Qt::DisplayRole 
) const [protected, virtual]

Provided for convenience of subclasses.

Definition at line 115 of file entitytreemodel.cpp.

QVariant EntityTreeModel::entityHeaderData ( int  section,
Qt::Orientation  orientation,
int  role,
HeaderGroup  headerGroup 
) const [protected, virtual]

Reimplement this to provide different header data.

This is needed when using one model with multiple proxies and views, and each should show different header data.

Definition at line 603 of file entitytreemodel.cpp.

bool EntityTreeModel::entityMatch ( const Collection &  collection,
const QVariant &  value,
Qt::MatchFlags  flags 
) const [protected, virtual]

Reimplement this in a subclass to return true if collection matches value with flags in the AmazingCompletionRole.

Definition at line 837 of file entitytreemodel.cpp.

bool EntityTreeModel::entityMatch ( const Item &  item,
const QVariant &  value,
Qt::MatchFlags  flags 
) const [protected, virtual]

Reimplement this in a subclass to return true if item matches value with flags in the AmazingCompletionRole.

Definition at line 829 of file entitytreemodel.cpp.

bool EntityTreeModel::includeRootCollection (  )  const

Returns whether the root collection is provided by the model.

Definition at line 1001 of file entitytreemodel.cpp.

EntityTreeModel::ItemPopulationStrategy EntityTreeModel::itemPopulationStrategy (  )  const

Returns the item population strategy of the model.

Definition at line 987 of file entitytreemodel.cpp.

QModelIndexList EntityTreeModel::match ( const QModelIndex &  start,
int  role,
const QVariant &  value,
int  hits = 1,
Qt::MatchFlags  flags = Qt::MatchFlags( Qt::MatchStartsWith | Qt::MatchWrap ) 
) const [virtual]

Reimplemented to handle the AmazingCompletionRole.

Definition at line 845 of file entitytreemodel.cpp.

QString EntityTreeModel::rootCollectionDisplayName (  )  const

Returns the display name of the root collection.

Definition at line 1015 of file entitytreemodel.cpp.

void EntityTreeModel::setCollectionFetchStrategy ( CollectionFetchStrategy  strategy  ) 

Sets the collection fetch strategy of the model.

Definition at line 1021 of file entitytreemodel.cpp.

void EntityTreeModel::setIncludeRootCollection ( bool  include  ) 

Sets whether the root collection shall be provided by the model.

See also:
setRootCollectionDisplayName()

Definition at line 993 of file entitytreemodel.cpp.

void EntityTreeModel::setItemPopulationStrategy ( ItemPopulationStrategy  strategy  ) 

Sets the item population strategy of the model.

Definition at line 960 of file entitytreemodel.cpp.

void EntityTreeModel::setRootCollectionDisplayName ( const QString &  name  ) 

Sets the display name of the root collection of the model.

The default display name is "[*]".

Note:
The display name for the root collection is only used if the root collection has been included with setIncludeRootCollection().

Definition at line 1007 of file entitytreemodel.cpp.

void EntityTreeModel::setShowSystemEntities ( bool  show  ) 

Some Entities are hidden in the model, but exist for internal purposes, for example, custom object directories in groupware resources.

They are hidden by default, but can be shown by setting show to true.

Most applications will not need to use this feature.

Definition at line 92 of file entitytreemodel.cpp.

bool EntityTreeModel::systemEntitiesShown (  )  const

Returns true if internal system entities are shown, and false otherwise.

Definition at line 86 of file entitytreemodel.cpp.


The documentation for this class was generated from the following files:
  • entitytreemodel.h
  • entitytreemodel.cpp

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.6.2-20100208
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal