| 1 | // SPDX-FileCopyrightText: 2018 James Turner |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "PreviewImageItem.hxx" |
| 5 | |
| 6 | #include <QDir> |
| 7 | #include <QFileInfo> |
| 8 | #include <QNetworkAccessManager> |
| 9 | #include <QNetworkReply> |
| 10 | #include <QQuickWindow> |
| 11 | #include <QSGSimpleTextureNode> |
| 12 | #include <QTimer> |
| 13 | |
| 14 | #include <Main/globals.hxx> |
| 15 | |
| 16 | |
| 17 | #include <simgear/package/Catalog.hxx> |
| 18 | #include <simgear/package/Package.hxx> |
| 19 | #include <simgear/package/Root.hxx> |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | QNetworkAccessManager* global_previewNetAccess = nullptr; |
| 24 | |
| 25 | } |
| 26 | |
| 27 | PreviewImageItem::PreviewImageItem(QQuickItem* parent) : |
| 28 | QQuickItem(parent) |
| 29 | { |
| 30 | setFlag(ItemHasContents); |
| 31 | // setImplicitWidth(STANDARD_THUMBNAIL_WIDTH); |
| 32 | // setImplicitHeight(STANDARD_THUMBNAIL_HEIGHT); |
| 33 | |
| 34 | Q_ASSERT(global_previewNetAccess); |
| 35 | } |
| 36 | |
| 37 | PreviewImageItem::~PreviewImageItem() = default; |
| 38 | |
| 39 | QSGNode *PreviewImageItem::updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData *) |
| 40 | { |
| 41 | if (m_image.isNull()) { |
| 42 | delete oldNode; |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
| 46 | QSGSimpleTextureNode* textureNode = static_cast<QSGSimpleTextureNode*>(oldNode); |
| 47 | if (m_imageDirty || !textureNode) { |
| 48 | if (!textureNode) { |
| 49 | textureNode = new QSGSimpleTextureNode; |
| 50 | textureNode->setOwnsTexture(true); |
| 51 | } |
| 52 | |
| 53 | QSGTexture* tex = window()->createTextureFromImage(m_image); |
| 54 | textureNode->setTexture(tex); |
| 55 | textureNode->markDirty(QSGBasicGeometryNode::DirtyMaterial); |
| 56 | m_imageDirty = false; |
| 57 | } |
| 58 | |
| 59 | textureNode->setRect(QRectF(0, 0, width(), height())); |
| 60 | return textureNode; |
| 61 | } |
| 62 | |
| 63 | QUrl PreviewImageItem::imageUrl() const |
| 64 | { |
| 65 | return m_imageUrl; |
| 66 | } |
| 67 | |
| 68 | QSize PreviewImageItem::sourceSize() const |
| 69 | { |
| 70 | return m_image.size(); |
| 71 | } |
| 72 | |
| 73 | QString PreviewImageItem::packageId() const |
| 74 | { |
| 75 | return m_packageId; |
| 76 | } |
| 77 | |
| 78 | void PreviewImageItem::setPackageId(QString packageId) |
| 79 | { |
| 80 | if (m_packageId == packageId) |
| 81 | return; |
| 82 | |
| 83 | m_packageId = packageId; |
| 84 | emit packageIdChanged(); |
| 85 | } |
| 86 | |
| 87 | void PreviewImageItem::setGlobalNetworkAccess(QNetworkAccessManager *netAccess) |
| 88 | { |
| 89 | global_previewNetAccess = netAccess; |
| 90 | } |
| 91 | |
| 92 | bool PreviewImageItem::isLoading() const |
| 93 | { |
| 94 | return m_requestActive; |
| 95 | } |
| 96 | |
| 97 | float PreviewImageItem::aspectRatio() const |
| 98 | { |
| 99 | return static_cast<float>(m_image.width()) / m_image.height(); |
| 100 | } |
| 101 | |
| 102 | void PreviewImageItem::clearImage() |
| 103 | { |
| 104 | m_image = QImage{}; |
| 105 | m_imageDirty = true; |
| 106 | update(); |
| 107 | emit isLoadingChanged(); |
| 108 | } |
| 109 | |
| 110 | void PreviewImageItem::clear() |
| 111 | { |
| 112 | m_imageUrl.clear(); |
| 113 | m_packageId.clear(); |
| 114 | m_requestActive = false; |
| 115 | |
| 116 | emit imageUrlChanged(); |
| 117 | emit isLoadingChanged(); |
| 118 | |
| 119 | clearImage(); |
| 120 | } |
| 121 | |
| 122 | void PreviewImageItem::setImageUrl( QUrl url) |
| 123 | { |
| 124 | if (m_imageUrl == url) |
| 125 | return; |
| 126 | |
| 127 | m_imageUrl = url; |
| 128 | m_urlsToTry.clear(); |
| 129 | m_downloadRetryCount = 0; |
| 130 | |
| 131 | if (m_imageUrl.isEmpty()) { |
| 132 | clear(); |
| 133 | } else { |
| 134 | // deferred since packageId may also change at the same time, |
| 135 | // and this impacts URL resolution |
| 136 | QTimer::singleShot(0, this, [this]() { |
| 137 | computeUrls(); |
| 138 | startDownload(); |
| 139 | }); |
| 140 | } |
| 141 | |
| 142 | emit imageUrlChanged(); |
| 143 | } |
| 144 | |
| 145 | void PreviewImageItem::computeUrls() |
| 146 | { |
| 147 | m_urlsToTry.clear(); |
| 148 | if (m_imageUrl.isEmpty()) |
| 149 | return; |
| 150 | |
| 151 | if (m_imageUrl.isRelative()) { |
| 152 | auto pkg = globals->packageRoot()->getPackageById(m_packageId.toStdString()); |
| 153 | if (!pkg) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | auto urls = pkg->catalog()->resolveUrl(m_imageUrl.toString().toStdString()); |
| 158 | for (const auto& u : urls) { |
| 159 | m_urlsToTry.push_back(QString::fromStdString(u)); |
| 160 | } |
| 161 | } else { |
| 162 | m_urlsToTry.append(m_imageUrl); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void PreviewImageItem::startDownload() |
| 167 | { |
| 168 | if (m_urlsToTry.isEmpty()) |
| 169 | return; |
| 170 | |
| 171 | QNetworkRequest request(m_urlsToTry.front()); |
| 172 | QNetworkReply* reply = global_previewNetAccess->get(request); |
| 173 | connect(reply, &QNetworkReply::finished, this, &PreviewImageItem::onFinished); |
| 174 | |
| 175 | #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) |
| 176 | connect(reply, &QNetworkReply::errorOccurred, |
| 177 | this, &PreviewImageItem::onDownloadError); |
| 178 | #else |
| 179 | connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), |
| 180 | this, SLOT(onDownloadError(QNetworkReply::NetworkError))); |
| 181 | #endif |
| 182 | m_requestActive = true; |
| 183 | emit isLoadingChanged(); |
| 184 | } |
| 185 | |
| 186 | void PreviewImageItem::setImage(QImage image) |
| 187 | { |
| 188 | m_image = image; |
| 189 | m_imageDirty = true; |
| 190 | setImplicitSize(m_image.width(), m_image.height()); |
| 191 | emit sourceSizeChanged(); |
| 192 | update(); |
| 193 | } |
| 194 | |
| 195 | void PreviewImageItem::onFinished() |
| 196 | { |
| 197 | QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 198 | reply->deleteLater(); |
| 199 | |
| 200 | if (m_urlsToTry.empty() || (reply->url() != m_urlsToTry.front())) { |
| 201 | // if replies arrive out of order, don't trample the correct one |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | QImage img; |
| 206 | if (!img.load(reply, nullptr)) { |
| 207 | qWarning() << Q_FUNC_INFO << "failed to read image data from" << reply->url(); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | setImage(img); |
| 212 | m_requestActive = false; |
| 213 | emit isLoadingChanged(); |
| 214 | } |
| 215 | |
| 216 | void PreviewImageItem::onDownloadError(QNetworkReply::NetworkError errorCode) |
| 217 | { |
| 218 | QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); |
| 219 | // remove the URL that just failed; this also ensures that onFinished() |
| 220 | // doesn't process this QNetworkReply further. |
| 221 | m_urlsToTry.pop_front(); |
| 222 | |
| 223 | if (m_urlsToTry.isEmpty()) { |
| 224 | qWarning() << Q_FUNC_INFO << "download failed for" << reply->url() << "with error code" << errorCode |
| 225 | << "and no more URLs to try"; |
| 226 | m_requestActive = false; |
| 227 | emit isLoadingChanged(); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | qInfo() << "Retrying download for" << reply->url() << ", next URL" << m_urlsToTry.front(); |
| 232 | startDownload(); |
| 233 | } |