QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 {
52 }
53 
55 {
57 }
58 
60 {
61  QString home = QgsProject::instance()->homePath();
62  if ( mProjectHome && mProjectHome->path().mid( QStringLiteral( PROJECT_HOME_PREFIX ).length() ) == home )
63  return;
64 
65  int idx = mRootItems.indexOf( mProjectHome );
66 
67  // using layoutAboutToBeChanged() was messing expanded items
68  if ( idx >= 0 )
69  {
70  beginRemoveRows( QModelIndex(), idx, idx );
71  mRootItems.remove( idx );
72  endRemoveRows();
73  }
74  delete mProjectHome;
75  mProjectHome = home.isNull() ? nullptr : new QgsProjectHomeItem( nullptr, tr( "Project Home" ), home, QStringLiteral( PROJECT_HOME_PREFIX ) + home );
76  if ( mProjectHome )
77  {
78  setupItemConnections( mProjectHome );
79 
80  beginInsertRows( QModelIndex(), 0, 0 );
81  mRootItems.insert( 0, mProjectHome );
82  endInsertRows();
83  }
84 }
85 
87 {
89 
90  // give the home directory a prominent third place
91  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, tr( "Home" ), QDir::homePath(), QStringLiteral( HOME_PREFIX ) + QDir::homePath() );
92  item->setSortKey( QStringLiteral( " 2" ) );
93  setupItemConnections( item );
94  mRootItems << item;
95 
96  // add favorite directories
97  mFavorites = new QgsFavoritesItem( nullptr, tr( "Favorites" ) );
98  if ( mFavorites )
99  {
100  setupItemConnections( mFavorites );
102  }
103 
104  // add drives
105  const auto drives { QDir::drives() };
106  for ( const QFileInfo &drive : drives )
107  {
108  const QString path = drive.absolutePath();
109 
110  if ( QgsDirectoryItem::hiddenPath( path ) )
111  continue;
112 
113  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
114  item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
115  mDriveItems.insert( path, item );
116 
117  setupItemConnections( item );
118  mRootItems << item;
119  }
120 
121 #ifdef Q_OS_MAC
122  QString path = QString( "/Volumes" );
123  QgsDirectoryItem *vols = new QgsDirectoryItem( nullptr, path, path );
124  mRootItems << vols;
125 #endif
126 
127  // container for displaying providers as sorted groups (by QgsDataProvider::DataCapability enum)
128  QMap<int, QgsDataItem *> providerMap;
129 
130  const auto constProviders = QgsApplication::dataItemProviderRegistry()->providers();
131  for ( QgsDataItemProvider *pr : constProviders )
132  {
133  int capabilities = pr->capabilities();
134  if ( capabilities == QgsDataProvider::NoDataCapabilities )
135  {
136  QgsDebugMsgLevel( pr->name() + " does not have any dataCapabilities", 4 );
137  continue;
138  }
139 
140  QgsDataItem *item = pr->createDataItem( QString(), nullptr ); // empty path -> top level
141  if ( item )
142  {
143  // Forward the signal from the root items to the model (and then to the app)
145  QgsDebugMsgLevel( "Add new top level item : " + item->name(), 4 );
146  setupItemConnections( item );
147  providerMap.insertMulti( capabilities, item );
148  }
149  }
150 
151  // add as sorted groups by QgsDataProvider::DataCapability enum
152  const auto constUniqueKeys = providerMap.uniqueKeys();
153  for ( int key : constUniqueKeys )
154  {
155  QList<QgsDataItem *> providerGroup = providerMap.values( key );
156  if ( providerGroup.size() > 1 )
157  {
158  std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
159  }
160 
161  const auto constProviderGroup = providerGroup;
162  for ( QgsDataItem *ditem : constProviderGroup )
163  {
164  mRootItems << ditem;
165  }
166  }
167 }
168 
170 {
171  const auto constMRootItems = mRootItems;
172  for ( QgsDataItem *item : constMRootItems )
173  {
174  delete item;
175  }
176 
177  mRootItems.clear();
178  mDriveItems.clear();
179 }
180 
181 QMap<QString, QgsDirectoryItem *> QgsBrowserModel::driveItems() const
182 {
183  return mDriveItems;
184 }
185 
186 
188 {
189  if ( ! mInitialized )
190  {
192  addRootItems();
193  mInitialized = true;
194  }
195 }
196 
197 
198 Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex &index ) const
199 {
200  if ( !index.isValid() )
201  return Qt::ItemFlags();
202 
203  Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
204 
205  QgsDataItem *ptr = reinterpret_cast< QgsDataItem * >( index.internalPointer() );
206  if ( ptr->hasDragEnabled() )
207  flags |= Qt::ItemIsDragEnabled;
208 
209  if ( ptr->acceptDrop() )
210  flags |= Qt::ItemIsDropEnabled;
211 
212  if ( ptr->capabilities2() & QgsDataItem::Rename )
213  flags |= Qt::ItemIsEditable;
214 
215  return flags;
216 }
217 
218 QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
219 {
220  if ( !index.isValid() )
221  return QVariant();
222 
223  QgsDataItem *item = dataItem( index );
224  if ( !item )
225  {
226  return QVariant();
227  }
228  else if ( role == Qt::DisplayRole || role == Qt::EditRole )
229  {
230  return item->name();
231  }
232  else if ( role == QgsBrowserModel::SortRole )
233  {
234  return item->sortKey();
235  }
236  else if ( role == Qt::ToolTipRole )
237  {
238  return item->toolTip();
239  }
240  else if ( role == Qt::DecorationRole && index.column() == 0 )
241  {
242  return item->icon();
243  }
244  else if ( role == QgsBrowserModel::PathRole )
245  {
246  return item->path();
247  }
248  else if ( role == QgsBrowserModel::CommentRole )
249  {
250  if ( item->type() == QgsDataItem::Layer )
251  {
252  QgsLayerItem *lyrItem = qobject_cast<QgsLayerItem *>( item );
253  return lyrItem->comments();
254  }
255  return QVariant();
256  }
257  else
258  {
259  // unsupported role
260  return QVariant();
261  }
262 }
263 
264 bool QgsBrowserModel::setData( const QModelIndex &index, const QVariant &value, int role )
265 {
266  if ( !index.isValid() )
267  return false;
268 
269 
270  QgsDataItem *item = dataItem( index );
271  if ( !item )
272  {
273  return false;
274  }
275 
276  if ( !( item->capabilities2() & QgsDataItem::Rename ) )
277  return false;
278 
279  switch ( role )
280  {
281  case Qt::EditRole:
282  {
283  return item->rename( value.toString() );
284  }
285  }
286  return false;
287 }
288 
289 QVariant QgsBrowserModel::headerData( int section, Qt::Orientation orientation, int role ) const
290 {
291  Q_UNUSED( section )
292  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
293  {
294  return QVariant( "header" );
295  }
296 
297  return QVariant();
298 }
299 
300 int QgsBrowserModel::rowCount( const QModelIndex &parent ) const
301 {
302  //QgsDebugMsg(QString("isValid = %1 row = %2 column = %3").arg(parent.isValid()).arg(parent.row()).arg(parent.column()));
303 
304  if ( !parent.isValid() )
305  {
306  // root item: its children are top level items
307  return mRootItems.count(); // mRoot
308  }
309  else
310  {
311  // ordinary item: number of its children
312  QgsDataItem *item = dataItem( parent );
313  //if ( item ) QgsDebugMsg(QString("path = %1 rowCount = %2").arg(item->path()).arg(item->rowCount()) );
314  return item ? item->rowCount() : 0;
315  }
316 }
317 
318 bool QgsBrowserModel::hasChildren( const QModelIndex &parent ) const
319 {
320  if ( !parent.isValid() )
321  return !mRootItems.isEmpty(); // root item: its children are top level items
322 
323  QgsDataItem *item = dataItem( parent );
324  return item && item->hasChildren();
325 }
326 
327 int QgsBrowserModel::columnCount( const QModelIndex &parent ) const
328 {
329  Q_UNUSED( parent )
330  return 1;
331 }
332 
333 QModelIndex QgsBrowserModel::findPath( const QString &path, Qt::MatchFlag matchFlag )
334 {
335  return findPath( this, path, matchFlag );
336 }
337 
338 QModelIndex QgsBrowserModel::findPath( QAbstractItemModel *model, const QString &path, Qt::MatchFlag matchFlag )
339 {
340  if ( !model )
341  return QModelIndex();
342 
343  QModelIndex index; // starting from root
344  bool foundChild = true;
345 
346  while ( foundChild )
347  {
348  foundChild = false; // assume that the next child item will not be found
349 
350  for ( int i = 0; i < model->rowCount( index ); i++ )
351  {
352  QModelIndex idx = model->index( i, 0, index );
353 
354  QString itemPath = model->data( idx, PathRole ).toString();
355  if ( itemPath == path )
356  {
357  QgsDebugMsgLevel( "Arrived " + itemPath, 4 );
358  return idx; // we have found the item we have been looking for
359  }
360 
361  // paths are slash separated identifier
362  if ( path.startsWith( itemPath + '/' ) )
363  {
364  foundChild = true;
365  index = idx;
366  break;
367  }
368  }
369  }
370 
371  if ( matchFlag == Qt::MatchStartsWith )
372  return index;
373 
374  QgsDebugMsgLevel( QStringLiteral( "path not found" ), 4 );
375  return QModelIndex(); // not found
376 }
377 
378 QModelIndex QgsBrowserModel::findUri( const QString &uri, QModelIndex index )
379 {
380  for ( int i = 0; i < this->rowCount( index ); i++ )
381  {
382  QModelIndex idx = this->index( i, 0, index );
383 
384  if ( qobject_cast<QgsLayerItem *>( dataItem( idx ) ) )
385  {
386  QString itemUri = qobject_cast<QgsLayerItem *>( dataItem( idx ) )->uri();
387 
388  if ( itemUri == uri )
389  {
390  QgsDebugMsgLevel( "Arrived " + itemUri, 4 );
391  return idx; // we have found the item we have been looking for
392  }
393  }
394 
395  QModelIndex childIdx = findUri( uri, idx );
396  if ( childIdx.isValid() )
397  return childIdx;
398  }
399  return QModelIndex();
400 }
401 
403 {
404  // deprecated, no use
405 }
406 
408 {
409  // TODO: put items creating currently children in threads to deleteLater (does not seem urget because reload() is not used in QGIS)
410  beginResetModel();
411  removeRootItems();
412  addRootItems();
413  endResetModel();
414 }
415 
417 {
418  const QList< QFileInfo > drives = QDir::drives();
419  // remove any removed drives
420  const QStringList existingDrives = mDriveItems.keys();
421  for ( const QString &drivePath : existingDrives )
422  {
423  bool stillExists = false;
424  for ( const QFileInfo &drive : drives )
425  {
426  if ( drivePath == drive.absolutePath() )
427  {
428  stillExists = true;
429  break;
430  }
431  }
432 
433  if ( stillExists )
434  continue;
435 
436  // drive has been removed, remove corresponding item
437  if ( QgsDirectoryItem *driveItem = mDriveItems.value( drivePath ) )
438  removeRootItem( driveItem );
439  }
440 
441  for ( const QFileInfo &drive : drives )
442  {
443  const QString path = drive.absolutePath();
444 
445  if ( QgsDirectoryItem::hiddenPath( path ) )
446  continue;
447 
448  // does an item for this drive already exist?
449  if ( !mDriveItems.contains( path ) )
450  {
451  QgsDirectoryItem *item = new QgsDirectoryItem( nullptr, path, path );
452  item->setSortKey( QStringLiteral( " 3 %1" ).arg( path ) );
453 
454  mDriveItems.insert( path, item );
455  setupItemConnections( item );
456 
457  beginInsertRows( QModelIndex(), mRootItems.count(), mRootItems.count() );
458  mRootItems << item;
459  endInsertRows();
460  }
461  }
462 }
463 
464 QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex &parent ) const
465 {
466  if ( column < 0 || column >= columnCount() || row < 0 )
467  return QModelIndex();
468 
469  QgsDataItem *p = dataItem( parent );
470  const QVector<QgsDataItem *> &items = p ? p->children() : mRootItems;
471  QgsDataItem *item = items.value( row, nullptr );
472  return item ? createIndex( row, column, item ) : QModelIndex();
473 }
474 
475 QModelIndex QgsBrowserModel::parent( const QModelIndex &index ) const
476 {
477  QgsDataItem *item = dataItem( index );
478  if ( !item )
479  return QModelIndex();
480 
481  return findItem( item->parent() );
482 }
483 
485 {
486  const QVector<QgsDataItem *> &items = parent ? parent->children() : mRootItems;
487 
488  for ( int i = 0; i < items.size(); i++ )
489  {
490  if ( items[i] == item )
491  return createIndex( i, 0, item );
492 
493  QModelIndex childIndex = findItem( item, items[i] );
494  if ( childIndex.isValid() )
495  return childIndex;
496  }
497 
498  return QModelIndex();
499 }
500 
502 {
503  QgsDebugMsgLevel( "parent mPath = " + parent->path(), 3 );
504  QModelIndex idx = findItem( parent );
505  if ( !idx.isValid() )
506  return;
507  QgsDebugMsgLevel( QStringLiteral( "valid" ), 3 );
508  beginInsertRows( idx, first, last );
509  QgsDebugMsgLevel( QStringLiteral( "end" ), 3 );
510 }
512 {
513  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
514  endInsertRows();
515 }
517 {
518  QgsDebugMsgLevel( "parent mPath = " + parent->path(), 3 );
519  QModelIndex idx = findItem( parent );
520  if ( !idx.isValid() )
521  return;
522  beginRemoveRows( idx, first, last );
523 }
525 {
526  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
527  endRemoveRows();
528 }
530 {
531  QgsDebugMsgLevel( QStringLiteral( "Entered" ), 3 );
532  QModelIndex idx = findItem( item );
533  if ( !idx.isValid() )
534  return;
535  emit dataChanged( idx, idx );
536 }
538 {
539  if ( !item )
540  return;
541  QModelIndex idx = findItem( item );
542  if ( !idx.isValid() )
543  return;
544  QgsDebugMsgLevel( QStringLiteral( "item %1 state changed %2 -> %3" ).arg( item->path() ).arg( oldState ).arg( item->state() ), 4 );
545  emit stateChanged( idx, oldState );
546 }
547 
548 void QgsBrowserModel::setupItemConnections( QgsDataItem *item )
549 {
550  connect( item, &QgsDataItem::beginInsertItems,
552  connect( item, &QgsDataItem::endInsertItems,
554  connect( item, &QgsDataItem::beginRemoveItems,
556  connect( item, &QgsDataItem::endRemoveItems,
558  connect( item, &QgsDataItem::dataChanged,
560  connect( item, &QgsDataItem::stateChanged,
562 
563  // if it's a collection item, also forwards connectionsChanged
564  QgsDataCollectionItem *collectionItem = qobject_cast<QgsDataCollectionItem *>( item );
565  if ( collectionItem )
567 }
568 
569 QStringList QgsBrowserModel::mimeTypes() const
570 {
571  QStringList types;
572  // In theory the mime type convention is: application/x-vnd.<vendor>.<application>.<type>
573  // but it seems a bit over formalized. Would be an application/x-qgis-uri better?
574  types << QStringLiteral( "application/x-vnd.qgis.qgis.uri" );
575  return types;
576 }
577 
578 QMimeData *QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
579 {
581  const auto constIndexes = indexes;
582  for ( const QModelIndex &index : constIndexes )
583  {
584  if ( index.isValid() )
585  {
586  QgsDataItem *ptr = reinterpret_cast< QgsDataItem * >( index.internalPointer() );
587  QgsMimeDataUtils::Uri uri = ptr->mimeUri();
588  if ( uri.isValid() )
589  lst.append( uri );
590  }
591  }
592  return QgsMimeDataUtils::encodeUriList( lst );
593 }
594 
595 bool QgsBrowserModel::dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent )
596 {
597  Q_UNUSED( row )
598  Q_UNUSED( column )
599 
600  QgsDataItem *destItem = dataItem( parent );
601  if ( !destItem )
602  {
603  QgsDebugMsgLevel( QStringLiteral( "DROP PROBLEM!" ), 4 );
604  return false;
605  }
606 
607  return destItem->handleDrop( data, action );
608 }
609 
610 QgsDataItem *QgsBrowserModel::dataItem( const QModelIndex &idx ) const
611 {
612  void *v = idx.internalPointer();
613  QgsDataItem *d = reinterpret_cast<QgsDataItem *>( v );
614  Q_ASSERT( !v || d );
615  return d;
616 }
617 
618 bool QgsBrowserModel::canFetchMore( const QModelIndex &parent ) const
619 {
620  QgsDataItem *item = dataItem( parent );
621  // if ( item )
622  // QgsDebugMsg( QStringLiteral( "path = %1 canFetchMore = %2" ).arg( item->path() ).arg( item && ! item->isPopulated() ) );
623  return ( item && item->state() == QgsDataItem::NotPopulated );
624 }
625 
626 void QgsBrowserModel::fetchMore( const QModelIndex &parent )
627 {
628  QgsDataItem *item = dataItem( parent );
629 
630  if ( !item || item->state() == QgsDataItem::Populating || item->state() == QgsDataItem::Populated )
631  return;
632 
633  QgsDebugMsgLevel( "path = " + item->path(), 4 );
634 
635  item->populate();
636 }
637 
638 /* Refresh dir path */
639 void QgsBrowserModel::refresh( const QString &path )
640 {
641  QModelIndex index = findPath( path );
642  refresh( index );
643 }
644 
645 /* Refresh item */
646 void QgsBrowserModel::refresh( const QModelIndex &index )
647 {
648  QgsDataItem *item = dataItem( index );
649  if ( !item || item->state() == QgsDataItem::Populating )
650  return;
651 
652  QgsDebugMsgLevel( "Refresh " + item->path(), 4 );
653 
654  item->refresh();
655 }
656 
657 void QgsBrowserModel::addFavoriteDirectory( const QString &directory, const QString &name )
658 {
659  Q_ASSERT( mFavorites );
660  mFavorites->addDirectory( directory, name );
661 }
662 
663 void QgsBrowserModel::removeFavorite( const QModelIndex &index )
664 {
665  QgsDirectoryItem *item = qobject_cast<QgsDirectoryItem *>( dataItem( index ) );
666  if ( !item )
667  return;
668 
669  mFavorites->removeDirectory( item );
670 }
671 
672 void QgsBrowserModel::removeFavorite( QgsFavoriteItem *favorite )
673 {
674  if ( !favorite )
675  return;
676 
677  mFavorites->removeDirectory( favorite );
678 }
679 
681 {
682  QgsSettings settings;
683  QStringList hiddenItems = settings.value( QStringLiteral( "browser/hiddenPaths" ),
684  QStringList() ).toStringList();
685  int idx = hiddenItems.indexOf( item->path() );
686  if ( idx != -1 )
687  {
688  hiddenItems.removeAt( idx );
689  }
690  else
691  {
692  hiddenItems << item->path();
693  }
694  settings.setValue( QStringLiteral( "browser/hiddenPaths" ), hiddenItems );
695  if ( item->parent() )
696  {
697  item->parent()->deleteChildItem( item );
698  }
699  else
700  {
701  removeRootItem( item );
702  }
703 }
704 
705 
706 void QgsBrowserModel::removeRootItem( QgsDataItem *item )
707 {
708  int i = mRootItems.indexOf( item );
709  beginRemoveRows( QModelIndex(), i, i );
710  mRootItems.remove( i );
711  QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem * >( item );
712  if ( !mDriveItems.key( dirItem ).isEmpty() )
713  {
714  mDriveItems.remove( mDriveItems.key( dirItem ) );
715  }
716  item->deleteLater();
717  endRemoveRows();
718 }
719 
A Collection: logical collection of layers or subcollections, e.g.
Definition: qgsdataitem.h:550
virtual QVariant sortKey() const
Returns the sorting key for the item.
void beginInsertItems(QgsDataItem *parent, int first, int last)
QString path() const
Definition: qgsdataitem.h:293
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
bool canFetchMore(const QModelIndex &parent) const override
QString name() const
Returns the name of the item (the displayed text for the item).
Definition: qgsdataitem.h:284
void connectionsChanged()
Connections changed in the browser, forwarded to the widget and used to notify the provider dialogs o...
void dataChanged(QgsDataItem *item)
QString toolTip() const
Definition: qgsdataitem.h:325
void hidePath(QgsDataItem *item)
Hide the given path in the browser model.
QgsBrowserModel(QObject *parent=nullptr)
Constructor for QgsBrowserModel, with the specified parent object.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
void connectionsChanged()
Emitted when the provider&#39;s connections of the child items have changed This signal is normally forwa...
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void fetchMore(const QModelIndex &parent) override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void refreshDrives()
Refreshes the list of drive items, removing any corresponding to removed drives and adding newly adde...
virtual bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
Definition: qgsdataitem.h:180
QModelIndex findPath(const QString &path, Qt::MatchFlag matchFlag=Qt::MatchExactly)
Returns index of item with given path.
void beginRemoveItems(QgsDataItem *parent, int first, int last)
virtual QIcon icon()
Q_DECL_DEPRECATED void connectItem(QgsDataItem *item)
#define PROJECT_HOME_PREFIX
Type type() const
Definition: qgsdataitem.h:265
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
bool isValid() const
Returns whether the object contains valid data.
virtual bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
void itemStateChanged(QgsDataItem *item, QgsDataItem::State oldState)
QStringList mimeTypes() const override
QMap< QString, QgsDirectoryItem * > driveItems() const
Returns a map of the root drive items shown in the browser.
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
void endRemoveItems()
State state() const
Item path used to access path in the tree, see QgsDataItem::mPath.
QString homePath
Definition: qgsproject.h:94
QgsDirectoryItem * mProjectHome
static void deleteLater(QVector< QgsDataItem *> &items)
QgsDataItem * parent() const
Gets item parent.
Definition: qgsdataitem.h:270
QgsFavoritesItem * mFavorites
void beginRemoveItems(QgsDataItem *parent, int first, int last)
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QModelIndex parent(const QModelIndex &index) const override
QVector< QgsDataItem * > mRootItems
#define HOME_PREFIX
Children not yet created.
Definition: qgsdataitem.h:104
Creating children in separate thread (populating or refreshing)
Definition: qgsdataitem.h:105
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or nullptr if no item exists at the index...
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Definition: qgsdataitem.h:197
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
Definition: qgsdataitem.h:205
QgsBrowserWatcher(QgsDataItem *item)
bool hasChildren()
static bool hiddenPath(const QString &path)
Check if the given path is hidden from the browser model.
void reload()
Reload the whole model.
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
A directory: contains subdirectories and layers.
Definition: qgsdataitem.h:590
Base class for all items in the model.
Definition: qgsdataitem.h:49
Custom sort role, see QgsDataItem::sortKey()
QVector< QgsDataItem * > children() const
Definition: qgsdataitem.h:276
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
Returns the model index corresponding to the specified data item.
QModelIndex findUri(const QString &uri, QModelIndex index=QModelIndex())
Returns index of layer item with given uri.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
~QgsBrowserModel() override
void addFavoriteDirectory(const QString &directory, const QString &name=QString())
Adds a directory to the favorites group.
void beginInsertItems(QgsDataItem *parent, int first, int last)
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Contains various Favorites directories.
Definition: qgsdataitem.h:699
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
Item can be renamed.
Definition: qgsdataitem.h:214
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:438
int columnCount(const QModelIndex &parent=QModelIndex()) const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
void stateChanged(const QModelIndex &index, QgsDataItem::State oldState)
Emitted when item children fetch was finished.
void refresh(const QString &path)
Refresh item specified by path.
QList< QgsMimeDataUtils::Uri > UriList
void endInsertItems()
QMimeData * mimeData(const QModelIndexList &indexes) const override
void homePathChanged()
Emitted when the home path of the project changes.
Item that represents a layer that can be opened with one of the providers.
Definition: qgsdataitem.h:436
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
void itemDataChanged(QgsDataItem *item)
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual void populate(const QVector< QgsDataItem *> &children)
Children created.
Definition: qgsdataitem.h:106
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Definition: qgsdataitem.h:173
virtual QString comments() const
Returns comments of the layer.
Definition: qgsdataitem.h:501
void addRootItems()
Populates the model.
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application&#39;s data item provider registry, which keeps a list of data item providers that...
virtual void refresh(const QVector< QgsDataItem *> &children)
Refresh the items from a specified list of child items.
This is the interface for those who want to add custom data items to the browser tree.
void stateChanged(QgsDataItem *item, QgsDataItem::State oldState)
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
Definition: qgsdataitem.h:249