~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
    Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
    Copyright (c) 2008 Stephen Kelly <steveire@gmail.com>
    Copyright (c) 2012 Laurent Montel <montel@kde.org>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "entitytreeview.h"

#include "dragdropmanager_p.h"

#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QDragMoveEvent>
#include <QtGui/QHeaderView>
#include <QtGui/QMenu>

#include <KAction>
#include <KLocale>
#include <KMessageBox>
#include <KUrl>
#include <KXMLGUIFactory>

#include <akonadi/collection.h>
#include <akonadi/control.h>
#include <akonadi/item.h>
#include <akonadi/entitytreemodel.h>

#include <kdebug.h>
#include <kxmlguiclient.h>

#include "progressspinnerdelegate_p.h"

using namespace Akonadi;

/**
 * @internal
 */
class EntityTreeView::Private
{
public:
  Private( EntityTreeView *parent )
      : mParent( parent )
#ifndef QT_NO_DRAGANDDROP
      , mDragDropManager( new DragDropManager( mParent ) )
#endif
      , mXmlGuiClient( 0 )
      , mDefaultPopupMenu( QLatin1String( "akonadi_collectionview_contextmenu" ) )
  {
  }

  void init();
  void itemClicked( const QModelIndex& );
  void itemDoubleClicked( const QModelIndex& );
  void itemCurrentChanged( const QModelIndex& );

  void slotSelectionChanged( const QItemSelection & selected, const QItemSelection & deselected );

  EntityTreeView *mParent;
  QBasicTimer mDragExpandTimer;
  DragDropManager *mDragDropManager;
  KXMLGUIClient *mXmlGuiClient;
  QString mDefaultPopupMenu;
};

void EntityTreeView::Private::init()
{
  Akonadi::DelegateAnimator *animator = new Akonadi::DelegateAnimator(mParent);
  Akonadi::ProgressSpinnerDelegate *customDelegate = new Akonadi::ProgressSpinnerDelegate(animator, mParent);
  mParent->setItemDelegate(customDelegate);

  mParent->header()->setClickable( true );
  mParent->header()->setStretchLastSection( false );
//   mParent->setRootIsDecorated( false );

  // QTreeView::autoExpandDelay has very strange behaviour. It toggles the collapse/expand state
  // of the item the cursor is currently over when a timer event fires.
  // The behaviour we want is to expand a collapsed row on drag-over, but not collapse it.
  // mDragExpandTimer is used to achieve this.
//   mParent->setAutoExpandDelay ( QApplication::startDragTime() );

  mParent->setSortingEnabled( true );
  mParent->sortByColumn( 0, Qt::AscendingOrder );
  mParent->setEditTriggers( QAbstractItemView::EditKeyPressed );
  mParent->setAcceptDrops( true );
#ifndef QT_NO_DRAGANDDROP
  mParent->setDropIndicatorShown( true );
  mParent->setDragDropMode( DragDrop );
  mParent->setDragEnabled( true );
#endif

  mParent->connect( mParent, SIGNAL(clicked(QModelIndex)),
                    mParent, SLOT(itemClicked(QModelIndex)) );
  mParent->connect( mParent, SIGNAL(doubleClicked(QModelIndex)),
                    mParent, SLOT(itemDoubleClicked(QModelIndex)) );

  Control::widgetNeedsAkonadi( mParent );
}

void EntityTreeView::Private::slotSelectionChanged( const QItemSelection & selected, const QItemSelection& )
{
  const int column = 0;
  foreach ( const QItemSelectionRange &range, selected ) {
    const QModelIndex index = range.topLeft();

    if ( index.column() > 0 )
      continue;

    for ( int row = index.row(); row <= range.bottomRight().row(); ++row ) {
      // Don't use canFetchMore here. We need to bypass the check in
      // the EntityFilterModel when it shows only collections.
      mParent->model()->fetchMore( index.sibling( row, column ) );
    }
  }

  if ( selected.size() == 1 ) {
    const QItemSelectionRange &range = selected.first();
    if ( range.topLeft().row() == range.bottomRight().row() )
      mParent->scrollTo( range.topLeft(), QTreeView::EnsureVisible );
  }
}

void EntityTreeView::Private::itemClicked( const QModelIndex &index )
{
  if ( !index.isValid() )
    return;
  QModelIndex idx = index.sibling( index.row(), 0);

  const Collection collection = idx.model()->data( idx, EntityTreeModel::CollectionRole ).value<Collection>();
  if ( collection.isValid() ) {
    emit mParent->clicked( collection );
  } else {
    const Item item = idx.model()->data( idx, EntityTreeModel::ItemRole ).value<Item>();
    if ( item.isValid() )
      emit mParent->clicked( item );
  }
}

void EntityTreeView::Private::itemDoubleClicked( const QModelIndex &index )
{
  if ( !index.isValid() )
    return;
  QModelIndex idx = index.sibling( index.row(), 0);
  const Collection collection = idx.model()->data( idx, EntityTreeModel::CollectionRole ).value<Collection>();
  if ( collection.isValid() ) {
    emit mParent->doubleClicked( collection );
  } else {
    const Item item = idx.model()->data( idx, EntityTreeModel::ItemRole ).value<Item>();
    if ( item.isValid() )
      emit mParent->doubleClicked( item );
  }
}

void EntityTreeView::Private::itemCurrentChanged( const QModelIndex &index )
{
  if ( !index.isValid() )
    return;
  QModelIndex idx = index.sibling( index.row(), 0);
  const Collection collection = idx.model()->data( idx, EntityTreeModel::CollectionRole ).value<Collection>();
  if ( collection.isValid() ) {
    emit mParent->currentChanged( collection );
  } else {
    const Item item = idx.model()->data( idx, EntityTreeModel::ItemRole ).value<Item>();
    if ( item.isValid() )
      emit mParent->currentChanged( item );
  }
}

EntityTreeView::EntityTreeView( QWidget * parent )
  : QTreeView( parent ),
    d( new Private( this ) )
{
  setSelectionMode( QAbstractItemView::SingleSelection );
  d->init();
}

EntityTreeView::EntityTreeView( KXMLGUIClient *xmlGuiClient, QWidget * parent )
  : QTreeView( parent ),
    d( new Private( this ) )
{
  d->mXmlGuiClient = xmlGuiClient;
  d->init();
}

EntityTreeView::~EntityTreeView()
{
  delete d->mDragDropManager;
  delete d;
}

void EntityTreeView::setModel( QAbstractItemModel * model )
{
  if ( selectionModel() ) {
    disconnect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
           this, SLOT(itemCurrentChanged(QModelIndex)) );

    disconnect( selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
           this, SLOT(slotSelectionChanged(QItemSelection,QItemSelection)) );
  }

  QTreeView::setModel( model );
  header()->setStretchLastSection( true );

  connect( selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
           SLOT(itemCurrentChanged(QModelIndex)) );

  connect( selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
           SLOT(slotSelectionChanged(QItemSelection,QItemSelection)) );
}


void EntityTreeView::timerEvent( QTimerEvent *event )
{
  if ( event->timerId() == d->mDragExpandTimer.timerId() ) {
    const QPoint pos = viewport()->mapFromGlobal( QCursor::pos() );
    if ( state() == QAbstractItemView::DraggingState && viewport()->rect().contains( pos ) )
      setExpanded( indexAt( pos ), true );
  }

  QTreeView::timerEvent( event );
}

#ifndef QT_NO_DRAGANDDROP
void EntityTreeView::dragMoveEvent( QDragMoveEvent * event )
{
  d->mDragExpandTimer.start( QApplication::startDragTime() , this );

  if ( d->mDragDropManager->dropAllowed( event ) ) {
    // All urls are supported. process the event.
    QTreeView::dragMoveEvent( event );
    return;
  }

  event->setDropAction( Qt::IgnoreAction );
}

void EntityTreeView::dropEvent( QDropEvent * event )
{
  d->mDragExpandTimer.stop();
  bool menuCanceled = false;
  if ( d->mDragDropManager->processDropEvent( event, menuCanceled, ( dropIndicatorPosition () == QAbstractItemView::OnItem ) ) )
    QTreeView::dropEvent( event );
}
#endif

#ifndef QT_NO_CONTEXTMENU
void EntityTreeView::contextMenuEvent( QContextMenuEvent * event )
{
  if ( !d->mXmlGuiClient || !model())
    return;

  const QModelIndex index = indexAt( event->pos() );
  QString popupName = d->mDefaultPopupMenu;

  if ( index.isValid() ) {				// popup not over empty space
    // check whether the index under the cursor is a collection or item
    const Item item = model()->data( index, EntityTreeModel::ItemRole ).value<Item>();
    popupName = ( item.isValid() ? QLatin1String( "akonadi_itemview_contextmenu" ) :
                                   QLatin1String( "akonadi_collectionview_contextmenu" ) );
  }

  QMenu *popup = static_cast<QMenu*>( d->mXmlGuiClient->factory()->container( popupName,
                                                                              d->mXmlGuiClient ) );
  if ( popup )
    popup->exec( event->globalPos() );
}
#endif

void EntityTreeView::setXmlGuiClient( KXMLGUIClient * xmlGuiClient )
{
  d->mXmlGuiClient = xmlGuiClient;
}

#ifndef QT_NO_DRAGANDDROP
void EntityTreeView::startDrag( Qt::DropActions supportedActions )
{
  d->mDragDropManager->startDrag( supportedActions );
}
#endif


void EntityTreeView::setDropActionMenuEnabled( bool enabled )
{
#ifndef QT_NO_DRAGANDDROP
  d->mDragDropManager->setShowDropActionMenu( enabled );
#endif
}

bool EntityTreeView::isDropActionMenuEnabled() const
{
#ifndef QT_NO_DRAGANDDROP
  return d->mDragDropManager->showDropActionMenu();
#else
  return false;
#endif
}

void EntityTreeView::setManualSortingActive(bool active)
{
#ifndef QT_NO_DRAGANDDROP
  d->mDragDropManager->setManualSortingActive( active );
#endif 
}

bool EntityTreeView::isManualSortingActive() const
{
#ifndef QT_NO_DRAGANDDROP
  return d->mDragDropManager->isManualSortingActive();
#else
  return false;
#endif 
}

void EntityTreeView::setDefaultPopupMenu( const QString &name )
{
  d->mDefaultPopupMenu = name;
}


#include "entitytreeview.moc"