From 97309f2f9fd876716aea91e09e4a444fa0ebaef8 Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 24 Feb 2024 21:49:50 +0100 Subject: [PATCH] started implementing RsCollectionModel --- .../src/gui/common/RsCollection.cpp | 7 ++- retroshare-gui/src/gui/common/RsCollection.h | 4 +- .../src/gui/common/RsCollectionModel.cpp | 56 +++++++++++++++++ .../src/gui/common/RsCollectionModel.h | 62 +++++++++++++++++++ 4 files changed, 124 insertions(+), 5 deletions(-) diff --git a/retroshare-gui/src/gui/common/RsCollection.cpp b/retroshare-gui/src/gui/common/RsCollection.cpp index c1a21f9a1..8f23a3d50 100644 --- a/retroshare-gui/src/gui/common/RsCollection.cpp +++ b/retroshare-gui/src/gui/common/RsCollection.cpp @@ -68,7 +68,7 @@ RsCollection::~RsCollection() void RsCollection::downloadFiles() const { -#ifdef TODO +#ifdef TODO_COLLECTION // print out the element names of all elements that are direct children // of the outermost element. QDomElement docElem = _xml_doc.documentElement(); @@ -83,7 +83,7 @@ void RsCollection::downloadFiles() const void RsCollection::autoDownloadFiles() const { -#ifdef TODO +#ifdef TODO_COLLECTION QDomElement docElem = _xml_doc.documentElement(); std::vector colFileInfos; @@ -322,13 +322,13 @@ void RsCollection::recursAddElements( d.appendChild(f) ; } } -#endif static void showErrorBox(const QString& fileName, const QString& error) { QMessageBox mb(QMessageBox::Warning, QObject::tr("Failed to process collection file"), QObject::tr("The collection file %1 could not be opened.\nReported error is: \n\n%2").arg(fileName).arg(error), QMessageBox::Ok); mb.exec(); } +#endif QString RsCollection::errorString(RsCollectionErrorCode code) { @@ -563,6 +563,7 @@ bool RsCollection::recursParseXml(QDomDocument& doc,const QDomNode& e,const RsFi n = n.nextSibling() ; } + return true; } bool RsCollection::recursExportToXml(QDomDocument& doc,QDomElement& e,const RsFileTree::DirData& dd) const { diff --git a/retroshare-gui/src/gui/common/RsCollection.h b/retroshare-gui/src/gui/common/RsCollection.h index ecc5aa6fc..2295acacd 100644 --- a/retroshare-gui/src/gui/common/RsCollection.h +++ b/retroshare-gui/src/gui/common/RsCollection.h @@ -91,14 +91,14 @@ public: // returns the file tree const RsFileTree& fileTree() const { return mFileTree; } + // total size of files in the collection + qulonglong size(); // Download the content. void downloadFiles() const ; // Auto Download all the content. void autoDownloadFiles() const ; - qulonglong size(); - static bool isCollectionFile(const QString& fileName); private: diff --git a/retroshare-gui/src/gui/common/RsCollectionModel.cpp b/retroshare-gui/src/gui/common/RsCollectionModel.cpp index e69de29bb..bcc61d86f 100644 --- a/retroshare-gui/src/gui/common/RsCollectionModel.cpp +++ b/retroshare-gui/src/gui/common/RsCollectionModel.cpp @@ -0,0 +1,56 @@ +#include + +#include "RsCollectionModel.h" + +// Indernal Id is always a quintptr_t (basically a uint with the size of a pointer). Depending on the +// architecture, the pointer may have 4 or 8 bytes. We use the low-level bit for type (0=dir, 1=file) and +// the remaining bits for the index (which will be accordingly understood as a FileIndex or a DirIndex) +// This way, index 0 is always the top dir. + +bool RsCollectionModel::convertIndexToInternalId(const EntryIndex& e,quintptr& ref) +{ + ref = (e.index << 1) || e.is_file; + return true; +} + +bool RsCollectionModel::convertInternalIdToIndex(quintptr ref, EntryIndex& e) +{ + e.is_file = (bool)(ref & 1); + e.index = ref >> 1; + return true; +} + +int RsCollectionModel::rowCount(const QModelIndex& parent) const +{ + if(!parent.isValid()) + return mCollection.fileTree().root(); + + EntryIndex i; + if(!convertInternalIdToIndex(parent.internalId(),i)) + return 0; + + if(i.is_file) + return 0; + else + return mCollection.fileTree().directoryData(i.index).subdirs.size(); +} + +int RsCollectionModel::columnCount(const QModelIndex&) const +{ + return 5; +} + +QVariant RsCollectionModel::headerData(int section, Qt::Orientation,int) const +{ + switch(section) + { + case 0: return tr("File"); + case 1: return tr("Path"); + case 2: return tr("Size"); + case 3: return tr("Hash"); + case 4: return tr("Count"); + default: + return QVariant(); + } +} + diff --git a/retroshare-gui/src/gui/common/RsCollectionModel.h b/retroshare-gui/src/gui/common/RsCollectionModel.h index e69de29bb..22b4a4ddf 100644 --- a/retroshare-gui/src/gui/common/RsCollectionModel.h +++ b/retroshare-gui/src/gui/common/RsCollectionModel.h @@ -0,0 +1,62 @@ +#include + +#include "RsCollection.h" + +class RsCollectionModel: public QAbstractItemModel +{ + Q_OBJECT + + public: + enum Roles{ FileNameRole = Qt::UserRole+1, SortRole = Qt::UserRole+2, FilterRole = Qt::UserRole+3 }; + + RsCollectionModel(bool mode, QObject *parent = 0); + virtual ~RsCollectionModel() ; + + + /* Callback from Core */ + void preMods(); + void postMods(); + + /* Callback from GUI */ + + void update() {} + void filterItems(const std::list& keywords, uint32_t& found) ; + + // Overloaded from QAbstractItemModel + virtual Qt::ItemFlags flags ( const QModelIndex & index ) const override; + virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex() ) const override; + virtual QModelIndex parent ( const QModelIndex & index ) const override; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override; + virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const override; + + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + + virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + virtual QStringList mimeTypes () const override; + virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const override; +#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) + virtual Qt::DropActions supportedDragActions() const override; +#endif + + protected: + struct EntryIndex { + bool is_file; // false=dir, true=file + uint64_t index; + }; + static bool convertIndexToInternalId(const EntryIndex& e,quintptr& ref); + static bool convertInternalIdToIndex(quintptr ref, EntryIndex& e); + + // virtual QVariant displayRole(const DirDetails&,int) const = 0 ; + // virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const =0; + + // QVariant decorationRole(const DirDetails&,int) const ; + // QVariant filterRole(const DirDetails& details,int coln) const; + + bool mUpdating ; + + const RsCollection& mCollection; + + // std::set mFilteredPointers ; +};