16 #include <QApplication> 18 #include <QtConcurrentMap> 33 #define PROJECT_HOME_PREFIX "project:" 34 #define HOME_PREFIX "home:" 37 : QFutureWatcher( nullptr )
45 return QString::localeAwareCompare( a->
name(), b->
name() ) < 0;
49 : QAbstractItemModel( parent )
70 beginRemoveRows( QModelIndex(), idx, idx );
80 beginInsertRows( QModelIndex(), 0, 0 );
93 QStringLiteral(
"special:Home" ) );
95 setupItemConnections( item );
107 const auto drives { QDir::drives() };
108 for (
const QFileInfo &drive : drives )
110 const QString path = drive.absolutePath();
116 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
117 mDriveItems.insert( path, item );
119 setupItemConnections( item );
124 QString path = QString(
"/Volumes" );
130 QMap<int, QgsDataItem *> providerMap;
135 int capabilities = pr->capabilities();
142 QgsDataItem *item = pr->createDataItem( QString(),
nullptr );
150 setupItemConnections( item );
151 providerMap.insertMulti( capabilities, item );
156 const auto constUniqueKeys = providerMap.uniqueKeys();
157 for (
int key : constUniqueKeys )
159 QList<QgsDataItem *> providerGroup = providerMap.values( key );
160 if ( providerGroup.size() > 1 )
162 std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
165 const auto constProviderGroup = providerGroup;
193 if ( ! mInitialized )
203 if ( !index.isValid() )
204 return Qt::ItemFlags();
206 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
213 return Qt::ItemFlags();
217 flags |= Qt::ItemIsDragEnabled;
221 flags |= Qt::ItemIsDropEnabled;
225 flags |= Qt::ItemIsEditable;
232 if ( !index.isValid() )
240 else if ( role == Qt::DisplayRole || role == Qt::EditRole )
248 else if ( role == Qt::ToolTipRole )
252 else if ( role == Qt::DecorationRole && index.column() == 0 )
282 if ( !index.isValid() )
300 return item->
rename( value.toString() );
310 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
312 return QVariant(
"header" );
322 if ( !parent.isValid() )
338 if ( !parent.isValid() )
353 return findPath(
this, path, matchFlag );
359 return QModelIndex();
362 bool foundChild =
true;
368 for (
int i = 0; i < model->rowCount( index ); i++ )
370 QModelIndex idx = model->index( i, 0, index );
372 QString itemPath = model->data( idx,
PathRole ).toString();
373 if ( itemPath == path )
380 if ( path.startsWith( itemPath +
'/' ) )
389 if ( matchFlag == Qt::MatchStartsWith )
393 return QModelIndex();
398 for (
int i = 0; i < this->
rowCount( index ); i++ )
400 QModelIndex idx = this->
index( i, 0, index );
402 if ( qobject_cast<QgsLayerItem *>(
dataItem( idx ) ) )
406 if ( itemUri == uri )
413 QModelIndex childIdx =
findUri( uri, idx );
414 if ( childIdx.isValid() )
417 return QModelIndex();
436 const QList< QFileInfo > drives = QDir::drives();
438 const QStringList existingDrives = mDriveItems.keys();
439 for (
const QString &drivePath : existingDrives )
441 bool stillExists =
false;
442 for (
const QFileInfo &drive : drives )
444 if ( drivePath == drive.absolutePath() )
456 removeRootItem( driveItem );
459 for (
const QFileInfo &drive : drives )
461 const QString path = drive.absolutePath();
467 if ( !mDriveItems.contains( path ) )
470 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
472 mDriveItems.insert( path, item );
473 setupItemConnections( item );
484 if ( column < 0 || column >=
columnCount() || row < 0 )
485 return QModelIndex();
490 return item ? createIndex( row, column, item ) : QModelIndex();
497 return QModelIndex();
506 for (
int i = 0; i < items.size(); i++ )
508 if ( items[i] == item )
509 return createIndex( i, 0, item );
511 QModelIndex childIndex =
findItem( item, items[i] );
512 if ( childIndex.isValid() )
515 return QModelIndex();
521 QModelIndex idx =
findItem( parent );
522 if ( !idx.isValid() )
525 beginInsertRows( idx, first, last );
536 QModelIndex idx =
findItem( parent );
537 if ( !idx.isValid() )
539 beginRemoveRows( idx, first, last );
550 if ( !idx.isValid() )
552 emit dataChanged( idx, idx );
559 if ( !idx.isValid() )
561 QgsDebugMsgLevel( QStringLiteral(
"item %1 state changed %2 -> %3" ).arg( item->
path() ).arg( oldState ).arg( item->
state() ), 4 );
565 void QgsBrowserModel::setupItemConnections(
QgsDataItem *item )
582 if ( collectionItem )
591 types << QStringLiteral(
"application/x-vnd.qgis.qgis.uri" );
598 const auto constIndexes = indexes;
599 for (
const QModelIndex &
index : constIndexes )
601 if (
index.isValid() )
628 void *v = idx.internalPointer();
699 QStringList hiddenItems = settings.
value( QStringLiteral(
"browser/hiddenPaths" ),
700 QStringList() ).toStringList();
701 int idx = hiddenItems.indexOf( item->
path() );
704 hiddenItems.removeAt( idx );
708 hiddenItems << item->
path();
710 settings.
setValue( QStringLiteral(
"browser/hiddenPaths" ), hiddenItems );
717 removeRootItem( item );
722 void QgsBrowserModel::removeRootItem(
QgsDataItem *item )
725 beginRemoveRows( QModelIndex(), i, i );
728 if ( !mDriveItems.key( dirItem ).isEmpty() )
730 mDriveItems.remove( mDriveItems.key( dirItem ) );
A Collection: logical collection of layers or subcollections, e.g.
virtual QVariant sortKey() const
Returns the sorting key for the item.
void beginInsertItems(QgsDataItem *parent, int first, int last)
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).
void connectionsChanged()
Connections changed in the browser, forwarded to the widget and used to notify the provider dialogs o...
void dataChanged(QgsDataItem *item)
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:
void connectionsChanged()
Emitted when the provider'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...
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)
Q_DECL_DEPRECATED void connectItem(QgsDataItem *item)
#define Q_NOWARN_DEPRECATED_PUSH
#define PROJECT_HOME_PREFIX
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 Q_DECL_DEPRECATED bool rename(const QString &name)
Sets a new name for the item, and returns true if the item was successfully renamed.
void setProviderKey(const QString &value)
Sets the provider key that created this item (e.g.
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
Item path used to access path in the tree, see QgsDataItem::mPath.
QgsDirectoryItem * mProjectHome
static void deleteLater(QVector< QgsDataItem *> &items)
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
QgsDataItem * parent() const
Gets item parent.
QgsFavoritesItem * mFavorites
void beginRemoveItems(QgsDataItem *parent, int first, int last)
#define QgsDebugMsgLevel(str, level)
QModelIndex parent(const QModelIndex &index) const override
QVector< QgsDataItem * > mRootItems
Children not yet created.
virtual Q_DECL_DEPRECATED bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
Creating children in separate thread (populating or refreshing)
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.
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
QgsBrowserWatcher(QgsDataItem *item)
static bool hiddenPath(const QString &path)
Check if the given path is hidden from the browser model.
void reload()
Reload the whole model.
A directory: contains subdirectories and layers.
Base class for all items in the model.
Custom sort role, see QgsDataItem::sortKey()
QVector< QgsDataItem * > children() const
#define Q_NOWARN_DEPRECATED_POP
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)
QString providerKey() const
Returns the provider key that created this item (e.g.
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.
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
Data item provider key that created the item,.
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.
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
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.
virtual Q_DECL_DEPRECATED bool handleDrop(const QMimeData *, Qt::DropAction)
Attempts to process the mime data dropped on this item.
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)
virtual QString comments() const
Returns comments of the layer.
void addRootItems()
Populates the model.
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application'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.