diff --git a/CMakeLists.txt b/CMakeLists.txt index 9240eee..10f81fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,142 +1,144 @@ cmake_minimum_required(VERSION 3.0) project(libksysguard) set(PROJECT_VERSION "5.18.80") set(PROJECT_VERSION_MAJOR 5) # check with non-Plasma consumers (e.g. KDevelop) before bumping these versions set(QT_MIN_VERSION "5.12.0") set(KF5_MIN_VERSION "5.66.0") find_package(ECM ${KF5_MIN_VERSION} REQUIRED NO_MODULE) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) include(KDEInstallDirs) include(KDECMakeSettings) include(KDECompilerSettings NO_POLICY_SCOPE) include(KDEClangFormat) include(ECMAddTests) include(ECMInstallIcons) include(ECMSetupVersion) include(ECMQtDeclareLoggingCategory) 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) set_package_properties(Qt5WebEngineWidgets PROPERTIES URL "git://code.qt.org/qt/qtwebenginewidgets.git" DESCRIPTION "Qt WebEngine module (web browsing engine)" TYPE OPTIONAL PURPOSE "Used by the HTML-based GUI ksysguard library" ) find_package(Qt5WebChannel ${QT_MIN_VERSION} CONFIG) set_package_properties(Qt5WebChannel PROPERTIES URL "git://code.qt.org/qt/qtwebchannel.git" DESCRIPTION "Qt WebChannel module" TYPE OPTIONAL PURPOSE "Used by the HTML-based GUI ksysguard library" ) find_package(KF5 REQUIRED COMPONENTS CoreAddons Config I18n WindowSystem Completion Auth WidgetsAddons IconThemes ConfigWidgets Service GlobalAccel KIO) find_package(KF5 OPTIONAL_COMPONENTS Plasma) set_package_properties(KF5Plasma PROPERTIES URL "https://cgit.kde.org/plasma-framework.git/" DESCRIPTION "The library of the plasma project" TYPE OPTIONAL PURPOSE "Used by signalplotter to use Plasma themes" ) find_package(ZLIB REQUIRED) set_package_properties(ZLIB PROPERTIES DESCRIPTION "Support for gzip compressed files and data streams" URL "http://www.zlib.net" TYPE REQUIRED ) check_library_exists(c clock_gettime "time.h" HAVE_CLOCK_GETTIME_C) ecm_setup_version(${PROJECT_VERSION} VARIABLE_PREFIX KSYSGUARD VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/ksysguard_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KF5SysGuardConfigVersion.cmake" SOVERSION 8 ) find_package(X11) set_package_properties(X11 PROPERTIES DESCRIPTION "X11 libraries" URL "https://www.x.org" TYPE OPTIONAL PURPOSE "Required for building the X11 based workspace" ) if(X11_FOUND) find_package(Qt5X11Extras REQUIRED) find_library(X11_XRes_LIB XRes ${X11_LIB_SEARCH_PATH}) find_path(X11_XRes_INCLUDE_PATH X11/extensions/XRes.h ${X11_INC_SEARCH_PATH}) if(X11_XRes_LIB AND X11_XRes_INCLUDE_PATH) set(X11_XRes_FOUND TRUE) endif() endif() set(WEBENGINE_SCRIPTING_ENABLED FALSE) if(Qt5WebEngineWidgets_FOUND AND Qt5WebChannel_FOUND) set(WEBENGINE_SCRIPTING_ENABLED TRUE) endif() add_feature_info("Scripting plugin support" WEBENGINE_SCRIPTING_ENABLED "Support scripting plugins using WebEngine and WebChannel") set(HAVE_X11 ${X11_FOUND}) set(HAVE_XRES ${X11_XRes_FOUND}) configure_file(config-ksysguard.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-ksysguard.h ) add_definitions(-DQT_NO_URL_CAST_FROM_STRING) add_definitions(-DQT_USE_QSTRINGBUILDER) add_definitions(-DQT_NO_CAST_FROM_ASCII) add_definitions(-DQT_NO_CAST_TO_ASCII) add_definitions(-DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT) if (EXISTS "${CMAKE_SOURCE_DIR}/.git") add_definitions(-DKF_DISABLE_DEPRECATED_BEFORE_AND_AT=0x054200) endif() set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) +add_subdirectory( formatter ) add_subdirectory( lsofui ) add_subdirectory( processcore ) add_subdirectory( processui ) if (KF5Plasma_FOUND) add_subdirectory( signalplotter ) endif() add_subdirectory( ksgrd ) if(BUILD_TESTING) add_subdirectory( autotests ) endif() # add clang-format target for all our real source files file(GLOB_RECURSE ALL_CLANG_FORMAT_SOURCE_FILES *.cpp *.h) kde_clang_format(${ALL_CLANG_FORMAT_SOURCE_FILES}) install(DIRECTORY scripts/ DESTINATION ${KDE_INSTALL_DATADIR}/ksysguard/scripts) set(CMAKECONFIG_INSTALL_DIR ${KDE_INSTALL_LIBDIR}/cmake/KF5SysGuard) configure_package_config_file(KF5SysGuardConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/KF5SysGuardConfig.cmake" INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/KF5SysGuardConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/KF5SysGuardConfigVersion.cmake DESTINATION ${CMAKECONFIG_INSTALL_DIR}) install(EXPORT libksysguardLibraryTargets NAMESPACE KF5:: DESTINATION ${CMAKECONFIG_INSTALL_DIR} FILE KF5SysGuardLibraryTargets.cmake ) install(FILES libksysguard.categories DESTINATION ${KDE_INSTALL_LOGGINGCATEGORIESDIR}) feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index f5b6c09..101df36 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,46 +1,48 @@ find_package(Qt5 REQUIRED CONFIG COMPONENTS Test) include_directories(${libksysguard_SOURCE_DIR}) if(Qt5WebEngineWidgets_FOUND) # Process unit test ecm_qt_declare_logging_category(processtest_debug_SRCS HEADER processcore_debug.h IDENTIFIER LIBKSYSGUARD_PROCESSCORE CATEGORY_NAME org.kde.libksysguard.processcore) ecm_add_test(processtest.cpp ${processtest_debug_SRCS} TEST_NAME processtest LINK_LIBRARIES KF5::ProcessUi Qt5::Test) endif() if (KF5Plasma_FOUND) set(SIGNALPLOTTER_DEBUG_SRCS) ecm_qt_declare_logging_category(SIGNALPLOTTER_DEBUG_SRCS HEADER ksignalplotter_debug.h IDENTIFIER LIBKSYSGUARD_KSIGNALPLOTTER CATEGORY_NAME org.kde.libksysguard.ksignalplotter) ecm_add_test(signalplotterbenchmark.cpp ../signalplotter/ksignalplotter.cpp ${SIGNALPLOTTER_DEBUG_SRCS} TEST_NAME signalplotterbenchmark LINK_LIBRARIES KF5::SignalPlotter Qt5::Test Qt5::Widgets KF5::IconThemes ) ecm_add_test(graphicssignalplotterbenchmark.cpp ../signalplotter/kgraphicssignalplotter.cpp ${SIGNALPLOTTER_DEBUG_SRCS} TEST_NAME graphicssignalplotterbenchmark LINK_LIBRARIES KF5::SignalPlotter Qt5::Test Qt5::Widgets KF5::IconThemes KF5::Plasma ) ecm_add_test(signalplottertest.cpp ../signalplotter/ksignalplotter.cpp ${SIGNALPLOTTER_DEBUG_SRCS} TEST_NAME signalplottertest LINK_LIBRARIES KF5::SignalPlotter Qt5::Test Qt5::Widgets KF5::IconThemes ) endif() ecm_add_test(chronotest.cpp TEST_NAME chronotest 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} # TEST_NAME "ksysguard-ksysguarddtest" # LINK_LIBRARIES # Qt5::Test # Qt5::Network # Qt5::Widgets # KF5::ConfigCore # KF5::CoreAddons # KF5::I18n # ) diff --git a/autotests/formattertest.cpp b/autotests/formattertest.cpp new file mode 100644 index 0000000..4fff1ea --- /dev/null +++ b/autotests/formattertest.cpp @@ -0,0 +1,69 @@ +/* + * Copyright 2020 Arjen Hiemstra + * + * 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 . + */ + +#include +#include + +#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("input"); + QTest::addColumn("unit"); + QTest::addColumn("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" diff --git a/formatter/CMakeLists.txt b/formatter/CMakeLists.txt new file mode 100644 index 0000000..8f6a77b --- /dev/null +++ b/formatter/CMakeLists.txt @@ -0,0 +1,36 @@ + +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) diff --git a/processcore/formatter.cpp b/formatter/Formatter.cpp similarity index 99% rename from processcore/formatter.cpp rename to formatter/Formatter.cpp index 4138498..83e04da 100644 --- a/processcore/formatter.cpp +++ b/formatter/Formatter.cpp @@ -1,435 +1,435 @@ /* Copyright (C) 2019 Vlad Zagorodniy formatBootTimestamp is based on TimeUtil class: Copyright (C) 2014 Gregor Mi 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 "formatter.h" +#include "Formatter.h" #include #include #include #include #ifdef Q_OS_OSX #include #include #else #include #endif #include namespace KSysGuard { // TODO: Is there a bit nicer way to handle formatting? static KLocalizedString unitFormat(Unit unit) { const static KLocalizedString B = ki18nc("Bytes unit symbol", "%1 B"); const static KLocalizedString KiB = ki18nc("Kilobytes unit symbol", "%1 KiB"); const static KLocalizedString MiB = ki18nc("Megabytes unit symbol", "%1 MiB"); const static KLocalizedString GiB = ki18nc("Gigabytes unit symbol", "%1 GiB"); const static KLocalizedString TiB = ki18nc("Terabytes unit symbol", "%1 TiB"); const static KLocalizedString PiB = ki18nc("Petabytes unit symbol", "%1 PiB"); const static KLocalizedString bps = ki18nc("Bytes per second unit symbol", "%1 B/s"); const static KLocalizedString Kbps = ki18nc("Kilobytes per second unit symbol", "%1 KiB/s"); const static KLocalizedString Mbps = ki18nc("Megabytes per second unit symbol", "%1 MiB/s"); const static KLocalizedString Gbps = ki18nc("Gigabytes per second unit symbol", "%1 GiB/s"); const static KLocalizedString Tbps = ki18nc("Gigabytes per second unit symbol", "%1 TiB/s"); const static KLocalizedString Pbps = ki18nc("Gigabytes per second unit symbol", "%1 PiB/s"); const static KLocalizedString Hz = ki18nc("Hertz unit symbol", "%1 Hz"); const static KLocalizedString kHz = ki18nc("Kilohertz unit symbol", "%1 kHz"); const static KLocalizedString MHz = ki18nc("Megahertz unit symbol", "%1 MHz"); const static KLocalizedString GHz = ki18nc("Gigahertz unit symbol", "%1 GHz"); const static KLocalizedString THz = ki18nc("Terahertz unit symbol", "%1 THz"); const static KLocalizedString PHz = ki18nc("Petahertz unit symbol", "%1 PHz"); const static KLocalizedString percent = ki18nc("Percent unit", "%1%"); const static KLocalizedString RPM = ki18nc("Revolutions per minute unit symbol", "%1 RPM"); const static KLocalizedString C = ki18nc("Celsius unit symbol", "%1°C"); const static KLocalizedString dBm = ki18nc("Decibels unit symbol", "%1 dBm"); const static KLocalizedString s = ki18nc("Seconds unit symbol", "%1s"); const static KLocalizedString V = ki18nc("Volts unit symbol", "%1 V"); const static KLocalizedString W = ki18nc("Watts unit symbol", "%1 W"); const static KLocalizedString rate = ki18nc("Rate unit symbol", "%1 s⁻¹"); const static KLocalizedString unitless = ki18nc("Unitless", "%1"); switch (unit) { case UnitByte: return B; case UnitKiloByte: return KiB; case UnitMegaByte: return MiB; case UnitGigaByte: return GiB; case UnitTeraByte: return TiB; case UnitPetaByte: return PiB; case UnitByteRate: return bps; case UnitKiloByteRate: return Kbps; case UnitMegaByteRate: return Mbps; case UnitGigaByteRate: return Gbps; case UnitTeraByteRate: return Tbps; case UnitPetaByteRate: return Pbps; case UnitHertz: return Hz; case UnitKiloHertz: return kHz; case UnitMegaHertz: return MHz; case UnitGigaHertz: return GHz; case UnitTeraHertz: return THz; case UnitPetaHertz: return PHz; case UnitCelsius: return C; case UnitDecibelMilliWatts: return dBm; case UnitPercent: return percent; case UnitRate: return rate; case UnitRpm: return RPM; case UnitSecond: return s; case UnitVolt: return V; case UnitWatt: return W; default: return unitless; } } static int unitOrder(Unit unit) { switch (unit) { case UnitByte: case UnitKiloByte: case UnitMegaByte: case UnitGigaByte: case UnitTeraByte: case UnitPetaByte: case UnitByteRate: case UnitKiloByteRate: case UnitMegaByteRate: case UnitGigaByteRate: case UnitTeraByteRate: case UnitPetaByteRate: return 1024; case UnitHertz: case UnitKiloHertz: case UnitMegaHertz: case UnitGigaHertz: case UnitTeraHertz: case UnitPetaHertz: return 1000; default: return 0; } } static Unit unitBase(Unit unit) { switch (unit) { case UnitByte: case UnitKiloByte: case UnitMegaByte: case UnitGigaByte: case UnitTeraByte: case UnitPetaByte: return UnitByte; case UnitByteRate: case UnitKiloByteRate: case UnitMegaByteRate: case UnitGigaByteRate: case UnitTeraByteRate: case UnitPetaByteRate: return UnitByteRate; case UnitHertz: case UnitKiloHertz: case UnitMegaHertz: case UnitGigaHertz: case UnitTeraHertz: case UnitPetaHertz: return UnitHertz; default: return unit; } } static Unit adjustedUnit(qreal value, Unit unit, MetricPrefix prefix) { const int order = unitOrder(unit); if (!order) { return unit; } const Unit baseUnit = unitBase(unit); const MetricPrefix basePrefix = MetricPrefix(unit - baseUnit); if (prefix == MetricPrefixAutoAdjust) { const qreal absoluteValue = value * std::pow(order, int(basePrefix)); if (absoluteValue > 0) { const int targetPrefix = std::log2(absoluteValue) / std::log2(order); if (targetPrefix <= MetricPrefixLast) { prefix = MetricPrefix(targetPrefix); } } if (prefix == MetricPrefixAutoAdjust) { prefix = basePrefix; } } return Unit(prefix + baseUnit); } static QString formatNumber(const QVariant &value, Unit unit, MetricPrefix prefix, FormatOptions options) { qreal amount = value.toDouble(); if (!options.testFlag(FormatOptionShowNull) && qFuzzyIsNull(amount)) { return QString(); } const Unit adjusted = adjustedUnit(amount, unit, prefix); if (adjusted != unit) { amount /= std::pow(unitOrder(unit), adjusted - unit); } const int precision = (value.type() != QVariant::Double && adjusted <= unit) ? 0 : 1; const QString text = QLocale().toString(amount, 'f', precision); return unitFormat(adjusted).subs(text).toString(); } static QString formatTime(const QVariant &value) { const qlonglong seconds = value.toLongLong(); const QString minutesString = QString::number(seconds / 60); const QString secondsScring = QStringLiteral("%1").arg(seconds % 60, 2, 10, QLatin1Char('0')); return minutesString + QLatin1Char(':') + secondsScring; } static QString formatBootTimestamp(const QVariant &value, FormatOptions options) { const qlonglong clockTicksSinceSystemBoot = value.toLongLong(); const QDateTime now = QDateTime::currentDateTime(); #ifdef Q_OS_OSX clock_serv_t cclock; mach_timespec_t tp; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &tp); mach_port_deallocate(mach_task_self(), cclock); #else timespec tp; clock_gettime(CLOCK_MONOTONIC, &tp); #endif const QDateTime systemBootTime = now.addSecs(-tp.tv_sec); const long clockTicksPerSecond = sysconf(_SC_CLK_TCK); const qreal secondsSinceSystemBoot = qreal(clockTicksSinceSystemBoot) / clockTicksPerSecond; const QDateTime absoluteStartTime = systemBootTime.addSecs(secondsSinceSystemBoot); if (!options.testFlag(FormatOptionAgo)) { return QLocale().toString(absoluteStartTime); } const qint64 totalSeconds = absoluteStartTime.secsTo(now); const qint64 totalMinutes = totalSeconds / 60.0; const qint64 totalHours = totalSeconds / 60.0 / 60.0; const qint64 totalDays = totalSeconds / 60.0 / 60.0 / 24.0; if (!totalMinutes) { return i18nc("contains a abbreviated time unit: (s)econds", "%1s ago", totalSeconds); } if (!totalHours) { const int seconds = totalSeconds - totalMinutes * 60; return i18nc("contains abbreviated time units: (m)inutes and (s)econds", "%1m %2s ago", totalMinutes, seconds); } if (!totalDays) { const int seconds = totalSeconds - totalMinutes * 60; const int minutes = totalMinutes - totalHours * 60; return i18nc("contains abbreviated time units: (h)ours, (m)inutes and (s)econds)", "%1h %2m %3s ago", totalHours, minutes, seconds); } const int minutes = totalMinutes - totalHours * 60; const int hours = totalHours - totalDays * 24; return i18ncp("contains also abbreviated time units: (h)ours and (m)inutes", "%1 day %2h %3m ago", "%1 days %2h %3m ago", totalDays, hours, minutes); } qreal Formatter::scaleDownFactor(const QVariant &value, Unit unit, MetricPrefix targetPrefix) { const Unit adjusted = adjustedUnit(value.toDouble(), unit, targetPrefix); if (adjusted == unit) { return 1; } return std::pow(unitOrder(unit), adjusted - unit); } KLocalizedString Formatter::localizedString(const QVariant &value, Unit unit, MetricPrefix targetPrefix) { const Unit adjusted = adjustedUnit(value.toDouble(), unit, targetPrefix); return unitFormat(adjusted); } QString Formatter::formatValue(const QVariant &value, Unit unit, MetricPrefix targetPrefix, FormatOptions options) { switch (unit) { case UnitByte: case UnitKiloByte: case UnitMegaByte: case UnitGigaByte: case UnitTeraByte: case UnitPetaByte: case UnitByteRate: case UnitKiloByteRate: case UnitMegaByteRate: case UnitGigaByteRate: case UnitTeraByteRate: case UnitPetaByteRate: case UnitHertz: case UnitKiloHertz: case UnitMegaHertz: case UnitGigaHertz: case UnitTeraHertz: case UnitPetaHertz: case UnitPercent: case UnitRate: case UnitRpm: case UnitCelsius: case UnitDecibelMilliWatts: case UnitVolt: case UnitWatt: case UnitSecond: return formatNumber(value, unit, targetPrefix, options); case UnitBootTimestamp: return formatBootTimestamp(value, options); case UnitTime: return formatTime(value); default: return value.toString(); } } QString Formatter::symbol(Unit unit) { // TODO: Is it possible to avoid duplication of these symbols? switch (unit) { case UnitByte: return i18nc("Bytes unit symbol", "B"); case UnitKiloByte: return i18nc("Kilobytes unit symbol", "KiB"); case UnitMegaByte: return i18nc("Megabytes unit symbol", "MiB"); case UnitGigaByte: return i18nc("Gigabytes unit symbol", "GiB"); case UnitTeraByte: return i18nc("Terabytes unit symbol", "TiB"); case UnitPetaByte: return i18nc("Petabytes unit symbol", "PiB"); case UnitByteRate: return i18nc("Bytes per second unit symbol", "B/s"); case UnitKiloByteRate: return i18nc("Kilobytes per second unit symbol", "KiB/s"); case UnitMegaByteRate: return i18nc("Megabytes per second unit symbol", "MiB/s"); case UnitGigaByteRate: return i18nc("Gigabytes per second unit symbol", "GiB/s"); case UnitTeraByteRate: return i18nc("Gigabytes per second unit symbol", "TiB/s"); case UnitPetaByteRate: return i18nc("Gigabytes per second unit symbol", "PiB/s"); case UnitHertz: return i18nc("Hertz unit symbol", "Hz"); case UnitKiloHertz: return i18nc("Kilohertz unit symbol", "kHz"); case UnitMegaHertz: return i18nc("Megahertz unit symbol", "MHz"); case UnitGigaHertz: return i18nc("Gigahertz unit symbol", "GHz"); case UnitTeraHertz: return i18nc("Terahertz unit symbol", "THz"); case UnitPetaHertz: return i18nc("Petahertz unit symbol", "PHz"); case UnitPercent: return i18nc("Percent unit", "%"); case UnitRpm: return i18nc("Revolutions per minute unit symbol", "RPM"); case UnitCelsius: return i18nc("Celsius unit symbol", "°C"); case UnitDecibelMilliWatts: return i18nc("Decibels unit symbol", "dBm"); case UnitSecond: return i18nc("Seconds unit symbol", "s"); case UnitVolt: return i18nc("Volts unit symbol", "V"); case UnitWatt: return i18nc("Watts unit symbol", "W"); case UnitRate: return i18nc("Rate unit symbol", "s⁻¹"); default: return QString(); } } } // namespace KSysGuard diff --git a/processcore/formatter.h b/formatter/Formatter.h similarity index 97% copy from processcore/formatter.h copy to formatter/Formatter.h index 2262d8e..268bc7c 100644 --- a/processcore/formatter.h +++ b/formatter/Formatter.h @@ -1,91 +1,92 @@ /* Copyright (C) 2019 Vlad Zagorodniy 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 "unit.h" +#include "formatter_export.h" +#include "Unit.h" // Qt #include #include 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 +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) diff --git a/formatter/Unit.cpp b/formatter/Unit.cpp new file mode 100644 index 0000000..ee7ac06 --- /dev/null +++ b/formatter/Unit.cpp @@ -0,0 +1,3 @@ +#include "Unit.h" + +#include "moc_Unit.cpp" diff --git a/processcore/unit.h b/formatter/Unit.h similarity index 97% copy from processcore/unit.h copy to formatter/Unit.h index 5ad3f57..7835ae3 100644 --- a/processcore/unit.h +++ b/formatter/Unit.h @@ -1,91 +1,93 @@ /* Copyright (C) 2019 Vlad Zagorodniy 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 +#include "formatter_export.h" + namespace KSysGuard { -Q_DECL_EXPORT Q_NAMESPACE +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 diff --git a/formatter/declarative/CMakeLists.txt b/formatter/declarative/CMakeLists.txt new file mode 100644 index 0000000..44cd83e --- /dev/null +++ b/formatter/declarative/CMakeLists.txt @@ -0,0 +1,8 @@ +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) diff --git a/formatter/declarative/FormatterPlugin.cpp b/formatter/declarative/FormatterPlugin.cpp new file mode 100644 index 0000000..dc9c573 --- /dev/null +++ b/formatter/declarative/FormatterPlugin.cpp @@ -0,0 +1,37 @@ +/* + Copyright (C) 2020 Arjen Hiemstra + + 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 + +using namespace KSysGuard; + +void FormatterPlugin::registerTypes(const char *uri) +{ + Q_ASSERT(QLatin1String(uri) == QLatin1String("org.kde.ksysguard.formatter")); + + qRegisterMetaType(); + qRegisterMetaType(); + qmlRegisterSingletonType(uri, 1, 0, "Formatter", [](QQmlEngine*, QJSEngine*) -> QObject* { return new FormatterWrapper(); }); + qmlRegisterUncreatableMetaObject(KSysGuard::staticMetaObject, uri, 1, 0, "Units", QStringLiteral("Contains unit enums")); +} diff --git a/formatter/declarative/FormatterPlugin.h b/formatter/declarative/FormatterPlugin.h new file mode 100644 index 0000000..1f6a47a --- /dev/null +++ b/formatter/declarative/FormatterPlugin.h @@ -0,0 +1,31 @@ +/* + Copyright (C) 2020 Arjen Hiemstra + + 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 + +class FormatterPlugin : public QQmlExtensionPlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") + +public: + void registerTypes(const char *uri) override; +}; diff --git a/formatter/declarative/FormatterWrapper.cpp b/formatter/declarative/FormatterWrapper.cpp new file mode 100644 index 0000000..3ffe686 --- /dev/null +++ b/formatter/declarative/FormatterWrapper.cpp @@ -0,0 +1,36 @@ +/* + Copyright (C) 2020 Arjen Hiemstra + + 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); +} + +} diff --git a/formatter/declarative/FormatterWrapper.h b/formatter/declarative/FormatterWrapper.h new file mode 100644 index 0000000..e029f00 --- /dev/null +++ b/formatter/declarative/FormatterWrapper.h @@ -0,0 +1,51 @@ +/* + Copyright (C) 2020 Arjen Hiemstra + + 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 + +#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 + ); +}; + +} diff --git a/formatter/declarative/qmldir b/formatter/declarative/qmldir new file mode 100644 index 0000000..1dcb872 --- /dev/null +++ b/formatter/declarative/qmldir @@ -0,0 +1,2 @@ +module org.kde.ksysguard.formatter +plugin FormatterPlugin diff --git a/processcore/CMakeLists.txt b/processcore/CMakeLists.txt index bd07f6f..25530bf 100644 --- a/processcore/CMakeLists.txt +++ b/processcore/CMakeLists.txt @@ -1,82 +1,81 @@ add_definitions(-DTRANSLATION_DOMAIN=\"processcore\") ########### next target ############### set(ksysguard_LIB_SRCS extended_process_list.cpp - formatter.cpp processes.cpp process.cpp process_attribute.cpp processes_local_p.cpp processes_remote_p.cpp processes_base_p.cpp 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) add_library(processcore ${ksysguard_LIB_SRCS}) add_library(KF5::ProcessCore ALIAS processcore) target_link_libraries(processcore PUBLIC Qt5::Core + KSysGuard::Formatter PRIVATE KF5::I18n KF5::AuthCore KF5::CoreAddons ${ZLIB_LIBRARIES} ) if( ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD" ) message(STATUS "Adding kvm library on NetBSD") target_link_libraries(processcore kvm) endif() if(NOT HAVE_CLOCK_GETTIME_C) target_link_libraries(processcore PRIVATE rt) endif() target_include_directories(processcore PUBLIC "$" "$" ) set_target_properties(processcore PROPERTIES VERSION ${KSYSGUARD_VERSION_STRING} SOVERSION ${KSYSGUARD_SOVERSION} EXPORT_NAME ProcessCore) install(TARGETS processcore EXPORT libksysguardLibraryTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) ########### install files ############### install( FILES processes.h process.h process_controller.h process_attribute.h process_data_provider.h formatter.h unit.h DESTINATION ${KDE_INSTALL_INCLUDEDIR}/ksysguard/processcore COMPONENT Devel ) #------ KAuth stuff # Auth example helper set(ksysguardprocesslist_helper_srcs helper.cpp process.cpp processes_local_p.cpp processes_base_p.cpp) add_executable(ksysguardprocesslist_helper ${ksysguardprocesslist_helper_srcs}) target_link_libraries(ksysguardprocesslist_helper Qt5::Core KF5::AuthCore KF5::I18n) install(TARGETS ksysguardprocesslist_helper DESTINATION ${KAUTH_HELPER_INSTALL_DIR}) kauth_install_helper_files(ksysguardprocesslist_helper org.kde.ksysguard.processlisthelper root) kauth_install_actions(org.kde.ksysguard.processlisthelper actions.actions) set_target_properties(ksysguardprocesslist_helper PROPERTIES COMPILE_FLAGS "-Wall -ggdb") diff --git a/processcore/formatter.h b/processcore/formatter.h index 2262d8e..32af39d 100644 --- a/processcore/formatter.h +++ b/processcore/formatter.h @@ -1,91 +1,20 @@ /* Copyright (C) 2019 Vlad Zagorodniy 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 "unit.h" - -// Qt -#include -#include - -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" diff --git a/processcore/unit.cpp b/processcore/unit.cpp deleted file mode 100644 index e8a395a..0000000 --- a/processcore/unit.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "unit.h" - -#include "moc_unit.cpp" diff --git a/processcore/unit.h b/processcore/unit.h index 5ad3f57..4fce88d 100644 --- a/processcore/unit.h +++ b/processcore/unit.h @@ -1,91 +1,20 @@ /* Copyright (C) 2019 Vlad Zagorodniy 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 - -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" diff --git a/tests/formatter/test_formatter.qml b/tests/formatter/test_formatter.qml new file mode 100644 index 0000000..e536be3 --- /dev/null +++ b/tests/formatter/test_formatter.qml @@ -0,0 +1,64 @@ +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 + + ColumnLayout { + anchors.fill: parent + + TextField { + id: input + Layout.fillWidth: true + } + + ComboBox { + id: unitCombo + + Layout.fillWidth: true + + textRole: "key" + + model: [ + { key: "Bytes", value: Formatter.Units.UnitByte }, + { key: "Kilobytes", value: Formatter.Units.UnitKiloByte }, + { key: "Megabytes", value: Formatter.Units.UnitMegaByte }, + { key: "Bytes per Second", value: Formatter.Units.UnitByteRate }, + { key: "Hertz", value: Formatter.Units.UnitHertz }, + { key: "Second", value: Formatter.Units.UnitSecond }, + { key: "Celcius", value: Formatter.Units.UnitCelsius }, + { key: "Volt", value: Formatter.Units.UnitVolt }, + { key: "Watt", value: Formatter.Units.UnitWatt } + ] + } + + ComboBox { + id: prefixCombo + + Layout.fillWidth: true + + textRole: "key" + + model: [ + { key: "None", value: Formatter.Units.MetricPrefixUnity }, + { key: "Auto-adjust", value: Formatter.Units.MetricPrefixAutoAdjust }, + { key: "Kilo", value: Formatter.Units.MetricPrefixKilo }, + { key: "Mega", value: Formatter.Units.MetricPrefixMega }, + { key: "Giga", value: Formatter.Units.MetricPrefixGiga }, + { key: "Tera", value: Formatter.Units.MetricPrefixTera }, + { key: "Peta", value: Formatter.Units.MetricPrefixPeta } + ] + } + + Label { + text: Formatter.Formatter.formatValueShowNull(input.text, + unitCombo.model[unitCombo.currentIndex].value, + prefixCombo.model[prefixCombo.currentIndex].value + ) + } + } +}