| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the Qt Charts module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 or (at your option) any later version | 
| 20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by | 
| 21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 | 
| 22 | ** included in the packaging of this file. Please review the following | 
| 23 | ** information to ensure the GNU General Public License requirements will | 
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 25 | ** | 
| 26 | ** $QT_END_LICENSE$ | 
| 27 | ** | 
| 28 | ****************************************************************************/ | 
| 29 |  | 
| 30 | #include "declarativechartnode_p.h" | 
| 31 | #include "declarativeabstractrendernode_p.h" | 
| 32 |  | 
| 33 | #include <QtQuick/QQuickWindow> | 
| 34 | #include <QtQuick/QSGImageNode> | 
| 35 | #include <QtQuick/QSGRendererInterface> | 
| 36 |  | 
| 37 | #ifndef QT_NO_OPENGL | 
| 38 | # include "declarativeopenglrendernode_p.h" | 
| 39 | #endif | 
| 40 |  | 
| 41 | QT_CHARTS_BEGIN_NAMESPACE | 
| 42 |  | 
| 43 | // This node handles displaying of the chart itself | 
| 44 | DeclarativeChartNode::DeclarativeChartNode(QQuickWindow *window) : | 
| 45 |     QSGRootNode(), | 
| 46 |     m_window(window), | 
| 47 |     m_renderNode(nullptr), | 
| 48 |     m_imageNode(nullptr) | 
| 49 | { | 
| 50 |     // Create a DeclarativeRenderNode for correct QtQuick Backend | 
| 51 | #ifndef QT_NO_OPENGL | 
| 52 |     if (m_window->rendererInterface()->graphicsApi() == QSGRendererInterface::OpenGL) | 
| 53 |         m_renderNode = new DeclarativeOpenGLRenderNode(m_window); | 
| 54 | #endif | 
| 55 |  | 
| 56 |     if (m_renderNode) { | 
| 57 |         m_renderNode->setFlag(OwnedByParent); | 
| 58 |         appendChildNode(node: m_renderNode); | 
| 59 |         m_renderNode->setRect(QRectF(0, 0, 0, 0)); // Hide child node by default | 
| 60 |     } | 
| 61 | } | 
| 62 |  | 
| 63 | DeclarativeChartNode::~DeclarativeChartNode() | 
| 64 | { | 
| 65 | } | 
| 66 |  | 
| 67 | // Must be called on render thread and in context | 
| 68 | void DeclarativeChartNode::createTextureFromImage(const QImage &chartImage) | 
| 69 | { | 
| 70 |     static auto const defaultTextureOptions = QQuickWindow::CreateTextureOptions(QQuickWindow::TextureHasAlphaChannel | | 
| 71 |                                                                                  QQuickWindow::TextureOwnsGLTexture); | 
| 72 |  | 
| 73 |     auto texture = m_window->createTextureFromImage(image: chartImage, options: defaultTextureOptions); | 
| 74 |     // Create Image node if needed | 
| 75 |     if (!m_imageNode) { | 
| 76 |         m_imageNode = m_window->createImageNode(); | 
| 77 |         m_imageNode->setFlag(OwnedByParent); | 
| 78 |         m_imageNode->setOwnsTexture(true); | 
| 79 |         m_imageNode->setTexture(texture); | 
| 80 |         prependChildNode(node: m_imageNode); | 
| 81 |     } else { | 
| 82 |         m_imageNode->setTexture(texture); | 
| 83 |     } | 
| 84 |     if (!m_rect.isEmpty()) | 
| 85 |         m_imageNode->setRect(m_rect); | 
| 86 | } | 
| 87 |  | 
| 88 | void DeclarativeChartNode::setRect(const QRectF &rect) | 
| 89 | { | 
| 90 |     m_rect = rect; | 
| 91 |  | 
| 92 |     if (m_imageNode) | 
| 93 |         m_imageNode->setRect(rect); | 
| 94 | } | 
| 95 |  | 
| 96 | QT_CHARTS_END_NAMESPACE | 
| 97 |  |