2017-11-02 17:20:42 +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.
|
|
|
|
|
|
|
|
#include "canvasdisplay.h"
|
|
|
|
|
|
|
|
#include <QDebug>
|
2017-11-03 13:56:43 +00:00
|
|
|
#include <QQuickItem>
|
2017-11-02 17:20:42 +00:00
|
|
|
|
|
|
|
#include "canvasconnection.h"
|
|
|
|
#include "fgcanvasgroup.h"
|
|
|
|
#include "fgcanvaspaintcontext.h"
|
|
|
|
#include "canvasitem.h"
|
2017-11-03 13:56:43 +00:00
|
|
|
#include "localprop.h"
|
2017-11-02 17:20:42 +00:00
|
|
|
|
|
|
|
CanvasDisplay::CanvasDisplay(QQuickItem* parent) :
|
|
|
|
QQuickItem(parent)
|
|
|
|
{
|
|
|
|
qDebug() << "created a canvas display";
|
|
|
|
|
|
|
|
setFlag(ItemHasContents);
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasDisplay::~CanvasDisplay()
|
|
|
|
{
|
2017-11-03 13:56:43 +00:00
|
|
|
qDebug() << Q_FUNC_INFO << "connection is" << m_connection;
|
|
|
|
delete m_rootItem;
|
2017-11-02 17:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasDisplay::updatePolish()
|
|
|
|
{
|
|
|
|
m_rootElement->polish();
|
|
|
|
}
|
|
|
|
|
2017-11-03 13:56:43 +00:00
|
|
|
void CanvasDisplay::geometryChanged(const QRectF &newGeometry, const QRectF &)
|
|
|
|
{
|
|
|
|
Q_UNUSED(newGeometry);
|
|
|
|
recomputeScaling();
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:20:42 +00:00
|
|
|
void CanvasDisplay::setCanvas(CanvasConnection *canvas)
|
|
|
|
{
|
|
|
|
if (m_connection == canvas)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_connection) {
|
|
|
|
disconnect(m_connection, nullptr, this, nullptr);
|
2017-11-03 13:56:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
qDebug() << "deleting items";
|
|
|
|
delete m_rootItem;
|
|
|
|
|
|
|
|
qDebug() << "deleting elements";
|
|
|
|
m_rootElement.reset();
|
|
|
|
|
|
|
|
qDebug() << "done";
|
2017-11-02 17:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_connection = canvas;
|
|
|
|
emit canvasChanged(m_connection);
|
|
|
|
|
2017-11-03 13:56:43 +00:00
|
|
|
if (m_connection) {
|
|
|
|
// delete existing children
|
|
|
|
|
|
|
|
connect(m_connection, &QObject::destroyed,
|
|
|
|
this, &CanvasDisplay::onConnectionDestroyed);
|
|
|
|
connect(m_connection, &CanvasConnection::statusChanged,
|
|
|
|
this, &CanvasDisplay::onConnectionStatusChanged);
|
|
|
|
connect(m_connection, &CanvasConnection::updated,
|
|
|
|
this, &CanvasDisplay::onConnectionUpdated);
|
2017-11-02 17:20:42 +00:00
|
|
|
|
2017-11-03 13:56:43 +00:00
|
|
|
onConnectionStatusChanged();
|
|
|
|
}
|
2017-11-02 17:20:42 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-03 13:56:43 +00:00
|
|
|
void CanvasDisplay::onConnectionDestroyed()
|
|
|
|
{
|
|
|
|
m_connection = nullptr;
|
|
|
|
emit canvasChanged(m_connection);
|
|
|
|
|
|
|
|
m_rootElement.reset();
|
|
|
|
}
|
|
|
|
|
2017-11-02 17:20:42 +00:00
|
|
|
void CanvasDisplay::onConnectionStatusChanged()
|
|
|
|
{
|
|
|
|
if (m_connection->status() == CanvasConnection::Connected) {
|
|
|
|
m_rootElement.reset(new FGCanvasGroup(nullptr, m_connection->propertyRoot()));
|
2017-11-03 13:56:43 +00:00
|
|
|
// this is important to elements can discover their connection
|
|
|
|
// by walking their parent chain
|
|
|
|
m_rootElement->setParent(m_connection);
|
|
|
|
|
|
|
|
connect(m_rootElement.get(), &FGCanvasGroup::canvasSizeChanged,
|
|
|
|
this, &CanvasDisplay::onCanvasSizeChanged);
|
|
|
|
|
|
|
|
m_rootItem = m_rootElement->createQuickItem(this);
|
|
|
|
onCanvasSizeChanged();
|
2017-11-02 17:20:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasDisplay::onConnectionUpdated()
|
|
|
|
{
|
2017-11-03 13:56:43 +00:00
|
|
|
if (m_rootElement) {
|
|
|
|
m_rootElement->polish();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasDisplay::onCanvasSizeChanged()
|
|
|
|
{
|
|
|
|
m_sourceSize = QSizeF(m_connection->propertyRoot()->value("size", 400).toDouble(),
|
|
|
|
m_connection->propertyRoot()->value("size[1]", 400).toDouble());
|
|
|
|
recomputeScaling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CanvasDisplay::recomputeScaling()
|
|
|
|
{
|
2017-11-03 15:31:57 +00:00
|
|
|
const double xScaleFactor = width() / m_sourceSize.width();
|
|
|
|
const double yScaleFactor = height() / m_sourceSize.height();
|
|
|
|
setScale(std::min(xScaleFactor, yScaleFactor));
|
2017-11-02 17:20:42 +00:00
|
|
|
}
|