Warning, file /frameworks/kquickcharts/src/datasource/ValueHistorySource.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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 "ValueHistorySource.h" 0009 0010 #include "QmlDeprecated.h" 0011 0012 #if QUICKCHARTS_BUILD_DEPRECATED_SINCE(5, 78) 0013 0014 ValueHistorySource::ValueHistorySource(QObject *parent) 0015 : ChartDataSource(parent) 0016 { 0017 QML_DEPRECATED("ValueHistorySource", "5.78", "Use HistoryProxySource instead") 0018 } 0019 0020 int ValueHistorySource::itemCount() const 0021 { 0022 return m_maximumHistory; 0023 } 0024 0025 QVariant ValueHistorySource::item(int index) const 0026 { 0027 if (index < 0 || index >= m_history.count()) { 0028 return QVariant{}; 0029 } 0030 0031 return m_history.at(index); 0032 } 0033 0034 QVariant ValueHistorySource::minimum() const 0035 { 0036 auto item = std::min_element(m_history.cbegin(), m_history.cend()); 0037 if (item != m_history.cend()) { 0038 return *item; 0039 } 0040 0041 return QVariant{}; 0042 } 0043 0044 QVariant ValueHistorySource::maximum() const 0045 { 0046 auto item = std::max_element(m_history.cbegin(), m_history.cend()); 0047 if (item != m_history.cend()) { 0048 return *item; 0049 } 0050 0051 return QVariant{}; 0052 } 0053 0054 QVariant ValueHistorySource::value() const 0055 { 0056 return m_value; 0057 } 0058 0059 void ValueHistorySource::setValue(const QVariant &newValue) 0060 { 0061 m_value = newValue; 0062 0063 if (!m_updateTimer) { 0064 m_history.prepend(newValue); 0065 while (m_history.size() > m_maximumHistory) { 0066 m_history.removeLast(); 0067 } 0068 0069 Q_EMIT dataChanged(); 0070 } 0071 } 0072 0073 int ValueHistorySource::maximumHistory() const 0074 { 0075 return m_maximumHistory; 0076 } 0077 0078 void ValueHistorySource::setMaximumHistory(int newMaximumHistory) 0079 { 0080 if (newMaximumHistory == m_maximumHistory) { 0081 return; 0082 } 0083 0084 m_maximumHistory = newMaximumHistory; 0085 while (m_history.size() > m_maximumHistory) { 0086 m_history.removeLast(); 0087 } 0088 Q_EMIT maximumHistoryChanged(); 0089 } 0090 0091 int ValueHistorySource::interval() const 0092 { 0093 return m_updateTimer ? m_updateTimer->interval() : -1; 0094 } 0095 0096 void ValueHistorySource::setInterval(int newInterval) 0097 { 0098 if (m_updateTimer && newInterval == m_updateTimer->interval()) { 0099 return; 0100 } 0101 0102 if (newInterval > 0) { 0103 if (!m_updateTimer) { 0104 m_updateTimer = std::make_unique<QTimer>(); 0105 // See ModelHistorySource.cpp line 110 0106 m_updateTimer->setTimerType(Qt::PreciseTimer); 0107 connect(m_updateTimer.get(), &QTimer::timeout, this, [this]() { 0108 m_history.prepend(m_value); 0109 while (m_history.size() > m_maximumHistory) { 0110 m_history.removeLast(); 0111 } 0112 Q_EMIT dataChanged(); 0113 }); 0114 } 0115 m_updateTimer->setInterval(newInterval); 0116 m_updateTimer->start(); 0117 } else { 0118 m_updateTimer.reset(); 0119 } 0120 0121 Q_EMIT intervalChanged(); 0122 } 0123 0124 void ValueHistorySource::clear() 0125 { 0126 m_history.clear(); 0127 Q_EMIT dataChanged(); 0128 } 0129 0130 #include "moc_ValueHistorySource.cpp" 0131 0132 #endif // QUICKCHARTS_BUILD_DEPRECATED_SINCE