Debian Codegraph @

utils/fgqcanvas/canvaspainteddisplay.cpp

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