QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsbrowsermodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsbrowsermodel.cpp
3  ---------------------
4  begin : July 2011
5  copyright : (C) 2011 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include <QDir>
16 #include <QApplication>
17 #include <QStyle>
18 #include <QtConcurrentMap>
19 #include <QUrl>
20 
21 #include "qgis.h"
22 #include "qgsapplication.h"
23 #include "qgsdataitemprovider.h"
25 #include "qgsdataprovider.h"
26 #include "qgsmimedatautils.h"
27 #include "qgslogger.h"
28 #include "qgsproviderregistry.h"
29 #include "qgsbrowsermodel.h"
30 #include "qgsproject.h"
31 #include "qgssettings.h"
32 
33 #define PROJECT_HOME_PREFIX "project:"
34 #define HOME_PREFIX "home:"
35 
37  : QFutureWatcher( nullptr )
38  , mItem( item )
39 {
40 }
41 
42 // sort function for QList<QgsDataItem*>, e.g. sorted/grouped provider listings
43 static bool cmpByDataItemName_( QgsDataItem *a, QgsDataItem *b )
44 {
45  return QString::localeAwareCompare( a->name(), b->name() ) < 0;
46 }
47 
49  : QAbstractItemModel( parent )
50 
51 {
53  this, &QgsBrowserModel::dataItemProviderAdded );
55  this, &QgsBrowserModel::dataItemProviderWillBeRemoved );
56 }
57 
59 {
61 }
62 
64 {
65  QString home = QgsProject::instance()->homePath();
66  if ( mProjectHome && mProjectHome->path().mid( QStringLiteral( PROJECT_HOME_PREFIX ).length() ) == home )
67  return;
68 
69  int idx = mRootItems.indexOf( mProjectHome );
70 
71  // using layoutAboutToBeChanged() was messing expanded items
72  if ( idx >= 0 )
73  {
74  beginRemoveRows( QModelIndex(), idx, idx );
75  mRootItems.remove( idx );
76  endRemoveRows();
77  }
78  delete mProjectHome;
79  mProjectHome = home.isNull() ? nullptr : new QgsProjectHomeItem( nullptr, tr( "Project Home" ), home, QStringLiteral( PROJECT_HOME_PREFIX ) + home );
80  if ( mProjectHome )
81  {
82  setupItemConnections( mProjectHome );
83 
84  beginInsertRows( QModelIndex(), 0, 0 );
85  mRootItems.insert( 0, mProjectHome );
86  endInsertRows();
87  }
88 }
89 
91 {
93 
94  // give the home directory a prominent third place
95  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, tr( "Home" ), QDir::homePath(),
96  QStringLiteral( HOME_PREFIX ) + QDir::homePath(),
97  QStringLiteral( "special:Home" ) );
98  item->setSortKey( QStringLiteral( " 2" ) );
99  setupItemConnections( item );
100  mRootItems << item;
101 
102  // add favorite directories
103  mFavorites = new QgsFavoritesItem( nullptr, tr( "Favorites" ) );
104  if ( mFavorites )
105  {
106  setupItemConnections( mFavorites );
108  }
109 
110  // add drives
111  const auto drives { QDir::drives() };
112  for ( const QFileInfo &drive : drives )
113  {
114  const QString path = drive.absolutePath();
115 
116  if ( QgsDirectoryItem::hiddenPath( path ) )
117  continue;
118 
119  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path, path, QStringLiteral( "special:Drives" ) );
120  item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
121  mDriveItems.insert( path, item );
122 
123  setupItemConnections( item );
124  mRootItems << item;
125  }
126 
127 #ifdef Q_OS_MAC
128  QString path = QString( "/Volumes" );
129  QgsDirectoryItem *vols = new QgsDirectoryItem( nullptr, path, path, path, QStringLiteral( "special:Volumes" ) );
130  mRootItems << vols;
131 #endif
132 
133  // container for displaying providers as sorted groups (by QgsDataProvider::DataCapability enum)
134  QMap<int, QgsDataItem *> providerMap;
135 
136  const auto constProviders = QgsApplication::dataItemProviderRegistry()->providers();
137  for ( QgsDataItemProvider *pr : constProviders )
138  {
139  if ( QgsDataItem *item = addProviderRootItem( pr ) )
140  {
141  providerMap.insertMulti( pr->capabilities(), item );
142  }
143  }
144 
145  // add as sorted groups by QgsDataProvider::DataCapability enum
146  const auto constUniqueKeys = providerMap.uniqueKeys();
147  for ( int key : constUniqueKeys )
148  {
149  QList<QgsDataItem *> providerGroup = providerMap.values( key );
150  if ( providerGroup.size() > 1 )
151  {
152  std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
153  }
154 
155  const auto constProviderGroup = providerGroup;
156  for ( QgsDataItem *ditem : constProviderGroup )
157  {
158  mRootItems << ditem;
159  }
160  }
161 }
162 
164 {
165  const auto constMRootItems = mRootItems;
166  for ( QgsDataItem *item : constMRootItems )
167  {
168  delete item;
169  }
170 
171  mRootItems.clear();
172  mDriveItems.clear();
173 }
174 
175 void QgsBrowserModel::dataItemProviderAdded( QgsDataItemProvider *provider )
176 {
177  if ( !mInitialized )
178  return;
179 
180  if ( QgsDataItem *item = addProviderRootItem( provider ) )
181  {
182  beginInsertRows( QModelIndex(), rowCount(), rowCount() );
183  mRootItems << item;
184  endInsertRows();
185  }
186 }
187 
188 void QgsBrowserModel::dataItemProviderWillBeRemoved( QgsDataItemProvider *provider )
189 {
190  const auto constMRootItems = mRootItems;
191  for ( QgsDataItem *item : constMRootItems )
192  {
193  if ( item->providerKey() == provider->name() )
194  {
195  removeRootItem( item );
196  break; // assuming there is max. 1 root item per provider
197  }
198  }
199 }
200 
201 void QgsBrowserModel::onConnectionsChanged( const QString &providerKey )
202 {
203  // refresh the matching provider
204  for ( QgsDataItem *item : qgis::as_const( mRootItems ) )
205  {
206  if ( item->providerKey() == providerKey )
207  {
208  item->refresh();
209  break; // assuming there is max. 1 root item per provider
210  }
211  }
212 
213  emit connectionsChanged( providerKey );
214 }
215 
216 QMap<QString, QgsDirectoryItem *> QgsBrowserModel::driveItems() const
217 {
218  return mDriveItems;
219 }
220 
221 
223 {
224  if ( ! mInitialized )
225  {
227  addRootItems();
228  mInitialized = true;
229  }
230 }
231 
232 Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const
233 {
234  if ( !index.isValid() )
235  return Qt::ItemFlags();
236 
237  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
238 
239  QgsDataItem *ptr = dataItem( index );
240 
241  if ( !ptr )
242  {
243  QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
244  return Qt::ItemFlags();
245  }
246 
247  if ( ptr->hasDragEnabled() )
248  flags |= Qt::ItemIsDragEnabled;
249 
251  if ( ptr->acceptDrop() )
252  flags |= Qt::ItemIsDropEnabled;
254 
255  if ( ptr->capabilities2() & QgsDataItem::Rename )
256  flags |= Qt::ItemIsEditable;
257 
258  return flags;
259 }
260 
261 QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
262 {
263  if ( !index.isValid() )
264  return QVariant();
265 
266  QgsDataItem *item = dataItem( index );
267  if ( !item )
268  {
269  return QVariant();
270  }
271  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
272  {
273  return item->name();
274  }
275  else if ( role == QgsBrowserModel::SortRole )
276  {
277  return item->sortKey();
278  }
279  else if ( role == Qt::ToolTipRole )
280  {
281  return item->toolTip();
282  }
283  else if ( role == Qt::DecorationRole && index.column() == 0 )
284  {
285  return item->icon();
286  }
287  else if ( role == QgsBrowserModel::PathRole )
288  {
289  return item->path();
290  }
291  else if ( role == QgsBrowserModel::CommentRole )
292  {
293  if ( item->type() == QgsDataItem::Layer )
294  {
295  QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
296  return lyrItem->comments();
297  }
298  return QVariant();
299  }
300  else if ( role == QgsBrowserModel::ProviderKeyRole )
301  {
302  return item->providerKey();
303  }
304  else
305  {
306  // unsupported role
307  return QVariant();
308  }
309 }
310 
311 bool QgsBrowserModel::setData( const QModelIndex &index, const QVariant &value, int role )
312 {
313  if ( !index.isValid() )
314  return false;
315 
316 
317  QgsDataItem *item = dataItem( index );
318  if ( !item )
319  {
320  return false;
321  }
322 
323  if ( !( item->capabilities2() & QgsDataItem::Rename ) )
324  return false;
325 
326  switch ( role )
327  {
328  case Qt::EditRole:
329  {
331  return item->rename( value.toString() );
333  }
334  }
335  return false;
336 }
337 
338 QVariant QgsBrowserModel::headerData( int section, Qt::Orientation orientation, int role ) const
339 {
340  Q_UNUSED( section )
341  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
342  {
343  return QVariant( "header" );
344  }
345 
346  return QVariant();
347 }
348 
349 int QgsBrowserModel::rowCount( const QModelIndex &parent ) const
350 {
351  //QgsDebugMsg(QString("isValid = %1 row = %2 column = %3").arg(parent.isValid()).arg(parent.row()).arg(parent.column()));
352 
353  if ( !parent.isValid() )
354  {
355  // root item: its children are top level items
356  return mRootItems.count(); // mRoot
357  }
358  else
359  {
360  // ordinary item: number of its children
361  QgsDataItem *item = dataItem( parent );
362  //if ( item ) QgsDebugMsg(QString("path = %1 rowCount = %2").arg(item->path()).arg(item->rowCount()) );
363  return item ? item->rowCount() : 0;
364  }
365 }
366 
367 bool QgsBrowserModel::hasChildren( const QModelIndex &parent ) const
368 {
369  if ( !parent.isValid() )
370  return !mRootItems.isEmpty(); // root item: its children are top level items
371 
372  QgsDataItem *item = dataItem( parent );
373  return item && item->hasChildren();
374 }
375 
376 int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
377 {
378  Q_UNUSED( parent )
379  return 1;
380 }
381 
382 QModelIndex QgsBrowserModel::findPath( const QString &path, Qt::MatchFlag matchFlag )
383 {
384  return findPath( this, path, matchFlag );
385 }
386 
387 QModelIndex QgsBrowserModel::findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag )
388 {
389  if ( !model )
390  return QModelIndex();
391 
392  QModelIndex index; // starting from root
393  bool foundChild = true;
394 
395  while ( foundChild )
396  {
397  foundChild = false; // assume that the next child item will not be found
398 
399  for ( int i = 0; i < model->rowCount( index ); i++ )
400  {
401  QModelIndex idx = model->index( i, 0, index );
402 
403  QString itemPath = model->data( idx, PathRole ).toString();
404  if ( itemPath == path )
405  {
406  QgsDebugMsgLevel( "Arrived " + itemPath, 4 );
407  return idx; // we have found the item we have been looking for
408  }
409 
410  // paths are slash separated identifier
411  if ( path.startsWith( itemPath + '/' ) )
412  {
413  foundChild = true;
414  index = idx;
415  break;
416  }
417  }
418  }
419 
420  if ( matchFlag == Qt::MatchStartsWith )
421  return index;
422 
423  QgsDebugMsgLevel( QStringLiteral( "path not found" ), 4 );
424  return QModelIndex(); // not found
425 }
426 
427 QModelIndex QgsBrowserModel::findUri( const QString &uri, QModelIndex index )
428 {
429  for ( int i = 0; i < this->rowCount( index ); i++ )
430  {
431  QModelIndex idx = this->index( i, 0, index );
432 
433  if ( qobject_cast<QgsLayerItem *>( dataItem( idx ) ) )
434  {
435  QString itemUri = qobject_cast<QgsLayerItem *>( dataItem( idx ) )->uri();
436 
437  if ( itemUri == uri )
438  {
439  QgsDebugMsgLevel( "Arrived " + itemUri, 4 );
440  return idx; // we have found the item we have been looking for
441  }
442  }
443 
444  QModelIndex childIdx = findUri( uri, idx );
445  if ( childIdx.isValid() )
446  return childIdx;
447  }
448  return QModelIndex();
449 }
450 
452 {
453  // deprecated, no use
454 }
455 
457 {
458  // TODO: put items creating currently children in threads to deleteLater (does not seem urget because reload() is not used in QGIS)
459  beginResetModel();
460  removeRootItems();
461  addRootItems();
462  endResetModel();
463 }
464 
466 {
467  const QList< QFileInfo > drives = QDir::drives();
468  // remove any removed drives
469  const QStringList existingDrives = mDriveItems.keys();
470  for ( const QString &drivePath : existingDrives )
471  {
472  bool stillExists = false;
473  for ( const QFileInfo &drive : drives )
474  {
475  if ( drivePath == drive.absolutePath() )
476  {
477  stillExists = true;
478  break;
479  }
480  }
481 
482  if ( stillExists )
483  continue;
484 
485  // drive has been removed, remove corresponding item
486  if ( QgsDirectoryItem *driveItem = mDriveItems.value( drivePath ) )
487  removeRootItem( driveItem );
488  }
489 
490  for ( const QFileInfo &drive : drives )
491  {
492  const QString path = drive.absolutePath();
493 
494  if ( QgsDirectoryItem::hiddenPath( path ) )
495  continue;
496 
497  // does an item for this drive already exist?
498  if ( !mDriveItems.contains( path ) )
499  {
500  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path, path, QStringLiteral( "special:Drives" ) );
501  item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
502 
503  mDriveItems.insert( path, item );
504  setupItemConnections( item );
505 
506  beginInsertRows( QModelIndex(), mRootItems.count(), mRootItems.count() );
507  mRootItems << item;
508  endInsertRows();
509  }
510  }
511 }
512 
513 QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
514 {
515  if ( column < 0 || column >= columnCount() || row < 0 )
516  return QModelIndex();
517 
518  QgsDataItem *p = dataItem( parent );
519  const QVector<QgsDataItem *> &items = p ? p->children() : mRootItems;
520  QgsDataItem *item = items.value( row, nullptr );
521  return item ? createIndex( row, column, item ) : QModelIndex();
522 }
523 
524 QModelIndex QgsBrowserModel::parent( const QModelIndex &index ) const
525 {
526  QgsDataItem *item = dataItem( index );
527  if ( !item )
528  return QModelIndex();
529 
530  return findItem( item->parent() );
531 }
532 
533 QModelIndex QgsBrowserModel::findItem( QgsDataItem *item, QgsDataItem *parent ) const
534 {
535  const QVector<QgsDataItem *> &items = parent ? parent->children() : mRootItems;
536 
537  for ( int i = 0; i < items.size(); i++ )
538  {
539  if ( items[i] == item )
540  return createIndex( i, 0, item );
541 
542  QModelIndex childIndex = findItem( item, items[i] );
543  if ( childIndex.isValid() )
544  return childIndex;
545  }
546  return QModelIndex();
547 }
548 
549 void QgsBrowserModel::beginInsertItems( QgsDataItem *parent, int first, int last )
550 {
551  QgsDebugMsgLevel( "parent mPath = " + parent->path(), 3 );
552  QModelIndex idx = findItem( parent );
553  if ( !idx.isValid() )
554  return;
555  QgsDebugMsgLevel( QStringLiteral( "valid" ), 3 );
556  beginInsertRows( idx, first, last );
557  QgsDebugMsgLevel( QStringLiteral( "end" ), 3 );
558 }
560 {
561  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
562  endInsertRows();
563 }
564 void QgsBrowserModel::beginRemoveItems( QgsDataItem *parent, int first, int last )
565 {
566  QgsDebugMsgLevel( "parent mPath = " + parent->path(), 3 );
567  QModelIndex idx = findItem( parent );
568  if ( !idx.isValid() )
569  return;
570  beginRemoveRows( idx, first, last );
571 }
573 {
574  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
575  endRemoveRows();
576 }
578 {
579  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
580  QModelIndex idx = findItem( item );
581  if ( !idx.isValid() )
582  return;
583  emit dataChanged( idx, idx );
584 }
586 {
587  if ( !item )
588  return;
589  QModelIndex idx = findItem( item );
590  if ( !idx.isValid() )
591  return;
592  QgsDebugMsgLevel( QStringLiteral( "item %1 state changed %2 -> %3" ).arg( item->path() ).arg( oldState ).arg( item->state() ), 4 );
593  emit stateChanged( idx, oldState );
594 }
595 
596 void QgsBrowserModel::setupItemConnections( QgsDataItem *item )
597 {
598  connect( item, &QgsDataItem::beginInsertItems,
600  connect( item, &QgsDataItem::endInsertItems,
602  connect( item, &QgsDataItem::beginRemoveItems,
604  connect( item, &QgsDataItem::endRemoveItems,
606  connect( item, &QgsDataItem::dataChanged,
608  connect( item, &QgsDataItem::stateChanged,
610 
611  // if it's a collection item, also forwards connectionsChanged
612  QgsDataCollectionItem *collectionItem = qobject_cast<QgsDataCollectionItem *>( item );
613  if ( collectionItem )
614  connect( collectionItem, &QgsDataCollectionItem::connectionsChanged, this, &QgsBrowserModel::onConnectionsChanged );
615 }
616 
617 QStringList QgsBrowserModel::mimeTypes() const
618 {
619  QStringList types;
620  // In theory the mime type convention is: application/x-vnd.<vendor>.<application>.<type>
621  // but it seems a bit over formalized. Would be an application/x-qgis-uri better?
622  types << QStringLiteral( "application/x-vnd.qgis.qgis.uri" );
623  return types;
624 }
625 
626 QMimeData *QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
627 {
629  const auto constIndexes = indexes;
630  for ( const QModelIndex &index : constIndexes )
631  {
632  if ( index.isValid() )
633  {
634  QgsDataItem *ptr = reinterpret_cast< QgsDataItem * >( index.internalPointer() );
635  QgsMimeDataUtils::Uri uri = ptr->mimeUri();
636  if ( uri.isValid() )
637  lst.append( uri );
638  }
639  }
640  return QgsMimeDataUtils::encodeUriList( lst );
641 }
642 
643 bool QgsBrowserModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
644 {
645  QgsDataItem *destItem = dataItem( parent );
646  if ( !destItem )
647  {
648  QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
649  return false;
650  }
651 
653  return destItem->handleDrop( data, action );
655 }
656 
657 QgsDataItem *QgsBrowserModel::dataItem( const QModelIndex &idx ) const
658 {
659  void *v = idx.internalPointer();
660  QgsDataItem *d = reinterpret_cast<QgsDataItem *>( v );
661  Q_ASSERT( !v || d );
662  return d;
663 }
664 
665 bool QgsBrowserModel::canFetchMore( const QModelIndex &parent ) const
666 {
667  QgsDataItem *item = dataItem( parent );
668  // if ( item )
669  // QgsDebugMsg( QStringLiteral( "path = %1 canFetchMore = %2" ).arg( item->path() ).arg( item && ! item->isPopulated() ) );
670  return ( item && item->state() == QgsDataItem::NotPopulated );
671 }
672 
673 void QgsBrowserModel::fetchMore( const QModelIndex &parent )
674 {
675  QgsDataItem *item = dataItem( parent );
676 
677  if ( !item || item->state() == QgsDataItem::Populating || item->state() == QgsDataItem::Populated )
678  return;
679 
680  QgsDebugMsgLevel( "path = " + item->path(), 4 );
681 
682  item->populate();
683 }
684 
685 /* Refresh dir path */
686 void QgsBrowserModel::refresh( const QString &path )
687 {
688  QModelIndex index = findPath( path );
689  refresh( index );
690 }
691 
692 /* Refresh item */
693 void QgsBrowserModel::refresh( const QModelIndex &index )
694 {
695  QgsDataItem *item = dataItem( index );
696  if ( !item || item->state() == QgsDataItem::Populating )
697  return;
698 
699  QgsDebugMsgLevel( "Refresh " + item->path(), 4 );
700 
701  item->refresh();
702 }
703 
704 void QgsBrowserModel::addFavoriteDirectory( const QString &directory, const QString &name )
705 {
706  Q_ASSERT( mFavorites );
707  mFavorites->addDirectory( directory, name );
708 }
709 
710 void QgsBrowserModel::removeFavorite( const QModelIndex &index )
711 {
712  QgsDirectoryItem *item = qobject_cast<QgsDirectoryItem *>( dataItem( index ) );
713  if ( !item )
714  return;
715 
716  mFavorites->removeDirectory( item );
717 }
718 
719 void QgsBrowserModel::removeFavorite( QgsFavoriteItem *favorite )
720 {
721  if ( !favorite )
722  return;
723 
724  mFavorites->removeDirectory( favorite );
725 }
726 
728 {
729  QgsSettings settings;
730  QStringList hiddenItems = settings.value( QStringLiteral( "browser/hiddenPaths" ),
731  QStringList() ).toStringList();
732  int idx = hiddenItems.indexOf( item->path() );
733  if ( idx != -1 )
734  {
735  hiddenItems.removeAt( idx );
736  }
737  else
738  {
739  hiddenItems << item->path();
740  }
741  settings.setValue( QStringLiteral( "browser/hiddenPaths" ), hiddenItems );
742  if ( item->parent() )
743  {
744  item->parent()->deleteChildItem( item );
745  }
746  else
747  {
748  removeRootItem( item );
749  }
750 }
751 
752 
753 void QgsBrowserModel::removeRootItem( QgsDataItem *item )
754 {
755  int i = mRootItems.indexOf( item );
756  beginRemoveRows( QModelIndex(), i, i );
757  mRootItems.remove( i );
758  QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem * >( item );
759  if ( !mDriveItems.key( dirItem ).isEmpty() )
760  {
761  mDriveItems.remove( mDriveItems.key( dirItem ) );
762  }
763  item->deleteLater();
764  endRemoveRows();
765 }
766 
767 QgsDataItem *QgsBrowserModel::addProviderRootItem( QgsDataItemProvider *pr )
768 {
769  int capabilities = pr->capabilities();
770  if ( capabilities == QgsDataProvider::NoDataCapabilities )
771  {
772  QgsDebugMsgLevel( pr->name() + " does not have any dataCapabilities", 4 );
773  return nullptr;
774  }
775 
776  QgsDataItem *item = pr->createDataItem( QString(), nullptr ); // empty path -> top level
777  if ( item )
778  {
779  // make sure the top level key is set always
780  item->setProviderKey( pr->name() );
781  // Forward the signal from the root items to the model (and then to the app)
782  connect( item, &QgsDataItem::connectionsChanged, this, &QgsBrowserModel::onConnectionsChanged );
783  QgsDebugMsgLevel( "Add new top level item : " + item->name(), 4 );
784  setupItemConnections( item );
785  }
786  return item;
787 }
QgsBrowserModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:376
QgsBrowserModel::findUri
QModelIndex findUri(const QString &uri, QModelIndex index=QModelIndex())
Returns index of layer item with given uri.
Definition: qgsbrowsermodel.cpp:427
QgsBrowserModel::addRootItems
void addRootItems()
Populates the model.
Definition: qgsbrowsermodel.cpp:90
QgsDataItemProvider::capabilities
virtual int capabilities() const =0
Returns combination of flags from QgsDataProvider::DataCapabilities.
QgsDataItemProviderRegistry::providerAdded
void providerAdded(QgsDataItemProvider *provider)
Emitted when a new data item provider has been added.
QgsDataItem::icon
virtual QIcon icon()
Definition: qgsdataitem.cpp:233
QgsBrowserModel::refresh
void refresh(const QString &path)
Refresh item specified by path.
Definition: qgsbrowsermodel.cpp:686
QgsMimeDataUtils::Uri::isValid
bool isValid() const
Returns whether the object contains valid data.
Definition: qgsmimedatautils.h:58
QgsBrowserModel::connectionsChanged
void connectionsChanged(const QString &providerKey)
Emitted when connections for the specified providerKey have changed in the browser.
QgsDataItem::rowCount
int rowCount()
Definition: qgsdataitem.cpp:459
QgsBrowserModel::reload
void reload()
Reload the whole model.
Definition: qgsbrowsermodel.cpp:456
QgsDataCollectionItem
Definition: qgsdataitem.h:618
QgsBrowserModel::beginRemoveItems
void beginRemoveItems(QgsDataItem *parent, int first, int last)
Definition: qgsbrowsermodel.cpp:564
QgsBrowserModel::initialize
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
Definition: qgsbrowsermodel.cpp:222
QgsBrowserModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:524
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:327
qgsdataitemproviderregistry.h
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsDirectoryItem::hiddenPath
static bool hiddenPath(const QString &path)
Check if the given path is hidden from the browser model.
Definition: qgsdataitem.cpp:1055
QgsBrowserModel::ProviderKeyRole
@ ProviderKeyRole
Data item provider key that created the item,.
Definition: qgsbrowsermodel.h:95
QgsBrowserModel::dropMimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: qgsbrowsermodel.cpp:643
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsDataItem::capabilities2
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:283
QgsDataItem::sortKey
virtual QVariant sortKey() const
Returns the sorting key for the item.
Definition: qgsdataitem.cpp:172
QgsBrowserModel::endInsertItems
void endInsertItems()
Definition: qgsbrowsermodel.cpp:559
QgsDataItem::refresh
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
Definition: qgsdataitem.cpp:402
QgsFavoritesItem::removeDirectory
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
Definition: qgsdataitem.cpp:1327
QgsBrowserModel::itemDataChanged
void itemDataChanged(QgsDataItem *item)
Definition: qgsbrowsermodel.cpp:577
QgsProject::homePath
QString homePath
Definition: qgsproject.h:97
QgsDataItem::setProviderKey
void setProviderKey(const QString &value)
Sets the provider key that created this item (e.g.
Definition: qgsdataitem.cpp:454
qgis.h
QgsBrowserModel::SortRole
@ SortRole
Custom sort role, see QgsDataItem::sortKey()
Definition: qgsbrowsermodel.h:94
QgsProject::instance
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:458
QgsBrowserModel::mRootItems
QVector< QgsDataItem * > mRootItems
Definition: qgsbrowsermodel.h:254
QgsBrowserModel::hasChildren
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:367
QgsDataItem::name
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:318
QgsSettings
Definition: qgssettings.h:61
QgsDataItem::dataChanged
void dataChanged(QgsDataItem *item)
QgsDataItem::deleteLater
static void deleteLater(QVector< QgsDataItem * > &items)
Definition: qgsdataitem.cpp:206
QgsDataItem::toolTip
QString toolTip() const
Definition: qgsdataitem.h:380
QgsDataItemProvider::createDataItem
virtual QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem)=0
Create a new instance of QgsDataItem (or nullptr) for given path and parent item.
qgsmimedatautils.h
QgsMimeDataUtils::UriList
QList< QgsMimeDataUtils::Uri > UriList
Definition: qgsmimedatautils.h:156
QgsBrowserModel::CommentRole
@ CommentRole
Item comment.
Definition: qgsbrowsermodel.h:93
QgsDataItem::beginRemoveItems
void beginRemoveItems(QgsDataItem *parent, int first, int last)
QgsProject::homePathChanged
void homePathChanged()
Emitted when the home path of the project changes.
QgsBrowserModel::findPath
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Returns index of item with given path.
Definition: qgsbrowsermodel.cpp:382
QgsDataItem::state
State state() const
Definition: qgsdataitem.cpp:589
QgsFavoritesItem::addDirectory
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
Definition: qgsdataitem.cpp:1307
QgsBrowserModel::removeRootItems
void removeRootItems()
Definition: qgsbrowsermodel.cpp:163
QgsBrowserModel::mProjectHome
QgsDirectoryItem * mProjectHome
Definition: qgsbrowsermodel.h:256
QgsDataItemProvider::name
virtual QString name()=0
Human-readable name of the provider name.
qgsapplication.h
PROJECT_HOME_PREFIX
#define PROJECT_HOME_PREFIX
Definition: qgsbrowsermodel.cpp:33
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:752
QgsDataItem::endRemoveItems
void endRemoveItems()
QgsDataItem::type
Type type() const
Definition: qgsdataitem.h:299
QgsDataItem::mimeUri
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
Definition: qgsdataitem.h:236
QgsBrowserModel::driveItems
QMap< QString, QgsDirectoryItem * > driveItems() const
Returns a map of the root drive items shown in the browser.
Definition: qgsbrowsermodel.cpp:216
QgsBrowserModel::updateProjectHome
void updateProjectHome()
Definition: qgsbrowsermodel.cpp:63
QgsDataItem::rename
virtual Q_DECL_DEPRECATED bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
Definition: qgsdataitem.cpp:584
QgsBrowserModel::removeFavorite
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
Definition: qgsbrowsermodel.cpp:710
QgsBrowserModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: qgsbrowsermodel.cpp:338
qgsproviderregistry.h
QgsDirectoryItem
Definition: qgsdataitem.h:671
HOME_PREFIX
#define HOME_PREFIX
Definition: qgsbrowsermodel.cpp:34
QgsBrowserModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgsbrowsermodel.cpp:261
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:304
QgsDataItemProviderRegistry::providers
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
Definition: qgsdataitemproviderregistry.cpp:48
QgsDataItemProvider
Definition: qgsdataitemprovider.h:45
QgsBrowserModel::stateChanged
void stateChanged(const QModelIndex &index, QgsDataItem::State oldState)
Emitted when item children fetch was finished.
QgsBrowserModel::connectItem
Q_DECL_DEPRECATED void connectItem(QgsDataItem *item)
Definition: qgsbrowsermodel.cpp:451
QgsDataItem::Rename
@ Rename
Item can be renamed.
Definition: qgsdataitem.h:245
QgsApplication::dataItemProviderRegistry
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
Definition: qgsapplication.cpp:2109
QgsBrowserModel::endRemoveItems
void endRemoveItems()
Definition: qgsbrowsermodel.cpp:572
QgsDataItem::acceptDrop
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:201
QgsDataItem::Populating
@ Populating
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:123
QgsBrowserModel::mFavorites
QgsFavoritesItem * mFavorites
Definition: qgsbrowsermodel.h:255
QgsSettings::setValue
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Definition: qgssettings.cpp:289
QgsDataItem::hasDragEnabled
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:228
QgsBrowserModel::PathRole
@ PathRole
Item path used to access path in the tree, see QgsDataItem::mPath.
Definition: qgsbrowsermodel.h:92
QgsBrowserWatcher::QgsBrowserWatcher
QgsBrowserWatcher(QgsDataItem *item)
Definition: qgsbrowsermodel.cpp:36
QgsMimeDataUtils::Uri
Definition: qgsmimedatautils.h:40
QgsDataItem::NotPopulated
@ NotPopulated
Children not yet created.
Definition: qgsdataitem.h:122
QgsBrowserModel::hidePath
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
Definition: qgsbrowsermodel.cpp:727
QgsDataItem::connectionsChanged
void connectionsChanged(const QString &providerKey=QString())
Emitted when the connections of the provider with the specified providerKey have changed.
QgsDataItem::endInsertItems
void endInsertItems()
QgsBrowserModel::beginInsertItems
void beginInsertItems(QgsDataItem *parent, int first, int last)
Definition: qgsbrowsermodel.cpp:549
qgsbrowsermodel.h
QgsBrowserModel::QgsBrowserModel
QgsBrowserModel(QObject *parent=nullptr)
Constructor for QgsBrowserModel, with the specified parent object.
Definition: qgsbrowsermodel.cpp:48
qgsdataitemprovider.h
QgsDataItem::hasChildren
bool hasChildren()
Definition: qgsdataitem.cpp:463
QgsMimeDataUtils::encodeUriList
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
Definition: qgsmimedatautils.cpp:201
QgsBrowserModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: qgsbrowsermodel.cpp:311
QgsBrowserModel::refreshDrives
void refreshDrives()
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly adde...
Definition: qgsbrowsermodel.cpp:465
QgsBrowserModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:349
QgsDataItemProviderRegistry::providerWillBeRemoved
void providerWillBeRemoved(QgsDataItemProvider *provider)
Emitted when a data item provider is about to be removed.
QgsLayerItem::comments
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:565
QgsBrowserModel::mimeData
QMimeData * mimeData(const QModelIndexList &indexes) const override
Definition: qgsbrowsermodel.cpp:626
QgsBrowserModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:513
QgsBrowserModel::mimeTypes
QStringList mimeTypes() const override
Definition: qgsbrowsermodel.cpp:617
QgsBrowserModel::itemStateChanged
void itemStateChanged(QgsDataItem *item, QgsDataItem::State oldState)
Definition: qgsbrowsermodel.cpp:585
QgsDataItem::populate
virtual void populate(const QVector< QgsDataItem * > &children)
Definition: qgsdataitem.cpp:335
qgssettings.h
QgsDataProvider::NoDataCapabilities
@ NoDataCapabilities
Definition: qgsdataprovider.h:75
QgsDataItem::handleDrop
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:211
QgsDataItem::Layer
@ Layer
Definition: qgsdataitem.h:80
QgsBrowserModel::addFavoriteDirectory
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
Definition: qgsbrowsermodel.cpp:704
QgsDataItem::children
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:310
qgsdataprovider.h
QgsLayerItem
Definition: qgsdataitem.h:499
QgsDataItem::Populated
@ Populated
Children created.
Definition: qgsdataitem.h:124
QgsBrowserModel::~QgsBrowserModel
~QgsBrowserModel() override
Definition: qgsbrowsermodel.cpp:58
QgsBrowserModel::findItem
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
Returns the model index corresponding to the specified data item.
Definition: qgsbrowsermodel.cpp:533
qgslogger.h
QgsDataItem::providerKey
QString providerKey() const
Returns the provider key that created this item (e.g.
Definition: qgsdataitem.cpp:449
QgsFavoritesItem
Definition: qgsdataitem.h:808
QgsDataItem
Definition: qgsdataitem.h:49
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:751
QgsDataItem::deleteChildItem
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
Definition: qgsdataitem.cpp:527
QgsDataItem::stateChanged
void stateChanged(QgsDataItem *item, QgsDataItem::State oldState)
QgsBrowserModel::dataItem
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index.
Definition: qgsbrowsermodel.cpp:657
QgsDataItem::setSortKey
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
Definition: qgsdataitem.cpp:177
QgsBrowserModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:232
QgsBrowserModel::fetchMore
void fetchMore(const QModelIndex &parent) override
Definition: qgsbrowsermodel.cpp:673
QgsDataItem::State
State
Definition: qgsdataitem.h:120
qgsproject.h
QgsBrowserModel::canFetchMore
bool canFetchMore(const QModelIndex &parent) const override
Definition: qgsbrowsermodel.cpp:665
QgsDataItem::beginInsertItems
void beginInsertItems(QgsDataItem *parent, int first, int last)