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 setupItemConnections( item );
105 Q_FOREACH (
const QFileInfo &drive, QDir::drives() )
107 const QString path = drive.absolutePath();
113 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
114 mDriveItems.insert( path, item );
116 setupItemConnections( item );
121 QString path = QString(
"/Volumes" );
127 QMap<int, QgsDataItem *> providerMap;
144 setupItemConnections( item );
145 providerMap.insertMulti( capabilities, item );
150 Q_FOREACH (
int key, providerMap.uniqueKeys() )
152 QList<QgsDataItem *> providerGroup = providerMap.values( key );
153 if ( providerGroup.size() > 1 )
155 std::sort( providerGroup.begin(), providerGroup.end(), cmpByDataItemName_ );
178 if ( ! mInitialized )
191 if ( !index.isValid() )
192 return Qt::ItemFlags();
194 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
201 return Qt::ItemFlags();
205 flags |= Qt::ItemIsDragEnabled;
208 flags |= Qt::ItemIsDropEnabled;
211 flags |= Qt::ItemIsEditable;
218 if ( !index.isValid() )
226 else if ( role == Qt::DisplayRole || role == Qt::EditRole )
234 else if ( role == Qt::ToolTipRole )
238 else if ( role == Qt::DecorationRole && index.column() == 0 )
264 if ( !index.isValid() )
281 return item->
rename( value.toString() );
290 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
292 return QVariant(
"header" );
302 if ( !parent.isValid() )
318 if ( !parent.isValid() )
333 return findPath(
this, path, matchFlag );
339 return QModelIndex();
342 bool foundChild =
true;
348 for (
int i = 0; i < model->rowCount( index ); i++ )
350 QModelIndex idx = model->index( i, 0, index );
352 QString itemPath = model->data( idx,
PathRole ).toString();
353 if ( itemPath == path )
360 if ( path.startsWith( itemPath +
'/' ) )
369 if ( matchFlag == Qt::MatchStartsWith )
373 return QModelIndex();
392 const QList< QFileInfo > drives = QDir::drives();
394 const QStringList existingDrives = mDriveItems.keys();
395 for (
const QString &drivePath : existingDrives )
397 bool stillExists =
false;
398 for (
const QFileInfo &drive : drives )
400 if ( drivePath == drive.absolutePath() )
411 if (
QgsDataItem *driveItem = mDriveItems.value( drivePath ) )
412 removeRootItem( driveItem );
415 for (
const QFileInfo &drive : drives )
417 const QString path = drive.absolutePath();
423 if ( !mDriveItems.contains( path ) )
426 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
428 mDriveItems.insert( path, item );
429 setupItemConnections( item );
440 if ( column < 0 || column >=
columnCount() || row < 0 )
441 return QModelIndex();
446 return item ? createIndex( row, column, item ) : QModelIndex();
453 return QModelIndex();
462 for (
int i = 0; i < items.size(); i++ )
464 if ( items[i] == item )
465 return createIndex( i, 0, item );
467 QModelIndex childIndex =
findItem( item, items[i] );
468 if ( childIndex.isValid() )
472 return QModelIndex();
478 QModelIndex idx =
findItem( parent );
479 if ( !idx.isValid() )
482 beginInsertRows( idx, first, last );
493 QModelIndex idx =
findItem( parent );
494 if ( !idx.isValid() )
496 beginRemoveRows( idx, first, last );
507 if ( !idx.isValid() )
509 emit dataChanged( idx, idx );
516 if ( !idx.isValid() )
518 QgsDebugMsgLevel( QStringLiteral(
"item %1 state changed %2 -> %3" ).arg( item->
path() ).arg( oldState ).arg( item->
state() ), 4 );
522 void QgsBrowserModel::setupItemConnections(
QgsDataItem *item )
539 if ( collectionItem )
548 types << QStringLiteral(
"application/x-vnd.qgis.qgis.uri" );
555 Q_FOREACH (
const QModelIndex &
index, indexes )
557 if ( index.isValid() )
585 void *v = idx.internalPointer();
648 QStringList hiddenItems = settings.
value( QStringLiteral(
"browser/hiddenPaths" ),
649 QStringList() ).toStringList();
650 int idx = hiddenItems.indexOf( item->
path() );
653 hiddenItems.removeAt( idx );
657 hiddenItems << item->
path();
659 settings.
setValue( QStringLiteral(
"browser/hiddenPaths" ), hiddenItems );
666 removeRootItem( item );
671 void QgsBrowserModel::removeRootItem(
QgsDataItem *item )
674 beginRemoveRows( QModelIndex(), i, i );
676 if ( !mDriveItems.key( item ).isEmpty() )
678 mDriveItems.remove( mDriveItems.key( item ) );
A Collection: logical collection of layers or subcollections, e.g.
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
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
void connectionsChanged()
Connections changed in the browser, forwarded to the widget and used to notify the provider dialogs o...
void dataChanged(QgsDataItem *item)
static void deleteLater(QVector< QgsDataItem * > &items)
QString name() const
Returns the name of the item (the displayed text for the 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.
virtual Capabilities capabilities2() const
Returns the capabilities for the data item.
This class is a composition of two QSettings instances:
QgsDataItem * parent() const
Gets item parent.
virtual QgsDataItem * createDataItem(const QString &path, QgsDataItem *parentItem)=0
Create a new instance of QgsDataItem (or null) for given path and parent item.
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
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.
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 PROJECT_HOME_PREFIX
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static QMimeData * encodeUriList(const UriList &layers)
Encodes a URI list to a new QMimeData object.
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
void projectSaved()
Emitted when the project file has been written and closed.
QVector< QgsDataItem * > children() const
bool isValid() const
Returns whether the object contains valid data.
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Item path used to access path in the tree, see QgsDataItem::mPath.
virtual QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
QgsDirectoryItem * mProjectHome
QgsFavoritesItem * mFavorites
void beginRemoveItems(QgsDataItem *parent, int first, int last)
#define QgsDebugMsgLevel(str, level)
QModelIndex parent(const QModelIndex &index) const override
QVector< QgsDataItem * > mRootItems
QgsDataItem * dataItem(const QModelIndex &idx) const
Returns the data item at the specified index, or a nullptr if no item exists at the index...
void readProject(const QDomDocument &)
Emitted when a project is being read.
virtual bool hasDragEnabled() const
Returns true if the item may be dragged.
Children not yet created.
Creating children in separate thread (populating or refreshing)
void initialize()
Delayed initialization, needed because the provider registry must be already populated.
void removeFavorite(const QModelIndex &index)
Removes a favorite directory from its corresponding model index.
virtual int capabilities()=0
Returns combination of flags from QgsDataProvider::DataCapabilities.
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.
virtual QVariant sortKey() const
Returns the sorting key for the item.
Custom sort role, see QgsDataItem::sortKey()
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.
virtual void populate(const QVector< QgsDataItem * > &children)
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.
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
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.
virtual QString comments() const
Returns comments of the layer.
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 void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
virtual QString name()=0
Human-readable name of the provider name.
void itemDataChanged(QgsDataItem *item)
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
Returns the model index corresponding to the specified data item.
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...
This is the interface for those who want to add custom data items to the browser tree.
void stateChanged(QgsDataItem *item, QgsDataItem::State oldState)