File indexing completed on 2024-04-21 16:35:02

0001 /*
0002     SPDX-FileCopyrightText: 2006 Bram Schoenmakers <bramschoenmakers@kde.nl>
0003     SPDX-FileCopyrightText: 2006 Tom Albers <toma@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef RSISTATS_H
0009 #define RSISTATS_H
0010 
0011 #include "rsiglobals.h"
0012 
0013 class QLabel;
0014 
0015 class RSIStatItem;
0016 
0017 /**
0018   This class records all statistics, gathered by the RSITimer.
0019   To add a stat, you should add an alias to the RSIStat enum, found
0020   in RSIGlobal. Then, add the statistic to the constructor of this class
0021   and to the updateLabel method. Don't forget to add a What's This text as well
0022   in the getWhatsThisText() method.
0023   If you add a statistic which is calculated from other statistics, don't
0024   forget to add those statistics as a dependency in the constructor of this
0025   class. The value of the derived statistic will be calculated in
0026   updateDependentStats().
0027   The last step involves to actually put it in the statistics widget. Use
0028   the addStat() method there.
0029 
0030   @see RSIGlobals
0031   @see RSIStatDialog
0032   @see RSITimer
0033 */
0034 class RSIStats
0035 {
0036 public:
0037     /** Default constructor. */
0038     RSIStats();
0039     /** Default destructor. */
0040     ~RSIStats();
0041 
0042     /** Sets all statistics to it's initial value. */
0043     void reset();
0044 
0045     /** Increase the value of statistic @p stat with @p delta (default: 1). */
0046     void increaseStat(RSIStat stat, int delta = 1);
0047 
0048     /**
0049      * Sets the value of a statistic.
0050      * @param stat The statistic in question.
0051      * @param val The value to be assigned to the statistic. In QVariant format.
0052      * @param ifmax If true, the value will only be assigned if the current
0053      * value is lower than the given @p value. Please note that derived stats
0054      * are updated regardless of the fact if a new value is set.
0055      */
0056     void setStat(RSIStat stat, const QVariant &val, bool ifmax = false);
0057 
0058     /**
0059      * Set the color of a given statistic.
0060      * @param stat The statistic in question.
0061      * @param color The color in QColor format.
0062      */
0063     void setColor(RSIStat stat, const QColor &color);
0064 
0065     /** Returns a description for the given @p stat. */
0066     QLabel *getDescription(RSIStat stat) const;
0067 
0068     /**
0069      * Updates all labels to the current value of their corresponding
0070      * statistic.
0071      */
0072     void updateLabels();
0073 
0074     /** Gets the value given the @p stat.*/
0075     QVariant getStat(RSIStat stat) const;
0076 
0077     /** Gets the value of the statistic @p stat in QLabel format. */
0078     QLabel *getLabel(RSIStat stat) const;
0079 
0080     /**
0081       This function prevents RSIStats from calls to updateLabel when
0082       it's not really needed, e.g. when the widget is not visible.
0083       @param b If true, it will update the labels as soon as the stats
0084       update.
0085     */
0086     void doUpdates(bool b);
0087 
0088 protected:
0089     /** Update the label of given @p stat to it's corresponding value. */
0090     void updateLabel(RSIStat stat);
0091 
0092     /**
0093      * Some statistics are calculated based on values of other statistics.
0094      * This function updates all statistics with @p stat as dependency.
0095      */
0096     void updateDependentStats(RSIStat stat);
0097 
0098     /**
0099      * Updates the given statistic.
0100      * @param stat The statistic you've just assigned a value to.
0101      * @param updateDerived If true, update the derived statistics when
0102      * calling this function.
0103      */
0104     void updateStat(RSIStat stat, bool updateDerived = true);
0105 
0106     /**
0107      * Retrieves What's This? text for a given statistic @p stat.
0108      */
0109     QString getWhatsThisText(RSIStat stat) const;
0110 
0111 private:
0112     static RSIStats *m_instance;
0113 
0114     bool m_doUpdates;
0115 
0116     QVector<RSIStatItem *> m_statistics;
0117     /** Contains formatted labels. */
0118     QVector<QLabel *> m_labels;
0119 };
0120 
0121 #endif // RSISTATS_H