]> git.sesse.net Git - kdenlive/blobdiff - src/timecodedisplay.cpp
Fix scripts with special chars in render name:
[kdenlive] / src / timecodedisplay.cpp
index f3e98203449847e27013ea6d718cd527ed1db1fa..55d5fd08525d8a6f3ea6fd8067e388164616e925 100644 (file)
@@ -39,18 +39,19 @@ TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
     lineedit->setFont(KGlobalSettings::toolBarFont());
     QFontMetrics fm = lineedit->fontMetrics();
     lineedit->setMaximumWidth(fm.width("88:88:88:888"));
-    setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
+    setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
 
-    slotUpdateTimeCodeFormat();
+    setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
 
     connect(uparrow, SIGNAL(clicked()), this, SLOT(slotValueUp()));
     connect(downarrow, SIGNAL(clicked()), this, SLOT(slotValueDown()));
     connect(lineedit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
+    connect(lineedit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(slotCursorPositionChanged(int, int)));
 }
 
 void TimecodeDisplay::slotValueUp()
 {
-    int val = value();
+    int val = getValue();
     val++;
     setValue(val);
     lineedit->clearFocus();
@@ -59,18 +60,18 @@ void TimecodeDisplay::slotValueUp()
 
 void TimecodeDisplay::slotValueDown()
 {
-    int val = value();
+    int val = getValue();
     val--;
     setValue(val);
     lineedit->clearFocus();
     emit editingFinished();
 }
 
-void TimecodeDisplay::setTimeCodeFormat(bool frametimecode)
+void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
 {
-    int val = value();
+    if (!init && m_frametimecode == frametimecode) return;
+    int val = getValue();
     m_frametimecode = frametimecode;
-    lineedit->setInputMask("");
     if (m_frametimecode) {
         QIntValidator *valid = new QIntValidator(lineedit);
         valid->setBottom(0);
@@ -86,6 +87,12 @@ void TimecodeDisplay::slotUpdateTimeCodeFormat()
     setTimeCodeFormat(KdenliveSettings::frametimecode());
 }
 
+void TimecodeDisplay::updateTimeCode(Timecode t)
+{
+    m_timecode = t;
+    setTimeCodeFormat(KdenliveSettings::frametimecode());
+}
+
 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
 {
     if (e->key() == Qt::Key_Up)
@@ -115,17 +122,15 @@ int TimecodeDisplay::minimum() const
     return m_minimum;
 }
 
-int TimecodeDisplay::value() const
+int TimecodeDisplay::getValue() const
 {
-    int frames;
-    if (m_frametimecode) frames = lineedit->text().toInt();
-    else frames = m_timecode.getFrameCount(lineedit->text());
-    return frames;
+    if (m_frametimecode) return lineedit->text().toInt();
+    else return m_timecode.getFrameCount(lineedit->text());
 }
 
 GenTime TimecodeDisplay::gentime() const
 {
-    return GenTime(value(), m_timecode.fps());
+    return GenTime(getValue(), m_timecode.fps());
 }
 
 Timecode TimecodeDisplay::timecode() const
@@ -151,15 +156,16 @@ void TimecodeDisplay::setValue(int value)
     if (m_maximum > m_minimum && value > m_maximum)
         value = m_maximum;
 
+    if (value == getValue() && !lineedit->text().isEmpty()) return;
     downarrow->setEnabled(value > m_minimum);
     uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
 
     if (m_frametimecode)
         lineedit->setText(QString::number(value));
-    else
-        lineedit->setText(m_timecode.getTimecodeFromFrames(value));
-
-    //emit valueChanged(value, true);
+    else {
+        QString v = m_timecode.getTimecodeFromFrames(value);
+        lineedit->setText(v);
+    }
 }
 
 void TimecodeDisplay::setValue(GenTime value)
@@ -167,4 +173,26 @@ void TimecodeDisplay::setValue(GenTime value)
     setValue(m_timecode.getTimecode(value));
 }
 
+void TimecodeDisplay::slotCursorPositionChanged(int oldPos, int newPos)
+{
+    lineedit->blockSignals(true);
+    QString text = lineedit->text();
+
+    if (newPos < text.size() && !text.at(newPos).isDigit()) {
+        // char at newPos is a separator (':' or ';')
+
+        // make it possible move the cursor backwards at separators
+        if (newPos == oldPos - 1)
+            lineedit->setSelection(newPos, -1);
+        else
+            lineedit->setSelection(newPos + 2, -1);
+    } else if (newPos < text.size()) {
+        lineedit->setSelection(newPos + 1, -1);
+    } else {
+        lineedit->setSelection(newPos, -1);
+    }
+
+    lineedit->blockSignals(false);
+}
+
 #include <timecodedisplay.moc>