QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  setupItemConnections( vols );
131  mRootItems << vols;
132 #endif
133 
134  // container for displaying providers as sorted groups (by QgsDataProvider::DataCapability enum)
135  QMultiMap<int, QgsDataItem *> providerMap;
136 
137  const auto constProviders = QgsApplication::dataItemProviderRegistry()->providers();
138  for ( QgsDataItemProvider *pr : constProviders )
139  {
140  if ( QgsDataItem *item = addProviderRootItem( pr ) )
141  {
142  providerMap.insert( pr->capabilities(), item );
143  }
144  }
145 
146  // add as sorted groups by QgsDataProvider::DataCapability enum
147  const auto constUniqueKeys = providerMap.uniqueKeys();
148  for ( int key : constUniqueKeys )
149  {
150  QList<QgsDataItem *> providerGroup = providerMap.values( key );
151  if ( providerGroup.size() > 1 )
152  {
153  std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
154  }
155 
156  const auto constProviderGroup = providerGroup;
157  for ( QgsDataItem *ditem : constProviderGroup )
158  {
159  mRootItems << ditem;
160  }
161  }
162 }
163 
165 {
166  const auto constMRootItems = mRootItems;
167  for ( QgsDataItem *item : constMRootItems )
168  {
169  delete item;
170  }
171 
172  mRootItems.clear();
173  mDriveItems.clear();
174 }
175 
176 void QgsBrowserModel::dataItemProviderAdded( QgsDataItemProvider *provider )
177 {
178  if ( !mInitialized )
179  return;
180 
181  if ( QgsDataItem *item = addProviderRootItem( provider ) )
182  {
183  beginInsertRows( QModelIndex(), rowCount(), rowCount() );
184  mRootItems << item;
185  endInsertRows();
186  }
187 }
188 
189 void QgsBrowserModel::dataItemProviderWillBeRemoved( QgsDataItemProvider *provider )
190 {
191  const auto constMRootItems = mRootItems;
192  for ( QgsDataItem *item : constMRootItems )
193  {
194  if ( item->providerKey() == provider->name() )
195  {
196  removeRootItem( item );
197  break; // assuming there is max. 1 root item per provider
198  }
199  }
200 }
201 
202 void QgsBrowserModel::onConnectionsChanged( const QString &providerKey )
203 {
204  // refresh the matching provider
205  for ( QgsDataItem *item : qgis::as_const( mRootItems ) )
206  {
207  if ( item->providerKey() == providerKey )
208  {
209  item->refresh();
210  break; // assuming there is max. 1 root item per provider
211  }
212  }
213 
214  emit connectionsChanged( providerKey );
215 }
216 
217 QMap<QString, QgsDirectoryItem *> QgsBrowserModel::driveItems() const
218 {
219  return mDriveItems;
220 }
221 
222 
224 {
225  if ( ! mInitialized )
226  {
228  addRootItems();
229  mInitialized = true;
230  }
231 }
232 
233 Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const
234 {
235  if ( !index.isValid() )
236  return Qt::ItemFlags();
237 
238  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
239 
240  QgsDataItem *ptr = dataItem( index );
241 
242  if ( !ptr )
243  {
244  QgsDebugMsgLevel( QStringLiteral( "FLAGS PROBLEM!" ), 4 );
245  return Qt::ItemFlags();
246  }
247 
248  if ( ptr->hasDragEnabled() )
249  flags |= Qt::ItemIsDragEnabled;
250 
252  if ( ptr->acceptDrop() )
253  flags |= Qt::ItemIsDropEnabled;
255 
256  if ( ptr->capabilities2() & QgsDataItem::Rename )
257  flags |= Qt::ItemIsEditable;
258 
259  return flags;
260 }
261 
262 QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
263 {
264  if ( !index.isValid() )
265  return QVariant();
266 
267  QgsDataItem *item = dataItem( index );
268  if ( !item )
269  {
270  return QVariant();
271  }
272  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
273  {
274  return item->name();
275  }
276  else if ( role == QgsBrowserModel::SortRole )
277  {
278  return item->sortKey();
279  }
280  else if ( role == Qt::ToolTipRole )
281  {
282  return item->toolTip();
283  }
284  else if ( role == Qt::DecorationRole && index.column() == 0 )
285  {
286  return item->icon();
287  }
288  else if ( role == QgsBrowserModel::PathRole )
289  {
290  return item->path();
291  }
292  else if ( role == QgsBrowserModel::CommentRole )
293  {
294  if ( item->type() == QgsDataItem::Layer )
295  {
296  QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
297  return lyrItem->comments();
298  }
299  return QVariant();
300  }
301  else if ( role == QgsBrowserModel::ProviderKeyRole )
302  {
303  return item->providerKey();
304  }
305  else
306  {
307  // unsupported role
308  return QVariant();
309  }
310 }
311 
312 bool QgsBrowserModel::setData( const QModelIndex &index, const QVariant &value, int role )
313 {
314  if ( !index.isValid() )
315  return false;
316 
317 
318  QgsDataItem *item = dataItem( index );
319  if ( !item )
320  {
321  return false;
322  }
323 
324  if ( !( item->capabilities2() & QgsDataItem::Rename ) )
325  return false;
326 
327  switch ( role )
328  {
329  case Qt::EditRole:
330  {
332  return item->rename( value.toString() );
334  }
335  }
336  return false;
337 }
338 
339 QVariant QgsBrowserModel::headerData( int section, Qt::Orientation orientation, int role ) const
340 {
341  Q_UNUSED( section )
342  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
343  {
344  return QVariant( "header" );
345  }
346 
347  return QVariant();
348 }
349 
350 int QgsBrowserModel::rowCount( const QModelIndex &parent ) const
351 {
352  //QgsDebugMsg(QString("isValid = %1 row = %2 column = %3").arg(parent.isValid()).arg(parent.row()).arg(parent.column()));
353 
354  if ( !parent.isValid() )
355  {
356  // root item: its children are top level items
357  return mRootItems.count(); // mRoot
358  }
359  else
360  {
361  // ordinary item: number of its children
362  QgsDataItem *item = dataItem( parent );
363  //if ( item ) QgsDebugMsg(QString("path = %1 rowCount = %2").arg(item->path()).arg(item->rowCount()) );
364  return item ? item->rowCount() : 0;
365  }
366 }
367 
368 bool QgsBrowserModel::hasChildren( const QModelIndex &parent ) const
369 {
370  if ( !parent.isValid() )
371  return !mRootItems.isEmpty(); // root item: its children are top level items
372 
373  QgsDataItem *item = dataItem( parent );
374  return item && item->hasChildren();
375 }
376 
377 int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
378 {
379  Q_UNUSED( parent )
380  return 1;
381 }
382 
383 QModelIndex QgsBrowserModel::findPath( const QString &path, Qt::MatchFlag matchFlag )
384 {
385  return findPath( this, path, matchFlag );
386 }
387 
388 QModelIndex QgsBrowserModel::findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag )
389 {
390  if ( !model )
391  return QModelIndex();
392 
393  QModelIndex index; // starting from root
394  bool foundChild = true;
395 
396  while ( foundChild )
397  {
398  foundChild = false; // assume that the next child item will not be found
399 
400  for ( int i = 0; i < model->rowCount( index ); i++ )
401  {
402  QModelIndex idx = model->index( i, 0, index );
403 
404  QString itemPath = model->data( idx, PathRole ).toString();
405  if ( itemPath == path )
406  {
407  QgsDebugMsgLevel( "Arrived " + itemPath, 4 );
408  return idx; // we have found the item we have been looking for
409  }
410 
411  // paths are slash separated identifier
412  if ( path.startsWith( itemPath + '/' ) )
413  {
414  foundChild = true;
415  index = idx;
416  break;
417  }
418  }
419  }
420 
421  if ( matchFlag == Qt::MatchStartsWith )
422  return index;
423 
424  QgsDebugMsgLevel( QStringLiteral( "path not found" ), 4 );
425  return QModelIndex(); // not found
426 }
427 
428 QModelIndex QgsBrowserModel::findUri( const QString &uri, QModelIndex index )
429 {
430  for ( int i = 0; i < this->rowCount( index ); i++ )
431  {
432  QModelIndex idx = this->index( i, 0, index );
433 
434  if ( qobject_cast<QgsLayerItem *>( dataItem( idx ) ) )
435  {
436  QString itemUri = qobject_cast<QgsLayerItem *>( dataItem( idx ) )->uri();
437 
438  if ( itemUri == uri )
439  {
440  QgsDebugMsgLevel( "Arrived " + itemUri, 4 );
441  return idx; // we have found the item we have been looking for
442  }
443  }
444 
445  QModelIndex childIdx = findUri( uri, idx );
446  if ( childIdx.isValid() )
447  return childIdx;
448  }
449  return QModelIndex();
450 }
451 
453 {
454  // deprecated, no use
455 }
456 
458 {
459  // TODO: put items creating currently children in threads to deleteLater (does not seem urget because reload() is not used in QGIS)
460  beginResetModel();
461  removeRootItems();
462  addRootItems();
463  endResetModel();
464 }
465 
467 {
468  const QList< QFileInfo > drives = QDir::drives();
469  // remove any removed drives
470  const QStringList existingDrives = mDriveItems.keys();
471  for ( const QString &drivePath : existingDrives )
472  {
473  bool stillExists = false;
474  for ( const QFileInfo &drive : drives )
475  {
476  if ( drivePath == drive.absolutePath() )
477  {
478  stillExists = true;
479  break;
480  }
481  }
482 
483  if ( stillExists )
484  continue;
485 
486  // drive has been removed, remove corresponding item
487  if ( QgsDirectoryItem *driveItem = mDriveItems.value( drivePath ) )
488  removeRootItem( driveItem );
489  }
490 
491  for ( const QFileInfo &drive : drives )
492  {
493  const QString path = drive.absolutePath();
494 
495  if ( QgsDirectoryItem::hiddenPath( path ) )
496  continue;
497 
498  // does an item for this drive already exist?
499  if ( !mDriveItems.contains( path ) )
500  {
501  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path, path, QStringLiteral( "special:Drives" ) );
502  item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
503 
504  mDriveItems.insert( path, item );
505  setupItemConnections( item );
506 
507  beginInsertRows( QModelIndex(), mRootItems.count(), mRootItems.count() );
508  mRootItems << item;
509  endInsertRows();
510  }
511  }
512 }
513 
514 QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
515 {
516  if ( column < 0 || column >= columnCount() || row < 0 )
517  return QModelIndex();
518 
519  QgsDataItem *p = dataItem( parent );
520  const QVector<QgsDataItem *> &items = p ? p->children() : mRootItems;
521  QgsDataItem *item = items.value( row, nullptr );
522  return item ? createIndex( row, column, item ) : QModelIndex();
523 }
524 
525 QModelIndex QgsBrowserModel::parent( const QModelIndex &index ) const
526 {
527  QgsDataItem *item = dataItem( index );
528  if ( !item )
529  return QModelIndex();
530 
531  return findItem( item->parent() );
532 }
533 
534 QModelIndex QgsBrowserModel::findItem( QgsDataItem *item, QgsDataItem *parent ) const
535 {
536  const QVector<QgsDataItem *> &items = parent ? parent->children() : mRootItems;
537 
538  for ( int i = 0; i < items.size(); i++ )
539  {
540  if ( items[i] == item )
541  return createIndex( i, 0, item );
542 
543  QModelIndex childIndex = findItem( item, items[i] );
544  if ( childIndex.isValid() )
545  return childIndex;
546  }
547  return QModelIndex();
548 }
549 
550 void QgsBrowserModel::beginInsertItems( QgsDataItem *parent, int first, int last )
551 {
552  QgsDebugMsgLevel( "parent mPath = " + parent->path(), 3 );
553  QModelIndex idx = findItem( parent );
554  if ( !idx.isValid() )
555  return;
556  QgsDebugMsgLevel( QStringLiteral( "valid" ), 3 );
557  beginInsertRows( idx, first, last );
558  QgsDebugMsgLevel( QStringLiteral( "end" ), 3 );
559 }
561 {
562  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
563  endInsertRows();
564 }
565 void QgsBrowserModel::beginRemoveItems( QgsDataItem *parent, int first, int last )
566 {
567  QgsDebugMsgLevel( "parent mPath = " + parent->path(), 3 );
568  QModelIndex idx = findItem( parent );
569  if ( !idx.isValid() )
570  return;
571  beginRemoveRows( idx, first, last );
572 }
574 {
575  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
576  endRemoveRows();
577 }
579 {
580  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
581  QModelIndex idx = findItem( item );
582  if ( !idx.isValid() )
583  return;
584  emit dataChanged( idx, idx );
585 }
587 {
588  if ( !item )
589  return;
590  QModelIndex idx = findItem( item );
591  if ( !idx.isValid() )
592  return;
593  QgsDebugMsgLevel( QStringLiteral( "item %1 state changed %2 -> %3" ).arg( item->path() ).arg( oldState ).arg( item->state() ), 4 );
594  emit stateChanged( idx, oldState );
595 }
596 
597 void QgsBrowserModel::setupItemConnections( QgsDataItem *item )
598 {
599  connect( item, &QgsDataItem::beginInsertItems,
601  connect( item, &QgsDataItem::endInsertItems,
603  connect( item, &QgsDataItem::beginRemoveItems,
605  connect( item, &QgsDataItem::endRemoveItems,
607  connect( item, &QgsDataItem::dataChanged,
609  connect( item, &QgsDataItem::stateChanged,
611 
612  // if it's a collection item, also forwards connectionsChanged
613  QgsDataCollectionItem *collectionItem = qobject_cast<QgsDataCollectionItem *>( item );
614  if ( collectionItem )
615  connect( collectionItem, &QgsDataCollectionItem::connectionsChanged, this, &QgsBrowserModel::onConnectionsChanged );
616 }
617 
618 QStringList QgsBrowserModel::mimeTypes() const
619 {
620  QStringList types;
621  // In theory the mime type convention is: application/x-vnd.<vendor>.<application>.<type>
622  // but it seems a bit over formalized. Would be an application/x-qgis-uri better?
623  types << QStringLiteral( "application/x-vnd.qgis.qgis.uri" );
624  return types;
625 }
626 
627 QMimeData *QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
628 {
630  const auto constIndexes = indexes;
631  for ( const QModelIndex &index : constIndexes )
632  {
633  if ( index.isValid() )
634  {
635  QgsDataItem *ptr = reinterpret_cast< QgsDataItem * >( index.internalPointer() );
636  QgsMimeDataUtils::Uri uri = ptr->mimeUri();
637  if ( uri.isValid() )
638  lst.append( uri );
639  }
640  }
641  return QgsMimeDataUtils::encodeUriList( lst );
642 }
643 
644 bool QgsBrowserModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int, int, const QModelIndex &parent )
645 {
646  QgsDataItem *destItem = dataItem( parent );
647  if ( !destItem )
648  {
649  QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
650  return false;
651  }
652 
654  return destItem->handleDrop( data, action );
656 }
657 
658 QgsDataItem *QgsBrowserModel::dataItem( const QModelIndex &idx ) const
659 {
660  void *v = idx.internalPointer();
661  QgsDataItem *d = reinterpret_cast<QgsDataItem *>( v );
662  Q_ASSERT( !v || d );
663  return d;
664 }
665 
666 bool QgsBrowserModel::canFetchMore( const QModelIndex &parent ) const
667 {
668  QgsDataItem *item = dataItem( parent );
669  // if ( item )
670  // QgsDebugMsg( QStringLiteral( "path = %1 canFetchMore = %2" ).arg( item->path() ).arg( item && ! item->isPopulated() ) );
671  return ( item && item->state() == QgsDataItem::NotPopulated );
672 }
673 
674 void QgsBrowserModel::fetchMore( const QModelIndex &parent )
675 {
676  QgsDataItem *item = dataItem( parent );
677 
678  if ( !item || item->state() == QgsDataItem::Populating || item->state() == QgsDataItem::Populated )
679  return;
680 
681  QgsDebugMsgLevel( "path = " + item->path(), 4 );
682 
683  item->populate();
684 }
685 
686 /* Refresh dir path */
687 void QgsBrowserModel::refresh( const QString &path )
688 {
689  QModelIndex index = findPath( path );
690  refresh( index );
691 }
692 
693 /* Refresh item */
694 void QgsBrowserModel::refresh( const QModelIndex &index )
695 {
696  QgsDataItem *item = dataItem( index );
697  if ( !item || item->state() == QgsDataItem::Populating )
698  return;
699 
700  QgsDebugMsgLevel( "Refresh " + item->path(), 4 );
701 
702  item->refresh();
703 }
704 
705 void QgsBrowserModel::addFavoriteDirectory( const QString &directory, const QString &name )
706 {
707  Q_ASSERT( mFavorites );
708  mFavorites->addDirectory( directory, name );
709 }
710 
711 void QgsBrowserModel::removeFavorite( const QModelIndex &index )
712 {
713  QgsDirectoryItem *item = qobject_cast<QgsDirectoryItem *>( dataItem( index ) );
714  if ( !item )
715  return;
716 
717  mFavorites->removeDirectory( item );
718 }
719 
720 void QgsBrowserModel::removeFavorite( QgsFavoriteItem *favorite )
721 {
722  if ( !favorite )
723  return;
724 
725  mFavorites->removeDirectory( favorite );
726 }
727 
729 {
730  QgsSettings settings;
731  QStringList hiddenItems = settings.value( QStringLiteral( "browser/hiddenPaths" ),
732  QStringList() ).toStringList();
733  int idx = hiddenItems.indexOf( item->path() );
734  if ( idx != -1 )
735  {
736  hiddenItems.removeAt( idx );
737  }
738  else
739  {
740  hiddenItems << item->path();
741  }
742  settings.setValue( QStringLiteral( "browser/hiddenPaths" ), hiddenItems );
743  if ( item->parent() )
744  {
745  item->parent()->deleteChildItem( item );
746  }
747  else
748  {
749  removeRootItem( item );
750  }
751 }
752 
753 
754 void QgsBrowserModel::removeRootItem( QgsDataItem *item )
755 {
756  int i = mRootItems.indexOf( item );
757  beginRemoveRows( QModelIndex(), i, i );
758  mRootItems.remove( i );
759  QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem * >( item );
760  if ( !mDriveItems.key( dirItem ).isEmpty() )
761  {
762  mDriveItems.remove( mDriveItems.key( dirItem ) );
763  }
764  item->deleteLater();
765  endRemoveRows();
766 }
767 
768 QgsDataItem *QgsBrowserModel::addProviderRootItem( QgsDataItemProvider *pr )
769 {
770  int capabilities = pr->capabilities();
771  if ( capabilities == QgsDataProvider::NoDataCapabilities )
772  {
773  QgsDebugMsgLevel( pr->name() + " does not have any dataCapabilities", 4 );
774  return nullptr;
775  }
776 
777  QgsDataItem *item = pr->createDataItem( QString(), nullptr ); // empty path -> top level
778  if ( item )
779  {
780  // make sure the top level key is always set
781  item->setProviderKey( pr->name() );
782  // Forward the signal from the root items to the model (and then to the app)
783  connect( item, &QgsDataItem::connectionsChanged, this, &QgsBrowserModel::onConnectionsChanged );
784  QgsDebugMsgLevel( "Add new top level item : " + item->name(), 4 );
785  setupItemConnections( item );
786  }
787  return item;
788 }
QgsBrowserModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:377
QgsBrowserModel::findUri
QModelIndex findUri(const QString &uri, QModelIndex index=QModelIndex())
Returns index of layer item with given uri.
Definition: qgsbrowsermodel.cpp:428
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:401
QgsBrowserModel::refresh
void refresh(const QString &path)
Refresh item specified by path.
Definition: qgsbrowsermodel.cpp:687
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:627
QgsBrowserModel::reload
void reload()
Reload the whole model.
Definition: qgsbrowsermodel.cpp:457
QgsDataCollectionItem
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:626
QgsBrowserModel::beginRemoveItems
void beginRemoveItems(QgsDataItem *parent, int first, int last)
Definition: qgsbrowsermodel.cpp:565
QgsBrowserModel::initialize
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
Definition: qgsbrowsermodel.cpp:223
QgsBrowserModel::parent
QModelIndex parent(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:525
QgsDataItem::path
QString path() const
Definition: qgsdataitem.h:333
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:1228
QgsBrowserModel::ProviderKeyRole
@ ProviderKeyRole
Data item provider key that created the item, see QgsDataItem::providerKey()
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:644
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:287
QgsDataItem::sortKey
virtual QVariant sortKey() const
Returns the sorting key for the item.
Definition: qgsdataitem.cpp:340
QgsBrowserModel::endInsertItems
void endInsertItems()
Definition: qgsbrowsermodel.cpp:560
QgsDataItem::refresh
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
Definition: qgsdataitem.cpp:570
QgsFavoritesItem::removeDirectory
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
Definition: qgsdataitem.cpp:1501
QgsBrowserModel::itemDataChanged
void itemDataChanged(QgsDataItem *item)
Definition: qgsbrowsermodel.cpp:578
QgsProject::homePath
QString homePath
Definition: qgsproject.h:99
QgsDataItem::setProviderKey
void setProviderKey(const QString &value)
Sets the provider key that created this item (e.g.
Definition: qgsdataitem.cpp:622
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:468
QgsBrowserModel::mRootItems
QVector< QgsDataItem * > mRootItems
Definition: qgsbrowsermodel.h:254
QgsBrowserModel::hasChildren
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:368
QgsDataItem::name
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:324
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsDataItem::dataChanged
void dataChanged(QgsDataItem *item)
QgsDataItem::deleteLater
static void deleteLater(QVector< QgsDataItem * > &items)
Definition: qgsdataitem.cpp:374
QgsDataItem::toolTip
QString toolTip() const
Definition: qgsdataitem.h:386
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:383
QgsDataItem::state
State state() const
Definition: qgsdataitem.cpp:757
QgsFavoritesItem::addDirectory
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
Definition: qgsdataitem.cpp:1481
QgsBrowserModel::removeRootItems
void removeRootItems()
Definition: qgsbrowsermodel.cpp:164
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:797
QgsDataItem::endRemoveItems
void endRemoveItems()
QgsDataItem::type
Type type() const
Definition: qgsdataitem.h:303
QgsDataItem::mimeUri
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
Definition: qgsdataitem.h:240
QgsBrowserModel::driveItems
QMap< QString, QgsDirectoryItem * > driveItems() const
Returns a map of the root drive items shown in the browser.
Definition: qgsbrowsermodel.cpp:217
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:752
QgsBrowserModel::removeFavorite
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
Definition: qgsbrowsermodel.cpp:711
QgsBrowserModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: qgsbrowsermodel.cpp:339
qgsproviderregistry.h
QgsDirectoryItem
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:743
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:262
QgsDataItem::parent
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:309
QgsDataItemProviderRegistry::providers
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
Definition: qgsdataitemproviderregistry.cpp:48
QgsDataItemProvider
This is the interface for those who want to add custom data items to the browser tree.
Definition: qgsdataitemprovider.h:46
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:452
QgsDataItem::Rename
@ Rename
Item can be renamed.
Definition: qgsdataitem.h:249
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:2158
QgsBrowserModel::endRemoveItems
void endRemoveItems()
Definition: qgsbrowsermodel.cpp:573
QgsDataItem::acceptDrop
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:205
QgsDataItem::Populating
@ Populating
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:127
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:232
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:41
QgsDataItem::NotPopulated
@ NotPopulated
Children not yet created.
Definition: qgsdataitem.h:126
QgsBrowserModel::hidePath
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
Definition: qgsbrowsermodel.cpp:728
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:550
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:631
QgsMimeDataUtils::encodeUriList
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
Definition: qgsmimedatautils.cpp:202
QgsBrowserModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: qgsbrowsermodel.cpp:312
QgsBrowserModel::refreshDrives
void refreshDrives()
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly adde...
Definition: qgsbrowsermodel.cpp:466
QgsBrowserModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:350
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:572
QgsBrowserModel::mimeData
QMimeData * mimeData(const QModelIndexList &indexes) const override
Definition: qgsbrowsermodel.cpp:627
QgsBrowserModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: qgsbrowsermodel.cpp:514
QgsBrowserModel::mimeTypes
QStringList mimeTypes() const override
Definition: qgsbrowsermodel.cpp:618
QgsBrowserModel::itemStateChanged
void itemStateChanged(QgsDataItem *item, QgsDataItem::State oldState)
Definition: qgsbrowsermodel.cpp:586
QgsDataItem::populate
virtual void populate(const QVector< QgsDataItem * > &children)
Definition: qgsdataitem.cpp:503
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:215
QgsDataItem::Layer
@ Layer
Definition: qgsdataitem.h:81
QgsBrowserModel::addFavoriteDirectory
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
Definition: qgsbrowsermodel.cpp:705
QgsDataItem::children
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:316
qgsdataprovider.h
QgsLayerItem
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:507
QgsDataItem::Populated
@ Populated
Children created.
Definition: qgsdataitem.h:128
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:534
qgslogger.h
QgsDataItem::providerKey
QString providerKey() const
Returns the provider key that created this item (e.g.
Definition: qgsdataitem.cpp:617
QgsFavoritesItem
Contains various Favorites directories.
Definition: qgsdataitem.h:880
QgsDataItem
Base class for all items in the model.
Definition: qgsdataitem.h:51
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:796
QgsDataItem::deleteChildItem
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
Definition: qgsdataitem.cpp:695
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:658
QgsDataItem::setSortKey
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
Definition: qgsdataitem.cpp:345
QgsBrowserModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsbrowsermodel.cpp:233
QgsBrowserModel::fetchMore
void fetchMore(const QModelIndex &parent) override
Definition: qgsbrowsermodel.cpp:674
QgsDataItem::State
State
Definition: qgsdataitem.h:125
qgsproject.h
QgsBrowserModel::canFetchMore
bool canFetchMore(const QModelIndex &parent) const override
Definition: qgsbrowsermodel.cpp:666
QgsDataItem::beginInsertItems
void beginInsertItems(QgsDataItem *parent, int first, int last)