• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 <QApplication>
37 #include <QDragMoveEvent>
38 #include <QHeaderView>
39 #include <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 
98  idx2 = idx2.parent();
99  }
100  return false;
101 }
102 
103 void CollectionView::Private::dragExpand()
104 {
105  mParent->setExpanded( dragOverIndex, true );
106  dragOverIndex = QModelIndex();
107 }
108 
109 void CollectionView::Private::itemClicked( const QModelIndex &index )
110 {
111  if ( !index.isValid() ) {
112  return;
113  }
114 
115  const Collection collection = index.model()->data( index, CollectionModel::CollectionRole ).value<Collection>();
116  if ( !collection.isValid() ) {
117  return;
118  }
119 
120  emit mParent->clicked( collection );
121 }
122 
123 void CollectionView::Private::itemCurrentChanged( const QModelIndex &index )
124 {
125  if ( !index.isValid() ) {
126  return;
127  }
128 
129  const Collection collection = index.model()->data( index, CollectionModel::CollectionRole ).value<Collection>();
130  if ( !collection.isValid() ) {
131  return;
132  }
133 
134  emit mParent->currentChanged( collection );
135 }
136 
137 CollectionView::CollectionView( QWidget * parent )
138  : QTreeView( parent ),
139  d( new Private( this ) )
140 {
141  d->init();
142 }
143 
144 CollectionView::CollectionView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
145  : QTreeView( parent ),
146  d( new Private( this ) )
147 {
148  d->xmlGuiClient = xmlGuiClient;
149  d->init();
150 }
151 
152 CollectionView::CollectionView( KXmlGuiWindow *xmlGuiWindow, QWidget * parent )
153  : QTreeView( parent ),
154  d( new Private( this ) )
155 {
156  d->xmlGuiClient = static_cast<KXMLGUIClient*>( xmlGuiWindow );
157  d->init();
158 }
159 
160 CollectionView::~CollectionView()
161 {
162  delete d;
163 }
164 
165 void CollectionView::setModel( QAbstractItemModel * model )
166 {
167  QTreeView::setModel( model );
168  header()->setStretchLastSection( true );
169 
170  connect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
171  this, SLOT(itemCurrentChanged(QModelIndex)) );
172 }
173 
174 void CollectionView::dragMoveEvent( QDragMoveEvent * event )
175 {
176  QModelIndex index = indexAt( event->pos() );
177  if ( d->dragOverIndex != index ) {
178  d->dragExpandTimer.stop();
179  if ( index.isValid() && !isExpanded( index ) && itemsExpandable() ) {
180  d->dragExpandTimer.start( QApplication::startDragTime() );
181  d->dragOverIndex = index;
182  }
183  }
184 
185  // Check if the collection under the cursor accepts this data type
186  const QStringList supportedContentTypes = model()->data( index, CollectionModel::CollectionRole ).value<Collection>().contentMimeTypes();
187  const QMimeData *mimeData = event->mimeData();
188  const KUrl::List urls = KUrl::List::fromMimeData( mimeData );
189  foreach ( const KUrl &url, urls ) {
190 
191  const Collection collection = Collection::fromUrl( url );
192  if ( collection.isValid() ) {
193  if ( !supportedContentTypes.contains( QString::fromLatin1( "inode/directory" ) ) ) {
194  break;
195  }
196 
197  // Check if we don't try to drop on one of the children
198  if ( d->hasParent( index, collection.id() ) ) {
199  break;
200  }
201  } else {
202  const QString type = url.queryItems()[ QString::fromLatin1( "type" ) ];
203  if ( !supportedContentTypes.contains( type ) ) {
204  break;
205  }
206  }
207 
208  QTreeView::dragMoveEvent( event );
209  return;
210  }
211 
212  event->setDropAction( Qt::IgnoreAction );
213 }
214 
215 void CollectionView::dragLeaveEvent( QDragLeaveEvent * event )
216 {
217  d->dragExpandTimer.stop();
218  d->dragOverIndex = QModelIndex();
219  QTreeView::dragLeaveEvent( event );
220 }
221 
222 void CollectionView::dropEvent( QDropEvent * event )
223 {
224  d->dragExpandTimer.stop();
225  d->dragOverIndex = QModelIndex();
226 
227  // open a context menu offering different drop actions (move, copy and cancel)
228  // TODO If possible, hide non available actions ...
229  QMenu popup( this );
230  QAction* moveDropAction = popup.addAction( KIcon( QString::fromLatin1( "edit-rename" ) ), i18n( "&Move here" ) );
231  QAction* copyDropAction = popup.addAction( KIcon( QString::fromLatin1( "edit-copy" ) ), i18n( "&Copy here" ) );
232  popup.addSeparator();
233  popup.addAction( KIcon( QString::fromLatin1( "process-stop" ) ), i18n( "Cancel" ) );
234 
235  QAction *activatedAction = popup.exec( QCursor::pos() );
236  if ( activatedAction == moveDropAction ) {
237  event->setDropAction( Qt::MoveAction );
238  } else if ( activatedAction == copyDropAction ) {
239  event->setDropAction( Qt::CopyAction );
240  } else {
241  return;
242  }
243 
244  QTreeView::dropEvent( event );
245 }
246 
247 void CollectionView::contextMenuEvent( QContextMenuEvent * event )
248 {
249  if ( !d->xmlGuiClient ) {
250  return;
251  }
252  QMenu *popup = static_cast<QMenu*>( d->xmlGuiClient->factory()->container(
253  QLatin1String( "akonadi_collectionview_contextmenu" ), d->xmlGuiClient ) );
254  if ( popup ) {
255  popup->exec( event->globalPos() );
256  }
257 }
258 
259 void CollectionView::setXmlGuiClient( KXMLGUIClient * xmlGuiClient )
260 {
261  d->xmlGuiClient = xmlGuiClient;
262 }
263 
264 void CollectionView::setXmlGuiWindow( KXmlGuiWindow * xmlGuiWindow )
265 {
266  d->xmlGuiClient = static_cast<KXMLGUIClient*>( xmlGuiWindow );
267 }
268 
269 #include "moc_collectionview.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:33 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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