2016-12-03 14:14:06 +00:00
|
|
|
#include "fgqcanvasimage.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "fgcanvaspaintcontext.h"
|
|
|
|
#include "localprop.h"
|
2016-12-21 10:25:58 +00:00
|
|
|
#include "fgqcanvasimageloader.h"
|
2016-12-03 14:14:06 +00:00
|
|
|
|
|
|
|
FGQCanvasImage::FGQCanvasImage(FGCanvasGroup* pr, LocalProp* prop) :
|
|
|
|
FGCanvasElement(pr, prop)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGQCanvasImage::doPaint(FGCanvasPaintContext *context) const
|
|
|
|
{
|
|
|
|
if (_imageDirty) {
|
|
|
|
rebuildImage();
|
|
|
|
_imageDirty = false;
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:26:20 +00:00
|
|
|
if (_sourceRectDirty) {
|
|
|
|
recomputeSourceRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF dstRect(0.0, 0.0, _destSize.width(), _destSize.height());
|
|
|
|
context->painter()->drawPixmap(dstRect, _image, _sourceRect);
|
2016-12-03 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FGQCanvasImage::onChildAdded(LocalProp *prop)
|
|
|
|
{
|
|
|
|
if (FGCanvasElement::onChildAdded(prop)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-18 23:52:16 +00:00
|
|
|
const QByteArray nm = prop->name();
|
2016-12-21 15:26:20 +00:00
|
|
|
if ((nm == "src") || (nm == "size") || (nm == "file")) {
|
2016-12-18 23:52:16 +00:00
|
|
|
connect(prop, &LocalProp::valueChanged, this, &FGQCanvasImage::markImageDirty);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:26:20 +00:00
|
|
|
if (nm == "source") {
|
|
|
|
FGQCanvasImage* self = this;
|
|
|
|
connect(prop, &LocalProp::childAdded, [self](LocalProp* newChild) {
|
|
|
|
connect(newChild, &LocalProp::valueChanged, self, &FGQCanvasImage::markSourceDirty);
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
qDebug() << "image saw child:" << prop->name();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-18 23:52:16 +00:00
|
|
|
void FGQCanvasImage::markImageDirty()
|
|
|
|
{
|
|
|
|
_imageDirty = true;
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:26:20 +00:00
|
|
|
void FGQCanvasImage::markSourceDirty()
|
|
|
|
{
|
|
|
|
_sourceRectDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGQCanvasImage::recomputeSourceRect() const
|
2016-12-03 14:14:06 +00:00
|
|
|
{
|
2016-12-21 15:26:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2016-12-03 14:14:06 +00:00
|
|
|
|
2016-12-21 15:26:20 +00:00
|
|
|
_sourceRect = QRectF(left, top, right - left, bottom - top);
|
|
|
|
_sourceRectDirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGQCanvasImage::rebuildImage() const
|
|
|
|
{
|
2016-12-21 10:25:58 +00:00
|
|
|
QByteArray file = _propertyRoot->value("file", QByteArray()).toByteArray();
|
|
|
|
if (!file.isEmpty()) {
|
|
|
|
_image = FGQCanvasImageLoader::instance()->getImage(file);
|
|
|
|
|
|
|
|
|
|
|
|
if (_image.isNull()) {
|
|
|
|
// get notified when the image loads
|
|
|
|
FGQCanvasImageLoader::instance()->connectToImageLoaded(file,
|
|
|
|
const_cast<FGQCanvasImage*>(this),
|
|
|
|
SLOT(markImageDirty()));
|
|
|
|
} else {
|
2016-12-21 15:26:20 +00:00
|
|
|
// loaded image ok!
|
2016-12-21 10:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
qDebug() << "src" << _propertyRoot->value("src", QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
_destSize = QSizeF(_propertyRoot->value("size[0]", 0.0).toFloat(),
|
|
|
|
_propertyRoot->value("size[1]", 0.0).toFloat());
|
|
|
|
|
|
|
|
_imageDirty = false;
|
2016-12-03 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGQCanvasImage::markStyleDirty()
|
|
|
|
{
|
|
|
|
}
|