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 <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Friedemann Kleint 2019-07-09 11:52:28 +02:00
parent cbd695a295
commit 6b9d319b26
3 changed files with 126 additions and 2 deletions

View File

@ -65,8 +65,10 @@ int main(int argc, char *argv[])
parser.setApplicationDescription("Qt Dir View Example");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption dontUseCustomDirectoryIconsOption("c", "Set QFileIconProvider::DontUseCustomDirectoryIcons");
QCommandLineOption dontUseCustomDirectoryIconsOption("c", "Set QFileSystemModel::DontUseCustomDirectoryIcons");
parser.addOption(dontUseCustomDirectoryIconsOption);
QCommandLineOption dontWatchOption("w", "Set QFileSystemModel::DontWatch");
parser.addOption(dontWatchOption);
parser.addPositionalArgument("directory", "The directory to start in.");
parser.process(app);
const QString rootPath = parser.positionalArguments().isEmpty()
@ -75,7 +77,9 @@ int main(int argc, char *argv[])
QFileSystemModel model;
model.setRootPath("");
if (parser.isSet(dontUseCustomDirectoryIconsOption))
model.iconProvider()->setOptions(QFileIconProvider::DontUseCustomDirectoryIcons);
model.setOption(QFileSystemModel::DontUseCustomDirectoryIcons);
if (parser.isSet(dontWatchOption))
model.setOption(QFileSystemModel::DontWatch);
QTreeView tree;
tree.setModel(&model);
if (!rootPath.isEmpty()) {

View File

@ -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)
{

View File

@ -61,6 +61,7 @@ class Q_WIDGETS_EXPORT QFileSystemModel : public QAbstractItemModel
Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks)
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables)
Q_PROPERTY(Options options READ options WRITE setOptions)
Q_SIGNALS:
void rootPathChanged(const QString &newPath);
@ -75,6 +76,15 @@ public:
FilePermissions = Qt::UserRole + 3
};
enum Option
{
DontWatch = 0x00000001,
DontResolveSymlinks = 0x00000002,
DontUseCustomDirectoryIcons = 0x00000004
};
Q_ENUM(Option)
Q_DECLARE_FLAGS(Options, Option)
explicit QFileSystemModel(QObject *parent = nullptr);
~QFileSystemModel();
@ -129,6 +139,11 @@ public:
void setNameFilters(const QStringList &filters);
QStringList nameFilters() const;
void setOption(Option option, bool on = true);
bool testOption(Option option) const;
void setOptions(Options options);
Options options() const;
QString filePath(const QModelIndex &index) const;
bool isDir(const QModelIndex &index) const;
qint64 size(const QModelIndex &index) const;
@ -165,6 +180,8 @@ inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const
inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const
{ return qvariant_cast<QIcon>(aindex.data(Qt::DecorationRole)); }
Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options)
QT_END_NAMESPACE
#endif // QFILESYSTEMMODEL_H