| 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 Data Visualization 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 "highlightseries.h" | 
| 31 |  | 
| 32 | using namespace QtDataVisualization; | 
| 33 |  | 
| 34 | //! [2] | 
| 35 | const float darkRedPos = 1.0f; | 
| 36 | const float redPos = 0.8f; | 
| 37 | const float yellowPos = 0.6f; | 
| 38 | const float greenPos = 0.4f; | 
| 39 | const float darkGreenPos = 0.2f; | 
| 40 | //! [2] | 
| 41 |  | 
| 42 | HighlightSeries::HighlightSeries() | 
| 43 |     : m_width(100), | 
| 44 |       m_height(100) | 
| 45 | { | 
| 46 |     setDrawMode(QSurface3DSeries::DrawSurface); | 
| 47 |     setFlatShadingEnabled(true); | 
| 48 |     setVisible(false); | 
| 49 | } | 
| 50 |  | 
| 51 | HighlightSeries::~HighlightSeries() | 
| 52 | { | 
| 53 | } | 
| 54 |  | 
| 55 | //! [0] | 
| 56 | void HighlightSeries::setTopographicSeries(TopographicSeries *series) | 
| 57 | { | 
| 58 |     m_topographicSeries = series; | 
| 59 |     m_srcWidth = m_topographicSeries->dataProxy()->array()->at(i: 0)->size(); | 
| 60 |     m_srcHeight = m_topographicSeries->dataProxy()->array()->size(); | 
| 61 |  | 
| 62 |     QObject::connect(sender: m_topographicSeries, signal: &QSurface3DSeries::selectedPointChanged, | 
| 63 |                      receiver: this, slot: &HighlightSeries::handlePositionChange); | 
| 64 | } | 
| 65 | //! [0] | 
| 66 |  | 
| 67 | //! [1] | 
| 68 | void HighlightSeries::handlePositionChange(const QPoint &position) | 
| 69 | { | 
| 70 |     m_position = position; | 
| 71 |  | 
| 72 |     if (position == invalidSelectionPosition()) { | 
| 73 |         setVisible(false); | 
| 74 |  | 
| 75 |         return; | 
| 76 |     } | 
| 77 |  | 
| 78 |     int halfWidth = m_width / 2; | 
| 79 |     int halfHeight = m_height / 2; | 
| 80 |  | 
| 81 |     int startX = position.y() - halfWidth; | 
| 82 |     if (startX < 0 ) | 
| 83 |         startX = 0; | 
| 84 |     int endX = position.y() + halfWidth; | 
| 85 |     if (endX > (m_srcWidth - 1)) | 
| 86 |         endX = m_srcWidth - 1; | 
| 87 |     int startZ = position.x() - halfHeight; | 
| 88 |     if (startZ < 0 ) | 
| 89 |         startZ = 0; | 
| 90 |     int endZ = position.x() + halfHeight; | 
| 91 |     if (endZ > (m_srcHeight - 1)) | 
| 92 |         endZ = m_srcHeight - 1; | 
| 93 |  | 
| 94 |     QSurfaceDataProxy *srcProxy = m_topographicSeries->dataProxy(); | 
| 95 |     const QSurfaceDataArray &srcArray = *srcProxy->array(); | 
| 96 |  | 
| 97 |     QSurfaceDataArray *dataArray = new QSurfaceDataArray; | 
| 98 |     dataArray->reserve(alloc: endZ - startZ); | 
| 99 |     for (int i = startZ; i < endZ; i++) { | 
| 100 |         QSurfaceDataRow *newRow = new QSurfaceDataRow(endX - startX); | 
| 101 |         QSurfaceDataRow *srcRow = srcArray.at(i); | 
| 102 |         for (int j = startX, p = 0; j < endX; j++, p++) { | 
| 103 |             QVector3D pos = srcRow->at(i: j).position(); | 
| 104 |             (*newRow)[p].setPosition(QVector3D(pos.x(), pos.y() + 0.1f, pos.z())); | 
| 105 |         } | 
| 106 |         *dataArray << newRow; | 
| 107 |     } | 
| 108 |  | 
| 109 |     dataProxy()->resetArray(newArray: dataArray); | 
| 110 |     setVisible(true); | 
| 111 | } | 
| 112 | //! [1] | 
| 113 |  | 
| 114 | //! [3] | 
| 115 | void HighlightSeries::handleGradientChange(float value) | 
| 116 | { | 
| 117 |     float ratio = m_minHeight / value; | 
| 118 |  | 
| 119 |     QLinearGradient gr; | 
| 120 |     gr.setColorAt(pos: 0.0f, color: Qt::black); | 
| 121 |     gr.setColorAt(pos: darkGreenPos * ratio, color: Qt::darkGreen); | 
| 122 |     gr.setColorAt(pos: greenPos * ratio, color: Qt::green); | 
| 123 |     gr.setColorAt(pos: yellowPos * ratio, color: Qt::yellow); | 
| 124 |     gr.setColorAt(pos: redPos * ratio, color: Qt::red); | 
| 125 |     gr.setColorAt(pos: darkRedPos * ratio, color: Qt::darkRed); | 
| 126 |  | 
| 127 |     setBaseGradient(gr); | 
| 128 |     setColorStyle(Q3DTheme::ColorStyleRangeGradient); | 
| 129 | } | 
| 130 | //! [3] | 
| 131 |  |