| 1 | // |
| 2 | // Copyright (C) 2017 James Turner [email protected] |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or |
| 5 | // modify it under the terms of the GNU General Public License as |
| 6 | // published by the Free Software Foundation; either version 2 of the |
| 7 | // License, or (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, but |
| 10 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | // General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | |
| 18 | #include "fgqcanvasimage.h" |
| 19 | |
| 20 | #include <QPainter> |
| 21 | #include <QDebug> |
| 22 | #include <QQmlComponent> |
| 23 | |
| 24 | #include "fgcanvaspaintcontext.h" |
| 25 | #include "localprop.h" |
| 26 | #include "fgqcanvasimageloader.h" |
| 27 | #include "canvasitem.h" |
| 28 | #include "canvasconnection.h" |
| 29 | |
| 30 | |
| 31 | #include <QSGGeometry> |
| 32 | #include <QSGGeometryNode> |
| 33 | #include <QSGFlatColorMaterial> |
| 34 | #include <QSGTexture> |
| 35 | #include <QSGSimpleTextureNode> |
| 36 | #include <QQuickWindow> |
| 37 | |
| 38 | class ImageQuickItem : public CanvasItem |
| 39 | { |
| 40 | Q_OBJECT |
| 41 | |
| 42 | public: |
| 43 | ImageQuickItem(QQuickItem* parent) |
| 44 | : CanvasItem(parent) |
| 45 | { |
| 46 | setFlag(ItemHasContents); |
| 47 | } |
| 48 | |
| 49 | void setSourceRect(const QRectF& sourceRect) |
| 50 | { |
| 51 | m_sourceRect = sourceRect; |
| 52 | update(); |
| 53 | } |
| 54 | |
| 55 | void setSize(const QSizeF &size) |
| 56 | { |
| 57 | m_size = size; |
| 58 | setImplicitSize(size.width(), size.height()); |
| 59 | update(); |
| 60 | } |
| 61 | |
| 62 | void setPixmap(QPixmap pixmap) |
| 63 | { |
| 64 | m_pixmap = pixmap; |
| 65 | update(); |
| 66 | } |
| 67 | |
| 68 | QSGNode* updateRealPaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData *data) override |
| 69 | { |
| 70 | if (m_pixmap.isNull()) { |
| 71 | return nullptr; |
| 72 | } |
| 73 | |
| 74 | QSGSimpleTextureNode* texNode = static_cast<QSGSimpleTextureNode*>(oldNode); |
| 75 | if (!texNode) { |
| 76 | texNode = new QSGSimpleTextureNode; |
| 77 | texNode->setOwnsTexture(true); |
| 78 | } |
| 79 | |
| 80 | texNode->setRect(QRectF(QPointF(), m_size)); |
| 81 | texNode->setSourceRect(m_sourceRect); |
| 82 | |
| 83 | if (m_texture) { |
| 84 | delete m_texture; |
| 85 | } |
| 86 | m_texture = window()->createTextureFromImage(m_pixmap.toImage(), QQuickWindow::TextureCanUseAtlas); |
| 87 | texNode->setTexture(m_texture); |
| 88 | return texNode; |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override |
| 93 | { |
| 94 | QQuickItem::geometryChanged(newGeometry, oldGeometry); |
| 95 | update(); |
| 96 | } |
| 97 | |
| 98 | QRectF boundingRect() const override |
| 99 | { |
| 100 | if (!widthValid() || !heightValid()) { |
| 101 | return QRectF(QPointF(), m_size); |
| 102 | } |
| 103 | |
| 104 | return QQuickItem::boundingRect(); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | QRectF m_sourceRect; |
| 109 | QSGTexture* m_texture = nullptr; |
| 110 | QSizeF m_size; |
| 111 | QPixmap m_pixmap; |
| 112 | }; |
| 113 | |
| 114 | FGQCanvasImage::FGQCanvasImage(FGCanvasGroup* pr, LocalProp* prop) : |
| 115 | FGCanvasElement(pr, prop) |
| 116 | { |
| 117 | } |
| 118 | |
| 119 | void FGQCanvasImage::doPolish() |
| 120 | { |
| 121 | if (_imageDirty) { |
| 122 | rebuildImage(); |
| 123 | _imageDirty = false; |
| 124 | } |
| 125 | |
| 126 | if (_sourceRectDirty) { |
| 127 | recomputeSourceRect(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void FGQCanvasImage::doPaint(FGCanvasPaintContext *context) const |
| 132 | { |
| 133 | QRectF dstRect(0.0, 0.0, _destSize.width(), _destSize.height()); |
| 134 | context->painter()->drawPixmap(dstRect, _image, _sourceRect); |
| 135 | } |
| 136 | |
| 137 | bool FGQCanvasImage::onChildAdded(LocalProp *prop) |
| 138 | { |
| 139 | if (FGCanvasElement::onChildAdded(prop)) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | const QByteArray nm = prop->name(); |
| 144 | if ((nm == "src") || (nm == "size") || (nm == "file")) { |
| 145 | connect(prop, &LocalProp::valueChanged, this, &FGQCanvasImage::markImageDirty); |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | if (nm == "source") { |
| 150 | FGQCanvasImage* self = this; |
| 151 | connect(prop, &LocalProp::childAdded, [self](LocalProp* newChild) { |
| 152 | connect(newChild, &LocalProp::valueChanged, self, &FGQCanvasImage::markSourceDirty); |
| 153 | }); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | void FGQCanvasImage::markImageDirty() |
| 161 | { |
| 162 | _imageDirty = true; |
| 163 | requestPolish(); |
| 164 | } |
| 165 | |
| 166 | void FGQCanvasImage::markSourceDirty() |
| 167 | { |
| 168 | _sourceRectDirty = true; |
| 169 | requestPolish(); |
| 170 | } |
| 171 | |
| 172 | void FGQCanvasImage::recomputeSourceRect() const |
| 173 | { |
| 174 | const float imageWidth = _image.width(); |
| 175 | const float imageHeight = _image.height(); |
| 176 | _sourceRect = QRectF(0, 0, imageWidth, imageHeight); |
| 177 | if (!_propertyRoot->hasChild("source")) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | const bool normalized = _propertyRoot->value("source/normalized", true).toBool(); |
| 182 | float left = _propertyRoot->value("source/left", 0.0).toFloat(); |
| 183 | float top = _propertyRoot->value("source/top", 0.0).toFloat(); |
| 184 | float right = _propertyRoot->value("source/right", 1.0).toFloat(); |
| 185 | float bottom = _propertyRoot->value("source/bottom", 1.0).toFloat(); |
| 186 | |
| 187 | if (normalized) { |
| 188 | left *= imageWidth; |
| 189 | right *= imageWidth; |
| 190 | top *= imageHeight; |
| 191 | bottom *= imageHeight; |
| 192 | } |
| 193 | |
| 194 | _sourceRect = QRectF(left, top, right - left, bottom - top); |
| 195 | _sourceRectDirty = false; |
| 196 | |
| 197 | if (_quickItem) { |
| 198 | _quickItem->setSourceRect(_sourceRect); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void FGQCanvasImage::rebuildImage() const |
| 203 | { |
| 204 | QByteArray file = _propertyRoot->value("file", QByteArray()).toByteArray(); |
| 205 | auto loader = connection()->imageLoader(); |
| 206 | if (!file.isEmpty()) { |
| 207 | _image = loader->getImage(file); |
| 208 | |
| 209 | |
| 210 | if (_image.isNull()) { |
| 211 | // get notified when the image loads |
| 212 | loader->connectToImageLoaded(file, |
| 213 | const_cast<FGQCanvasImage*>(this), |
| 214 | SLOT(markImageDirty())); |
| 215 | } else { |
| 216 | // loaded image ok! |
| 217 | } |
| 218 | |
| 219 | } else { |
| 220 | qDebug() << "src" << _propertyRoot->value("src", QString()); |
| 221 | } |
| 222 | |
| 223 | _destSize = QSizeF(_propertyRoot->value("size[0]", 0.0).toFloat(), |
| 224 | _propertyRoot->value("size[1]", 0.0).toFloat()); |
| 225 | |
| 226 | _imageDirty = false; |
| 227 | |
| 228 | if (_quickItem) { |
| 229 | _quickItem->setSize(_destSize); |
| 230 | _quickItem->setPixmap(_image); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void FGQCanvasImage::markStyleDirty() |
| 235 | { |
| 236 | } |
| 237 | |
| 238 | void FGQCanvasImage::doDestroy() |
| 239 | { |
| 240 | delete _quickItem; |
| 241 | } |
| 242 | |
| 243 | CanvasItem *FGQCanvasImage::createQuickItem(QQuickItem *parent) |
| 244 | { |
| 245 | _quickItem = new ImageQuickItem(parent); |
| 246 | |
| 247 | _quickItem->setSourceRect(_sourceRect); |
| 248 | _quickItem->setSize(_destSize); |
| 249 | _quickItem->setPixmap(_image); |
| 250 | |
| 251 | return _quickItem; |
| 252 | } |
| 253 | |
| 254 | CanvasItem *FGQCanvasImage::quickItem() const |
| 255 | { |
| 256 | return _quickItem; |
| 257 | } |
| 258 | |
| 259 | void FGQCanvasImage::dumpElement() |
| 260 | { |
| 261 | qDebug() << "Image: " << _source << " at " << _propertyRoot->path(); |
| 262 | } |
| 263 | |
| 264 | #include "fgqcanvasimage.moc" |