]> git.sesse.net Git - kdenlive/blobdiff - src/timecode.cpp
Integrate with the required MLT hooks for getting Movit to work.
[kdenlive] / src / timecode.cpp
index bcea5d5c1caeaaa664be01b598ef8b4c725e836d..d36b0f2148a0c32f2914fe01a54cb605d9bf9e22 100644 (file)
@@ -80,16 +80,14 @@ return frameNumber;
 */
 
 
-#include <QValidator>
 
 #include <KDebug>
-#include <KLocale>
+#include <KLocalizedString>
 
 #include "timecode.h"
 
 Timecode::Timecode(Formats format, double framesPerSecond)
 {
-    m_validator = new QRegExpValidator(0);
     setFormat(framesPerSecond, format);
 }
 
@@ -107,12 +105,6 @@ void Timecode::setFormat(double framesPerSecond, Formats format)
         m_dropFrames = round(m_realFps * .066666); //Number of frames to drop on the minute marks is the nearest integer to 6% of the framerate
         m_framesPer10Minutes = round(m_realFps * 600); //Number of frames per ten minutes
     }
-    QRegExp regExp;
-    if (m_dropFrameTimecode)
-        regExp.setPattern("^\\d{2}:\\d{2}:\\d{2};\\d{2}$");
-    else
-        regExp.setPattern("^\\d{2}:\\d{2}:\\d{2}:\\d{2}$");
-    m_validator->setRegExp(regExp);
 }
 
 double Timecode::fps() const
@@ -125,51 +117,55 @@ bool Timecode::df() const
     return m_dropFrameTimecode;
 }
 
-const QValidator *Timecode::validator() const
+const QString Timecode::mask(const GenTime& t) const
 {
-    return m_validator;
+    if (t < GenTime()) {
+        if (m_dropFrameTimecode) return "#99:99:99,99";
+        else return "#99:99:99:99";
+    }
+    if (m_dropFrameTimecode) return "99:99:99,99";
+    else return "99:99:99:99";
 }
 
 QString Timecode::reformatSeparators(QString duration) const
 {
     if (m_dropFrameTimecode)
-        return duration.replace(8, 1, ';');
+        return duration.replace(8, 1, ',');
     return duration.replace(8, 1, ':');
 }
 
-int Timecode::getDisplayFrameCount(const QString duration, bool frameDisplay) const
+int Timecode::getDisplayFrameCount(const QString &duration, bool frameDisplay) const
 {
     if (frameDisplay) return duration.toInt();
     return getFrameCount(duration);
 }
 
-int Timecode::getFrameCount(const QString duration) const
+int Timecode::getFrameCount(const QString &duration) const
 {
+    if (duration.isEmpty()) {
+        return 0;
+    }
+    int hours, minutes, seconds, frames;
+    int offset = 0;
+    if (duration.at(0) == '-') {
+        offset = 1;
+        hours = duration.mid(1, 2).toInt();
+    } else {
+        hours = duration.left(2).toInt();
+    }
+    minutes = duration.mid(3 + offset, 2).toInt();
+    seconds = duration.mid(6 + offset, 2).toInt();
+    frames = duration.right(2).toInt();
     if (m_dropFrameTimecode) {
-
         //CONVERT DROP FRAME TIMECODE TO A FRAME NUMBER
         //Code by David Heidelberger, adapted from Andrew Duncan
         //Given ints called hours, minutes, seconds, frames, and a double called framerate
 
-        //Get Hours, Minutes, Seconds, Frames from timecode
-        int hours, minutes, seconds, frames;
-
-        hours = duration.section(':', 0, 0).toInt();
-        minutes = duration.section(':', 1, 1).toInt();
-        if (duration.contains(';')) {
-            seconds = duration.section(';', 0, 0).section(':', 2, 2).toInt();
-            frames = duration.section(';', 1, 1).toInt();
-        } else {
-            //Handle Drop Frame timecode frame calculations, even if the timecode supplied uses incorrect "99:99:99:99" format instead of "99:99:99;99"
-            seconds = duration.section(':', 2, 2).toInt();
-            frames = duration.section(':', 3, 3).toInt();
-        }
-
         int totalMinutes = (60 * hours) + minutes; //Total number of minutes
         int frameNumber = ((m_displayedFramesPerSecond * 3600 * hours) + (m_displayedFramesPerSecond * 60 * minutes) + (m_displayedFramesPerSecond * seconds) + frames) - (m_dropFrames * (totalMinutes - floor(totalMinutes / 10)));
         return frameNumber;
     }
-    return (int)((duration.section(':', 0, 0).toInt()*3600.0 + duration.section(':', 1, 1).toInt()*60.0 + duration.section(':', 2, 2).toInt()) * m_realFps + duration.section(':', 3, 3).toInt());
+    return (int)((hours * 3600.0 + minutes * 60.0 + seconds) * m_realFps + frames);
 }
 
 QString Timecode::getDisplayTimecode(const GenTime & time, bool frameDisplay) const
@@ -214,20 +210,34 @@ const QString Timecode::getTimecodeFromFrames(int frames) const
 
 
 //static
-QString Timecode::getStringTimecode(int frames, const double &fps)
+QString Timecode::getStringTimecode(int frames, const double &fps, bool showFrames)
 {
     // Returns the timecode in an hh:mm:ss format
+
+    bool negative = false;
+    if (frames < 0) {
+        negative = true;
+        frames = qAbs(frames);
+    }
+
     int seconds = (int)(frames / fps);
+    int frms = frames % (int) (fps + 0.5);
     int minutes = seconds / 60;
     seconds = seconds % 60;
     int hours = minutes / 60;
     minutes = minutes % 60;
     QString text;
+    if (negative)
+        text.append('-');
     text.append(QString::number(hours).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(minutes).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(seconds).rightJustified(2, '0', false));
+    if (showFrames) {
+        text.append('.');
+        text.append(QString::number(frms).rightJustified(2, '0', false));
+    }
     return text;
 }
 
@@ -237,6 +247,13 @@ QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
 {
     // Returns the timecode in an easily read display, like 3 min. 5 sec.
     int frames = (int) time.frames(fps);
+
+    bool negative = false;
+    if (frames < 0) {
+        negative = true;
+        frames = qAbs(frames);
+    }
+
     int seconds = (int)(frames / fps);
     frames = frames - ((int)(fps * seconds));
 
@@ -248,6 +265,8 @@ QString Timecode::getEasyTimecode(const GenTime & time, const double &fps)
     QString text;
     bool trim = false;
 
+    if (negative)
+        text.append('-');
     if (hours != 0) {
         text.append(QString::number(hours).rightJustified(2, '0', false));
         text.append(' ' + i18n("hour") + ' ');
@@ -291,6 +310,13 @@ const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
     if (m_dropFrameTimecode) {
         return getTimecodeDropFrame(frames);
     }
+
+    bool negative = false;
+    if (frames < 0) {
+        negative = true;
+        frames = qAbs(frames);
+    }
+
     int seconds = frames / m_displayedFramesPerSecond;
     frames = frames % m_displayedFramesPerSecond;
 
@@ -300,6 +326,8 @@ const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
     minutes = minutes % 60;
 
     QString text;
+    if (negative)
+        text.append('-');
     text.append(QString::number(hours).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(minutes).rightJustified(2, '0', false));
@@ -314,6 +342,13 @@ const QString Timecode::getTimecodeHH_MM_SS_FF(int frames) const
 const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
 {
     int hundredths = (int)(time.seconds() * 100);
+
+    bool negative = false;
+    if (hundredths < 0) {
+        negative = true;
+        hundredths = qAbs(hundredths);
+    }
+
     int seconds = hundredths / 100;
     hundredths = hundredths % 100;
     int minutes = seconds / 60;
@@ -322,14 +357,15 @@ const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
     minutes = minutes % 60;
 
     QString text;
-
+    if (negative)
+        text.append('-');
     text.append(QString::number(hours).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(minutes).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(seconds).rightJustified(2, '0', false));
     if (m_dropFrameTimecode)
-        text.append(';');
+        text.append(',');
     else
         text.append(':');
     text.append(QString::number(hundredths).rightJustified(2, '0', false));
@@ -339,12 +375,13 @@ const QString Timecode::getTimecodeHH_MM_SS_HH(const GenTime & time) const
 
 const QString Timecode::getTimecodeFrames(const GenTime & time) const
 {
-    return QString::number(time.frames(m_realFps));
+    return QString::number((int) time.frames(m_realFps));
 }
 
 const QString Timecode::getTimecodeSeconds(const GenTime & time) const
 {
-    return QString::number(time.seconds());
+    QLocale locale;
+    return locale.toString(time.seconds());
 }
 
 const QString Timecode::getTimecodeDropFrame(const GenTime & time) const
@@ -359,6 +396,12 @@ const QString Timecode::getTimecodeDropFrame(int framenumber) const
     //Given an int called framenumber and a double called framerate
     //Framerate should be 29.97, 59.94, or 23.976, otherwise the calculations will be off.
 
+    bool negative = false;
+    if (framenumber < 0) {
+        negative = true;
+        framenumber = qAbs(framenumber);
+    }
+
     int d = floor(framenumber / m_framesPer10Minutes);
     int m = framenumber % m_framesPer10Minutes;
 
@@ -374,12 +417,14 @@ const QString Timecode::getTimecodeDropFrame(int framenumber) const
     int hours = floor(floor(floor(framenumber / m_displayedFramesPerSecond) / 60) / 60);
 
     QString text;
+    if (negative)
+        text.append('-');
     text.append(QString::number(hours).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(minutes).rightJustified(2, '0', false));
     text.append(':');
     text.append(QString::number(seconds).rightJustified(2, '0', false));
-    text.append(';');
+    text.append(',');
     text.append(QString::number(frames).rightJustified(2, '0', false));
 
     return text;