Debian Codegraph @

utils/fgqcanvas/canvasdisplay.cpp

raw symbols SCIP
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 "canvasdisplay.h"
19
20 #include <QDebug>
21 #include <QQuickItem>
22
23 #include "canvasconnection.h"
24 #include "fgcanvasgroup.h"
25 #include "fgcanvaspaintcontext.h"
26 #include "canvasitem.h"
27 #include "localprop.h"
28
29 CanvasDisplay::CanvasDisplay(QQuickItem* parent) :
30 QQuickItem(parent)
31 {
32 setTransformOrigin(QQuickItem::TopLeft);
33 setFlag(ItemHasContents);
34 }
35
36 CanvasDisplay::~CanvasDisplay()
37 {
38 delete m_rootItem;
39 }
40
41 void CanvasDisplay::updatePolish()
42 {
43 m_rootElement->polish();
44 }
45
46 void CanvasDisplay::geometryChanged(const QRectF &newGeometry, const QRectF &)
47 {
48 Q_UNUSED(newGeometry);
49 recomputeScaling();
50 }
51
52 void CanvasDisplay::setCanvas(CanvasConnection *canvas)
53 {
54 if (m_connection == canvas)
55 return;
56
57 if (m_connection) {
58 disconnect(m_connection, nullptr, this, nullptr);
59
60
61 qDebug() << "deleting items";
62 delete m_rootItem;
63
64 qDebug() << "deleting elements";
65 m_rootElement.reset();
66
67 qDebug() << "done";
68 }
69
70 m_connection = canvas;
71 emit canvasChanged(m_connection);
72
73 if (m_connection) {
74 connect(m_connection, &QObject::destroyed,
75 this, &CanvasDisplay::onConnectionDestroyed);
76 connect(m_connection, &CanvasConnection::statusChanged,
77 this, &CanvasDisplay::onConnectionStatusChanged);
78 connect(m_connection, &CanvasConnection::updated,
79 this, &CanvasDisplay::onConnectionUpdated);
80
81 onConnectionStatusChanged();
82 }
83
84 }
85
86 void CanvasDisplay::onConnectionDestroyed()
87 {
88 m_connection = nullptr;
89 emit canvasChanged(m_connection);
90
91 m_rootElement.reset();
92 }
93
94 void CanvasDisplay::onConnectionStatusChanged()
95 {
96 if ((m_connection->status() == CanvasConnection::Connected) ||
97 (m_connection->status() == CanvasConnection::Snapshot))
98 {
99 m_rootElement.reset(new FGCanvasGroup(nullptr, m_connection->propertyRoot()));
100 // this is important to elements can discover their connection
101 // by walking their parent chain
102 m_rootElement->setParent(m_connection);
103
104 connect(m_rootElement.get(), &FGCanvasGroup::canvasSizeChanged,
105 this, &CanvasDisplay::onCanvasSizeChanged);
106
107 m_rootItem = m_rootElement->createQuickItem(this);
108 onCanvasSizeChanged();
109
110 if (m_connection->status() == CanvasConnection::Snapshot) {
111 m_connection->propertyRoot()->recursiveNotifyRestored();
112 m_rootElement->polish();
113 update();
114 }
115 }
116 }
117
118 void CanvasDisplay::onConnectionUpdated()
119 {
120 if (m_rootElement) {
121 m_rootElement->polish();
122 update();
123 }
124 }
125
126 void CanvasDisplay::onCanvasSizeChanged()
127 {
128 m_sourceSize = QSizeF(m_connection->propertyRoot()->value("size", 256).toDouble(),
129 m_connection->propertyRoot()->value("size[1]", 256).toDouble());
130 setImplicitSize(m_sourceSize.width(), m_sourceSize.height());
131 recomputeScaling();
132 }
133
134 void CanvasDisplay::recomputeScaling()
135 {
136 const double xScaleFactor = width() / m_sourceSize.width();
137 const double yScaleFactor = height() / m_sourceSize.height();
138 setScale(std::min(xScaleFactor, yScaleFactor));
139 }