Commit 757cedae authored by David Redondo's avatar David Redondo 🏎
Browse files

Use Formatter Units instead of having a copy of the enum

parent 0f75a31d
......@@ -7,6 +7,6 @@ set_source_files_properties("${CMAKE_CURRENT_SOURCE_DIR}/../ksysguard_iface.xml"
qt5_add_dbus_interface(SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/../ksysguard_iface.xml" kstatsiface)
add_executable(kstatsviewer ${SOURCES})
target_link_libraries(kstatsviewer Qt5::Core Qt5::DBus)
target_link_libraries(kstatsviewer Qt5::Core Qt5::DBus KSysGuard::Formatter)
install(TARGETS kstatsviewer DESTINATION ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
......@@ -29,9 +29,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <iostream>
#include "kstatsiface.h"
#include <ksysguard/formatter/Formatter.h>
QString unitToString(KSysGuard::utils::Unit unit);
#include "kstatsiface.h"
class SensorWatcher : public QCoreApplication
{
......@@ -155,77 +156,10 @@ void SensorWatcher::showSensorDetails(const SensorInfoMap &sensors)
std::cout << " Name: " << qPrintable(info.name) << "\n";
std::cout << " Short Name: " << qPrintable(info.shortName) << "\n";
std::cout << " Description: " << qPrintable(info.description) << "\n";
std::cout << " Unit: " << qPrintable(unitToString(info.unit)) << "\n";
std::cout << " Unit: " << qPrintable(KSysGuard::Formatter::symbol(info.unit)) << "\n";
std::cout << " Minimum: " << info.min << "\n";
std::cout << " Maximum: " << info.max << "\n";
}
}
QString unitToString(KSysGuard::utils::Unit unit)
{
using namespace KSysGuard::utils;
switch (unit) {
case UnitByte:
return QStringLiteral("B");
case UnitKiloByte:
return QStringLiteral("KiB");
case UnitMegaByte:
return QStringLiteral("MiB");
case UnitGigaByte:
return QStringLiteral("GiB");
case UnitTeraByte:
return QStringLiteral("TiB");
case UnitPetaByte:
return QStringLiteral("PiB");
case UnitByteRate:
return QStringLiteral("B/s");
case UnitKiloByteRate:
return QStringLiteral("KiB/s");
case UnitMegaByteRate:
return QStringLiteral("MiB/s");
case UnitGigaByteRate:
return QStringLiteral("GiB/s");
case UnitTeraByteRate:
return QStringLiteral("TiB/s");
case UnitPetaByteRate:
return QStringLiteral("PiB/s");
case UnitHertz:
return QStringLiteral("Hz");
case UnitKiloHertz:
return QStringLiteral("kHz");
case UnitMegaHertz:
return QStringLiteral("MHz");
case UnitGigaHertz:
return QStringLiteral("GHz");
case UnitTeraHertz:
return QStringLiteral("THz");
case UnitPetaHertz:
return QStringLiteral("PHz");
case UnitPercent:
return QStringLiteral("%");
case UnitRpm:
return QStringLiteral("RPM");
case UnitCelsius:
return QStringLiteral("°C");
case UnitDecibelMilliWatts:
return QStringLiteral("dBm");
case UnitSecond:
return QStringLiteral("s");
case UnitVolt:
return QStringLiteral("V");
case UnitWatt:
return QStringLiteral("W");
case UnitRate:
return QStringLiteral("s⁻¹");
case UnitInvalid:
return QStringLiteral("Invalid");
case UnitNone:
return QStringLiteral("None");
default:
return QString();
}
}
#include "main.moc"
......@@ -210,7 +210,7 @@ void AggregateSensor::delayedEmitDataChanged()
PercentageSensor::PercentageSensor(SensorObject *provider, const QString &id, const QString &name)
: SensorProperty(id, name, provider)
{
setUnit(KSysGuard::utils::UnitPercent);
setUnit(KSysGuard::UnitPercent);
setMax(100);
}
......
......@@ -9,7 +9,7 @@ set(ksgrdbackend_LIB_SRCS
add_library(ksgrdbackend ${ksgrdbackend_LIB_SRCS})
add_library(KSysGuard::StatsBackend ALIAS ksgrdbackend)
target_link_libraries(ksgrdbackend PUBLIC Qt5::Core Qt5::DBus)
target_link_libraries(ksgrdbackend PUBLIC Qt5::Core Qt5::DBus KSysGuard::Formatter)
set_target_properties(ksgrdbackend PROPERTIES
EXPORT_NAME SysGuardBackend
......
......@@ -142,7 +142,7 @@ void SensorProperty::setMax(SensorProperty *other)
});
}
void SensorProperty::setUnit(KSysGuard::utils::Unit unit)
void SensorProperty::setUnit(KSysGuard::Unit unit)
{
if (m_info.unit == unit) {
return;
......
......@@ -74,7 +74,7 @@ public:
* For example to mark the usedSpace of a disk to be the same as the disk capacity
*/
void setMax(SensorProperty *other);
void setUnit(KSysGuard::utils::Unit unit);
void setUnit(KSysGuard::Unit unit);
void setVariantType(QVariant::Type type);
bool isSubscribed() const;
......
......@@ -25,72 +25,7 @@
#include <QDBusArgument>
namespace KSysGuard
{
namespace utils
{
/**
* This enum type is used to specify metric prefixes.
*/
enum MetricPrefix {
MetricPrefixAutoAdjust = -1,
MetricPrefixUnity = 0,
MetricPrefixKilo,
MetricPrefixMega,
MetricPrefixGiga,
MetricPrefixTera,
MetricPrefixPeta,
MetricPrefixLast = MetricPrefixPeta
};
/**
* This enum types is used to specify units.
*/
enum Unit {
UnitInvalid = -1,
UnitNone = 0,
// Byte size units.
UnitByte = 100,
UnitKiloByte = MetricPrefixKilo + UnitByte,
UnitMegaByte = MetricPrefixMega + UnitByte,
UnitGigaByte = MetricPrefixGiga + UnitByte,
UnitTeraByte = MetricPrefixTera + UnitByte,
UnitPetaByte = MetricPrefixPeta + UnitByte,
// Data rate units.
UnitByteRate = 200,
UnitKiloByteRate = MetricPrefixKilo + UnitByteRate,
UnitMegaByteRate = MetricPrefixMega + UnitByteRate,
UnitGigaByteRate = MetricPrefixGiga + UnitByteRate,
UnitTeraByteRate = MetricPrefixTera + UnitByteRate,
UnitPetaByteRate = MetricPrefixPeta + UnitByteRate,
// Frequency.
UnitHertz = 300,
UnitKiloHertz = MetricPrefixKilo + UnitHertz,
UnitMegaHertz = MetricPrefixMega + UnitHertz,
UnitGigaHertz = MetricPrefixGiga + UnitHertz,
UnitTeraHertz = MetricPrefixTera + UnitHertz,
UnitPetaHertz = MetricPrefixPeta + UnitHertz,
// Time units.
UnitBootTimestamp = 400,
UnitSecond,
UnitTime,
// Misc units.
UnitCelsius = 500,
UnitDecibelMilliWatts,
UnitPercent,
UnitRate,
UnitRpm,
UnitVolt,
UnitWatt,
};
}
}
#include <ksysguard/formatter/Unit.h>
//Data that is static for the lifespan of the sensor
class SensorInfo
......@@ -101,7 +36,7 @@ public:
QString shortName;
QString description; // translated
QVariant::Type variantType = QVariant::Invalid;
KSysGuard::utils::Unit unit = KSysGuard::utils::UnitInvalid; //Both a format hint and implies data type (i.e double/string)
KSysGuard::Unit unit = KSysGuard::UnitInvalid; //Both a format hint and implies data type (i.e double/string)
qreal min = 0;
qreal max = 0;
};
......@@ -151,7 +86,7 @@ inline const QDBusArgument &operator>>(const QDBusArgument &argument, SensorInfo
argument >> t;
s.variantType = static_cast<QVariant::Type>(t);
argument >> t;
s.unit = static_cast<KSysGuard::utils::Unit>(t);
s.unit = static_cast<KSysGuard::Unit>(t);
argument >> s.min;
argument >> s.max;
argument.endStructure();
......
......@@ -246,21 +246,21 @@ void KSGRDIface::onSensorUpdated(int id, const QList<QByteArray> &answer)
}
}
KSysGuard::utils::Unit KSGRDIface::unitFromString(const QString &unitString) const
KSysGuard::Unit KSGRDIface::unitFromString(const QString &unitString) const
{
using namespace KSysGuard;
static const QHash<QString, utils::Unit> map({ { "%", utils::UnitPercent },
{ "1/s", utils::UnitRate },
{ "°C", utils::UnitCelsius },
{ "dBm", utils::UnitDecibelMilliWatts },
{ "KB", utils::UnitKiloByte },
{ "KB/s", utils::UnitKiloByteRate },
{ "MHz", utils::UnitMegaHertz },
{ "port", utils::UnitNone },
{ "rpm", utils::UnitRpm },
{ "s", utils::UnitTime },
{ "V", utils::UnitVolt } });
return map.value(unitString, utils::UnitNone);
static const QHash<QString, Unit> map({ { "%", UnitPercent },
{ "1/s", UnitRate },
{ "°C", UnitCelsius },
{ "dBm", UnitDecibelMilliWatts },
{ "KB", UnitKiloByte },
{ "KB/s", UnitKiloByteRate },
{ "MHz", UnitMegaHertz },
{ "port", UnitNone },
{ "rpm", UnitRpm },
{ "s", UnitTime },
{ "V", UnitVolt } });
return map.value(unitString, UnitNone);
}
void KSGRDIface::update()
......
......@@ -59,7 +59,7 @@ private:
void subscribe(const QString &sensorPath);
void unsubscribe(const QString &sensorPath);
KSysGuard::utils::Unit unitFromString(const QString &unitString) const;
KSysGuard::Unit unitFromString(const QString &unitString) const;
void addAggregateSensors();
QString shortNameFor(const QString &key);
......
......@@ -17,21 +17,21 @@ AllDevicesObject::AllDevicesObject(SensorContainer *parent)
{
m_downloadSensor = new AggregateSensor(this, QStringLiteral("download"), i18nc("@title", "Download Rate"));
m_downloadSensor->setShortName(i18nc("@title Short for Download Rate", "Download"));
m_downloadSensor->setUnit(KSysGuard::utils::UnitByteRate);
m_downloadSensor->setUnit(KSysGuard::UnitByteRate);
m_downloadSensor->setMatchSensors(QRegularExpression{"^(?!all).*$"}, QStringLiteral("download"));
m_uploadSensor = new AggregateSensor(this, QStringLiteral("upload"), i18nc("@title", "Upload Rate"));
m_uploadSensor->setShortName(i18nc("@title Short for Upload Rate", "Upload"));
m_uploadSensor->setUnit(KSysGuard::utils::UnitByteRate);
m_uploadSensor->setUnit(KSysGuard::UnitByteRate);
m_uploadSensor->setMatchSensors(QRegularExpression{"^(?!all).*$"}, QStringLiteral("upload"));
m_totalDownloadSensor = new AggregateSensor(this, QStringLiteral("totalDownload"), i18nc("@title", "Total Downloaded"));
m_totalDownloadSensor->setShortName(i18nc("@title Short for Total Downloaded", "Downloaded"));
m_totalDownloadSensor->setUnit(KSysGuard::utils::UnitByte);
m_totalDownloadSensor->setUnit(KSysGuard::UnitByte);
m_totalDownloadSensor->setMatchSensors(QRegularExpression{"^(?!all).*$"}, QStringLiteral("totalDownload"));
m_totalUploadSensor = new AggregateSensor(this, QStringLiteral("totalUpload"), i18nc("@title", "Total Uploaded"));
m_totalUploadSensor->setShortName(i18nc("@title Short for Total Uploaded", "Uploaded"));
m_totalUploadSensor->setUnit(KSysGuard::utils::UnitByte);
m_totalUploadSensor->setUnit(KSysGuard::UnitByte);
m_totalUploadSensor->setMatchSensors(QRegularExpression{"^(?!all).*$"}, QStringLiteral("upload"));
}
......@@ -16,7 +16,7 @@ NetworkDevice::NetworkDevice(const QString &id, const QString &name)
m_signalSensor = new SensorProperty(QStringLiteral("signal"), i18nc("@title", "Signal Strength"), this);
m_signalSensor->setShortName(i18nc("@title Short of Signal Strength", "Signal"));
m_signalSensor->setUnit(KSysGuard::utils::UnitPercent);
m_signalSensor->setUnit(KSysGuard::UnitPercent);
m_signalSensor->setMin(0);
m_signalSensor->setMax(100);
......@@ -28,17 +28,17 @@ NetworkDevice::NetworkDevice(const QString &id, const QString &name)
m_downloadSensor = new SensorProperty(QStringLiteral("download"), i18nc("@title", "Download Rate"), this);
m_downloadSensor->setShortName(i18nc("@title Short for Download Rate", "Download"));
m_downloadSensor->setUnit(KSysGuard::utils::UnitByteRate);
m_downloadSensor->setUnit(KSysGuard::UnitByteRate);
m_uploadSensor = new SensorProperty(QStringLiteral("upload"), i18nc("@title", "Upload Rate"), this);
m_uploadSensor->setShortName(i18nc("@title Short for Upload Rate", "Upload"));
m_uploadSensor->setUnit(KSysGuard::utils::UnitByteRate);
m_uploadSensor->setUnit(KSysGuard::UnitByteRate);
m_totalDownloadSensor = new SensorProperty(QStringLiteral("totalDownload"), i18nc("@title", "Total Downloaded"), this);
m_totalDownloadSensor->setShortName(i18nc("@title Short for Total Downloaded", "Downloaded"));
m_totalDownloadSensor->setUnit(KSysGuard::utils::UnitByte);
m_totalDownloadSensor->setUnit(KSysGuard::UnitByte);
m_totalUploadSensor = new SensorProperty(QStringLiteral("totalUpload"), i18nc("@title", "Total Uploaded"), this);
m_totalUploadSensor->setShortName(i18nc("@title Short for Total Uploaded", "Uploaded"));
m_totalUploadSensor->setUnit(KSysGuard::utils::UnitByte);
m_totalUploadSensor->setUnit(KSysGuard::UnitByte);
}
......@@ -62,49 +62,49 @@ GPU::GPU(int index, SensorContainer *parent)
m_pwr = new SensorProperty(QStringLiteral("power"), this);
m_pwr->setName(i18nc("@title", "GPU %1 Power Usage", displayIndex));
m_pwr->setShortName(i18nc("@title GPU Power Usage", "Power"));
m_pwr->setUnit(KSysGuard::utils::UnitWatt);
m_pwr->setUnit(KSysGuard::UnitWatt);
m_pwr->setVariantType(QVariant::UInt);
m_temp = new SensorProperty(QStringLiteral("temperature"), this);
m_temp->setName(i18nc("@title", "GPU %1 Temperature", displayIndex));
m_temp->setShortName(i18nc("@title GPU Temperature", "Temperature"));
m_temp->setUnit(KSysGuard::utils::UnitCelsius);
m_temp->setUnit(KSysGuard::UnitCelsius);
m_temp->setVariantType(QVariant::Double);
m_sm = new SensorProperty(QStringLiteral("sharedMemory"), this);
m_sm->setName(i18nc("@title", "GPU %1 Shared Memory Usage", displayIndex));
m_sm->setShortName(i18nc("@title GPU Shared Memory Usage", "Shared Memory"));
m_sm->setUnit(KSysGuard::utils::UnitPercent);
m_sm->setUnit(KSysGuard::UnitPercent);
m_sm->setVariantType(QVariant::UInt);
m_mem = new SensorProperty(QStringLiteral("memory"), this);
m_mem->setName(i18nc("@title", "GPU %1 Memory Usage", displayIndex));
m_mem->setShortName(i18nc("@title GPU Memory Usage", "Memory"));
m_mem->setUnit(KSysGuard::utils::UnitPercent);
m_mem->setUnit(KSysGuard::UnitPercent);
m_mem->setVariantType(QVariant::UInt);
m_enc = new SensorProperty(QStringLiteral("encoderUsage"), this);
m_enc->setName(i18nc("@title", "GPU %1 Encoder Usage", displayIndex));
m_enc->setShortName(i18nc("@title GPU Encoder Usage", "Encoder"));
m_enc->setUnit(KSysGuard::utils::UnitPercent);
m_enc->setUnit(KSysGuard::UnitPercent);
m_enc->setVariantType(QVariant::UInt);
m_dec = new SensorProperty(QStringLiteral("decoderUsage"), this);
m_dec->setName(i18nc("@title", "GPU %1 Decoder Usage", displayIndex));
m_dec->setShortName(i18nc("@title GPU Decoder Usage", "Decoder"));
m_dec->setUnit(KSysGuard::utils::UnitPercent);
m_dec->setUnit(KSysGuard::UnitPercent);
m_dec->setVariantType(QVariant::UInt);
m_memClock = new SensorProperty(QStringLiteral("memoryClock"), this);
m_memClock->setName(i18nc("@title", "GPU %1 Memory Clock", displayIndex));
m_memClock->setName(i18nc("@title GPU Memory Clock", "Memory Clock"));
m_memClock->setUnit(KSysGuard::utils::UnitMegaHertz);
m_memClock->setUnit(KSysGuard::UnitMegaHertz);
m_memClock->setVariantType(QVariant::UInt);
m_processorClock = new SensorProperty(QStringLiteral("processorClock"), this);
m_processorClock->setName(i18nc("@title", "GPU %1 Processor Clock", displayIndex));
m_processorClock->setName(i18nc("@title GPU Processor Clock", "Processor Clock"));
m_processorClock->setUnit(KSysGuard::utils::UnitMegaHertz);
m_processorClock->setUnit(KSysGuard::UnitMegaHertz);
m_processorClock->setVariantType(QVariant::UInt);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment