16 #include <QApplication> 18 #include <QtConcurrentMap> 34 : QFutureWatcher( nullptr )
42 return QString::localeAwareCompare( a->
name(), b->
name() ) < 0;
46 : QAbstractItemModel( parent )
67 beginRemoveRows( QModelIndex(), idx, idx );
72 mProjectHome = home.isNull() ? nullptr :
new QgsProjectHomeItem(
nullptr, tr(
"Project Home" ), home,
"project:" + home );
77 beginInsertRows( QModelIndex(), 0, 0 );
90 QStyle *style = QApplication::style();
91 QIcon homeIcon( style->standardPixmap( QStyle::SP_DirHomeIcon ) );
105 Q_FOREACH (
const QFileInfo &drive, QDir::drives() )
107 QString path = drive.absolutePath();
113 item->
setSortKey( QStringLiteral(
" 3 %1" ).arg( path ) );
120 QString path = QString(
"/Volumes" );
127 QMap<int, QgsDataItem *> providerMap;
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_ );
177 if ( ! mInitialized )
189 if ( !index.isValid() )
190 return Qt::ItemFlags();
192 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
196 flags |= Qt::ItemIsDragEnabled;
199 flags |= Qt::ItemIsDropEnabled;
205 if ( !index.isValid() )
213 else if ( role == Qt::DisplayRole )
221 else if ( role == Qt::ToolTipRole )
225 else if ( role == Qt::DecorationRole && index.column() == 0 )
252 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
254 return QVariant(
"header" );
264 if ( !parent.isValid() )
280 if ( !parent.isValid() )
295 return findPath(
this, path, matchFlag );
301 return QModelIndex();
304 bool foundChild =
true;
310 for (
int i = 0; i < model->rowCount( index ); i++ )
312 QModelIndex idx = model->index( i, 0, index );
314 QString itemPath = model->data( idx,
PathRole ).toString();
315 if ( itemPath == path )
322 if ( path.startsWith( itemPath +
'/' ) )
331 if ( matchFlag == Qt::MatchStartsWith )
335 return QModelIndex();
349 if ( column < 0 || column >=
columnCount() || row < 0 )
350 return QModelIndex();
355 return item ? createIndex( row, column, item ) : QModelIndex();
362 return QModelIndex();
371 for (
int i = 0; i < items.size(); i++ )
373 if ( items[i] == item )
374 return createIndex( i, 0, item );
376 QModelIndex childIndex =
findItem( item, items[i] );
377 if ( childIndex.isValid() )
381 return QModelIndex();
387 QModelIndex idx =
findItem( parent );
388 if ( !idx.isValid() )
391 beginInsertRows( idx, first, last );
402 QModelIndex idx =
findItem( parent );
403 if ( !idx.isValid() )
405 beginRemoveRows( idx, first, last );
416 if ( !idx.isValid() )
418 emit dataChanged( idx, idx );
425 if ( !idx.isValid() )
427 QgsDebugMsgLevel( QString(
"item %1 state changed %2 -> %3" ).arg( item->
path() ).arg( oldState ).arg( item->
state() ), 4 );
448 if ( collectionItem )
457 types << QStringLiteral(
"application/x-vnd.qgis.qgis.uri" );
464 Q_FOREACH (
const QModelIndex &
index, indexes )
466 if ( index.isValid() )
471 QMimeData *
mimeData =
new QMimeData();
472 QUrl url = QUrl::fromLocalFile( ptr->
path() );
475 mimeData->setUrls( urls );
504 void *v = idx.internalPointer();
567 QStringList hiddenItems = settings.
value( QStringLiteral(
"browser/hiddenPaths" ),
568 QStringList() ).toStringList();
569 int idx = hiddenItems.indexOf( item->
path() );
572 hiddenItems.removeAt( idx );
576 hiddenItems << item->
path();
578 settings.
setValue( QStringLiteral(
"browser/hiddenPaths" ), hiddenItems );
586 beginRemoveRows( QModelIndex(), i, i );
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)
This class is a composition of two QSettings instances:
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
Provides the number of rows of data exposed by the model.
void fetchMore(const QModelIndex &parent) override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Used to supply item data to views and delegates.
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)
Return index of item with given path.
void beginRemoveItems(QgsDataItem *parent, int first, int last)
void connectItem(QgsDataItem *item)
static QMimeData * encodeUriList(const UriList &layers)
void setIcon(const QIcon &icon)
bool isValid() const
Returns whether the object contains valid data.
void itemStateChanged(QgsDataItem *item, QgsDataItem::State oldState)
QStringList mimeTypes() const override
Returns a list of mime that can describe model indexes.
void projectSaved()
emitted when the project file has been written and closed
bool hasChildren(const QModelIndex &parent=QModelIndex()) const override
Item path used to access path in the tree, see QgsDataItem::mPath.
QgsDirectoryItem * mProjectHome
void setValue(const QString &key, const QVariant &value, const QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static void deleteLater(QVector< QgsDataItem *> &items)
QgsDataItem * parent() const
Get item parent.
QgsFavoritesItem * mFavorites
void beginRemoveItems(QgsDataItem *parent, int first, int last)
#define QgsDebugMsgLevel(str, level)
QModelIndex parent(const QModelIndex &index) const override
Returns the parent of the model item with the given index.
QVector< QgsDataItem * > mRootItems
void readProject(const QDomDocument &)
emitted when project is being read
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.
QgsDataItem * dataItem(const QModelIndex &idx) const
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 int capabilities()=0
Return combination of flags from QgsDataProvider::DataCapabilities.
virtual QgsMimeDataUtils::Uri mimeUri() const
Return 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
QModelIndex findItem(QgsDataItem *item, QgsDataItem *parent=nullptr) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Provides views with information to show in their headers.
~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)
Contains various Favorites directories.
void removeDirectory(QgsDirectoryItem *item)
Removes an existing directory from the favorites group.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Returns the index of the item in the model specified by the given row, column and parent index...
void addDirectory(const QString &directory, const QString &name=QString())
Adds a new directory to the favorites group.
static QgsProject * instance()
Returns the QgsProject singleton instance.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Provides the number of columns of data exposed by the model.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Handles the data supplied by a drag and drop operation that ended with the given action.
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
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
Represents a QGIS project.
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
Used by other components to obtain information about each item provided by the model.
virtual void populate(const QVector< QgsDataItem *> &children)
virtual bool acceptDrop()
Returns whether the item accepts drag and dropped layers - e.g.
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)