• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
collectionview.cpp
1 /*
2  Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "collectionview.h"
21 
22 #include "collection.h"
23 #include "collectionmodel.h"
24 #include "control.h"
25 
26 #include <kaction.h>
27 #include <kdebug.h>
28 #include <klocale.h>
29 #include <kmessagebox.h>
30 #include <kurl.h>
31 #include <kxmlguifactory.h>
32 #include <kxmlguiwindow.h>
33 
34 #include <QtCore/QDebug>
35 #include <QtCore/QTimer>
36 #include <QtGui/QApplication>
37 #include <QtGui/QDragMoveEvent>
38 #include <QtGui/QHeaderView>
39 #include <QtGui/QMenu>
40 
41 using namespace Akonadi;
42 
46 class CollectionView::Private
47 {
48  public:
49  Private( CollectionView *parent )
50  : mParent( parent ),
51  xmlGuiClient( 0 )
52  {
53  }
54 
55  void init();
56  void dragExpand();
57  void itemClicked( const QModelIndex& );
58  void itemCurrentChanged( const QModelIndex& );
59  bool hasParent( const QModelIndex& idx, Collection::Id parentId );
60 
61  CollectionView *mParent;
62  QModelIndex dragOverIndex;
63  QTimer dragExpandTimer;
64 
65  KXMLGUIClient *xmlGuiClient;
66 };
67 
68 void CollectionView::Private::init()
69 {
70  mParent->header()->setClickable( true );
71  mParent->header()->setStretchLastSection( false );
72 
73  mParent->setSortingEnabled( true );
74  mParent->sortByColumn( 0, Qt::AscendingOrder );
75  mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
76  mParent->setAcceptDrops( true );
77  mParent->setDropIndicatorShown( true );
78  mParent->setDragDropMode( DragDrop );
79  mParent->setDragEnabled( true );
80 
81  dragExpandTimer.setSingleShot( true );
82  mParent->connect( &dragExpandTimer, SIGNAL(timeout()), SLOT(dragExpand()) );
83 
84  mParent->connect( mParent, SIGNAL(clicked(QModelIndex)),
85  mParent, SLOT(itemClicked(QModelIndex)) );
86 
87  Control::widgetNeedsAkonadi( mParent );
88 }
89 
90 bool CollectionView::Private::hasParent( const QModelIndex& idx, Collection::Id parentId )
91 {
92  QModelIndex idx2 = idx;
93  while ( idx2.isValid() ) {
94  if ( mParent->model()->data( idx2, CollectionModel::CollectionIdRole).toLongLong() == parentId )
95  return true;
96 
97  idx2 = idx2.parent();
98  }
99  return false;
100 }
101 
102 void CollectionView::Private::dragExpand()
103 {
104  mParent->setExpanded( dragOverIndex, true );
105  dragOverIndex = QModelIndex();
106 }
107 
108 void CollectionView::Private::itemClicked( const QModelIndex &index )
109 {
110  if ( !index.isValid() )
111  return;
112 
113  const Collection collection = index.model()->data( index, CollectionModel::CollectionRole ).value<Collection>();
114  if ( !collection.isValid() )
115  return;
116 
117  emit mParent->clicked( collection );
118 }
119 
120 void CollectionView::Private::itemCurrentChanged( const QModelIndex &index )
121 {
122  if ( !index.isValid() )
123  return;
124 
125  const Collection collection = index.model()->data( index, CollectionModel::CollectionRole ).value<Collection>();
126  if ( !collection.isValid() )
127  return;
128 
129  emit mParent->currentChanged( collection );
130 }
131 
132 CollectionView::CollectionView( QWidget * parent )
133  : QTreeView( parent ),
134  d( new Private( this ) )
135 {
136  d->init();
137 }
138 
139 CollectionView::CollectionView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
140  : QTreeView( parent ),
141  d( new Private( this ) )
142 {
143  d->xmlGuiClient = xmlGuiClient;
144  d->init();
145 }
146 
147 CollectionView::CollectionView( KXmlGuiWindow *xmlGuiWindow, QWidget * parent )
148  : QTreeView( parent ),
149  d( new Private( this ) )
150 {
151  d->xmlGuiClient = static_cast<KXMLGUIClient*>( xmlGuiWindow );
152  d->init();
153 }
154 
155 CollectionView::~CollectionView()
156 {
157  delete d;
158 }
159 
160 void CollectionView::setModel( QAbstractItemModel * model )
161 {
162  QTreeView::setModel( model );
163  header()->setStretchLastSection( true );
164 
165  connect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
166  this, SLOT(itemCurrentChanged(QModelIndex)) );
167 }
168 
169 void CollectionView::dragMoveEvent( QDragMoveEvent * event )
170 {
171  QModelIndex index = indexAt( event->pos() );
172  if ( d->dragOverIndex != index ) {
173  d->dragExpandTimer.stop();
174  if ( index.isValid() && !isExpanded( index ) && itemsExpandable() ) {
175  d->dragExpandTimer.start( QApplication::startDragTime() );
176  d->dragOverIndex = index;
177  }
178  }
179 
180  // Check if the collection under the cursor accepts this data type
181  const QStringList supportedContentTypes = model()->data( index, CollectionModel::CollectionRole ).value<Collection>().contentMimeTypes();
182  const QMimeData *mimeData = event->mimeData();
183  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
184  foreach ( const KUrl &url, urls ) {
185 
186  const Collection collection = Collection::fromUrl( url );
187  if ( collection.isValid() ) {
188  if ( !supportedContentTypes.contains( QString::fromLatin1( "inode/directory" ) ) )
189  break;
190 
191  // Check if we don't try to drop on one of the children
192  if ( d->hasParent( index, collection.id() ) )
193  break;
194  } else {
195  const QString type = url.queryItems()[ QString::fromLatin1( "type" ) ];
196  if ( !supportedContentTypes.contains( type ) )
197  break;
198  }
199 
200  QTreeView::dragMoveEvent( event );
201  return;
202  }
203 
204  event->setDropAction( Qt::IgnoreAction );
205 }
206 
207 void CollectionView::dragLeaveEvent( QDragLeaveEvent * event )
208 {
209  d->dragExpandTimer.stop();
210  d->dragOverIndex = QModelIndex();
211  QTreeView::dragLeaveEvent( event );
212 }
213 
214 void CollectionView::dropEvent( QDropEvent * event )
215 {
216  d->dragExpandTimer.stop();
217  d->dragOverIndex = QModelIndex();
218 
219  // open a context menu offering different drop actions (move, copy and cancel)
220  // TODO If possible, hide non available actions ...
221  QMenu popup( this );
222  QAction* moveDropAction = popup.addAction( KIcon( QString::fromLatin1( "edit-rename" ) ), i18n( "&Move here" ) );
223  QAction* copyDropAction = popup.addAction( KIcon( QString::fromLatin1( "edit-copy" ) ), i18n( "&Copy here" ) );
224  popup.addSeparator();
225  popup.addAction( KIcon( QString::fromLatin1( "process-stop" ) ), i18n( "Cancel" ) );
226 
227  QAction *activatedAction = popup.exec( QCursor::pos() );
228  if ( activatedAction == moveDropAction ) {
229  event->setDropAction( Qt::MoveAction );
230  } else if ( activatedAction == copyDropAction ) {
231  event->setDropAction( Qt::CopyAction );
232  } else {
233  return;
234  }
235 
236  QTreeView::dropEvent( event );
237 }
238 
239 void CollectionView::contextMenuEvent( QContextMenuEvent * event )
240 {
241  if ( !d->xmlGuiClient )
242  return;
243  QMenu *popup = static_cast<QMenu*>( d->xmlGuiClient->factory()->container(
244  QLatin1String( "akonadi_collectionview_contextmenu" ), d->xmlGuiClient ) );
245  if ( popup )
246  popup->exec( event->globalPos() );
247 }
248 
249 void CollectionView::setXmlGuiClient( KXMLGUIClient * xmlGuiClient )
250 {
251  d->xmlGuiClient = xmlGuiClient;
252 }
253 
254 void CollectionView::setXmlGuiWindow( KXmlGuiWindow * xmlGuiWindow )
255 {
256  d->xmlGuiClient = static_cast<KXMLGUIClient*>( xmlGuiWindow );
257 }
258 
259 #include "collectionview.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:04 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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