File indexing completed on 2024-12-08 12:23:42
0001 /* 0002 * This file is part of KQuickCharts 0003 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "RangeGroup.h" 0009 0010 #include <cmath> 0011 0012 #include <QVector> 0013 0014 RangeGroup::RangeGroup(QObject *parent) 0015 : QObject(parent) 0016 { 0017 connect(this, &RangeGroup::fromChanged, this, &RangeGroup::rangeChanged); 0018 connect(this, &RangeGroup::toChanged, this, &RangeGroup::rangeChanged); 0019 connect(this, &RangeGroup::automaticChanged, this, &RangeGroup::rangeChanged); 0020 connect(this, &RangeGroup::minimumChanged, this, &RangeGroup::rangeChanged); 0021 connect(this, &RangeGroup::incrementChanged, this, &RangeGroup::rangeChanged); 0022 } 0023 0024 qreal RangeGroup::from() const 0025 { 0026 return m_from; 0027 } 0028 0029 void RangeGroup::setFrom(qreal from) 0030 { 0031 if (qFuzzyCompare(m_from, from)) { 0032 return; 0033 } 0034 0035 m_from = from; 0036 Q_EMIT fromChanged(); 0037 } 0038 0039 qreal RangeGroup::to() const 0040 { 0041 return m_to; 0042 } 0043 0044 void RangeGroup::setTo(qreal to) 0045 { 0046 if (qFuzzyCompare(m_to, to)) { 0047 return; 0048 } 0049 0050 m_to = to; 0051 Q_EMIT toChanged(); 0052 } 0053 0054 bool RangeGroup::automatic() const 0055 { 0056 return m_automatic; 0057 } 0058 0059 void RangeGroup::setAutomatic(bool automatic) 0060 { 0061 if (m_automatic == automatic) { 0062 return; 0063 } 0064 0065 m_automatic = automatic; 0066 Q_EMIT automaticChanged(); 0067 } 0068 0069 qreal RangeGroup::distance() const 0070 { 0071 return m_to - m_from; 0072 } 0073 0074 qreal RangeGroup::minimum() const 0075 { 0076 return m_minimum; 0077 } 0078 0079 void RangeGroup::setMinimum(qreal newMinimum) 0080 { 0081 if (newMinimum == m_minimum) { 0082 return; 0083 } 0084 0085 m_minimum = newMinimum; 0086 Q_EMIT minimumChanged(); 0087 } 0088 0089 qreal RangeGroup::increment() const 0090 { 0091 return m_increment; 0092 } 0093 0094 void RangeGroup::setIncrement(qreal newIncrement) 0095 { 0096 if (newIncrement == m_increment) { 0097 return; 0098 } 0099 0100 m_increment = newIncrement; 0101 Q_EMIT incrementChanged(); 0102 } 0103 0104 bool RangeGroup::isValid() const 0105 { 0106 return m_automatic || (m_to > m_from); 0107 } 0108 0109 RangeGroup::RangeResult RangeGroup::calculateRange(const QVector<ChartDataSource *> &sources, 0110 std::function<qreal(ChartDataSource *)> minimumCallback, 0111 std::function<qreal(ChartDataSource *)> maximumCallback) 0112 { 0113 RangeResult result; 0114 0115 auto min = std::numeric_limits<qreal>::max(); 0116 auto max = std::numeric_limits<qreal>::min(); 0117 0118 if (!m_automatic) { 0119 min = m_from; 0120 max = m_to; 0121 } else { 0122 std::for_each(sources.begin(), sources.end(), [&](ChartDataSource *source) { 0123 min = std::min(min, minimumCallback(source)); 0124 max = std::max(max, maximumCallback(source)); 0125 }); 0126 } 0127 0128 max = std::max(max, m_minimum); 0129 if (m_increment > 0.0) { 0130 max = m_increment * std::ceil(max / m_increment); 0131 } 0132 0133 result.start = min; 0134 result.end = max; 0135 result.distance = max - min; 0136 0137 return result; 0138 } 0139 0140 #include "moc_RangeGroup.cpp"