From f68ae6d2f810a0e3e80ae235744ce0cc3b88e87d Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 20 Feb 2024 17:14:11 +0400 Subject: [PATCH] Thumbnail field of RowNode can be data or url Now the 0'th field of ContentManagerModel's row representation (which is used for the book thumbnail) can hold either the favicon image data or its url (before it held only the image data). --- src/contentmanagermodel.cpp | 26 ++++++++++++++++++-------- src/contentmanagermodel.h | 4 +++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 495d70f..6000d85 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -33,8 +33,15 @@ QVariant ContentManagerModel::data(const QModelIndex& index, int role) const auto item = static_cast(index.internalPointer()); const auto displayRole = role == Qt::DisplayRole; const auto additionalInfoRole = role == Qt::UserRole+1; - if (displayRole || additionalInfoRole) - return item->data(index.column()); + if (displayRole || additionalInfoRole) { + const QVariant r = item->data(index.column()); + if ( index.column() == 0 && r.type() == QVariant::String ) { + // Thumbnail was requested but what we have is only its URL + return QVariant(); + } else { + return r; + } + } return QVariant(); } @@ -111,22 +118,24 @@ void ContentManagerModel::setBooksData(const BookInfoList& data) emit dataChanged(QModelIndex(), QModelIndex()); } -QByteArray ContentManagerModel::getThumbnail(const BookInfo& bookItem) const +// Returns either data of the thumbnail (as a QByteArray) or a URL (as a +// QString) from where the actual data can be obtained. +QVariant ContentManagerModel::getThumbnail(const BookInfo& bookItem) const { const QVariant faviconEntry = bookItem["favicon"]; if ( faviconEntry.type() == QVariant::ByteArray ) - return faviconEntry.toByteArray(); + return faviconEntry; const auto faviconUrl = faviconEntry.toString(); return m_iconMap.contains(faviconUrl) ? m_iconMap[faviconUrl] - : QByteArray(); + : faviconEntry; } std::shared_ptr ContentManagerModel::createNode(BookInfo bookItem) const { QString id = bookItem["id"].toString(); - const QByteArray bookIcon = getThumbnail(bookItem); + const QVariant bookIcon = getThumbnail(bookItem); std::weak_ptr weakRoot = rootNode; auto rowNodePtr = std::shared_ptr(new RowNode({bookIcon, bookItem["title"], @@ -167,8 +176,9 @@ void ContentManagerModel::refreshIcons() td.clearQueue(); for (auto i = 0; i < rowCount() && i < m_data.size(); i++) { const auto& bookItem = m_data[i]; - if ( bookItem["favicon"].type() == QVariant::String ) { - const auto faviconUrl = bookItem["favicon"].toString(); + const QVariant favicon = bookItem["favicon"]; + if ( favicon.type() == QVariant::String ) { + const auto faviconUrl = favicon.toString(); if (faviconUrl != "" && !m_iconMap.contains(faviconUrl)) { td.addDownload(faviconUrl, bookItem["id"].toString()); } diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h index 2daf4c6..6cc5197 100644 --- a/src/contentmanagermodel.h +++ b/src/contentmanagermodel.h @@ -57,7 +57,9 @@ protected: // functions void fetchMore(const QModelIndex &parent) override; private: // functions - QByteArray getThumbnail(const BookInfo& bookItem) const; + // Returns either data of the thumbnail (as a QByteArray) or a URL (as a + // QString) from where the actual data can be obtained. + QVariant getThumbnail(const BookInfo& bookItem) const; private: // data BookInfoList m_data;