36#include <QApplication>
39#include <QElapsedTimer>
43#include <QRegularExpression>
48#include <QTreeWidgetItem>
50#include <QtConcurrentMap>
51#include <QtConcurrentRun>
53#include "moc_qgsdataitem.cpp"
55using namespace Qt::StringLiterals;
58#define CPL_SUPRESS_CPLUSPLUS
60#include "cpl_string.h"
86 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
89 QgsDebugError( u
"mFutureWatcher not finished (should not happen) -> waitForFinished()"_s );
90 mDeferredDelete =
true;
91 mFutureWatcher->waitForFinished();
99 const thread_local QRegularExpression rx(
"[\\\\/]" );
100 return QString(
string ).replace( rx, u
"|"_s );
122 child->deleteLater();
126 if ( mFutureWatcher && !mFutureWatcher->isFinished() )
128 QgsDebugMsgLevel( u
"mFutureWatcher not finished -> schedule to delete later"_s, 2 );
129 mDeferredDelete =
true;
133 QObject::deleteLater();
139 const auto constItems = items;
158 child->QObject::setParent(
nullptr );
159 child->moveToThread( targetThread );
161 QObject::moveToThread( targetThread );
172 return sPopulatingIcon->
icon();
174 if ( !
mIcon.isNull() )
193 return QVector<QgsDataItem *>();
216 if ( !mFutureWatcher )
218 mFutureWatcher = std::make_unique<QFutureWatcher<QVector<QgsDataItem *> >>( this );
222 mFutureWatcher->setFuture( QtConcurrent::run( runCreateChildren,
this ) );
227QVector<QgsDataItem *> QgsDataItem::runCreateChildren(
QgsDataItem *item )
241 child->moveToThread( qApp->thread() );
249 QgsDebugMsgLevel( u
"path = %1 children.size() = %2"_s.arg(
path() ).arg( mFutureWatcher->result().size() ), 3 );
254 QObject::deleteLater();
260 populate( mFutureWatcher->result() );
264 refresh( mFutureWatcher->result() );
279 std::function< void(
QgsDataItem *,
int ) > setChildAncestorDepthRecursive;
280 setChildAncestorDepthRecursive = [&setChildAncestorDepthRecursive](
QgsDataItem * child,
int depth )
286 setChildAncestorDepthRecursive( nextChild, depth + 1 );
294 setChildAncestorDepthRecursive( child, 1 );
329 if ( !mFutureWatcher )
331 mFutureWatcher = std::make_unique<QFutureWatcher<QVector<QgsDataItem *> >>( this );
334 mFutureWatcher->setFuture( QtConcurrent::run( runCreateChildren,
this ) );
343 mParent->refreshConnections( key );
358 QVector<QgsDataItem *> remove;
366 remove.append( child );
368 const auto constRemove = remove;
388 mChildren.value( index )->refresh( child->children() );
396 child->deleteLater();
432 disconnect(
this,
nullptr,
mParent,
nullptr );
467 if (
mChildren.at( i )->mName.localeAwareCompare( child->
mName ) >= 0 )
485 const int i =
mChildren.indexOf( child );
496 const int i =
mChildren.indexOf( child );
512 for (
int i = 0; i < items.size(); i++ )
514 Q_ASSERT_X( items[i],
"findItem", u
"item %1 is nullptr"_s.arg( i ).toLatin1() );
516 if ( items[i]->
equal( item ) )
528 while ( result && depth > 0 )
531 result = result->
parent();
538 return ( metaObject()->className() == other->metaObject()->className() &&
545 return QList<QAction *>();
602 if ( !sPopulatingIcon )
626 return QList<QMenu *>();
Provides global constants and enumerations for use throughout the application.
BrowserItemState
Browser item states.
@ NotPopulated
Children not yet created.
@ Populating
Creating children in separate thread (populating or refreshing).
@ Populated
Children created.
@ Fertile
Can create children. Even items without this capability may have children, but cannot create them,...
@ RefreshChildrenWhenItemIsRefreshed
When the item is refreshed, all its populated children will also be refreshed in turn.
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
@ Fast
CreateChildren() is fast enough to be run in main thread when refreshing items, most root items (wms,...
BrowserItemType
Browser item types.
@ Directory
Represents a file directory.
QFlags< BrowserItemCapability > BrowserItemCapabilities
Browser item capabilities.
Provides common functionality for database based connections.
virtual QIcon icon() const
Returns an icon representing the connection.
Animated icon is keeping an animation running if there are listeners connected to frameChanged.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Represents a coordinate reference system (CRS).
Base class for all items in the model.
void stateChanged(QgsDataItem *item, Qgis::BrowserItemState oldState)
Emitted when an item's state is changed.
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
static int findItem(QVector< QgsDataItem * > items, QgsDataItem *item)
bool hasChildren() const
Returns whether this item has children.
Qgis::BrowserItemType mType
virtual QList< QMenu * > menus(QWidget *parent)
Returns the list of menus available for this item.
int rowCount() const
Returns the number of rows of this item.
virtual void deleteChildItem(QgsDataItem *child)
Removes and deletes a child item, emitting relevant signals to the model.
QVector< QgsDataItem * > mChildren
int mCreatorAncestorDepth
Creator ancestor depth.
virtual QVariant sortKey() const
Returns the sorting key for the item.
Qgis::BrowserItemState mState
virtual bool handleDoubleClick()
Called when a user double clicks on the item.
virtual void refresh(const QVector< QgsDataItem * > &children)
Refresh the items from a specified list of child items.
void dataChanged(QgsDataItem *item)
Emitted when data changes for an item.
void setParent(QgsDataItem *parent)
Set item parent and connect / disconnect parent to / from item signals.
int creatorAncestorDepth() const
Returns the hierarchical depth of the item's original creator/source.
static void deleteLater(QVector< QgsDataItem * > &items)
void endRemoveItems()
Emitted after child items have been removed from this data item.
virtual bool layerCollection() const
Returns true if the data item is a collection of layers The default implementation returns false,...
QVector< QgsDataItem * > children() const
void beginRemoveItems(QgsDataItem *parent, int first, int last)
Emitted before child items are removed from this data item.
QgsDataItem * ancestorAtDepth(int depth) const
Returns the ancestor item at the specified depth.
virtual void deleteLater()
Safely delete the item:
virtual QgsAbstractDatabaseProviderConnection * databaseConnection() const
For data items that represent a DB connection or one of its children, this method returns a connectio...
Qgis::BrowserItemType type() const
QgsDataItem(Qgis::BrowserItemType type, QgsDataItem *parent, const QString &name, const QString &path, const QString &providerKey=QString())
Constructor for QgsDataItem, with the specified parent item.
virtual QVector< QgsDataItem * > createChildren()
Create children.
QMap< QString, QIcon > mIconMap
Qgis::BrowserItemState state() const
virtual void childrenCreated()
virtual QgsDataItem * removeChildItem(QgsDataItem *child)
Removes a child item and returns it without deleting it.
static QString pathComponent(const QString &component)
Create path component replacing path separators.
virtual QList< QAction * > actions(QWidget *parent)
Returns the list of actions available for this item.
QVariant mSortKey
Custom sort key. If invalid, name() will be used for sorting instead.
void updateIcon()
Will request a repaint of this icon.
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 connectionsChanged(const QString &providerKey=QString())
Emitted when the connections of the provider with the specified providerKey have changed.
virtual Q_DECL_DEPRECATED QgsMimeDataUtils::Uri mimeUri() const
Returns mime URI for the data item.
QString name() const
Returns the name of the item (the displayed text for the item).
virtual void setState(Qgis::BrowserItemState state)
Set item state.
void beginInsertItems(QgsDataItem *parent, int first, int last)
Emitted before child items are added to this data item.
virtual void refreshConnections(const QString &providerKey=QString())
Causes a data item provider to refresh all registered connections.
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
virtual void addChildItem(QgsDataItem *child, bool refresh=false)
Inserts a new child item.
void setName(const QString &name)
Sets the name of the item (the displayed text for the item).
QgsDataItem * parent() const
Gets item parent.
void moveToThread(QThread *targetThread)
Move object and all its descendants to thread.
virtual QgsMimeDataUtils::UriList mimeUris() const
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
QString providerKey() const
Returns the provider key that created this item (e.g.
void endInsertItems()
Emitted after child items have been added to this data item.
virtual Q_DECL_DEPRECATED bool setCrs(const QgsCoordinateReferenceSystem &crs)
Writes the selected crs into data source.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
void setProviderKey(const QString &value)
Sets the provider key that created this item (e.g.
virtual void populate(const QVector< QgsDataItem * > &children)
virtual bool equal(const QgsDataItem *other)
Returns true if this item is equal to another item (by testing item type and path).
virtual void depopulate()
Remove children recursively and set as not populated. This is used when refreshing collapsed items.
bool deferredDelete() const
Returns true if the item is scheduled to be deleted.
QgsErrorItem(QgsDataItem *parent, const QString &error, const QString &path)
QList< QgsMimeDataUtils::Uri > UriList
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
QString filePath
Path to file, if uri is associated with a file.
QString uri
Identifier of the data source recognized by its providerKey.