| 1 | // SPDX-FileCopyrightText: 2018 James Turner |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "ThumbnailImageItem.hxx" |
| 5 | |
| 6 | #include <QSGSimpleTextureNode> |
| 7 | #include <QQuickWindow> |
| 8 | #include <QFileInfo> |
| 9 | #include <QDir> |
| 10 | |
| 11 | #include <Main/globals.hxx> |
| 12 | |
| 13 | #include <simgear/package/Root.hxx> |
| 14 | #include <simgear/package/Package.hxx> |
| 15 | #include <simgear/package/Delegate.hxx> |
| 16 | #include <simgear/package/Catalog.hxx> |
| 17 | #include <simgear/package/Install.hxx> |
| 18 | |
| 19 | using namespace simgear; |
| 20 | |
| 21 | const int STANDARD_THUMBNAIL_HEIGHT = 128; |
| 22 | const int STANDARD_THUMBNAIL_WIDTH = 172; |
| 23 | |
| 24 | class ThumbnailImageItem::ThumbnailPackageDelegate : public pkg::Delegate |
| 25 | { |
| 26 | public: |
| 27 | ThumbnailPackageDelegate(ThumbnailImageItem* o) : owner(o) {} |
| 28 | |
| 29 | void catalogRefreshed(pkg::CatalogRef, StatusCode) override {} |
| 30 | void startInstall(pkg::InstallRef) override {} |
| 31 | void installProgress(pkg::InstallRef, unsigned int, unsigned int) override {} |
| 32 | void finishInstall(pkg::InstallRef, StatusCode ) override {} |
| 33 | void dataForThumbnail(const std::string& aPackageId, |
| 34 | size_t length, const uint8_t* bytes) override; |
| 35 | |
| 36 | ThumbnailImageItem* owner; |
| 37 | }; |
| 38 | |
| 39 | void ThumbnailImageItem::ThumbnailPackageDelegate::dataForThumbnail( |
| 40 | const std::string& aPackageId, |
| 41 | size_t length, const uint8_t* bytes) |
| 42 | { |
| 43 | if (aPackageId != owner->packageId()) { |
| 44 | // this can happen if the user changes the aircraft while we have a pending thumbnail request |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | const auto iLength = static_cast<int>(length); |
| 49 | QImage img = QImage::fromData(QByteArray::fromRawData(reinterpret_cast<const char*>(bytes), iLength)); |
| 50 | if (img.isNull()) { |
| 51 | if (length > 0) { |
| 52 | // warn if we had valid bytes but couldn't load it, i.e corrupted data or similar |
| 53 | qWarning() << "failed to load image data for package ID:" << QString::fromStdString(aPackageId); |
| 54 | owner->clearImage(); |
| 55 | } |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | owner->setImage(img); |
| 60 | } |
| 61 | |
| 62 | ThumbnailImageItem::ThumbnailImageItem(QQuickItem* parent) : |
| 63 | QQuickItem(parent), |
| 64 | m_delegate(new ThumbnailPackageDelegate(this)), |
| 65 | m_maximumSize(9999, 9999) |
| 66 | { |
| 67 | globals->packageRoot()->addDelegate(m_delegate.get()); |
| 68 | setFlag(ItemHasContents); |
| 69 | setImplicitWidth(STANDARD_THUMBNAIL_WIDTH); |
| 70 | setImplicitHeight(STANDARD_THUMBNAIL_HEIGHT); |
| 71 | } |
| 72 | |
| 73 | ThumbnailImageItem::~ThumbnailImageItem() |
| 74 | { |
| 75 | globals->packageRoot()->removeDelegate(m_delegate.get()); |
| 76 | } |
| 77 | |
| 78 | QSGNode *ThumbnailImageItem::updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData *) |
| 79 | { |
| 80 | if (m_image.isNull()) { |
| 81 | delete oldNode; |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | QSGSimpleTextureNode* textureNode = static_cast<QSGSimpleTextureNode*>(oldNode); |
| 86 | if (m_imageDirty || !textureNode) { |
| 87 | if (!textureNode) { |
| 88 | textureNode = new QSGSimpleTextureNode; |
| 89 | textureNode->setOwnsTexture(true); |
| 90 | } |
| 91 | QSGTexture* tex = window()->createTextureFromImage(m_image, QQuickWindow::TextureIsOpaque); |
| 92 | textureNode->setTexture(tex); |
| 93 | textureNode->markDirty(QSGBasicGeometryNode::DirtyMaterial); |
| 94 | m_imageDirty = false; |
| 95 | } |
| 96 | |
| 97 | textureNode->setRect(QRectF(0, 0, width(), height())); |
| 98 | return textureNode; |
| 99 | } |
| 100 | |
| 101 | QUrl ThumbnailImageItem::url() const |
| 102 | { |
| 103 | return m_imageUrl; |
| 104 | } |
| 105 | |
| 106 | QString ThumbnailImageItem::aircraftUri() const |
| 107 | { |
| 108 | return m_aircraftUri; |
| 109 | } |
| 110 | |
| 111 | QSize ThumbnailImageItem::sourceSize() const |
| 112 | { |
| 113 | return m_image.size(); |
| 114 | } |
| 115 | |
| 116 | QSize ThumbnailImageItem::maximumSize() const |
| 117 | { |
| 118 | return m_maximumSize; |
| 119 | } |
| 120 | |
| 121 | void ThumbnailImageItem::setAircraftUri(QString uri) |
| 122 | { |
| 123 | if (m_aircraftUri == uri) |
| 124 | return; |
| 125 | |
| 126 | m_aircraftUri = uri; |
| 127 | |
| 128 | if (uri.startsWith("package:")) { |
| 129 | globals->packageRoot()->requestThumbnailData(packageId()); |
| 130 | } else { |
| 131 | QFileInfo aircraftSetPath(QUrl(uri).toLocalFile()); |
| 132 | const QString thumbnailPath = aircraftSetPath.dir().filePath("thumbnail.jpg"); |
| 133 | m_imageUrl = QUrl::fromLocalFile(thumbnailPath); |
| 134 | |
| 135 | if (QFileInfo(thumbnailPath).exists()) { |
| 136 | QImage img; |
| 137 | if (img.load(thumbnailPath)) { |
| 138 | setImage(img); |
| 139 | } else { |
| 140 | qWarning() << Q_FUNC_INFO << "failed to load thumbnail from:" << thumbnailPath; |
| 141 | clearImage(); |
| 142 | } |
| 143 | } |
| 144 | } // of local aircraft case |
| 145 | |
| 146 | emit aircraftUriChanged(); |
| 147 | } |
| 148 | |
| 149 | std::string ThumbnailImageItem::packageId() const |
| 150 | { |
| 151 | if (!m_aircraftUri.startsWith("package:")) { |
| 152 | return {}; |
| 153 | } |
| 154 | |
| 155 | return m_aircraftUri.toStdString().substr(8); |
| 156 | } |
| 157 | |
| 158 | void ThumbnailImageItem::setMaximumSize(QSize maximumSize) |
| 159 | { |
| 160 | if (m_maximumSize == maximumSize) |
| 161 | return; |
| 162 | |
| 163 | m_maximumSize = maximumSize; |
| 164 | emit maximumSizeChanged(m_maximumSize); |
| 165 | |
| 166 | if (!m_image.isNull()) { |
| 167 | setImplicitSize(qMin(m_maximumSize.width(), m_image.width()), |
| 168 | qMin(m_maximumSize.height(), m_image.height())); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void ThumbnailImageItem::setImage(QImage image) |
| 173 | { |
| 174 | m_image = image; |
| 175 | m_imageDirty = true; |
| 176 | setImplicitSize(qMin(m_maximumSize.width(), m_image.width()), |
| 177 | qMin(m_maximumSize.height(), m_image.height())); |
| 178 | emit sourceSizeChanged(); |
| 179 | update(); |
| 180 | } |
| 181 | |
| 182 | void ThumbnailImageItem::clearImage() |
| 183 | { |
| 184 | m_image = QImage{}; |
| 185 | m_imageDirty = true; |
| 186 | setImplicitSize(m_maximumSize.width(), m_maximumSize.height()); |
| 187 | emit sourceSizeChanged(); |
| 188 | update(); |
| 189 | } |