2017-01-09 16:55:03 +00:00
|
|
|
//
|
|
|
|
// Copyright (C) 2017 James Turner zakalawe@mac.com
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
#include "fgqcanvasimage.h"
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
2017-01-09 16:55:03 +00:00
|
|
|
#include <QQmlComponent>
|
2016-12-03 14:14:06 +00:00
|
|
|
|
|
|
|
#include "fgcanvaspaintcontext.h"
|
|
|
|
#include "localprop.h"
|
2016-12-21 10:25:58 +00:00
|
|
|
#include "fgqcanvasimageloader.h"
|
2017-01-09 16:55:03 +00:00
|
|
|
#include "canvasitem.h"
|
2017-11-03 13:56:43 +00:00
|
|
|
#include "canvasconnection.h"
|
2017-01-09 16:55:03 +00:00
|
|
|
|
2016-12-03 14:14:06 +00:00
|
|
|
|
2017-02-17 05:38:40 +00:00
|
|
|
#include <QSGGeometry>
|
|
|
|
#include <QSGGeometryNode>
|
|
|
|
#include <QSGFlatColorMaterial>
|
|
|
|
#include <QSGTexture>
|
|
|
|
#include <QSGSimpleTextureNode>
|
|
|
|
#include <QQuickWindow>
|
|
|
|
|
|
|
|
class ImageQuickItem : public CanvasItem
|
2016-12-03 14:14:06 +00:00
|
|
|
{
|
2017-02-17 05:38:40 +00:00
|
|
|
Q_OBJECT
|
2016-12-03 14:14:06 +00:00
|
|
|
|
2017-02-17 05:38:40 +00:00
|
|
|
public:
|
|
|
|
ImageQuickItem(QQuickItem* parent)
|
|
|
|
: CanvasItem(parent)
|
|
|
|
{
|
|
|
|
setFlag(ItemHasContents);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSourceRect(const QRectF& sourceRect)
|
|
|
|
{
|
|
|
|
m_sourceRect = sourceRect;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSize(const QSizeF &size)
|
|
|
|
{
|
|
|
|
m_size = size;
|
2017-04-24 11:24:50 +00:00
|
|
|
setImplicitSize(size.width(), size.height());
|
2017-02-17 05:38:40 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPixmap(QPixmap pixmap)
|
|
|
|
{
|
2017-04-24 11:24:50 +00:00
|
|
|
m_pixmap = pixmap;
|
2017-02-17 05:38:40 +00:00
|
|
|
update();
|
2017-11-03 13:56:43 +00:00
|
|
|
}
|
2017-02-17 05:38:40 +00:00
|
|
|
|
|
|
|
virtual QSGNode* updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData *data)
|
|
|
|
{
|
2017-04-24 11:24:50 +00:00
|
|
|
if (m_pixmap.isNull()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-17 05:38:40 +00:00
|
|
|
QSGSimpleTextureNode* texNode = static_cast<QSGSimpleTextureNode*>(oldNode);
|
|
|
|
if (!texNode) {
|
|
|
|
texNode = new QSGSimpleTextureNode;
|
|
|
|
texNode->setOwnsTexture(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
texNode->setRect(QRectF(QPointF(), m_size));
|
|
|
|
texNode->setSourceRect(m_sourceRect);
|
|
|
|
|
2017-04-24 11:24:50 +00:00
|
|
|
if (m_texture) {
|
|
|
|
delete m_texture;
|
|
|
|
}
|
|
|
|
m_texture = window()->createTextureFromImage(m_pixmap.toImage(), QQuickWindow::TextureCanUseAtlas);
|
|
|
|
texNode->setTexture(m_texture);
|
2017-02-17 05:38:40 +00:00
|
|
|
return texNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
|
|
|
|
{
|
|
|
|
QQuickItem::geometryChanged(newGeometry, oldGeometry);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF boundingRect() const
|
|
|
|
{
|
2017-04-24 11:24:50 +00:00
|
|
|
if (!widthValid() || !heightValid()) {
|
2017-02-17 05:38:40 +00:00
|
|
|
return QRectF(QPointF(), m_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QQuickItem::boundingRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QRectF m_sourceRect;
|
2017-04-24 11:24:50 +00:00
|
|
|
QSGTexture* m_texture = nullptr;
|
2017-02-17 05:38:40 +00:00
|
|
|
QSizeF m_size;
|
2017-04-24 11:24:50 +00:00
|
|
|
QPixmap m_pixmap;
|
2017-02-17 05:38:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FGQCanvasImage::FGQCanvasImage(FGCanvasGroup* pr, LocalProp* prop) :
|
|
|
|
FGCanvasElement(pr, prop)
|
|
|
|
{
|
2017-01-09 16:55:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 13:56:43 +00:00
|
|
|
void FGQCanvasImage::doPolish()
|
2016-12-03 14:14:06 +00:00
|
|
|
{
|
|
|
|
if (_imageDirty) {
|
|
|
|
rebuildImage();
|
|
|
|
_imageDirty = false;
|
|
|
|
}
|
|
|
|
|
2016-12-21 15:26:20 +00:00
|
|
|
if (_sourceRectDirty) {
|
|
|
|
recomputeSourceRect();
|
|
|
|
}
|
2017-11-03 13:56:43 +00:00
|
|
|
}
|
2016-12-21 15:26:20 +00:00
|
|
|
|
2017-11-03 13:56:43 +00:00
|
|
|
void FGQCanvasImage::doPaint(FGCanvasPaintContext *context) const
|
|
|
|
{
|
2016-12-21 15:26:20 +00:00
|
|
|
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
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-18 23:52:16 +00:00
|
|
|
void FGQCanvasImage::markImageDirty()
|
|
|
|
{
|
|
|
|
_imageDirty = true;
|
2017-11-03 13:56:43 +00:00
|
|
|
requestPolish();
|
2016-12-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 15:26:20 +00:00
|
|
|
void FGQCanvasImage::markSourceDirty()
|
|
|
|
{
|
|
|
|
_sourceRectDirty = true;
|
2017-11-03 13:56:43 +00:00
|
|
|
requestPolish();
|
2016-12-21 15:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-02-17 05:38:40 +00:00
|
|
|
|
|
|
|
if (_quickItem) {
|
|
|
|
_quickItem->setSourceRect(_sourceRect);
|
|
|
|
}
|
2016-12-21 15:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGQCanvasImage::rebuildImage() const
|
|
|
|
{
|
2016-12-21 10:25:58 +00:00
|
|
|
QByteArray file = _propertyRoot->value("file", QByteArray()).toByteArray();
|
2017-11-03 13:56:43 +00:00
|
|
|
auto loader = connection()->imageLoader();
|
2016-12-21 10:25:58 +00:00
|
|
|
if (!file.isEmpty()) {
|
2017-11-03 13:56:43 +00:00
|
|
|
_image = loader->getImage(file);
|
2016-12-21 10:25:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (_image.isNull()) {
|
|
|
|
// get notified when the image loads
|
2017-11-03 13:56:43 +00:00
|
|
|
loader->connectToImageLoaded(file,
|
|
|
|
const_cast<FGQCanvasImage*>(this),
|
|
|
|
SLOT(markImageDirty()));
|
2016-12-21 10:25:58 +00:00
|
|
|
} 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;
|
2017-02-17 05:38:40 +00:00
|
|
|
|
|
|
|
if (_quickItem) {
|
|
|
|
_quickItem->setSize(_destSize);
|
|
|
|
_quickItem->setPixmap(_image);
|
|
|
|
}
|
2016-12-03 14:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGQCanvasImage::markStyleDirty()
|
|
|
|
{
|
|
|
|
}
|
2017-01-09 16:55:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
CanvasItem *FGQCanvasImage::createQuickItem(QQuickItem *parent)
|
|
|
|
{
|
2017-02-17 05:38:40 +00:00
|
|
|
_quickItem = new ImageQuickItem(parent);
|
|
|
|
|
|
|
|
_quickItem->setSourceRect(_sourceRect);
|
|
|
|
_quickItem->setSize(_destSize);
|
|
|
|
_quickItem->setPixmap(_image);
|
|
|
|
|
2017-01-09 16:55:03 +00:00
|
|
|
return _quickItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasItem *FGQCanvasImage::quickItem() const
|
|
|
|
{
|
|
|
|
return _quickItem;
|
|
|
|
}
|
2017-02-17 05:38:40 +00:00
|
|
|
|
|
|
|
#include "fgqcanvasimage.moc"
|