File indexing completed on 2024-05-05 05:54:31
0001 /* 0002 SPDX-FileCopyrightText: 2014 Elvis Angelaccio <elvis.angelaccio@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef STOPWATCH_H 0008 #define STOPWATCH_H 0009 0010 #include <QElapsedTimer> 0011 #include <QObject> 0012 #include <QTime> 0013 0014 class QTimerEvent; 0015 0016 /** 0017 * @brief A Stopwatch class. 0018 * Stopwatch is a simple QObject implementing a real stopwatch. 0019 * The class provides public Q_SLOTS to start/pause/reset its internal timer. 0020 * A slot for laps recording exists too, but the computing of lap times is not a task of this class: 0021 * Stopwatch simply emits a signal, that the receiver can use to compute lap times. 0022 */ 0023 class Stopwatch : public QObject 0024 { 0025 Q_OBJECT 0026 0027 public: 0028 0029 enum class Granularity : int 0030 { 0031 Milliseconds = 1, /**< Stopwatch refreshed every msec. */ 0032 Hundredths = 10, /**< Stopwatch refreshed every 10 msec. */ 0033 Tenths = 100, /**< Stopwatch refreshed every 100 msec. */ 0034 Seconds = 1000 /**< Stopwatch refreshed every sec. */ 0035 }; 0036 0037 explicit Stopwatch(QObject *parent = nullptr); 0038 0039 /** 0040 * Set the stopwatch refresh granularity 0041 * @param g The granularity to be set. 0042 */ 0043 void setGranularity(Granularity g); 0044 0045 /** 0046 * Check if the stopwatch is running 0047 * @return true if running, false otherwise 0048 */ 0049 bool isRunning() const; 0050 0051 /** 0052 * Check if the stopwatch is paused 0053 * @return true if paused, false otherwise 0054 */ 0055 bool isPaused() const; 0056 0057 /** 0058 * Check if the stopwatch is inactive 0059 * @return true if inactive, false otherwise 0060 */ 0061 bool isInactive() const; 0062 0063 /** 0064 * Read-only access to the stopwatch underlying data 0065 * @return The stopwatch raw counter 0066 */ 0067 int raw() const; 0068 0069 /** 0070 * (Re)-initialize (deserialize) the stopwatch from the given raw data counter. 0071 * Only an inactive stopwatch is meant to be (re)-initialized (deserialized). 0072 * @param rawData The raw milliseconds counter for the stopwatch 0073 * @return true if the operation succeeds (i.e. the stopwatch was inactive), false otherwise 0074 */ 0075 bool initialize(int rawData); 0076 0077 public Q_SLOTS: 0078 0079 /** 0080 * Start the stopwatch, if inactive or paused. 0081 */ 0082 void start(); 0083 0084 /** 0085 * Pause the stopwatch, if running. 0086 */ 0087 void pause(); 0088 0089 /** 0090 * Reset the stopwatch to the inactive state. 0091 */ 0092 void reset(); 0093 0094 /** 0095 * Tells the stopwatch to compute a new lap time. 0096 */ 0097 void storeLap(); 0098 0099 Q_SIGNALS: 0100 0101 /** 0102 * The stopwatch has been started. 0103 */ 0104 void running(); 0105 0106 /** 0107 * The stopwatch has been paused. 0108 */ 0109 void paused(); 0110 0111 /** 0112 * The stopwatch has been reset. 0113 */ 0114 void inactive(); 0115 0116 /** 0117 * Emits a signal with the last lap *absolute* time. 0118 * This class does not compute *relatives* lap times. 0119 * You can compute them simply by the difference between consecutives absolute times. 0120 * @param lapTime The absolute time of the last lap. 0121 */ 0122 void lap(const QTime& lapTime); 0123 0124 /** 0125 * Emits a signal with the current stopwatch time. 0126 * @param t Current stopwatch time. 0127 */ 0128 void time(int t); 0129 0130 protected: 0131 0132 void timerEvent(QTimerEvent *event) override; 0133 0134 private: 0135 0136 int granularity() const; 0137 0138 enum class State 0139 { 0140 Inactive, /**< Inactive stopwatch. */ 0141 Running, /**< Running stopwatch. */ 0142 Paused /**< Paused stopwatch. */ 0143 }; 0144 0145 static constexpr int INACTIVE_TIMER_ID = -1; /** Used for timerId initialization */ 0146 0147 int m_timerId = INACTIVE_TIMER_ID; /** ID for the QObject timer */ 0148 int m_accumulator = 0; /** milliseconds internal counter */ 0149 State m_state = State::Inactive; /** Stopwatch current state */ 0150 Granularity m_granularity = Granularity::Hundredths; /** Stopwatch current granularity */ 0151 0152 QElapsedTimer m_elapsedTimer; /** Stopwatch core class*/ 0153 0154 Q_DISABLE_COPY(Stopwatch) 0155 }; 0156 0157 0158 0159 #endif