31#include <QFileSystemWatcher>
37#include "moc_qgsdirectoryitem.cpp"
39using namespace Qt::StringLiterals;
59void QgsDirectoryItem::init(
const QString &dirName )
68 mIsSymLink = fi.isSymLink();
71 switch ( mMonitoring )
84 settings.
beginGroup( u
"qgis/browserPathColors"_s );
86 settingKey.replace(
'/',
"|||"_L1 );
87 if ( settings.
childKeys().contains( settingKey ) )
89 const QString colorString = settings.
value( settingKey ).toString();
90 mIconColor = QColor( colorString );
101 switch ( mMonitoring )
114 const QVector<QgsDataItem *> childItems =
children();
117 if (
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem *>( child ) )
118 dirItem->reevaluateMonitoring();
121 createOrDestroyFileSystemWatcher();
124void QgsDirectoryItem::createOrDestroyFileSystemWatcher()
126 if ( !mMonitored && mFileSystemWatcher )
128 mFileSystemWatcher->deleteLater();
129 mFileSystemWatcher =
nullptr;
133 mFileSystemWatcher =
new QFileSystemWatcher(
this );
134 mFileSystemWatcher->addPath(
mDirPath );
146 if ( color == mIconColor )
156 settings.
beginGroup( u
"qgis/browserPathColors"_s );
157 QString settingKey = directory;
158 settingKey.replace(
'/',
"|||"_L1 );
159 if ( color.isValid() )
160 settings.
setValue( settingKey, color.name( QColor::HexArgb ) );
162 settings.
remove( settingKey );
169 return homeDirIcon( mIconColor, mIconColor.darker() );
176 if ( mIsDir && mIsSymLink )
183 return openDirIcon( mIconColor, mIconColor.darker() );
186 return iconDir( mIconColor, mIconColor.darker() );
199 QStringList noMonitorDirs = settings.
value( u
"qgis/disableMonitorItemUris"_s, QStringList() ).toStringList();
200 QStringList alwaysMonitorDirs = settings.
value( u
"qgis/alwaysMonitorItemUris"_s, QStringList() ).toStringList();
202 switch ( mMonitoring )
207 noMonitorDirs.removeAll(
mDirPath );
208 settings.
setValue( u
"qgis/disableMonitorItemUris"_s, noMonitorDirs );
210 alwaysMonitorDirs.removeAll(
mDirPath );
211 settings.
setValue( u
"qgis/alwaysMonitorItemUris"_s, alwaysMonitorDirs );
219 if ( !noMonitorDirs.contains(
mDirPath ) )
222 settings.
setValue( u
"qgis/disableMonitorItemUris"_s, noMonitorDirs );
225 alwaysMonitorDirs.removeAll(
mDirPath );
226 settings.
setValue( u
"qgis/alwaysMonitorItemUris"_s, alwaysMonitorDirs );
234 noMonitorDirs.removeAll(
mDirPath );
235 settings.
setValue( u
"qgis/disableMonitorItemUris"_s, noMonitorDirs );
237 if ( !alwaysMonitorDirs.contains(
mDirPath ) )
239 alwaysMonitorDirs.append(
mDirPath );
240 settings.
setValue( u
"qgis/alwaysMonitorItemUris"_s, alwaysMonitorDirs );
248 const QVector<QgsDataItem *> childItems =
children();
251 if (
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem *>( child ) )
252 dirItem->reevaluateMonitoring();
255 createOrDestroyFileSystemWatcher();
265 const QStringList entries = dir.entryList( QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase );
266 for (
const QString &subdir : entries )
274 const QString subdirPath = dir.absoluteFilePath( subdir );
278 const QString
path =
mPath + (
mPath.endsWith(
'/' ) ? QString() : u
"/"_s ) + subdir;
282 bool handledByProvider =
false;
285 if ( provider->handlesDirectoryPath( subdirPath ) )
287 handledByProvider =
true;
291 if ( handledByProvider )
301 const QStringList fileEntries = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::Files, QDir::Name );
302 for (
const QString &
name : fileEntries )
310 const QString
path = dir.absoluteFilePath(
name );
311 const QFileInfo fileInfo(
path );
322 bool createdItem =
false;
349 if ( fileInfo.suffix().compare(
"qgs"_L1, Qt::CaseInsensitive ) == 0 || fileInfo.suffix().compare(
"qgz"_L1, Qt::CaseInsensitive ) == 0 )
367 if ( !mFileSystemWatcher )
369 mFileSystemWatcher =
new QFileSystemWatcher(
this );
370 mFileSystemWatcher->addPath(
mDirPath );
373 mLastScan = QDateTime::currentDateTime();
377 if ( mFileSystemWatcher )
379 delete mFileSystemWatcher;
380 mFileSystemWatcher =
nullptr;
388 if ( mLastScan.msecsTo( QDateTime::currentDateTime() ) <
QgsSettings().value( u
"browser/minscaninterval"_s, 10000 ).toInt() )
395 mRefreshLater =
true;
409 QTimer::singleShot( 100,
this, [
this] {
refresh(); } );
416 const QStringList hiddenItems = settings.
value( u
"browser/hiddenPaths"_s, QStringList() ).toStringList();
417 const int idx = hiddenItems.indexOf(
path );
424 if ( settings.
value( u
"qgis/disableMonitorItemUris"_s, QStringList() ).toStringList().contains(
path ) )
426 else if ( settings.
value( u
"qgis/alwaysMonitorItemUris"_s, QStringList() ).toStringList().contains(
path ) )
435 const QString originalPath = QDir::cleanPath(
path );
436 QString currentPath = originalPath;
438 while ( currentPath != prevPath )
440 prevPath = currentPath;
441 currentPath = QFileInfo( currentPath ).path();
463 return QgsSettings().
value( u
"/qgis/monitorDirectoriesInBrowser"_s,
true ).toBool();
472 QgsDebugMsgLevel( u
"directory changed during createChildren() -> refresh() again"_s, 3 );
473 mRefreshLater =
false;
482 if ( mFileSystemWatcher && mMonitored )
493 const QgsDirectoryItem *otherDirItem = qobject_cast< const QgsDirectoryItem * >( other );
519 : QTreeWidget( parent )
521 setRootIsDecorated(
false );
526 labels << tr(
"Name" ) << tr(
"Size" ) << tr(
"Date" ) << tr(
"Permissions" ) << tr(
"Owner" ) << tr(
"Group" ) << tr(
"Type" );
527 setHeaderLabels( labels );
534 QList<QTreeWidgetItem *> items;
536 const QDir dir( path );
537 const QStringList entries = dir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase );
538 for (
const QString &name : entries )
540 const QFileInfo fi( dir.absoluteFilePath( name ) );
544 if ( fi.size() > 1024 )
546 size = u
"%1 KiB"_s.arg( QLocale().toString( fi.size() / 1024.0,
'f', 1 ) );
548 else if ( fi.size() > 1.048576e6 )
550 size = u
"%1 MiB"_s.arg( QLocale().toString( fi.size() / 1.048576e6,
'f', 1 ) );
554 size = u
"%1 B"_s.arg( fi.size() );
557 texts << QLocale().toString( fi.lastModified(), QLocale::ShortFormat );
559 perm += fi.permission( QFile::ReadOwner ) ?
'r' :
'-';
560 perm += fi.permission( QFile::WriteOwner ) ?
'w' :
'-';
561 perm += fi.permission( QFile::ExeOwner ) ?
'x' :
'-';
563 perm += fi.permission( QFile::ReadGroup ) ?
'r' :
'-';
564 perm += fi.permission( QFile::WriteGroup ) ?
'w' :
'-';
565 perm += fi.permission( QFile::ExeGroup ) ?
'x' :
'-';
566 perm += fi.permission( QFile::ReadOther ) ?
'r' :
'-';
567 perm += fi.permission( QFile::WriteOther ) ?
'w' :
'-';
568 perm += fi.permission( QFile::ExeOther ) ?
'x' :
'-';
576 if ( fi.isDir() && fi.isSymLink() )
578 type = tr(
"folder" );
581 else if ( fi.isDir() )
583 type = tr(
"folder" );
584 icon = iconDirectory;
586 else if ( fi.isFile() && fi.isSymLink() )
591 else if ( fi.isFile() )
599 QTreeWidgetItem *item =
new QTreeWidgetItem( texts );
600 item->setIcon( 0, icon );
604 addTopLevelItems( items );
608 const QList<QVariant> lst = settings.
value( u
"dataitem/directoryHiddenColumns"_s ).toList();
609 for (
const QVariant &colVariant : lst )
611 setColumnHidden( colVariant.toInt(),
true );
617 if ( event->button() == Qt::RightButton )
623 labels << tr(
"Name" ) << tr(
"Size" ) << tr(
"Date" ) << tr(
"Permissions" ) << tr(
"Owner" ) << tr(
"Group" ) << tr(
"Type" );
624 for (
int i = 0; i < labels.count(); i++ )
627 action->setObjectName( QString::number( i ) );
628 action->setCheckable(
true );
629 action->setChecked( !isColumnHidden( i ) );
632 popupMenu.exec( event->globalPos() );
638 QAction *action = qobject_cast<QAction *>( sender() );
642 const int columnIndex = action->objectName().toInt();
643 setColumnHidden( columnIndex, !isColumnHidden( columnIndex ) );
648 for (
int i = 0; i < columnCount(); i++ )
650 if ( isColumnHidden( i ) )
651 lst.append( QVariant( i ) );
653 settings.
setValue( u
"dataitem/directoryHiddenColumns"_s, lst );
@ Files
Can provides items which corresponds to files.
@ Directories
Can provides items which corresponds to directories.
BrowserItemState
Browser item states.
@ NotPopulated
Children not yet created.
@ Populating
Creating children in separate thread (populating or refreshing).
@ Populated
Children created.
@ ItemRepresentsFile
Item's path() directly represents a file on disk.
BrowserDirectoryMonitoring
Browser directory item monitoring switches.
@ Default
Use default logic to determine whether directory should be monitored.
@ AlwaysMonitor
Always monitor the directory, regardless of the default logic.
@ NeverMonitor
Never monitor the directory, regardless of the default logic.
QFlags< DataItemProviderCapability > DataItemProviderCapabilities
Capabilities for data item providers.
@ Directory
Represents a file directory.
static QgsDataItemProviderRegistry * dataItemProviderRegistry()
Returns the application's data item provider registry, which keeps a list of data item providers that...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QIcon homeDirIcon(const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Shared home directory icon.
QgsDataCollectionItem(QgsDataItem *parent, const QString &name, const QString &path=QString(), const QString &providerKey=QString())
Constructor for QgsDataCollectionItem, with the specified parent item.
static QIcon iconDir(const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Returns the standard browser directory icon.
static QIcon openDirIcon(const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Shared open directory icon.
QList< QgsDataItemProvider * > providers() const
Returns the list of available providers.
Interface for providers that add custom data items to the browser tree.
void setSortKey(const QVariant &key)
Sets a custom sorting key for the item.
Qgis::BrowserItemType mType
void setToolTip(const QString &msg)
void dataChanged(QgsDataItem *item)
Emitted when data changes for an item.
QVector< QgsDataItem * > children() const
virtual void deleteLater()
Safely delete the item:
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.
Qgis::BrowserItemState state() const
virtual void childrenCreated()
QString name() const
Returns the name of the item (the displayed text for the item).
virtual void setState(Qgis::BrowserItemState state)
Set item state.
virtual void setCapabilities(Qgis::BrowserItemCapabilities capabilities)
Sets the capabilities for the data item.
QgsDataItem * parent() const
Gets item parent.
QString providerKey() const
Returns the provider key that created this item (e.g.
virtual Qgis::BrowserItemCapabilities capabilities2() const
Returns the capabilities for the data item.
QVector< QgsDataItem * > createChildren() override
Create children.
Q_DECL_DEPRECATED QWidget * paramWidget() override
static Qgis::BrowserDirectoryMonitoring monitoringForPath(const QString &path)
Returns the monitoring setting for a directory path.
QgsMimeDataUtils::UriList mimeUris() const override
Returns mime URIs for the data item, most data providers will only return a single URI but some data ...
Qgis::BrowserDirectoryMonitoring monitoring() const
Returns the monitoring setting for this directory item.
static void setCustomColor(const QString &directory, const QColor &color)
Sets a custom icon color to use for the items for the corresponding directory path.
bool equal(const QgsDataItem *other) override
Returns true if this item is equal to another item (by testing item type and path).
QString dirPath() const
Returns the full path to the directory the item represents.
void setMonitoring(Qgis::BrowserDirectoryMonitoring monitoring)
Sets the monitoring setting for this directory.
QColor iconColor() const
Returns the directory's icon color.
void childrenCreated() override
void setIconColor(const QColor &color)
Sets the directory's icon color.
QgsDirectoryItem(QgsDataItem *parent, const QString &name, const QString &path)
Constructor for QgsDirectoryItem, with the specified parent item.
void reevaluateMonitoring()
Re-evaluate whether the directory item should be monitored for changes.
static bool hiddenPath(const QString &path)
Check if the given path is hidden from the browser model.
void setState(Qgis::BrowserItemState state) override
Set item state.
static bool pathShouldByMonitoredByDefault(const QString &path)
Returns true if a directory path should be monitored by default.
static bool pathIsSlowDevice(const QString &path)
Returns true if the specified path is assumed to reside on a slow device, e.g.
static bool isVsiArchiveFileExtension(const QString &extension)
Returns true if a file extension is a supported archive style container (e.g.
QList< QgsMimeDataUtils::Uri > UriList
QVariant sortKey() const override
Returns the sorting key for the item.
QgsProjectHomeItem(QgsDataItem *parent, const QString &name, const QString &dirPath, const QString &path)
Constructor for QgsProjectHomeItem.
Data item that can be used to represent QGIS projects.
Stores settings for use within QGIS.
void endGroup()
Resets the group to what it was before the corresponding beginGroup() call.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
QStringList childKeys() const
Returns a list of all top-level keys that can be read using the QSettings object.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
static QgsDataItem * itemFromPath(QgsDataItem *parent, const QString &path, const QString &name)
Creates a new data item from the specified path.
#define QgsDebugMsgLevel(str, level)
QString filePath
Path to file, if uri is associated with a file.
QString uri
Identifier of the data source recognized by its providerKey.
QString name
Human readable name to be used e.g. in layer tree.
QString layerType
Type of URI.