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

View file

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

View file

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