plugin/fsm: rename files -> entries

Also rename filter & add props
This commit is contained in:
2 * r + 2 * t 2025-08-31 23:31:33 +10:00
parent 7729546469
commit 018a4a90b2
3 changed files with 48 additions and 29 deletions

View file

@ -10,15 +10,15 @@
int FileSystemModel::rowCount(const QModelIndex& parent) const {
Q_UNUSED(parent);
return m_files.size();
return m_entries.size();
}
QVariant FileSystemModel::data(const QModelIndex& index, int role) const {
if (!index.isValid() || index.row() >= m_files.size()) {
if (!index.isValid() || index.row() >= m_entries.size()) {
return QVariant();
}
const FileSystemEntry* file = m_files.at(index.row());
const FileSystemEntry* file = m_entries.at(index.row());
switch (role) {
case FilePathRole:
return file->path();
@ -91,8 +91,8 @@ void FileSystemModel::setFilter(Filter filter) {
update();
}
QList<FileSystemEntry*> FileSystemModel::files() const {
return m_files;
QList<FileSystemEntry*> FileSystemModel::entries() const {
return m_entries;
}
void FileSystemModel::watchDirIfRecursive(const QString& path) {
@ -106,7 +106,7 @@ void FileSystemModel::watchDirIfRecursive(const QString& path) {
void FileSystemModel::update() {
updateWatcher();
updateFiles();
updateEntries();
}
void FileSystemModel::updateWatcher() {
@ -122,50 +122,52 @@ void FileSystemModel::updateWatcher() {
watchDirIfRecursive(m_path);
}
void FileSystemModel::updateFiles() {
void FileSystemModel::updateEntries() {
if (m_path.isEmpty()) {
beginResetModel();
qDeleteAll(m_files);
m_files.clear();
emit filesChanged();
qDeleteAll(m_entries);
m_entries.clear();
emit entriesChanged();
endResetModel();
return;
}
beginResetModel();
qDeleteAll(m_files);
m_files.clear();
qDeleteAll(m_entries);
m_entries.clear();
const auto flags = m_recursive ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags;
std::optional<QDirIterator> iter;
if (m_filter == ImagesOnly) {
if (m_filter == Images) {
QStringList filters;
for (const auto& format : QImageReader::supportedImageFormats()) {
filters << "*." + format;
}
iter.emplace(m_path, filters, QDir::Files, flags);
} else {
} else if (m_filter == Files) {
iter.emplace(m_path, QDir::Files, flags);
} else {
iter.emplace(m_path, QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot, flags);
}
while (iter.value().hasNext()) {
QString file = iter.value().next();
QString entry = iter.value().next();
if (m_filter == ImagesOnly) {
QImageReader reader(file);
if (m_filter == Images) {
QImageReader reader(entry);
if (reader.canRead()) {
m_files << new FileSystemEntry(file, m_dir.relativeFilePath(file), this);
m_entries << new FileSystemEntry(entry, m_dir.relativeFilePath(entry), this);
}
} else {
m_files << new FileSystemEntry(file, m_dir.relativeFilePath(file), this);
m_entries << new FileSystemEntry(entry, m_dir.relativeFilePath(entry), this);
}
}
emit filesChanged();
emit entriesChanged();
endResetModel();
}

View file

@ -6,6 +6,7 @@
#include <QFileSystemWatcher>
#include <QFileInfo>
#include <QDir>
#include <QImageReader>
class FileSystemEntry : public QObject {
Q_OBJECT
@ -17,6 +18,8 @@ class FileSystemEntry : public QObject {
Q_PROPERTY(QString name READ name CONSTANT);
Q_PROPERTY(QString parentDir READ parentDir CONSTANT);
Q_PROPERTY(qint64 size READ size CONSTANT);
Q_PROPERTY(bool isDir READ isDir CONSTANT);
Q_PROPERTY(bool isImage READ isImage CONSTANT);
public:
explicit FileSystemEntry(const QString& path, const QString& relativePath, QObject* parent = nullptr)
@ -28,12 +31,25 @@ public:
QString name() const { return m_fileInfo.fileName(); };
QString parentDir() const { return m_fileInfo.absolutePath(); };
qint64 size() const { return m_fileInfo.size(); };
bool isDir() const { return m_fileInfo.isDir(); };
bool isImage() {
if (!m_isImageInitialised) {
QImageReader reader(m_path);
m_isImage = reader.canRead();
m_isImageInitialised = true;
}
return m_isImage;
}
private:
const QFileInfo m_fileInfo;
const QString m_path;
const QString m_relativePath;
bool m_isImage;
bool m_isImageInitialised;
};
class FileSystemModel : public QAbstractListModel {
@ -44,7 +60,7 @@ class FileSystemModel : public QAbstractListModel {
Q_PROPERTY(bool recursive READ recursive WRITE setRecursive NOTIFY recursiveChanged);
Q_PROPERTY(Filter filter READ filter WRITE setFilter NOTIFY filterChanged);
Q_PROPERTY(QList<FileSystemEntry*> files READ files NOTIFY filesChanged);
Q_PROPERTY(QList<FileSystemEntry*> entries READ entries NOTIFY entriesChanged);
public:
enum Roles {
@ -57,14 +73,15 @@ public:
enum Filter {
NoFilter,
ImagesOnly
Images,
Files
};
Q_ENUM(Filter)
explicit FileSystemModel(QObject* parent = nullptr)
: QAbstractListModel(parent), m_recursive(true), m_filter(NoFilter) {
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::watchDirIfRecursive);
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::updateFiles);
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &FileSystemModel::updateEntries);
}
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
@ -80,18 +97,18 @@ public:
Filter filter() const;
void setFilter(Filter filter);
QList<FileSystemEntry*> files() const;
QList<FileSystemEntry*> entries() const;
signals:
void pathChanged();
void recursiveChanged();
void filterChanged();
void filesChanged();
void entriesChanged();
private:
QDir m_dir;
QFileSystemWatcher m_watcher;
QList<FileSystemEntry*> m_files;
QList<FileSystemEntry*> m_entries;
QString m_path;
bool m_recursive;
@ -100,5 +117,5 @@ private:
void watchDirIfRecursive(const QString& path);
void update();
void updateWatcher();
void updateFiles();
void updateEntries();
};

View file

@ -40,7 +40,7 @@ Searcher {
reloadableId: "wallpapers"
list: wallpapers.files
list: wallpapers.entries
useFuzzy: Config.launcher.useFuzzy.wallpapers
extraOpts: useFuzzy ? ({}) : ({
forward: false
@ -76,7 +76,7 @@ Searcher {
id: wallpapers
path: Paths.expandTilde(Paths.wallsdir)
filter: FileSystemModel.ImagesOnly
filter: FileSystemModel.Images
}
Process {