]> git.sesse.net Git - kdenlive/blobdiff - src/timecodedisplay.cpp
Fix build with Qt < 4.6:
[kdenlive] / src / timecodedisplay.cpp
index e37dc6109c2a5bfb05739c99149c125aba5eebef..8f65b722bb81d4ac4f1383081d0f368bbde1f416 100644 (file)
 #include "timecodedisplay.h"
 #include "kdenlivesettings.h"
 
-#include <QSize>
-#include <QStyle>
-#include <QStylePainter>
-#include <QStyleOptionSlider>
 #include <QLineEdit>
 #include <QValidator>
-#include <QHBoxLayout>
-#include <QFrame>
 #include <QMouseEvent>
-#include <QToolButton>
 
 #include <kglobal.h>
 #include <klocale.h>
 #include <kdebug.h>
-
+#include <krestrictedline.h>
+#include <KColorScheme>
+#include <KRestrictedLine>
 
 TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
-        : QWidget(parent),
+        : QAbstractSpinBox(parent),
+        m_timecode(t),
         m_minimum(0),
         m_maximum(-1)
 {
-    setupUi(this);
-    lineedit->setFont(KGlobalSettings::toolBarFont());
-    QFontMetrics fm = lineedit->fontMetrics();
-    lineedit->setMaximumWidth(fm.width("88:88:88:888"));
-    slotPrepareTimeCodeFormat(t);
-    connect(uparrow, SIGNAL(clicked()), this, SLOT(slotValueUp()));
-    connect(downarrow, SIGNAL(clicked()), this, SLOT(slotValueDown()));
-    connect(lineedit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
+    lineEdit()->setFont(KGlobalSettings::toolBarFont());
+    lineEdit()->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
+    QFontMetrics fm = lineEdit()->fontMetrics();
+#if QT_VERSION >= 0x040600
+    setMinimumWidth(fm.width("88:88:88:88888888") + contentsMargins().right() + contentsMargins().right());
+#else
+    int left, top, right, bottom;
+    getContentsMargins(&left, &top, &right, &bottom);
+    setMinimumWidth(fm.width("88:88:88:88888888") + left + right);
+#endif
+    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
+    setAccelerated(true);
+
+    setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
+
+    connect(lineEdit(), SIGNAL(returnPressed()), this, SLOT(slotEditingFinished()));
+    connect(lineEdit(), SIGNAL(cursorPositionChanged(int, int)), this, SLOT(slotCursorPositionChanged(int, int)));
 }
 
-TimecodeDisplay::~TimecodeDisplay()
+// virtual protected
+QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
 {
+    QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
+    if (getValue() > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
+    if (m_maximum == -1 || getValue() < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
+    return result;
 }
 
-void TimecodeDisplay::slotValueUp()
+// virtual
+void TimecodeDisplay::stepBy(int steps)
 {
-    int val = value();
-    val++;
-    if (m_maximum > -1 && val > m_maximum) val = m_maximum;
+    int val = getValue();
+    val += steps;
     setValue(val);
-    lineedit->clearFocus();
     emit editingFinished();
 }
 
-void TimecodeDisplay::slotValueDown()
+void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
 {
-    int val = value();
-    val--;
-    if (val < m_minimum) val = m_minimum;
+    if (!init && m_frametimecode == frametimecode) return;
+    int val = getValue();
+    m_frametimecode = frametimecode;
+    if (m_frametimecode) {
+        QIntValidator *valid = new QIntValidator(lineEdit());
+        valid->setBottom(0);
+        lineEdit()->setValidator(valid);
+    } else {
+        lineEdit()->setValidator(m_timecode.validator());
+    }
     setValue(val);
-    lineedit->clearFocus();
-    emit editingFinished();
 }
 
-void TimecodeDisplay::slotPrepareTimeCodeFormat(Timecode t)
+void TimecodeDisplay::slotUpdateTimeCodeFormat()
+{
+    setTimeCodeFormat(KdenliveSettings::frametimecode());
+}
+
+void TimecodeDisplay::updateTimeCode(Timecode t)
 {
     m_timecode = t;
-    m_frametimecode = KdenliveSettings::frametimecode();
-    QString val = lineedit->text();
-    lineedit->setInputMask("");
-    if (m_frametimecode) {
-        int frames = m_timecode.getFrameCount(lineedit->text());
-        QIntValidator *valid = new QIntValidator(lineedit);
-        valid->setBottom(0);
-        lineedit->setValidator(valid);
-        lineedit->setText(QString::number(frames));
-    } else {
-        int pos = lineedit->text().toInt();
-        lineedit->setValidator(m_timecode.validator());
-        lineedit->setText(m_timecode.getTimecodeFromFrames(pos));
-    }
+    setTimeCodeFormat(KdenliveSettings::frametimecode());
 }
 
 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
 {
-    if (e->key() == Qt::Key_Up) slotValueUp();
-    else if (e->key() == Qt::Key_Down) slotValueDown();
-    else QWidget::keyPressEvent(e);
+    if (e->key() == Qt::Key_Return)
+        slotEditingFinished();
+    else
+        QAbstractSpinBox::keyPressEvent(e);
 }
 
-void TimecodeDisplay::wheelEvent(QWheelEvent *e)
+void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
 {
-    if (e->delta() > 0) slotValueUp();
-    else slotValueDown();
+    QAbstractSpinBox::mouseReleaseEvent(e);
+    if (!lineEdit()->underMouse()) {
+        clearFocus();
+    }
 }
 
+/*
+void TimecodeDisplay::wheelEvent(QWheelEvent *e)
+{
+    if (e->delta() > 0)
+        slotValueUp();
+    else
+        slotValueDown();
+}*/
+
 
 int TimecodeDisplay::maximum() const
 {
@@ -118,12 +136,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(getValue(), m_timecode.fps());
 }
 
 Timecode TimecodeDisplay::timecode() const
@@ -139,25 +160,61 @@ void TimecodeDisplay::setRange(int min, int max)
 
 void TimecodeDisplay::setValue(const QString &value)
 {
-    if (m_frametimecode) {
-        lineedit->setText(QString::number(m_timecode.getFrameCount(value)));
-    } else lineedit->setText(value);
+    setValue(m_timecode.getFrameCount(value));
 }
 
 void TimecodeDisplay::setValue(int value)
 {
-    /*    if (value < m_minimum)
-            value = m_minimum;
-        if (value > m_maximum)
-            value = m_maximum;*/
-    if (m_frametimecode) lineedit->setText(QString::number(value));
-    else lineedit->setText(m_timecode.getTimecodeFromFrames(value));
-
-    /*    setEditText(KGlobal::locale()->formatNumber(value, d->decimals));
-        d->slider->blockSignals(true);
-        d->slider->setValue(int((value - d->minimum) * 256 / (d->maximum - d->minimum) + 0.5));
-        d->slider->blockSignals(false);*/
-    //emit valueChanged(value, true);
+    if (value < m_minimum)
+        value = m_minimum;
+    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 {
+        QString v = m_timecode.getTimecodeFromFrames(value);
+        lineEdit()->setText(v);
+    }
+}
+
+void TimecodeDisplay::setValue(GenTime value)
+{
+    setValue(m_timecode.getTimecode(value));
+}
+
+void TimecodeDisplay::slotCursorPositionChanged(int oldPos, int newPos)
+{
+    if (!lineEdit()->hasFocus()) return;
+    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);
+}
+
+void TimecodeDisplay::slotEditingFinished()
+{
+    clearFocus();
+    lineEdit()->deselect();
+    emit editingFinished();
 }
 
 #include <timecodedisplay.moc>