1 | /*************************************************************************** |
2 | time.h - description |
3 | ------------------- |
4 | begin : Sat Sep 14 2002 |
5 | copyright : (C) 2002 by Jason Wood |
6 | email : jasonwood@blueyonder.co.uk |
7 | ***************************************************************************/ |
8 | |
9 | /*************************************************************************** |
10 | * * |
11 | * This program is free software; you can redistribute it and/or modify * |
12 | * it under the terms of the GNU General Public License as published by * |
13 | * the Free Software Foundation; either version 2 of the License, or * |
14 | * (at your option) any later version. * |
15 | * * |
16 | ***************************************************************************/ |
17 | |
18 | #ifndef GENTIME_H |
19 | #define GENTIME_H |
20 | |
21 | #include <QString> |
22 | #include <cmath> |
23 | |
24 | /** |
25 | * @class GenTime |
26 | * @brief Encapsulates a time, which can be set in various forms and outputted in various forms. |
27 | * @author Jason Wood |
28 | */ |
29 | |
30 | class GenTime |
31 | { |
32 | public: |
33 | /** @brief Creates a GenTime object, with a time of 0 seconds. */ |
34 | GenTime(); |
35 | |
36 | /** @brief Creates a GenTime object, with time given in seconds. */ |
37 | explicit GenTime(double seconds); |
38 | |
39 | /** @brief Creates a GenTime object, by passing number of frames and how many frames per second. */ |
40 | GenTime(int frames, double framesPerSecond); |
41 | |
42 | /** @brief Gets the time, in seconds. */ |
43 | double seconds() const; |
44 | |
45 | /** @brief Gets the time, in milliseconds */ |
46 | double ms() const; |
47 | |
48 | /** @brief Gets the time in frames. |
49 | * @param framesPerSecond Number of frames per second */ |
50 | double frames(double framesPerSecond) const; |
51 | |
52 | QString toString() const; |
53 | |
54 | |
55 | /* |
56 | * Operators. |
57 | */ |
58 | |
59 | /// Unary minus |
60 | GenTime operator -() { |
61 | return GenTime(-m_time); |
62 | } |
63 | |
64 | /// Addition |
65 | GenTime & operator+=(GenTime op) { |
66 | m_time += op.m_time; |
67 | return *this; |
68 | } |
69 | |
70 | /// Subtraction |
71 | GenTime & operator-=(GenTime op) { |
72 | m_time -= op.m_time; |
73 | return *this; |
74 | } |
75 | |
76 | /** @brief Adds two GenTimes. */ |
77 | GenTime operator+(GenTime op) const { |
78 | return GenTime(m_time + op.m_time); |
79 | } |
80 | |
81 | /** @brief Subtracts one genTime from another. */ |
82 | GenTime operator-(GenTime op) const { |
83 | return GenTime(m_time - op.m_time); |
84 | } |
85 | |
86 | /** @brief Multiplies one GenTime by a double value, returning a GenTime. */ |
87 | GenTime operator*(double op) const { |
88 | return GenTime(m_time * op); |
89 | } |
90 | |
91 | /** @brief Divides one GenTime by a double value, returning a GenTime. */ |
92 | GenTime operator/(double op) const { |
93 | return GenTime(m_time / op); |
94 | } |
95 | |
96 | bool operator<(GenTime op) const { |
97 | return m_time + s_delta < op.m_time; |
98 | } |
99 | |
100 | bool operator>(GenTime op) const { |
101 | return m_time > op.m_time + s_delta; |
102 | } |
103 | |
104 | bool operator>=(GenTime op) const { |
105 | return m_time + s_delta >= op.m_time; |
106 | } |
107 | |
108 | bool operator<=(GenTime op) const { |
109 | return m_time <= op.m_time + s_delta; |
110 | } |
111 | |
112 | bool operator==(GenTime op) const { |
113 | return fabs(m_time - op.m_time) < s_delta; |
114 | } |
115 | |
116 | bool operator!=(GenTime op) const { |
117 | return fabs(m_time - op.m_time) >= s_delta; |
118 | } |
119 | |
120 | private: |
121 | /** Holds the time in seconds for this object. */ |
122 | double m_time; |
123 | |
124 | /** A delta value that is used to get around floating point rounding issues. */ |
125 | static double s_delta; |
126 | }; |
127 | |
128 | #endif |
129 | |