1 /*
2     SPDX-FileCopyrightText: 2002 Jason Wood <jasonwood@blueyonder.co.uk>
3 
4     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6 
7 #ifndef GENTIME_H
8 #define GENTIME_H
9 
10 #include <QString>
11 #include <cmath>
12 
13 /**
14  * @class GenTime
15  * @brief Encapsulates a time, which can be set in various forms and outputted in various forms.
16  * @author Jason Wood
17  */
18 class GenTime
19 {
20 public:
21     /** @brief Creates a GenTime object, with a time of 0 seconds. */
22     GenTime();
23 
24     /** @brief Creates a GenTime object, with time given in seconds. */
25     explicit GenTime(double seconds);
26 
27     /** @brief Creates a GenTime object, by passing number of frames and how many frames per second. */
28     GenTime(int frames, double framesPerSecond);
29 
30     /** @brief Gets the time, in seconds. */
31     double seconds() const;
32 
33     /** @brief Gets the time, in milliseconds */
34     double ms() const;
35 
36     /** @brief Gets the time in frames.
37      * @param framesPerSecond Number of frames per second */
38     int frames(double framesPerSecond) const;
39 
40     QString toString() const;
41 
42     /*
43      * Operators.
44      */
45 
46     /// Unary minus
47     GenTime operator-();
48 
49     /// Addition
50     GenTime &operator+=(GenTime op);
51 
52     /// Subtraction
53     GenTime &operator-=(GenTime op);
54 
55     /** @brief Adds two GenTimes. */
56     GenTime operator+(GenTime op) const;
57 
58     /** @brief Subtracts one genTime from another. */
59     GenTime operator-(GenTime op) const;
60 
61     /** @brief Multiplies one GenTime by a double value, returning a GenTime. */
62     GenTime operator*(double op) const;
63 
64     /** @brief Divides one GenTime by a double value, returning a GenTime. */
65     GenTime operator/(double op) const;
66 
67     /* All the comparison operators considers that two GenTime that differs by less
68     than one frame are equal.
69     The fps used to carry this computation must be set using the static function setFps
70     */
71     bool operator<(GenTime op) const;
72 
73     bool operator>(GenTime op) const;
74 
75     bool operator>=(GenTime op) const;
76 
77     bool operator<=(GenTime op) const;
78 
79     bool operator==(GenTime op) const;
80 
81     bool operator!=(GenTime op) const;
82 
83     /** @brief Sets the fps used to determine if two GenTimes are equal */
84     static void setFps(double fps);
85 
86 private:
87     /** Holds the time in seconds for this object. */
88     double m_time;
89 
90     /** A delta value that is used to get around floating point rounding issues. */
91     static double s_delta;
92 };
93 
94 #endif
95