From 6b9d319b26da2e4b6f939ee92a176f8604c1c539 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 9 Jul 2019 11:52:28 +0200 Subject: QFileSystemModel: Add options Add Options flags similar to QFileDialog: - DontWatch: Do not use file system watchers for simple use cases like line edit completion. This brings it closer to QDirModel, which then can be deprecated. - DontResolveSymlinks: Similar to QFileDialog::DontResolveSymlinks. - DontUseCustomDirectoryIcons: matching QFileIconProvider::DontUseCustomDirectoryIcons for convenience. Task-number: QTBUG-76493 Change-Id: I09d3cb73ef902a700e6ebfba427e2d990fce4b4c Reviewed-by: Qt CI Bot Reviewed-by: Volker Hilsheimer --- src/widgets/dialogs/qfilesystemmodel.cpp | 103 +++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) (limited to 'src/widgets/dialogs/qfilesystemmodel.cpp') diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 820fc6e220..212223359a 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -1263,6 +1263,107 @@ Qt::DropActions QFileSystemModel::supportedDropActions() const return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction; } +/*! + \enum QFileSystemModel::Option + \since 5.14 + + \value DontWatch Do not add file watchers to the paths. + This reduces overhead when using the model for simple tasks + like line edit completion. + + \value DontResolveSymlinks Don't resolve symlinks in the file + system model. By default, symlinks are resolved. + + \value DontUseCustomDirectoryIcons Always use the default directory icon. + Some platforms allow the user to set a different icon. Custom icon lookup + causes a big performance impact over network or removable drives. + This sets the QFileIconProvider::DontUseCustomDirectoryIcons + option in the icon provider accordingly. + + \sa resolveSymlinks +*/ + +/*! + \since 5.14 + Sets the given \a option to be enabled if \a on is true; otherwise, + clears the given \a option. + + Options should be set before changing properties. + + \sa options, testOption() +*/ +void QFileSystemModel::setOption(Option option, bool on) +{ + QFileSystemModel::Options previousOptions = options(); + setOptions(previousOptions.setFlag(option, on)); +} + +/*! + \since 5.14 + + Returns \c true if the given \a option is enabled; otherwise, returns + false. + + \sa options, setOption() +*/ +bool QFileSystemModel::testOption(Option option) const +{ + return options().testFlag(option); +} + +/*! + \property QFileSystemModel::options + \brief the various options that affect the model + \since 5.14 + + By default, all options are disabled. + + Options should be set before changing properties. + + \sa setOption(), testOption() +*/ +void QFileSystemModel::setOptions(Options options) +{ + const Options changed = (options ^ QFileSystemModel::options()); + + if (changed.testFlag(DontResolveSymlinks)) + setResolveSymlinks(!options.testFlag(DontResolveSymlinks)); + +#if QT_CONFIG(filesystemwatcher) + Q_D(QFileSystemModel); + if (changed.testFlag(DontWatch)) + d->fileInfoGatherer.setWatching(!options.testFlag(DontWatch)); +#endif + + if (changed.testFlag(DontUseCustomDirectoryIcons)) { + if (auto provider = iconProvider()) { + QFileIconProvider::Options providerOptions = provider->options(); + providerOptions.setFlag(QFileIconProvider::DontUseCustomDirectoryIcons, + options.testFlag(QFileSystemModel::DontUseCustomDirectoryIcons)); + provider->setOptions(providerOptions); + } else { + qWarning("Setting QFileSystemModel::DontUseCustomDirectoryIcons has no effect when no provider is used"); + } + } +} + +QFileSystemModel::Options QFileSystemModel::options() const +{ + QFileSystemModel::Options result; + result.setFlag(DontResolveSymlinks, !resolveSymlinks()); +#if QT_CONFIG(filesystemwatcher) + Q_D(const QFileSystemModel); + result.setFlag(DontWatch, !d->fileInfoGatherer.isWatching()); +#else + result.setFlag(DontWatch); +#endif + if (auto provider = iconProvider()) { + result.setFlag(DontUseCustomDirectoryIcons, + provider->options().testFlag(QFileIconProvider::DontUseCustomDirectoryIcons)); + } + return result; +} + /*! Returns the path of the item stored in the model under the \a index given. @@ -1509,6 +1610,8 @@ QDir::Filters QFileSystemModel::filter() const This is only relevant on Windows. By default, this property is \c true. + + \sa QFileSystemModel::Options */ void QFileSystemModel::setResolveSymlinks(bool enable) { -- cgit v1.2.3