Warning, file /plasma/libksysguard/systemstats/SensorProperty.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "SensorProperty.h"
0008 #include "SensorObject.h"
0009 
0010 using namespace KSysGuard;
0011 
0012 class Q_DECL_HIDDEN SensorProperty::Private
0013 {
0014 public:
0015     SensorObject *parent = nullptr;
0016     SensorInfo info;
0017     QString id;
0018     QString name;
0019     QString prefix;
0020     QVariant value;
0021     QVariant initialValue;
0022     int subscribers = 0;
0023 };
0024 
0025 SensorProperty::SensorProperty(const QString &id, SensorObject *parent)
0026     : SensorProperty(id, QString(), parent)
0027 {
0028 }
0029 
0030 SensorProperty::SensorProperty(const QString &id, const QString &name, SensorObject *parent)
0031     : SensorProperty(id, name, QVariant(), parent)
0032 {
0033 }
0034 
0035 SensorProperty::SensorProperty(const QString &id, const QString &name, const QVariant &initialValue, SensorObject *parent)
0036     : QObject(parent)
0037     , d(std::make_unique<Private>())
0038 {
0039     d->id = id;
0040     d->parent = parent;
0041     setName(name);
0042     d->initialValue = initialValue;
0043     if (initialValue.isValid()) {
0044         setValue(initialValue);
0045     }
0046     parent->addProperty(this);
0047 }
0048 
0049 SensorProperty::~SensorProperty()
0050 {
0051 }
0052 
0053 SensorInfo SensorProperty::info() const
0054 {
0055     return d->info;
0056 }
0057 
0058 QString SensorProperty::id() const
0059 {
0060     return d->id;
0061 }
0062 
0063 QString SensorProperty::path() const
0064 {
0065     return d->parent->path() % QLatin1Char('/') % d->id;
0066 }
0067 
0068 void SensorProperty::setName(const QString &name)
0069 {
0070     if (d->name == name) {
0071         return;
0072     }
0073 
0074     d->name = name;
0075     d->info.name = d->prefix.isEmpty() ? d->name : d->prefix % QLatin1Char(' ') % d->name;
0076     Q_EMIT sensorInfoChanged();
0077 }
0078 
0079 void SensorProperty::setShortName(const QString &name)
0080 {
0081     if (d->info.shortName == name) {
0082         return;
0083     }
0084 
0085     d->info.shortName = name;
0086     Q_EMIT sensorInfoChanged();
0087 }
0088 
0089 void SensorProperty::setPrefix(const QString &prefix)
0090 {
0091     if (d->prefix == prefix) {
0092         return;
0093     }
0094 
0095     d->prefix = prefix;
0096     d->info.name = prefix.isEmpty() ? d->name : prefix % QLatin1Char(' ') % d->name;
0097     Q_EMIT sensorInfoChanged();
0098 }
0099 
0100 void SensorProperty::setDescription(const QString &description)
0101 {
0102     if (d->info.description == description) {
0103         return;
0104     }
0105 
0106     d->info.description = description;
0107     Q_EMIT sensorInfoChanged();
0108 }
0109 
0110 void SensorProperty::setMin(qreal min)
0111 {
0112     if (qFuzzyCompare(d->info.min, min)) {
0113         return;
0114     }
0115 
0116     d->info.min = min;
0117     Q_EMIT sensorInfoChanged();
0118 }
0119 
0120 void SensorProperty::setMax(qreal max)
0121 {
0122     if (qFuzzyCompare(d->info.max, max)) {
0123         return;
0124     }
0125 
0126     d->info.max = max;
0127     Q_EMIT sensorInfoChanged();
0128 }
0129 
0130 void SensorProperty::setMax(SensorProperty *other)
0131 {
0132     setMax(other->value().toReal());
0133     if (isSubscribed()) {
0134         other->subscribe();
0135     }
0136     connect(this, &SensorProperty::subscribedChanged, this, [this, other](bool isSubscribed) {
0137         if (isSubscribed) {
0138             other->subscribe();
0139             setMax(other->value().toReal());
0140         } else {
0141             other->unsubscribe();
0142         }
0143     });
0144     connect(other, &SensorProperty::valueChanged, this, [this, other]() {
0145         setMax(other->value().toReal());
0146     });
0147 }
0148 
0149 void SensorProperty::setUnit(KSysGuard::Unit unit)
0150 {
0151     if (d->info.unit == unit) {
0152         return;
0153     }
0154 
0155     d->info.unit = unit;
0156     Q_EMIT sensorInfoChanged();
0157 }
0158 
0159 void SensorProperty::setVariantType(QVariant::Type type)
0160 {
0161     if (d->info.variantType == type) {
0162         return;
0163     }
0164 
0165     d->info.variantType = type;
0166     Q_EMIT sensorInfoChanged();
0167 }
0168 
0169 bool SensorProperty::isSubscribed() const
0170 {
0171     return d->subscribers > 0;
0172 }
0173 
0174 void SensorProperty::subscribe()
0175 {
0176     d->subscribers++;
0177     if (d->subscribers == 1) {
0178         Q_EMIT subscribedChanged(true);
0179     }
0180 }
0181 
0182 void SensorProperty::unsubscribe()
0183 {
0184     d->subscribers--;
0185     if (d->subscribers == 0) {
0186         if (d->initialValue.isValid()) {
0187             setValue(d->initialValue);
0188         }
0189         Q_EMIT subscribedChanged(false);
0190     }
0191 }
0192 
0193 QVariant SensorProperty::value() const
0194 {
0195     return d->value;
0196 }
0197 
0198 void SensorProperty::setValue(const QVariant &value)
0199 {
0200     d->value = value;
0201     Q_EMIT valueChanged();
0202 }