diff options
Diffstat (limited to 'plugin/src')
| -rw-r--r-- | plugin/src/Caelestia/Models/filesystemmodel.cpp | 67 | ||||
| -rw-r--r-- | plugin/src/Caelestia/Models/filesystemmodel.hpp | 16 | ||||
| -rw-r--r-- | plugin/src/Caelestia/cutils.cpp | 13 | ||||
| -rw-r--r-- | plugin/src/Caelestia/cutils.hpp | 4 |
4 files changed, 80 insertions, 20 deletions
diff --git a/plugin/src/Caelestia/Models/filesystemmodel.cpp b/plugin/src/Caelestia/Models/filesystemmodel.cpp index 54807b5..eb30fa8 100644 --- a/plugin/src/Caelestia/Models/filesystemmodel.cpp +++ b/plugin/src/Caelestia/Models/filesystemmodel.cpp @@ -26,6 +26,10 @@ QString FileSystemEntry::name() const { return m_fileInfo.fileName(); }; +QString FileSystemEntry::baseName() const { + return m_fileInfo.baseName(); +}; + QString FileSystemEntry::parentDir() const { return m_fileInfo.absolutePath(); }; @@ -149,6 +153,21 @@ void FileSystemModel::setShowHidden(bool showHidden) { update(); } +bool FileSystemModel::sortReverse() const { + return m_sortReverse; +} + +void FileSystemModel::setSortReverse(bool sortReverse) { + if (m_sortReverse == sortReverse) { + return; + } + + m_sortReverse = sortReverse; + emit sortReverseChanged(); + + update(); +} + FileSystemModel::Filter FileSystemModel::filter() const { return m_filter; } @@ -164,6 +183,21 @@ void FileSystemModel::setFilter(Filter filter) { update(); } +QStringList FileSystemModel::nameFilters() const { + return m_nameFilters; +} + +void FileSystemModel::setNameFilters(const QStringList& nameFilters) { + if (m_nameFilters == nameFilters) { + return; + } + + m_nameFilters = nameFilters; + emit nameFiltersChanged(); + + update(); +} + QList<FileSystemEntry*> FileSystemModel::entries() const { return m_entries; } @@ -238,22 +272,22 @@ void FileSystemModel::updateEntries() { } void FileSystemModel::updateEntriesForDir(const QString& dir) { - const bool recursive = m_recursive; - const bool showHidden = m_showHidden; + const auto recursive = m_recursive; + const auto showHidden = m_showHidden; const auto filter = m_filter; + const auto nameFilters = m_nameFilters; const auto oldEntries = m_entries; const auto baseDir = m_dir; - const auto future = QtConcurrent::run([dir, recursive, showHidden, filter, oldEntries, baseDir]( - QPromise<QPair<QSet<QString>, QSet<QString>>>& promise) { + const auto future = QtConcurrent::run([=](QPromise<QPair<QSet<QString>, QSet<QString>>>& promise) { const auto flags = recursive ? QDirIterator::Subdirectories : QDirIterator::NoIteratorFlags; std::optional<QDirIterator> iter; if (filter == Images) { - QStringList nameFilters; + QStringList extraNameFilters = nameFilters; for (const auto& format : QImageReader::supportedImageFormats()) { - nameFilters << "*." + format; + extraNameFilters << "*." + format; } QDir::Filters filters = QDir::Files; @@ -261,7 +295,7 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) { filters |= QDir::Hidden; } - iter.emplace(dir, nameFilters, filters, flags); + iter.emplace(dir, extraNameFilters, filters, flags); } else { QDir::Filters filters; @@ -277,7 +311,11 @@ void FileSystemModel::updateEntriesForDir(const QString& dir) { filters |= QDir::Hidden; } - iter.emplace(dir, filters, flags); + if (nameFilters.isEmpty()) { + iter.emplace(dir, filters, flags); + } else { + iter.emplace(dir, nameFilters, filters, flags); + } } QSet<QString> newPaths; @@ -376,13 +414,16 @@ void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet for (const auto& path : addedPaths) { newEntries << new FileSystemEntry(path, m_dir.relativeFilePath(path), this); } - std::sort(newEntries.begin(), newEntries.end(), &FileSystemModel::compareEntries); + const auto comp = [this](const FileSystemEntry* a, const FileSystemEntry* b) { + return compareEntries(a, b); + }; + std::sort(newEntries.begin(), newEntries.end(), comp); int insertStart = -1; int prevRow = -1; QList<FileSystemEntry*> batchItems; for (const auto& entry : newEntries) { - const auto it = std::lower_bound(m_entries.begin(), m_entries.end(), entry, &FileSystemModel::compareEntries); + const auto it = std::lower_bound(m_entries.begin(), m_entries.end(), entry, comp); int row = static_cast<int>(it - m_entries.begin()); if (insertStart == -1) { @@ -420,11 +461,11 @@ void FileSystemModel::applyChanges(const QSet<QString>& removedPaths, const QSet emit entriesChanged(); } -bool FileSystemModel::compareEntries(const FileSystemEntry* a, const FileSystemEntry* b) { +bool FileSystemModel::compareEntries(const FileSystemEntry* a, const FileSystemEntry* b) const { if (a->isDir() != b->isDir()) { - return a->isDir(); + return m_sortReverse ^ a->isDir(); } - return a->relativePath().localeAwareCompare(b->relativePath()) < 0; + return m_sortReverse ^ (a->relativePath().localeAwareCompare(b->relativePath()) < 0); } } // namespace caelestia diff --git a/plugin/src/Caelestia/Models/filesystemmodel.hpp b/plugin/src/Caelestia/Models/filesystemmodel.hpp index 4ea5f0a..cd2240d 100644 --- a/plugin/src/Caelestia/Models/filesystemmodel.hpp +++ b/plugin/src/Caelestia/Models/filesystemmodel.hpp @@ -19,6 +19,7 @@ class FileSystemEntry : public QObject { Q_PROPERTY(QString path READ path CONSTANT) Q_PROPERTY(QString relativePath READ relativePath CONSTANT) Q_PROPERTY(QString name READ name CONSTANT) + Q_PROPERTY(QString baseName READ baseName CONSTANT) Q_PROPERTY(QString parentDir READ parentDir CONSTANT) Q_PROPERTY(QString suffix READ suffix CONSTANT) Q_PROPERTY(qint64 size READ size CONSTANT) @@ -32,6 +33,7 @@ public: [[nodiscard]] QString path() const; [[nodiscard]] QString relativePath() const; [[nodiscard]] QString name() const; + [[nodiscard]] QString baseName() const; [[nodiscard]] QString parentDir() const; [[nodiscard]] QString suffix() const; [[nodiscard]] qint64 size() const; @@ -60,7 +62,9 @@ class FileSystemModel : public QAbstractListModel { Q_PROPERTY(bool recursive READ recursive WRITE setRecursive NOTIFY recursiveChanged) Q_PROPERTY(bool watchChanges READ watchChanges WRITE setWatchChanges NOTIFY watchChangesChanged) Q_PROPERTY(bool showHidden READ showHidden WRITE setShowHidden NOTIFY showHiddenChanged) + Q_PROPERTY(bool sortReverse READ sortReverse WRITE setSortReverse NOTIFY sortReverseChanged) Q_PROPERTY(Filter filter READ filter WRITE setFilter NOTIFY filterChanged) + Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters NOTIFY nameFiltersChanged) Q_PROPERTY(QList<FileSystemEntry*> entries READ entries NOTIFY entriesChanged) @@ -91,9 +95,15 @@ public: [[nodiscard]] bool showHidden() const; void setShowHidden(bool showHidden); + [[nodiscard]] bool sortReverse() const; + void setSortReverse(bool sortReverse); + [[nodiscard]] Filter filter() const; void setFilter(Filter filter); + [[nodiscard]] QStringList nameFilters() const; + void setNameFilters(const QStringList& nameFilters); + [[nodiscard]] QList<FileSystemEntry*> entries() const; signals: @@ -101,7 +111,9 @@ signals: void recursiveChanged(); void watchChangesChanged(); void showHiddenChanged(); + void sortReverseChanged(); void filterChanged(); + void nameFiltersChanged(); void entriesChanged(); void added(const FileSystemEntry* entry); @@ -117,7 +129,9 @@ private: bool m_recursive; bool m_watchChanges; bool m_showHidden; + bool m_sortReverse; Filter m_filter; + QStringList m_nameFilters; void watchDirIfRecursive(const QString& path); void update(); @@ -125,7 +139,7 @@ private: void updateEntries(); void updateEntriesForDir(const QString& dir); void applyChanges(const QSet<QString>& removedPaths, const QSet<QString>& addedPaths); - [[nodiscard]] static bool compareEntries(const FileSystemEntry* a, const FileSystemEntry* b); + [[nodiscard]] bool compareEntries(const FileSystemEntry* a, const FileSystemEntry* b) const; }; } // namespace caelestia diff --git a/plugin/src/Caelestia/cutils.cpp b/plugin/src/Caelestia/cutils.cpp index e93a590..f0902c3 100644 --- a/plugin/src/Caelestia/cutils.cpp +++ b/plugin/src/Caelestia/cutils.cpp @@ -87,10 +87,6 @@ void CUtils::saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, Q }); } -bool CUtils::copyFile(const QUrl& source, const QUrl& target) const { - return this->copyFile(source, target, true); -} - bool CUtils::copyFile(const QUrl& source, const QUrl& target, bool overwrite) const { if (!source.isLocalFile()) { qWarning() << "CUtils::copyFile: source" << source << "is not a local file"; @@ -108,6 +104,15 @@ bool CUtils::copyFile(const QUrl& source, const QUrl& target, bool overwrite) co return QFile::copy(source.toLocalFile(), target.toLocalFile()); } +bool CUtils::deleteFile(const QUrl& path) const { + if (!path.isLocalFile()) { + qWarning() << "CUtils::deleteFile: path" << path << "is not a local file"; + return false; + } + + return QFile::remove(path.toLocalFile()); +} + void CUtils::getDominantColour(QQuickItem* item, QJSValue callback) { this->getDominantColour(item, 128, callback); } diff --git a/plugin/src/Caelestia/cutils.hpp b/plugin/src/Caelestia/cutils.hpp index 892ff86..57ece14 100644 --- a/plugin/src/Caelestia/cutils.hpp +++ b/plugin/src/Caelestia/cutils.hpp @@ -21,8 +21,8 @@ public: Q_INVOKABLE void saveItem(QQuickItem* target, const QUrl& path, const QRect& rect, QJSValue onSaved, QJSValue onFailed); // clang-format on - Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target) const; - Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite) const; + Q_INVOKABLE bool copyFile(const QUrl& source, const QUrl& target, bool overwrite = true) const; + Q_INVOKABLE bool deleteFile(const QUrl& path) const; Q_INVOKABLE void getDominantColour(QQuickItem* item, QJSValue callback); Q_INVOKABLE void getDominantColour(QQuickItem* item, int rescaleSize, QJSValue callback); |