File indexing completed on 2024-04-21 16:17:39

0001 /*
0002     SPDX-FileCopyrightText: 2019 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 // Own
0010 #include "Unit.h"
0011 #include "formatter_export.h"
0012 
0013 // Qt
0014 #include <QString>
0015 #include <QVariant>
0016 
0017 class KLocalizedString;
0018 
0019 namespace KSysGuard
0020 {
0021 /**
0022  * This enum type is used to specify format options.
0023  */
0024 enum FormatOption {
0025     FormatOptionNone = 0,
0026     FormatOptionAgo = 1 << 0,
0027     FormatOptionShowNull = 1 << 1,
0028 };
0029 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
0030 
0031 /**
0032  * A class for formatting sensor values
0033  * @see FormatterWrapper, for using it from Qml
0034  */
0035 class FORMATTER_EXPORT Formatter
0036 {
0037 public:
0038     /**
0039      * Returns the scale factor suitable for display.
0040      *
0041      * @param value The maximum output value.
0042      * @param unit The unit of the value.
0043      * @param targetPrefix Preferred metric prefix.
0044      */
0045     static qreal scaleDownFactor(const QVariant &value, Unit unit, MetricPrefix targetPrefix = MetricPrefixAutoAdjust);
0046 
0047     /**
0048      * Returns localized string that is suitable for display.
0049      *
0050      * @param value The maximum output value.
0051      * @param unit The unit of the value.
0052      * @param targetPrefix Preferred metric prefix.
0053      */
0054     static KLocalizedString localizedString(const QVariant &value, Unit unit, MetricPrefix targetPrefix = MetricPrefixAutoAdjust);
0055 
0056     /**
0057      * Converts @p value to the appropriate displayable string.
0058      *
0059      * The returned string is localized.
0060      *
0061      * @param value The value to be converted.
0062      * @param unit The unit of the value.
0063      * @param targetPrefix Preferred metric prefix.
0064      * @param options
0065      */
0066     static QString formatValue(const QVariant &value, Unit unit, MetricPrefix targetPrefix = MetricPrefixAutoAdjust, FormatOptions options = FormatOptionNone);
0067 
0068     /**
0069      * Returns a symbol that corresponds to the given @p unit.
0070      *
0071      * The returned unit symbol is localized.
0072      */
0073     static QString symbol(Unit unit);
0074 
0075     /**
0076      * Return the maximum length of a formatted string for the specified unit and font.
0077      *
0078      * @param unit The unit to use.
0079      * @param font The font to use.
0080      */
0081     static qreal maximumLength(Unit unit, const QFont &font);
0082 };
0083 
0084 } // namespace KSysGuard
0085 
0086 Q_DECLARE_OPERATORS_FOR_FLAGS(KSysGuard::FormatOptions)