]> git.sesse.net Git - kdenlive/blob - src/gentime.h
Merge branch 'master' into audioAlign
[kdenlive] / src / gentime.h
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     /** @brief Rounds the GenTime's value to the nearest frame.
53     * @param framesPerSecond Number of frames per second */
54     GenTime & roundNearestFrame(double framesPerSecond);
55
56     QString toString() const;
57
58
59     /*
60      * Operators.
61      */
62     
63     GenTime & operator+=(GenTime op) {
64         m_time += op.m_time;
65         return *this;
66     }
67
68     GenTime & operator-=(GenTime op) {
69         m_time -= op.m_time;
70         return *this;
71     }
72
73     /** @brief Adds two GenTimes. */
74     GenTime operator+(GenTime op) const {
75         return GenTime(m_time + op.m_time);
76     }
77
78     /** @brief Subtracts one genTime from another. */
79     GenTime operator-(GenTime op) const {
80         return GenTime(m_time - op.m_time);
81     }
82
83     /** @brief Multiplies one GenTime by a double value, returning a GenTime. */
84     GenTime operator*(double op) const {
85         return GenTime(m_time * op);
86     }
87
88     /** @brief Divides one GenTime by a double value, returning a GenTime. */
89     GenTime operator/(double op) const {
90         return GenTime(m_time / op);
91     }
92
93     bool operator<(GenTime op) const {
94         return m_time + s_delta < op.m_time;
95     }
96
97     bool operator>(GenTime op) const {
98         return m_time > op.m_time + s_delta;
99     }
100
101     bool operator>=(GenTime op) const {
102         return m_time + s_delta >= op.m_time;
103     }
104
105     bool operator<=(GenTime op) const {
106         return m_time <= op.m_time + s_delta;
107     }
108
109     bool operator==(GenTime op) const {
110         return fabs(m_time - op.m_time) < s_delta;
111     }
112
113     bool operator!=(GenTime op) const {
114         return fabs(m_time - op.m_time) >= s_delta;
115     }
116
117 private:
118     /** Holds the time for this object. */
119     double m_time;
120
121     /** A delta value that is used to get around floating point rounding issues. */
122     static double s_delta;
123 };
124
125 #endif