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 "variantbardataproxy.h"
31
32using namespace QtDataVisualization;
33
34VariantBarDataProxy::VariantBarDataProxy() :
35 QBarDataProxy()
36{
37}
38
39VariantBarDataProxy::VariantBarDataProxy(VariantDataSet *newSet,
40 VariantBarDataMapping *mapping) :
41 QBarDataProxy()
42{
43 setDataSet(newSet);
44 setMapping(mapping);
45}
46
47VariantBarDataProxy::~VariantBarDataProxy()
48{
49 delete m_dataSet;
50}
51
52void VariantBarDataProxy::setDataSet(VariantDataSet *newSet)
53{
54 if (!m_dataSet.isNull())
55 QObject::disconnect(sender: m_dataSet.data(), signal: 0, receiver: this, member: 0);
56
57 m_dataSet = newSet;
58
59 if (!m_dataSet.isNull()) {
60 QObject::connect(sender: m_dataSet.data(), signal: &VariantDataSet::itemsAdded, receiver: this,
61 slot: &VariantBarDataProxy::handleItemsAdded);
62 QObject::connect(sender: m_dataSet.data(), signal: &VariantDataSet::dataCleared, receiver: this,
63 slot: &VariantBarDataProxy::handleDataCleared);
64 }
65 resolveDataSet();
66}
67
68VariantDataSet *VariantBarDataProxy::dataSet()
69{
70 return m_dataSet.data();
71}
72
73void VariantBarDataProxy::setMapping(VariantBarDataMapping *mapping)
74{
75 if (!m_mapping.isNull())
76 QObject::disconnect(sender: m_mapping.data(), signal: &VariantBarDataMapping::mappingChanged, receiver: this,
77 slot: &VariantBarDataProxy::handleMappingChanged);
78
79 m_mapping = mapping;
80
81 if (!m_mapping.isNull())
82 QObject::connect(sender: m_mapping.data(), signal: &VariantBarDataMapping::mappingChanged, receiver: this,
83 slot: &VariantBarDataProxy::handleMappingChanged);
84
85 resolveDataSet();
86}
87
88VariantBarDataMapping *VariantBarDataProxy::mapping()
89{
90 return m_mapping.data();
91}
92
93void VariantBarDataProxy::handleItemsAdded(int index, int count)
94{
95 Q_UNUSED(index)
96 Q_UNUSED(count)
97
98 // Resolve new items
99 resolveDataSet();
100}
101
102void VariantBarDataProxy::handleDataCleared()
103{
104 // Data cleared, reset array
105 resetArray(newArray: 0);
106}
107
108void VariantBarDataProxy::handleMappingChanged()
109{
110 resolveDataSet();
111}
112
113// Resolve entire dataset into QBarDataArray.
114//! [0]
115void VariantBarDataProxy::resolveDataSet()
116{
117 // If we have no data or mapping, or the categories are not defined, simply clear the array
118 if (m_dataSet.isNull() || m_mapping.isNull() || !m_mapping->rowCategories().size()
119 || !m_mapping->columnCategories().size()) {
120 resetArray(newArray: 0);
121 return;
122 }
123 const VariantDataItemList &itemList = m_dataSet->itemList();
124
125 int rowIndex = m_mapping->rowIndex();
126 int columnIndex = m_mapping->columnIndex();
127 int valueIndex = m_mapping->valueIndex();
128 const QStringList &rowList = m_mapping->rowCategories();
129 const QStringList &columnList = m_mapping->columnCategories();
130
131 // Sort values into rows and columns
132 typedef QHash<QString, float> ColumnValueMap;
133 QHash <QString, ColumnValueMap> itemValueMap;
134 foreach (const VariantDataItem *item, itemList) {
135 itemValueMap[item->at(i: rowIndex).toString()][item->at(i: columnIndex).toString()]
136 = item->at(i: valueIndex).toReal();
137 }
138
139 // Create a new data array in format the parent class understands
140 QBarDataArray *newProxyArray = new QBarDataArray;
141 foreach (QString rowKey, rowList) {
142 QBarDataRow *newProxyRow = new QBarDataRow(columnList.size());
143 for (int i = 0; i < columnList.size(); i++)
144 (*newProxyRow)[i].setValue(itemValueMap[rowKey][columnList.at(i)]);
145 newProxyArray->append(t: newProxyRow);
146 }
147
148 // Finally, reset the data array in the parent class
149 resetArray(newArray: newProxyArray);
150}
151//! [0]
152

source code of qtdatavis3d/examples/datavisualization/customproxy/variantbardataproxy.cpp