From c0035ada020fe90085c698c0829f771c8d542031 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 16 May 2020 21:54:01 +0200 Subject: Add QDirFileTree implementation. --- src/qdirfiletree.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/qdirfiletree.cpp (limited to 'src/qdirfiletree.cpp') diff --git a/src/qdirfiletree.cpp b/src/qdirfiletree.cpp new file mode 100644 index 00000000..5570060a --- /dev/null +++ b/src/qdirfiletree.cpp @@ -0,0 +1,46 @@ +#include "qdirfiletree.h" + +#include + +using namespace MOBase; + +/** + * + */ +std::shared_ptr QDirFileTree::makeTree(QDir directory) { + return std::shared_ptr(new QDirFileTree(nullptr, directory)); +} + +/** + * + */ +QDirFileTree::QDirFileTree(std::shared_ptr parent, QDir directory) : FileTreeEntry(parent, directory.dirName()), IFileTree(), qDir(directory) { + qDir.setFilter(qDir.filter() | QDir::NoDotAndDotDot); +} + +/** + * No mutable operations allowed. + */ +bool QDirFileTree::beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) { return false; } +bool QDirFileTree::beforeInsert(IFileTree const* entry, FileTreeEntry const* name) { return false; } +bool QDirFileTree::beforeRemove(IFileTree const* entry, FileTreeEntry const* name) { return false; } +std::shared_ptr QDirFileTree::makeFile(std::shared_ptr parent, QString name, QDateTime time) const { return nullptr; } +std::shared_ptr QDirFileTree::makeDirectory(std::shared_ptr parent, QString name) const { return nullptr; } + +void QDirFileTree::doPopulate(std::shared_ptr parent, std::vector>& entries) const { + QDirIterator iter(qDir); + while (iter.hasNext()) { + QString name = iter.next(); + QFileInfo info = iter.fileInfo(); + if (info.isDir()) { + entries.push_back(std::shared_ptr(new QDirFileTree(parent, QDir(info.absoluteFilePath())))); + } + else { + entries.push_back(createFileEntry(parent, info.fileName(), info.fileTime(QFileDevice::FileModificationTime))); + } + } +} + +std::shared_ptr QDirFileTree::doClone() const { + return std::shared_ptr(new QDirFileTree(nullptr, qDir)); +} -- cgit v1.3.1 From 064ef4cfeb7307cae1cfbc640ca3c47e8032f365 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 23 May 2020 17:33:01 +0200 Subject: Update IFileTree implementations following uibase changes. --- src/archivefiletree.cpp | 29 ++++++++++------------------- src/qdirfiletree.cpp | 19 +++++++++---------- src/qdirfiletree.h | 7 ++++--- 3 files changed, 23 insertions(+), 32 deletions(-) (limited to 'src/qdirfiletree.cpp') diff --git a/src/archivefiletree.cpp b/src/archivefiletree.cpp index 0b715f4f..6ba06924 100644 --- a/src/archivefiletree.cpp +++ b/src/archivefiletree.cpp @@ -33,23 +33,11 @@ class ArchiveFileEntry : public virtual FileTreeEntry { public: /** - * @brief Create a new entry corresponding to a file. + * @brief Create a new entry. * - * @param parent The tree containing this file. - * @param name The name of this file. - * @param index The index of the file in the archive. - * @param time The modification time of this file. - */ - ArchiveFileEntry(std::shared_ptr parent, QString name, int index, QDateTime time) : - FileTreeEntry(parent, name, time), m_Index(index) { - } - - /** - * @brief Create a new entry corresponding to a directory. - * - * @param parent The tree containing this directory. - * @param name The name of this directory. - * @param index The index of the directory in the archive, or -1. + * @param parent The tree containing this entry. + * @param name The name of this entry. + * @param index The index of the entry in the archive. */ ArchiveFileEntry(std::shared_ptr parent, QString name, int index) : FileTreeEntry(parent, name), m_Index(index) { @@ -98,7 +86,7 @@ public: // Overrides: /** * @override */ - std::shared_ptr addFile(QString path, QDateTime time = QDateTime()) override { + std::shared_ptr addFile(QString path) override { // Cannot add file to an archive. throw UnsupportedOperationException(QObject::tr("Cannot create file within an archive.")); } @@ -159,7 +147,7 @@ protected: return std::make_shared(parent, name, -1, std::vector{}); } - virtual void doPopulate(std::shared_ptr parent, std::vector>& entries) const override { + virtual bool doPopulate(std::shared_ptr parent, std::vector>& entries) const override { // Sort by name: std::sort(std::begin(m_Files), std::end(m_Files), @@ -200,7 +188,7 @@ protected: // If it is not a directory, then it is a file in directly under this tree: if (!std::get<1>(p)) { entries.push_back( - std::make_shared(parent, currentName, std::get<2>(p), QDateTime())); + std::make_shared(parent, currentName, std::get<2>(p))); currentName = ""; } else { @@ -219,6 +207,9 @@ protected: if (currentName != "") { entries.push_back(std::make_shared(parent, currentName, currentIndex, std::move(currentFiles))); } + + // Let the parent class sort the entries: + return false; } virtual std::shared_ptr doClone() const override { diff --git a/src/qdirfiletree.cpp b/src/qdirfiletree.cpp index 5570060a..e80c7127 100644 --- a/src/qdirfiletree.cpp +++ b/src/qdirfiletree.cpp @@ -14,9 +14,7 @@ std::shared_ptr QDirFileTree::makeTree(QDir directory) { /** * */ -QDirFileTree::QDirFileTree(std::shared_ptr parent, QDir directory) : FileTreeEntry(parent, directory.dirName()), IFileTree(), qDir(directory) { - qDir.setFilter(qDir.filter() | QDir::NoDotAndDotDot); -} +QDirFileTree::QDirFileTree(std::shared_ptr parent, QDir directory) : FileTreeEntry(parent, directory.dirName()), IFileTree(), qDir(directory) { } /** * No mutable operations allowed. @@ -24,21 +22,22 @@ QDirFileTree::QDirFileTree(std::shared_ptr parent, QDir directo bool QDirFileTree::beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) { return false; } bool QDirFileTree::beforeInsert(IFileTree const* entry, FileTreeEntry const* name) { return false; } bool QDirFileTree::beforeRemove(IFileTree const* entry, FileTreeEntry const* name) { return false; } -std::shared_ptr QDirFileTree::makeFile(std::shared_ptr parent, QString name, QDateTime time) const { return nullptr; } +std::shared_ptr QDirFileTree::makeFile(std::shared_ptr parent, QString name) const { return nullptr; } std::shared_ptr QDirFileTree::makeDirectory(std::shared_ptr parent, QString name) const { return nullptr; } -void QDirFileTree::doPopulate(std::shared_ptr parent, std::vector>& entries) const { - QDirIterator iter(qDir); - while (iter.hasNext()) { - QString name = iter.next(); - QFileInfo info = iter.fileInfo(); +bool QDirFileTree::doPopulate(std::shared_ptr parent, std::vector>& entries) const { + auto infoList = qDir.entryInfoList(qDir.filter() | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst | QDir::IgnoreCase); + for (auto& info : infoList) { if (info.isDir()) { entries.push_back(std::shared_ptr(new QDirFileTree(parent, QDir(info.absoluteFilePath())))); } else { - entries.push_back(createFileEntry(parent, info.fileName(), info.fileTime(QFileDevice::FileModificationTime))); + entries.push_back(createFileEntry(parent, info.fileName())); } } + + // Vector is already sorted: + return true; } std::shared_ptr QDirFileTree::doClone() const { diff --git a/src/qdirfiletree.h b/src/qdirfiletree.h index 000ef69a..e6b8706e 100644 --- a/src/qdirfiletree.h +++ b/src/qdirfiletree.h @@ -43,17 +43,18 @@ public: */ static std::shared_ptr makeTree(QDir directory); + QDirFileTree(std::shared_ptr parent, QDir directory); + protected: - QDirFileTree(std::shared_ptr parent, QDir directory); virtual bool beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) override; virtual bool beforeInsert(IFileTree const* entry, FileTreeEntry const* name) override; virtual bool beforeRemove(IFileTree const* entry, FileTreeEntry const* name) override; - virtual std::shared_ptr makeFile(std::shared_ptr parent, QString name, QDateTime time) const override; + virtual std::shared_ptr makeFile(std::shared_ptr parent, QString name) const override; virtual std::shared_ptr makeDirectory(std::shared_ptr parent, QString name) const override; - virtual void doPopulate(std::shared_ptr parent, std::vector>& entries) const override; + virtual bool doPopulate(std::shared_ptr parent, std::vector>& entries) const override; virtual std::shared_ptr doClone() const override; QDir qDir; -- cgit v1.3.1 From 2652ed120964e568527fd7e1858a3dd91611fca4 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 23 May 2020 18:40:21 +0200 Subject: Update QDirFileTree to hide implementation and allow querying time through QFileInfo. --- src/qdirfiletree.cpp | 91 ++++++++++++++++++++++++++++++++++------------------ src/qdirfiletree.h | 15 ++------- 2 files changed, 61 insertions(+), 45 deletions(-) (limited to 'src/qdirfiletree.cpp') diff --git a/src/qdirfiletree.cpp b/src/qdirfiletree.cpp index e80c7127..691bac87 100644 --- a/src/qdirfiletree.cpp +++ b/src/qdirfiletree.cpp @@ -4,42 +4,69 @@ using namespace MOBase; -/** - * - */ -std::shared_ptr QDirFileTree::makeTree(QDir directory) { - return std::shared_ptr(new QDirFileTree(nullptr, directory)); -} +class QFileTreeEntry : public virtual FileTreeEntry { +public: + using FileTreeEntry::FileTreeEntry; -/** - * - */ -QDirFileTree::QDirFileTree(std::shared_ptr parent, QDir directory) : FileTreeEntry(parent, directory.dirName()), IFileTree(), qDir(directory) { } + QFileTreeEntry(std::shared_ptr parent, QFileInfo fileInfo) : + FileTreeEntry(parent, fileInfo.fileName()), m_FileInfo(fileInfo) { } -/** - * No mutable operations allowed. - */ -bool QDirFileTree::beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) { return false; } -bool QDirFileTree::beforeInsert(IFileTree const* entry, FileTreeEntry const* name) { return false; } -bool QDirFileTree::beforeRemove(IFileTree const* entry, FileTreeEntry const* name) { return false; } -std::shared_ptr QDirFileTree::makeFile(std::shared_ptr parent, QString name) const { return nullptr; } -std::shared_ptr QDirFileTree::makeDirectory(std::shared_ptr parent, QString name) const { return nullptr; } - -bool QDirFileTree::doPopulate(std::shared_ptr parent, std::vector>& entries) const { - auto infoList = qDir.entryInfoList(qDir.filter() | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst | QDir::IgnoreCase); - for (auto& info : infoList) { - if (info.isDir()) { - entries.push_back(std::shared_ptr(new QDirFileTree(parent, QDir(info.absoluteFilePath())))); - } - else { - entries.push_back(createFileEntry(parent, info.fileName())); + QDateTime time() const override { + return m_FileInfo.lastModified(); + } + +protected: + QFileInfo m_FileInfo; +}; + +class QDirFileTreeImpl : public QDirFileTree { +public: + + + /** + * + */ + QDirFileTreeImpl(std::shared_ptr parent, QDir dir) : + FileTreeEntry(parent, dir.dirName()), QDirFileTree(), qDir(dir) { } + +protected: + + /** + * No mutable operations allowed. + */ + bool beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) override { return false; } + bool beforeInsert(IFileTree const* entry, FileTreeEntry const* name) override { return false; } + bool beforeRemove(IFileTree const* entry, FileTreeEntry const* name) override { return false; } + std::shared_ptr makeFile(std::shared_ptr parent, QString name) const override { return nullptr; } + std::shared_ptr makeDirectory(std::shared_ptr parent, QString name) const override { return nullptr; } + + bool doPopulate(std::shared_ptr parent, std::vector>& entries) const override { + auto infoList = qDir.entryInfoList(qDir.filter() | QDir::NoDotAndDotDot, QDir::Name | QDir::DirsFirst | QDir::IgnoreCase); + for (auto& info : infoList) { + if (info.isDir()) { + entries.push_back(std::make_shared(parent, QDir(info.absoluteFilePath()))); + } + else { + entries.push_back(std::make_shared(parent, info)); + } } + + // Vector is already sorted: + return true; } - // Vector is already sorted: - return true; -} + std::shared_ptr QDirFileTree::doClone() const { + return std::make_shared(nullptr, qDir); + } + +private: + QDir qDir; -std::shared_ptr QDirFileTree::doClone() const { - return std::shared_ptr(new QDirFileTree(nullptr, qDir)); +}; + +/** + * + */ +std::shared_ptr QDirFileTree::makeTree(QDir directory) { + return std::make_shared(nullptr, directory); } diff --git a/src/qdirfiletree.h b/src/qdirfiletree.h index e6b8706e..1f85b04f 100644 --- a/src/qdirfiletree.h +++ b/src/qdirfiletree.h @@ -43,22 +43,11 @@ public: */ static std::shared_ptr makeTree(QDir directory); - QDirFileTree(std::shared_ptr parent, QDir directory); - protected: + using IFileTree::IFileTree; - virtual bool beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination, FileTreeEntry const* source) override; - virtual bool beforeInsert(IFileTree const* entry, FileTreeEntry const* name) override; - virtual bool beforeRemove(IFileTree const* entry, FileTreeEntry const* name) override; - - virtual std::shared_ptr makeFile(std::shared_ptr parent, QString name) const override; - virtual std::shared_ptr makeDirectory(std::shared_ptr parent, QString name) const override; - virtual bool doPopulate(std::shared_ptr parent, std::vector>& entries) const override; - virtual std::shared_ptr doClone() const override; - - QDir qDir; - + virtual bool doPopulate(std::shared_ptr parent, std::vector>& entries) const = 0; }; #endif -- cgit v1.3.1 From 5f794fec2ce2c297a6641a2db1e1152370adf636 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Sat, 23 May 2020 21:45:57 +0200 Subject: Remove time() method from FileTreeEntry. --- src/qdirfiletree.cpp | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) (limited to 'src/qdirfiletree.cpp') diff --git a/src/qdirfiletree.cpp b/src/qdirfiletree.cpp index 691bac87..770a9df8 100644 --- a/src/qdirfiletree.cpp +++ b/src/qdirfiletree.cpp @@ -4,21 +4,6 @@ using namespace MOBase; -class QFileTreeEntry : public virtual FileTreeEntry { -public: - using FileTreeEntry::FileTreeEntry; - - QFileTreeEntry(std::shared_ptr parent, QFileInfo fileInfo) : - FileTreeEntry(parent, fileInfo.fileName()), m_FileInfo(fileInfo) { } - - QDateTime time() const override { - return m_FileInfo.lastModified(); - } - -protected: - QFileInfo m_FileInfo; -}; - class QDirFileTreeImpl : public QDirFileTree { public: @@ -47,7 +32,7 @@ protected: entries.push_back(std::make_shared(parent, QDir(info.absoluteFilePath()))); } else { - entries.push_back(std::make_shared(parent, info)); + entries.push_back(createFileEntry(parent, info.fileName())); } } -- cgit v1.3.1