]> git.sesse.net Git - kdenlive/blob - src/gentime.h
Starting KDE4 porting
[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 <cmath>
22
23 /**Encapsulates a time, which can be set in various forms and outputted in various forms.
24   *@author Jason Wood
25   */
26
27 class GenTime {
28   public:
29         /** Creates a time object, with a time of 0 seconds. */
30     GenTime();
31
32         /** Creates a time object, with time given in seconds. */
33     explicit GenTime(double seconds);
34
35         /** Creates a time object, by passing number of frames and how many frames per second */
36      GenTime(int frames, double framesPerSecond);
37
38         /** returns the time, in seconds */
39     double seconds() const {
40         return m_time;
41     }
42         /** Returns the time, in milliseconds */ double ms() const;
43
44         /** Returns the time in frames, after being given the number of frames per second */
45     double frames(double framesPerSecond) const;
46
47      GenTime & operator+=(GenTime op) {
48         m_time += op.m_time;
49         return *this;
50     }
51         /** Adds two GenTimes */ GenTime operator+(GenTime op) const {
52         return GenTime(m_time + op.m_time);
53     }
54         /** Subtracts one genTime from another */ GenTime operator-(GenTime op) const {
55         return GenTime(m_time - op.m_time);
56     }
57         /** Multiplies one GenTime by a double value, returning a GenTime */
58         GenTime operator*(double op) const {
59         return GenTime(m_time * op);
60     }
61         /** Divides one GenTime by a double value, returning a GenTime */
62         GenTime operator/(double op) const {
63         return GenTime(m_time / op);
64     }
65     /* Implementation of < operator; Works identically as with basic types. */
66         bool operator<(GenTime op) const {
67         return m_time + s_delta < op.m_time;
68     }
69     /* Implementation of > operator; Works identically as with basic types. */
70         bool operator>(GenTime op) const {
71         return m_time > op.m_time + s_delta;
72     }
73     /* Implementation of >= operator; Works identically as with basic types. */
74         bool operator>=(GenTime op) const {
75         return m_time + s_delta >= op.m_time;
76     }
77     /* Implementation of <= operator; Works identically as with basic types. */
78         bool operator<=(GenTime op) const {
79         return m_time <= op.m_time + s_delta;
80     }
81     /* Implementation of == operator; Works identically as with basic types. */
82         bool operator==(GenTime op) const {
83         return fabs(m_time - op.m_time) < s_delta;
84     }
85     /* Implementation of != operator; Works identically as with basic types. */
86         bool operator!=(GenTime op) const {
87         return fabs(m_time - op.m_time) >= s_delta;
88     }
89     /* Rounds the GenTIme's value to the nearest frame */
90         GenTime & roundNearestFrame(double framesPerSecond) {
91         m_time = floor((m_time * framesPerSecond) + 0.5) / framesPerSecond;
92         return *this;
93     }
94
95     ~GenTime();
96   private:                      // Private attributes
97   /** Holds the time for this object. */
98     double m_time;
99
100   /** A delta value that is used to get around floating point rounding issues. */
101     static double s_delta;
102 };
103
104 #endif