31#include <QFileSystemWatcher>
37#include "moc_qgsdirectoryitem.cpp"
39using namespace Qt::StringLiterals;
61void QgsDirectoryItem::init(
const QString &dirName )
70 mIsSymLink = fi.isSymLink();
73 switch ( mMonitoring )
86 settings.
beginGroup( u
"qgis/browserPathColors"_s );
88 settingKey.replace(
'/',
"|||"_L1 );
89 if ( settings.
childKeys().contains( settingKey ) )
91 const QString colorString = settings.
value( settingKey ).toString();
92 mIconColor = QColor( colorString );
103 switch ( mMonitoring )
116 const QVector<QgsDataItem *> childItems =
children();
119 if (
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem *>( child ) )
120 dirItem->reevaluateMonitoring();
123 createOrDestroyFileSystemWatcher();
126void QgsDirectoryItem::createOrDestroyFileSystemWatcher()
128 if ( !mMonitored && mFileSystemWatcher )
130 mFileSystemWatcher->deleteLater();
131 mFileSystemWatcher =
nullptr;
135 mFileSystemWatcher =
new QFileSystemWatcher(
this );
136 mFileSystemWatcher->addPath(
mDirPath );
148 if ( color == mIconColor )
158 settings.
beginGroup( u
"qgis/browserPathColors"_s );
159 QString settingKey = directory;
160 settingKey.replace(
'/',
"|||"_L1 );
161 if ( color.isValid() )
162 settings.
setValue( settingKey, color.name( QColor::HexArgb ) );
164 settings.
remove( settingKey );
171 return homeDirIcon( mIconColor, mIconColor.darker() );
178 if ( mIsDir && mIsSymLink )
180 return mIconColor.isValid()
187 return openDirIcon( mIconColor, mIconColor.darker() );
190 return iconDir( mIconColor, mIconColor.darker() );
203 QStringList noMonitorDirs = settings.
value( u
"qgis/disableMonitorItemUris"_s, QStringList() ).toStringList();
204 QStringList alwaysMonitorDirs = settings.
value( u
"qgis/alwaysMonitorItemUris"_s, QStringList() ).toStringList();
206 switch ( mMonitoring )
211 noMonitorDirs.removeAll(
mDirPath );
212 settings.
setValue( u
"qgis/disableMonitorItemUris"_s, noMonitorDirs );
214 alwaysMonitorDirs.removeAll(
mDirPath );
215 settings.
setValue( u
"qgis/alwaysMonitorItemUris"_s, alwaysMonitorDirs );
223 if ( !noMonitorDirs.contains(
mDirPath ) )
226 settings.
setValue( u
"qgis/disableMonitorItemUris"_s, noMonitorDirs );
229 alwaysMonitorDirs.removeAll(
mDirPath );
230 settings.
setValue( u
"qgis/alwaysMonitorItemUris"_s, alwaysMonitorDirs );
238 noMonitorDirs.removeAll(
mDirPath );
239 settings.
setValue( u
"qgis/disableMonitorItemUris"_s, noMonitorDirs );
241 if ( !alwaysMonitorDirs.contains(
mDirPath ) )
243 alwaysMonitorDirs.append(
mDirPath );
244 settings.
setValue( u
"qgis/alwaysMonitorItemUris"_s, alwaysMonitorDirs );
252 const QVector<QgsDataItem *> childItems =
children();
255 if (
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem *>( child ) )
256 dirItem->reevaluateMonitoring();
259 createOrDestroyFileSystemWatcher();
269 const QStringList entries = dir.entryList( QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase );
270 for (
const QString &subdir : entries )
278 const QString subdirPath = dir.absoluteFilePath( subdir );
282 const QString
path =
mPath + (
mPath.endsWith(
'/' ) ? QString() : u
"/"_s ) + subdir;
286 bool handledByProvider =
false;
289 if ( provider->handlesDirectoryPath( subdirPath ) )
291 handledByProvider =
true;
295 if ( handledByProvider )
305 const QStringList fileEntries = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::Files, QDir::Name );
306 for (
const QString &
name : fileEntries )
314 const QString
path = dir.absoluteFilePath(
name );
315 const QFileInfo fileInfo(
path );
326 bool createdItem =
false;
354 if ( fileInfo.suffix().compare(
"qgs"_L1, Qt::CaseInsensitive ) == 0 ||
355 fileInfo.suffix().compare(
"qgz"_L1, Qt::CaseInsensitive ) == 0 )
374 if ( !mFileSystemWatcher )
376 mFileSystemWatcher =
new QFileSystemWatcher(
this );
377 mFileSystemWatcher->addPath(
mDirPath );
380 mLastScan = QDateTime::currentDateTime();
384 if ( mFileSystemWatcher )
386 delete mFileSystemWatcher;
387 mFileSystemWatcher =
nullptr;
395 if ( mLastScan.msecsTo( QDateTime::currentDateTime() ) <
QgsSettings().value( u
"browser/minscaninterval"_s, 10000 ).toInt() )
402 mRefreshLater =
true;
416 QTimer::singleShot( 100,
this, [
this] {
refresh(); } );
423 const QStringList hiddenItems = settings.
value( u
"browser/hiddenPaths"_s,
424 QStringList() ).toStringList();
425 const int idx = hiddenItems.indexOf(
path );
432 if ( settings.
value( u
"qgis/disableMonitorItemUris"_s, QStringList() ).toStringList().contains(
path ) )
434 else if ( settings.
value( u
"qgis/alwaysMonitorItemUris"_s, QStringList() ).toStringList().contains(
path ) )
443 const QString originalPath = QDir::cleanPath(
path );
444 QString currentPath = originalPath;
446 while ( currentPath != prevPath )
448 prevPath = currentPath;
449 currentPath = QFileInfo( currentPath ).path();
471 return QgsSettings().
value( u
"/qgis/monitorDirectoriesInBrowser"_s,
true ).toBool();
480 QgsDebugMsgLevel( u
"directory changed during createChildren() -> refresh() again"_s, 3 );
481 mRefreshLater =
false;
490 if ( mFileSystemWatcher && mMonitored )
501 const QgsDirectoryItem *otherDirItem = qobject_cast< const QgsDirectoryItem * >( other );
527 : QTreeWidget( parent )
529 setRootIsDecorated(
false );
534 labels << tr(
"Name" ) << tr(
"Size" ) << tr(
"Date" ) << tr(
"Permissions" ) << tr(
"Owner" ) << tr(
"Group" ) << tr(
"Type" );
535 setHeaderLabels( labels );
542 QList<QTreeWidgetItem *> items;
544 const QDir dir( path );
545 const QStringList entries = dir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase );
546 for (
const QString &name : entries )
548 const QFileInfo fi( dir.absoluteFilePath( name ) );
552 if ( fi.size() > 1024 )
554 size = u
"%1 KiB"_s.arg( QLocale().toString( fi.size() / 1024.0,
'f', 1 ) );
556 else if ( fi.size() > 1.048576e6 )
558 size = u
"%1 MiB"_s.arg( QLocale().toString( fi.size() / 1.048576e6,
'f', 1 ) );
562 size = u
"%1 B"_s.arg( fi.size() );
565 texts << QLocale().toString( fi.lastModified(), QLocale::ShortFormat );
567 perm += fi.permission( QFile::ReadOwner ) ?
'r' :
'-';
568 perm += fi.permission( QFile::WriteOwner ) ?
'w' :
'-';
569 perm += fi.permission( QFile::ExeOwner ) ?
'x' :
'-';
571 perm += fi.permission( QFile::ReadGroup ) ?
'r' :
'-';
572 perm += fi.permission( QFile::WriteGroup ) ?
'w' :
'-';
573 perm += fi.permission( QFile::ExeGroup ) ?
'x' :
'-';
574 perm += fi.permission( QFile::ReadOther ) ?
'r' :
'-';
575 perm += fi.permission( QFile::WriteOther ) ?
'w' :
'-';
576 perm += fi.permission( QFile::ExeOther ) ?
'x' :
'-';
584 if ( fi.isDir() && fi.isSymLink() )
586 type = tr(
"folder" );
589 else if ( fi.isDir() )
591 type = tr(
"folder" );
592 icon = iconDirectory;
594 else if ( fi.isFile() && fi.isSymLink() )
599 else if ( fi.isFile() )
607 QTreeWidgetItem *item =
new QTreeWidgetItem( texts );
608 item->setIcon( 0, icon );
612 addTopLevelItems( items );
616 const QList<QVariant> lst = settings.
value( u
"dataitem/directoryHiddenColumns"_s ).toList();
617 for (
const QVariant &colVariant : lst )
619 setColumnHidden( colVariant.toInt(),
true );
625 if ( event->button() == Qt::RightButton )
631 labels << tr(
"Name" ) << tr(
"Size" ) << tr(
"Date" ) << tr(
"Permissions" ) << tr(
"Owner" ) << tr(
"Group" ) << tr(
"Type" );
632 for (
int i = 0; i < labels.count(); i++ )
635 action->setObjectName( QString::number( i ) );
636 action->setCheckable(
true );
637 action->setChecked( !isColumnHidden( i ) );
640 popupMenu.exec( event->globalPos() );
646 QAction *action = qobject_cast<QAction *>( sender() );
650 const int columnIndex = action->objectName().toInt();
651 setColumnHidden( columnIndex, !isColumnHidden( columnIndex ) );
656 for (
int i = 0; i < columnCount(); i++ )
658 if ( isColumnHidden( i ) )
659 lst.append( QVariant( i ) );
661 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.