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

akonadi

entitylistview.cpp

00001 /*
00002     Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
00003     Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
00004     Copyright (c) 2009 Kevin Ottens <ervin@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "entitylistview.h"
00023 
00024 #include "dragdropmanager_p.h"
00025 
00026 #include <QtCore/QDebug>
00027 #include <QtCore/QTimer>
00028 #include <QtGui/QApplication>
00029 #include <QtGui/QDragMoveEvent>
00030 #include <QtGui/QHeaderView>
00031 #include <QtGui/QMenu>
00032 
00033 #include <KAction>
00034 #include <KLocale>
00035 #include <KMessageBox>
00036 #include <KUrl>
00037 #include <KXMLGUIFactory>
00038 
00039 #include <kdebug.h>
00040 #include <kxmlguiclient.h>
00041 
00042 #include <akonadi/collection.h>
00043 #include <akonadi/control.h>
00044 #include <akonadi/item.h>
00045 #include <akonadi/entitytreemodel.h>
00046 
00047 using namespace Akonadi;
00048 
00052 class EntityListView::Private
00053 {
00054 public:
00055   Private( EntityListView *parent )
00056       : mParent( parent ), mDragDropManager( new DragDropManager( mParent ) ), mXmlGuiClient( 0 )
00057   {
00058   }
00059 
00060   void init();
00061   void itemClicked( const QModelIndex& );
00062   void itemDoubleClicked( const QModelIndex& );
00063   void itemCurrentChanged( const QModelIndex& );
00064 
00065   EntityListView *mParent;
00066   DragDropManager *mDragDropManager;
00067   KXMLGUIClient *mXmlGuiClient;
00068 };
00069 
00070 void EntityListView::Private::init()
00071 {
00072   mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
00073   mParent->setAcceptDrops( true );
00074   mParent->setDropIndicatorShown( true );
00075   mParent->setDragDropMode( DragDrop );
00076   mParent->setDragEnabled( true );
00077 
00078   mParent->connect( mParent, SIGNAL( clicked( const QModelIndex& ) ),
00079                     mParent, SLOT( itemClicked( const QModelIndex& ) ) );
00080   mParent->connect( mParent, SIGNAL( doubleClicked( const QModelIndex& ) ),
00081                     mParent, SLOT( itemDoubleClicked( const QModelIndex& ) ) );
00082 
00083   Control::widgetNeedsAkonadi( mParent );
00084 }
00085 
00086 void EntityListView::Private::itemClicked( const QModelIndex &index )
00087 {
00088   if ( !index.isValid() )
00089     return;
00090 
00091   const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00092   if ( collection.isValid() ) {
00093     emit mParent->clicked( collection );
00094   } else {
00095     const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00096     if ( item.isValid() )
00097       emit mParent->clicked( item );
00098   }
00099 }
00100 
00101 void EntityListView::Private::itemDoubleClicked( const QModelIndex &index )
00102 {
00103   if ( !index.isValid() )
00104     return;
00105 
00106   const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00107   if ( collection.isValid() ) {
00108     emit mParent->doubleClicked( collection );
00109   } else {
00110     const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00111     if ( item.isValid() )
00112       emit mParent->doubleClicked( item );
00113   }
00114 }
00115 
00116 void EntityListView::Private::itemCurrentChanged( const QModelIndex &index )
00117 {
00118   if ( !index.isValid() )
00119     return;
00120 
00121   const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00122   if ( collection.isValid() ) {
00123     emit mParent->currentChanged( collection );
00124   } else {
00125     const Item item = index.model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
00126     if ( item.isValid() )
00127       emit mParent->currentChanged( item );
00128   }
00129 }
00130 
00131 EntityListView::EntityListView( QWidget * parent )
00132   : QListView( parent ),
00133     d( new Private( this ) )
00134 {
00135   setSelectionMode( QAbstractItemView::SingleSelection );
00136   d->init();
00137 }
00138 
00139 EntityListView::EntityListView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
00140   : QListView( parent ),
00141     d( new Private( this ) )
00142 {
00143   d->mXmlGuiClient = xmlGuiClient;
00144   d->init();
00145 }
00146 
00147 EntityListView::~EntityListView()
00148 {
00149   delete d->mDragDropManager;
00150   delete d;
00151 }
00152 
00153 void EntityListView::setModel( QAbstractItemModel * model )
00154 {
00155   if ( selectionModel() ) {
00156     disconnect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00157            this, SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00158   }
00159 
00160   QListView::setModel( model );
00161 
00162   connect( selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
00163            SLOT( itemCurrentChanged( const QModelIndex& ) ) );
00164 }
00165 
00166 void EntityListView::dragMoveEvent( QDragMoveEvent * event )
00167 {
00168   if ( d->mDragDropManager->dropAllowed( event ) ) {
00169     // All urls are supported. process the event.
00170     QListView::dragMoveEvent( event );
00171     return;
00172   }
00173 
00174   event->setDropAction( Qt::IgnoreAction );
00175 }
00176 
00177 void EntityListView::dropEvent( QDropEvent * event )
00178 {
00179   if ( d->mDragDropManager->processDropEvent( event ) ) {
00180     QListView::dropEvent( event );
00181   }
00182 }
00183 
00184 void EntityListView::contextMenuEvent( QContextMenuEvent * event )
00185 {
00186   if ( !d->mXmlGuiClient )
00187     return;
00188 
00189   const QModelIndex index = indexAt( event->pos() );
00190 
00191   QMenu *popup = 0;
00192 
00193   // check if the index under the cursor is a collection or item
00194   const Collection collection = model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00195   if ( collection.isValid() ) {
00196     popup = static_cast<QMenu*>( d->mXmlGuiClient->factory()->container(
00197                                  QLatin1String( "akonadi_favoriteview_contextmenu" ), d->mXmlGuiClient ) );
00198     if ( popup )
00199       popup->exec( event->globalPos() );
00200   }
00201 }
00202 
00203 void EntityListView::setXmlGuiClient( KXMLGUIClient *xmlGuiClient )
00204 {
00205   d->mXmlGuiClient = xmlGuiClient;
00206 }
00207 
00208 void EntityListView::startDrag( Qt::DropActions supportedActions )
00209 {
00210   d->mDragDropManager->startDrag( supportedActions );
00211 }
00212 
00213 #include "entitylistview.moc"

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