File indexing completed on 2022-11-29 14:07:20

0001 /*
0002     SPDX-FileCopyrightText: 2002 Jason Wood <jasonwood@blueyonder.co.uk>
0003 
0004     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QString>
0010 #include <cmath>
0011 
0012 /**
0013  * @class GenTime
0014  * @brief Encapsulates a time, which can be set in various forms and outputted in various forms.
0015  * @author Jason Wood
0016  */
0017 class GenTime
0018 {
0019 public:
0020     /** @brief Creates a GenTime object, with a time of 0 seconds. */
0021     GenTime();
0022 
0023     /** @brief Creates a GenTime object, with time given in seconds. */
0024     explicit GenTime(double seconds);
0025 
0026     /** @brief Creates a GenTime object, by passing number of frames and how many frames per second. */
0027     GenTime(int frames, double framesPerSecond);
0028 
0029     /** @brief Gets the time, in seconds. */
0030     double seconds() const;
0031 
0032     /** @brief Gets the time, in milliseconds */
0033     double ms() const;
0034 
0035     /** @brief Gets the time in frames.
0036      * @param framesPerSecond Number of frames per second */
0037     int frames(double framesPerSecond) const;
0038 
0039     QString toString() const;
0040 
0041     /*
0042      * Operators.
0043      */
0044 
0045     /// Unary minus
0046     GenTime operator-();
0047 
0048     /// Addition
0049     GenTime &operator+=(GenTime op);
0050 
0051     /// Subtraction
0052     GenTime &operator-=(GenTime op);
0053 
0054     /** @brief Adds two GenTimes. */
0055     GenTime operator+(GenTime op) const;
0056 
0057     /** @brief Subtracts one genTime from another. */
0058     GenTime operator-(GenTime op) const;
0059 
0060     /** @brief Multiplies one GenTime by a double value, returning a GenTime. */
0061     GenTime operator*(double op) const;
0062 
0063     /** @brief Divides one GenTime by a double value, returning a GenTime. */
0064     GenTime operator/(double op) const;
0065 
0066     /** All the comparison operators considers that two GenTime that differs by less
0067     than one frame are equal.
0068     The fps used to carry this computation must be set using the static function setFps
0069     */
0070     bool operator<(GenTime op) const;
0071 
0072     bool operator>(GenTime op) const;
0073 
0074     bool operator>=(GenTime op) const;
0075 
0076     bool operator<=(GenTime op) const;
0077 
0078     bool operator==(GenTime op) const;
0079 
0080     bool operator!=(GenTime op) const;
0081 
0082     /** @brief Sets the fps used to determine if two GenTimes are equal */
0083     static void setFps(double fps);
0084 
0085 private:
0086     /** Holds the time in seconds for this object. */
0087     double m_time;
0088 
0089     /** A delta value that is used to get around floating point rounding issues. */
0090     static double s_delta;
0091 };
0092 
0093 Q_DECLARE_TYPEINFO(GenTime, Q_COMPLEX_TYPE); //TODO Q_COMPLEX_TYPE is the default, but does Q_MOVABLE_TYPE fit better?