From 967f1c14264184225c9b63f52bd88c5f519293b3 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 21 Dec 2016 15:26:20 +0000 Subject: [PATCH] Fix source / dest rect in FGQCanvas image. --- utils/fgqcanvas/fgqcanvasimage.cpp | 53 ++++++++++++++++++++++++++---- utils/fgqcanvas/fgqcanvasimage.h | 5 ++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/utils/fgqcanvas/fgqcanvasimage.cpp b/utils/fgqcanvas/fgqcanvasimage.cpp index b6e3daf79..310e0d4ad 100644 --- a/utils/fgqcanvas/fgqcanvasimage.cpp +++ b/utils/fgqcanvas/fgqcanvasimage.cpp @@ -20,7 +20,12 @@ void FGQCanvasImage::doPaint(FGCanvasPaintContext *context) const _imageDirty = false; } - context->painter()->drawPixmap(0, 0, _image); + if (_sourceRectDirty) { + recomputeSourceRect(); + } + + QRectF dstRect(0.0, 0.0, _destSize.width(), _destSize.height()); + context->painter()->drawPixmap(dstRect, _image, _sourceRect); } bool FGQCanvasImage::onChildAdded(LocalProp *prop) @@ -30,11 +35,19 @@ bool FGQCanvasImage::onChildAdded(LocalProp *prop) } const QByteArray nm = prop->name(); - if ((nm == "source") || (nm == "src") || (nm == "size") || (nm == "file")) { + if ((nm == "src") || (nm == "size") || (nm == "file")) { connect(prop, &LocalProp::valueChanged, this, &FGQCanvasImage::markImageDirty); return true; } + if (nm == "source") { + FGQCanvasImage* self = this; + connect(prop, &LocalProp::childAdded, [self](LocalProp* newChild) { + connect(newChild, &LocalProp::valueChanged, self, &FGQCanvasImage::markSourceDirty); + }); + return true; + } + qDebug() << "image saw child:" << prop->name(); return false; } @@ -44,9 +57,39 @@ void FGQCanvasImage::markImageDirty() _imageDirty = true; } +void FGQCanvasImage::markSourceDirty() +{ + _sourceRectDirty = true; +} + +void FGQCanvasImage::recomputeSourceRect() const +{ + const float imageWidth = _image.width(); + const float imageHeight = _image.height(); + _sourceRect = QRectF(0, 0, imageWidth, imageHeight); + if (!_propertyRoot->hasChild("source")) { + return; + } + + const bool normalized = _propertyRoot->value("source/normalized", true).toBool(); + float left = _propertyRoot->value("source/left", 0.0).toFloat(); + float top = _propertyRoot->value("source/top", 0.0).toFloat(); + float right = _propertyRoot->value("source/right", 1.0).toFloat(); + float bottom = _propertyRoot->value("source/bottom", 1.0).toFloat(); + + if (normalized) { + left *= imageWidth; + right *= imageWidth; + top *= imageHeight; + bottom *= imageHeight; + } + + _sourceRect = QRectF(left, top, right - left, bottom - top); + _sourceRectDirty = false; +} + void FGQCanvasImage::rebuildImage() const { - QByteArray file = _propertyRoot->value("file", QByteArray()).toByteArray(); if (!file.isEmpty()) { _image = FGQCanvasImageLoader::instance()->getImage(file); @@ -58,17 +101,15 @@ void FGQCanvasImage::rebuildImage() const const_cast(this), SLOT(markImageDirty())); } else { - qDebug() << "have image" << _image.size(); + // loaded image ok! } } else { - qDebug() << "source" << _propertyRoot->value("source", QString()); qDebug() << "src" << _propertyRoot->value("src", QString()); } _destSize = QSizeF(_propertyRoot->value("size[0]", 0.0).toFloat(), _propertyRoot->value("size[1]", 0.0).toFloat()); - qDebug() << "dest-size" << _destSize; _imageDirty = false; } diff --git a/utils/fgqcanvas/fgqcanvasimage.h b/utils/fgqcanvas/fgqcanvasimage.h index fe3471bfe..4752be8b1 100644 --- a/utils/fgqcanvas/fgqcanvasimage.h +++ b/utils/fgqcanvas/fgqcanvasimage.h @@ -18,18 +18,21 @@ protected: private slots: void markImageDirty(); + void markSourceDirty(); private: bool onChildAdded(LocalProp *prop) override; void rebuildImage() const; - + void recomputeSourceRect() const; private: mutable bool _imageDirty; + mutable bool _sourceRectDirty = true; mutable QPixmap _image; QString _source; mutable QSizeF _destSize; + mutable QRectF _sourceRect; }; #endif // FGQCANVASIMAGE_H