1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "declarativechartnode_p.h" |
5 | #include "declarativeabstractrendernode_p.h" |
6 | |
7 | #include <QtQuick/QQuickWindow> |
8 | #include <QtQuick/QSGImageNode> |
9 | #include <QtQuick/QSGRendererInterface> |
10 | |
11 | #ifndef QT_NO_OPENGL |
12 | # include "declarativeopenglrendernode_p.h" |
13 | #endif |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | // This node handles displaying of the chart itself |
18 | DeclarativeChartNode::DeclarativeChartNode(QQuickWindow *window) : |
19 | QSGRootNode(), |
20 | m_window(window), |
21 | m_renderNode(nullptr), |
22 | m_imageNode(nullptr) |
23 | { |
24 | // Create a DeclarativeRenderNode for correct QtQuick Backend |
25 | #ifndef QT_NO_OPENGL |
26 | if (m_window->rendererInterface()->graphicsApi() == QSGRendererInterface::OpenGL) |
27 | m_renderNode = new DeclarativeOpenGLRenderNode(m_window); |
28 | #endif |
29 | |
30 | if (m_renderNode) { |
31 | m_renderNode->setFlag(OwnedByParent); |
32 | appendChildNode(node: m_renderNode); |
33 | m_renderNode->setRect(QRectF(0, 0, 0, 0)); // Hide child node by default |
34 | } |
35 | } |
36 | |
37 | DeclarativeChartNode::~DeclarativeChartNode() |
38 | { |
39 | } |
40 | |
41 | // Must be called on render thread and in context |
42 | void DeclarativeChartNode::createTextureFromImage(const QImage &chartImage) |
43 | { |
44 | static auto const defaultTextureOptions = QQuickWindow::CreateTextureOptions(QQuickWindow::TextureHasAlphaChannel | |
45 | QQuickWindow::TextureOwnsGLTexture); |
46 | |
47 | auto texture = m_window->createTextureFromImage(image: chartImage, options: defaultTextureOptions); |
48 | // Create Image node if needed |
49 | if (!m_imageNode) { |
50 | m_imageNode = m_window->createImageNode(); |
51 | m_imageNode->setFlag(OwnedByParent); |
52 | m_imageNode->setOwnsTexture(true); |
53 | m_imageNode->setTexture(texture); |
54 | prependChildNode(node: m_imageNode); |
55 | } else { |
56 | m_imageNode->setTexture(texture); |
57 | } |
58 | if (!m_rect.isEmpty()) |
59 | m_imageNode->setRect(m_rect); |
60 | } |
61 | |
62 | void DeclarativeChartNode::setRect(const QRectF &rect) |
63 | { |
64 | m_rect = rect; |
65 | |
66 | if (m_imageNode) |
67 | m_imageNode->setRect(rect); |
68 | } |
69 | |
70 | QT_END_NAMESPACE |
71 |