Commit 182fbd95 authored by Arjen Hiemstra's avatar Arjen Hiemstra

Separate processcore/{formatter,unit} into its own library

Summary:
This moves Formatter and Unit out of ProcessCore and into its own Formatter library.
This allows us to reuse them in other places, like a new sensor library that will be
posted later.

It includes a declarative plugin that makes things available to QML.

It also contains a small unit test for Formatter.

Test Plan:
- The new unit test passes.
- KSysGuard still runs correctly.

Reviewers: #plasma, davidedmundson

Reviewed By: #plasma, davidedmundson

Subscribers: zzag, ngraham, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D27869
parent 5acaea96
......@@ -25,6 +25,7 @@ include(CMakePackageConfigHelpers)
include(CheckIncludeFiles)
include(CheckLibraryExists)
include(FeatureSummary)
include(GenerateExportHeader)
find_package(Qt5 ${QT_MIN_VERSION} REQUIRED CONFIG COMPONENTS DBus Network Widgets)
find_package(Qt5WebEngineWidgets ${QT_MIN_VERSION} CONFIG)
......@@ -104,6 +105,7 @@ endif()
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
add_subdirectory( formatter )
add_subdirectory( lsofui )
add_subdirectory( processcore )
add_subdirectory( processui )
......
......@@ -32,6 +32,8 @@ ecm_add_test(chronotest.cpp
LINK_LIBRARIES Qt5::Test KF5::I18n
)
ecm_add_test(formattertest.cpp LINK_LIBRARIES Qt5::Test KSysGuard::Formatter)
# set( ksysguarddtest_SRCS ksysguarddtest.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorAgent.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorManager.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorSocketAgent.cpp ${libksysguard_SOURCE_DIR}/ksgrd/SensorShellAgent.cpp)
#
# ecm_add_test(${ksysguarddtest_SRCS}
......
/*
* Copyright 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <QTest>
#include <QLocale>
#include "Formatter.h"
#include "Unit.h"
#define QSL QStringLiteral
class FormatterTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
// Ensure we use a known locale for the test.
QLocale::setDefault(QLocale{QLocale::English, QLocale::UnitedStates});
}
void testDouble_data()
{
QTest::addColumn<double>("input");
QTest::addColumn<KSysGuard::Unit>("unit");
QTest::addColumn<QString>("output");
QTest::newRow("1.0, B") << 1.0 << KSysGuard::UnitByte << QSL("1.0 B");
QTest::newRow("1.0, KiB") << 1.0 << KSysGuard::UnitKiloByte << QSL("1.0 KiB");
QTest::newRow("1.0, KiB/s") << 1.0 << KSysGuard::UnitKiloByteRate << QSL("1.0 KiB/s");
QTest::newRow("1.0, %") << 1.0 << KSysGuard::UnitPercent << QSL("1.0%");
QTest::newRow("0.213, B") << 0.213 << KSysGuard::UnitByte << QString::number(0.2) + QSL(" B");
QTest::newRow("5.647, KiB") << 5.647 << KSysGuard::UnitKiloByte << QString::number(5.6) + QSL(" KiB");
QTest::newRow("99.99, KiB/s") << 99.99 << KSysGuard::UnitKiloByteRate << QString::number(100.0, 'f', 1) + QSL(" KiB/s");
QTest::newRow("0.2567, %") << 0.2567 << KSysGuard::UnitPercent << QString::number(0.3) + QSL("%");
}
void testDouble()
{
QFETCH(double, input);
QFETCH(KSysGuard::Unit, unit);
QFETCH(QString, output);
auto formatted = KSysGuard::Formatter::formatValue(input, unit);
QCOMPARE(formatted, output);
}
};
QTEST_MAIN(FormatterTest);
#include "formattertest.moc"
set(Formatter_SOVERSION 1)
set(formatter_SRCS
Formatter.cpp
Unit.cpp
)
set(formatter_HDRS
Formatter.h
Unit.h
)
add_library(Formatter ${formatter_SRCS})
add_library(KSysGuard::Formatter ALIAS Formatter)
target_link_libraries(Formatter
PUBLIC
Qt5::Core
PRIVATE
KF5::I18n
)
generate_export_header(Formatter)
set_target_properties(Formatter PROPERTIES
LIBRARY_OUTPUT_NAME KSysGuardFormatter
VERSION ${KSYSGUARD_VERSION_STRING}
SOVERSION ${Formatter_SOVERSION}
PUBLIC_HEADER "${formatter_HDRS}"
)
add_subdirectory(declarative)
install(TARGETS Formatter EXPORT libksysguardLibraryTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
install(TARGETS Formatter PUBLIC_HEADER DESTINATION ${KDE_INSTALL_INCLUDEDIR}/ksysguard/formatter)
......@@ -20,7 +20,7 @@
Boston, MA 02110-1301, USA.
*/
#include "formatter.h"
#include "Formatter.h"
#include <KLocalizedString>
......
/*
Copyright (C) 2019 Vlad Zagorodniy <vladzzag@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
// Own
#include "formatter_export.h"
#include "Unit.h"
// Qt
#include <QString>
#include <QVariant>
class KLocalizedString;
namespace KSysGuard
{
/**
* This enum type is used to specify format options.
*/
enum FormatOption {
FormatOptionNone = 0,
FormatOptionAgo = 1 << 0,
FormatOptionShowNull = 1 << 1,
};
Q_DECLARE_FLAGS(FormatOptions, FormatOption)
class FORMATTER_EXPORT Formatter
{
public:
/**
* Returns the scale factor suitable for display.
*
* @param value The maximum output value.
* @param unit The unit of the value.
* @param targetPrefix Preferred metric prefix.
*/
static qreal scaleDownFactor(const QVariant &value, Unit unit,
MetricPrefix targetPrefix = MetricPrefixAutoAdjust);
/**
* Returns localized string that is suitable for display.
*
* @param value The maximum output value.
* @param unit The unit of the value.
* @param targetPrefix Preferred metric prefix.
*/
static KLocalizedString localizedString(const QVariant &value, Unit unit,
MetricPrefix targetPrefix = MetricPrefixAutoAdjust);
/**
* Converts @p value to the appropriate displayable string.
*
* The returned string is localized.
*
* @param value The value to be converted.
* @param unit The unit of the value.
* @param targetPrefix Preferred metric prefix.
* @param options
*/
static QString formatValue(const QVariant &value, Unit unit,
MetricPrefix targetPrefix = MetricPrefixAutoAdjust,
FormatOptions options = FormatOptionNone);
/**
* Returns a symbol that corresponds to the given @p unit.
*
* The returned unit symbol is localized.
*/
static QString symbol(Unit unit);
};
} // namespace KSysGuard
Q_DECLARE_OPERATORS_FOR_FLAGS(KSysGuard::FormatOptions)
#include "Unit.h"
#include "moc_Unit.cpp"
/*
Copyright (C) 2019 Vlad Zagorodniy <vladzzag@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
// Qt
#include <QMetaType>
#include "formatter_export.h"
namespace KSysGuard
{
FORMATTER_EXPORT Q_NAMESPACE
/**
* This enum type is used to specify metric prefixes.
*/
enum MetricPrefix {
MetricPrefixAutoAdjust = -1,
MetricPrefixUnity = 0,
MetricPrefixKilo,
MetricPrefixMega,
MetricPrefixGiga,
MetricPrefixTera,
MetricPrefixPeta,
MetricPrefixLast = MetricPrefixPeta
};
Q_ENUM_NS(MetricPrefix)
/**
* 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,
};
Q_ENUM_NS(Unit)
} // namespace KSysGuard
include_directories(${CMAKE_CURRENT_BINARY_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/..)
add_library(FormatterPlugin SHARED FormatterPlugin.cpp FormatterWrapper.cpp)
target_link_libraries(FormatterPlugin Qt5::Qml KSysGuard::Formatter)
install(TARGETS FormatterPlugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/ksysguard/formatter)
install(FILES qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/ksysguard/formatter)
/*
Copyright (C) 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "FormatterPlugin.h"
#include "Unit.h"
#include "FormatterWrapper.h"
#include <QQmlEngine>
using namespace KSysGuard;
void FormatterPlugin::registerTypes(const char *uri)
{
Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.ksysguard.formatter"));
qRegisterMetaType<KSysGuard::Unit>();
qRegisterMetaType<KSysGuard::MetricPrefix>();
qmlRegisterSingletonType<KSysGuard::FormatterWrapper>(uri, 1, 0, "Formatter", [](QQmlEngine*, QJSEngine*) -> QObject* { return new FormatterWrapper(); });
qmlRegisterUncreatableMetaObject(KSysGuard::staticMetaObject, uri, 1, 0, "Units", QStringLiteral("Contains unit enums"));
}
/*
Copyright (C) 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include <QQmlExtensionPlugin>
class FormatterPlugin : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public:
void registerTypes(const char *uri) override;
};
/*
Copyright (C) 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "FormatterWrapper.h"
#include "Formatter.h"
namespace KSysGuard {
QString FormatterWrapper::formatValue(const QVariant &value, KSysGuard::Unit unit, KSysGuard::MetricPrefix targetPrefix)
{
return Formatter::formatValue(value, unit, targetPrefix);
}
QString FormatterWrapper::formatValueShowNull(const QVariant &value, KSysGuard::Unit unit, KSysGuard::MetricPrefix targetPrefix)
{
return Formatter::formatValue(value, unit, targetPrefix, FormatOptionShowNull);
}
}
/*
Copyright (C) 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#pragma once
#include <QObject>
#include "Unit.h"
namespace KSysGuard {
/**
* Tiny helper class to make Formatter usable from QML.
*
* An instance of this class will be exposed as a Singleton object to QML. It
* allows formatting of values from the QML side.
*
* This effectively wraps Formatter::formatValue, removing the FormatOptions flag
* that I couldn't get to work.
*/
class FormatterWrapper : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QString formatValue(const QVariant &value, KSysGuard::Unit unit,
KSysGuard::MetricPrefix targetPrefix = MetricPrefixAutoAdjust
);
Q_INVOKABLE QString formatValueShowNull(const QVariant &value, KSysGuard::Unit unit,
KSysGuard::MetricPrefix targetPrefix = MetricPrefixAutoAdjust
);
};
}
module org.kde.ksysguard.formatter
plugin FormatterPlugin
......@@ -4,7 +4,6 @@ add_definitions(-DTRANSLATION_DOMAIN=\"processcore\")
set(ksysguard_LIB_SRCS
extended_process_list.cpp
formatter.cpp
processes.cpp
process.cpp
process_attribute.cpp
......@@ -14,7 +13,6 @@ set(ksysguard_LIB_SRCS
processes_atop_p.cpp
process_controller.cpp
process_data_provider.cpp
unit.cpp
)
ecm_qt_declare_logging_category(ksysguard_LIB_SRCS HEADER processcore_debug.h IDENTIFIER LIBKSYSGUARD_PROCESSCORE CATEGORY_NAME org.kde.libksysguard.processcore)
......@@ -24,6 +22,7 @@ add_library(KF5::ProcessCore ALIAS processcore)
target_link_libraries(processcore
PUBLIC
Qt5::Core
KSysGuard::Formatter
PRIVATE
KF5::I18n
KF5::AuthCore
......
......@@ -17,75 +17,4 @@
Boston, MA 02110-1301, USA.
*/
#pragma once
// Own
#include "unit.h"
// Qt
#include <QString>
#include <QVariant>
class KLocalizedString;
namespace KSysGuard
{
/**
* This enum type is used to specify format options.
*/
enum FormatOption {
FormatOptionNone = 0,
FormatOptionAgo = 1 << 0,
FormatOptionShowNull = 1 << 1,
};
Q_DECLARE_FLAGS(FormatOptions, FormatOption)
class Q_DECL_EXPORT Formatter
{
public:
/**
* Returns the scale factor suitable for display.
*
* @param value The maximum output value.
* @param unit The unit of the value.
* @param targetPrefix Preferred metric prefix.
*/
static qreal scaleDownFactor(const QVariant &value, Unit unit,
MetricPrefix targetPrefix = MetricPrefixAutoAdjust);
/**
* Returns localized string that is suitable for display.
*
* @param value The maximum output value.
* @param unit The unit of the value.
* @param targetPrefix Preferred metric prefix.
*/
static KLocalizedString localizedString(const QVariant &value, Unit unit,
MetricPrefix targetPrefix = MetricPrefixAutoAdjust);
/**
* Converts @p value to the appropriate displayable string.
*
* The returned string is localized.
*
* @param value The value to be converted.
* @param unit The unit of the value.
* @param targetPrefix Preferred metric prefix.
* @param options
*/
static QString formatValue(const QVariant &value, Unit unit,
MetricPrefix targetPrefix = MetricPrefixAutoAdjust,
FormatOptions options = FormatOptionNone);
/**
* Returns a symbol that corresponds to the given @p unit.
*
* The returned unit symbol is localized.
*/
static QString symbol(Unit unit);
};
} // namespace KSysGuard
Q_DECLARE_OPERATORS_FOR_FLAGS(KSysGuard::FormatOptions)
#include "formatter/Formatter.h"
#include "unit.h"
#include "moc_unit.cpp"
......@@ -17,75 +17,4 @@
Boston, MA 02110-1301, USA.
*/
#pragma once
// Qt
#include <QMetaType>
namespace KSysGuard
{
Q_DECL_EXPORT Q_NAMESPACE
/**
* This enum type is used to specify metric prefixes.
*/
enum MetricPrefix {
MetricPrefixAutoAdjust = -1,
MetricPrefixUnity = 0,
MetricPrefixKilo,
MetricPrefixMega,
MetricPrefixGiga,
MetricPrefixTera,
MetricPrefixPeta,
MetricPrefixLast = MetricPrefixPeta
};
Q_ENUM_NS(MetricPrefix)
/**
* 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,
};
Q_ENUM_NS(Unit)
} // namespace KSysGuard
#include "formatter/Unit.h"
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import org.kde.ksysguard.formatter 1.0 as Formatter
Pane {
width: 400
height: 400