33#include <QFileSystemWatcher>
39#include "moc_qgsdirectoryitem.cpp"
41using namespace Qt::StringLiterals;
63void QgsDirectoryItem::init(
const QString &dirName )
72 mIsSymLink = fi.isSymLink();
75 switch ( mMonitoring )
88 settings.
beginGroup( u
"qgis/browserPathColors"_s );
90 settingKey.replace(
'/',
"|||"_L1 );
91 if ( settings.
childKeys().contains( settingKey ) )
93 const QString colorString = settings.
value( settingKey ).toString();
94 mIconColor = QColor( colorString );
105 switch ( mMonitoring )
118 const QVector<QgsDataItem *> childItems =
children();
121 if (
QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem *>( child ) )
122 dirItem->reevaluateMonitoring();
125 createOrDestroyFileSystemWatcher();
128void QgsDirectoryItem::createOrDestroyFileSystemWatcher()
130 if ( !mMonitored && mFileSystemWatcher )
132 mFileSystemWatcher->deleteLater();
133 mFileSystemWatcher =
nullptr;
137 mFileSystemWatcher =
new QFileSystemWatcher(
this );
138 mFileSystemWatcher->addPath(
mDirPath );
150 if ( color == mIconColor )
160 settings.
beginGroup( u
"qgis/browserPathColors"_s );
161 QString settingKey = directory;
162 settingKey.replace(
'/',
"|||"_L1 );
163 if ( color.isValid() )
164 settings.
setValue( settingKey, color.name( QColor::HexArgb ) );
166 settings.
remove( settingKey );
173 return homeDirIcon( mIconColor, mIconColor.darker() );
180 if ( mIsDir && mIsSymLink )
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;
353 if ( fileInfo.suffix().compare(
"qgs"_L1, Qt::CaseInsensitive ) == 0 || fileInfo.suffix().compare(
"qgz"_L1, Qt::CaseInsensitive ) == 0 )
371 if ( !mFileSystemWatcher )
373 mFileSystemWatcher =
new QFileSystemWatcher(
this );
374 mFileSystemWatcher->addPath(
mDirPath );
377 mLastScan = QDateTime::currentDateTime();
381 if ( mFileSystemWatcher )
383 delete mFileSystemWatcher;
384 mFileSystemWatcher =
nullptr;
392 if ( mLastScan.msecsTo( QDateTime::currentDateTime() ) <
QgsSettings().value( u
"browser/minscaninterval"_s, 10000 ).toInt() )
399 mRefreshLater =
true;
413 QTimer::singleShot( 100,
this, [
this] {
refresh(); } );
420 const QStringList hiddenItems = settings.
value( u
"browser/hiddenPaths"_s, QStringList() ).toStringList();
421 const int idx = hiddenItems.indexOf(
path );
428 if ( settings.
value( u
"qgis/disableMonitorItemUris"_s, QStringList() ).toStringList().contains(
path ) )
430 else if ( settings.
value( u
"qgis/alwaysMonitorItemUris"_s, QStringList() ).toStringList().contains(
path ) )
439 const QString originalPath = QDir::cleanPath(
path );
440 QString currentPath = originalPath;
442 while ( currentPath != prevPath )
444 prevPath = currentPath;
445 currentPath = QFileInfo( currentPath ).path();
476 QgsDebugMsgLevel( u
"directory changed during createChildren() -> refresh() again"_s, 3 );
477 mRefreshLater =
false;
486 if ( mFileSystemWatcher && mMonitored )
497 const QgsDirectoryItem *otherDirItem = qobject_cast< const QgsDirectoryItem * >( other );
523 : QTreeWidget( parent )
525 setRootIsDecorated(
false );
530 labels << tr(
"Name" ) << tr(
"Size" ) << tr(
"Date" ) << tr(
"Permissions" ) << tr(
"Owner" ) << tr(
"Group" ) << tr(
"Type" );
531 setHeaderLabels( labels );
538 QList<QTreeWidgetItem *> items;
540 const QDir dir( path );
541 const QStringList entries = dir.entryList( QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name | QDir::IgnoreCase );
542 for (
const QString &name : entries )
544 const QFileInfo fi( dir.absoluteFilePath( name ) );
548 if ( fi.size() > 1024 )
550 size = u
"%1 KiB"_s.arg( QLocale().toString( fi.size() / 1024.0,
'f', 1 ) );
552 else if ( fi.size() > 1.048576e6 )
554 size = u
"%1 MiB"_s.arg( QLocale().toString( fi.size() / 1.048576e6,
'f', 1 ) );
558 size = u
"%1 B"_s.arg( fi.size() );
561 texts << QLocale().toString( fi.lastModified(), QLocale::ShortFormat );
563 perm += fi.permission( QFile::ReadOwner ) ?
'r' :
'-';
564 perm += fi.permission( QFile::WriteOwner ) ?
'w' :
'-';
565 perm += fi.permission( QFile::ExeOwner ) ?
'x' :
'-';
567 perm += fi.permission( QFile::ReadGroup ) ?
'r' :
'-';
568 perm += fi.permission( QFile::WriteGroup ) ?
'w' :
'-';
569 perm += fi.permission( QFile::ExeGroup ) ?
'x' :
'-';
570 perm += fi.permission( QFile::ReadOther ) ?
'r' :
'-';
571 perm += fi.permission( QFile::WriteOther ) ?
'w' :
'-';
572 perm += fi.permission( QFile::ExeOther ) ?
'x' :
'-';
580 if ( fi.isDir() && fi.isSymLink() )
582 type = tr(
"folder" );
585 else if ( fi.isDir() )
587 type = tr(
"folder" );
588 icon = iconDirectory;
590 else if ( fi.isFile() && fi.isSymLink() )
595 else if ( fi.isFile() )
603 QTreeWidgetItem *item =
new QTreeWidgetItem( texts );
604 item->setIcon( 0, icon );
608 addTopLevelItems( items );
612 const QList<QVariant> lst = settings.
value( u
"dataitem/directoryHiddenColumns"_s ).toList();
613 for (
const QVariant &colVariant : lst )
615 setColumnHidden( colVariant.toInt(),
true );
621 if ( event->button() == Qt::RightButton )
627 labels << tr(
"Name" ) << tr(
"Size" ) << tr(
"Date" ) << tr(
"Permissions" ) << tr(
"Owner" ) << tr(
"Group" ) << tr(
"Type" );
628 for (
int i = 0; i < labels.count(); i++ )
631 action->setObjectName( QString::number( i ) );
632 action->setCheckable(
true );
633 action->setChecked( !isColumnHidden( i ) );
636 popupMenu.exec( event->globalPos() );
642 QAction *action = qobject_cast<QAction *>( sender() );
646 const int columnIndex = action->objectName().toInt();
647 setColumnHidden( columnIndex, !isColumnHidden( columnIndex ) );
652 for (
int i = 0; i < columnCount(); i++ )
654 if ( isColumnHidden( i ) )
655 lst.append( QVariant( i ) );
657 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.
static const QgsSettingsEntryBool * settingsMonitorDirectoriesInBrowser
Settings entry for monitor directories in browser.
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.
A boolean settings entry.
static QgsSettingsTreeNode * sTreeQgis
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.