]> git.sesse.net Git - kdenlive/blobdiff - src/timecodedisplay.cpp
Const'ref
[kdenlive] / src / timecodedisplay.cpp
index d0c6e22ce32a355922f9914ed59d2839750f8938..a98659ed9b832de832a8cd74608cd9db9e42ea22 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>
-
-
-TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
-        : QWidget(parent),
+#include <krestrictedline.h>
+#include <KColorScheme>
+#include <KRestrictedLine>
+
+TimecodeDisplay::TimecodeDisplay(const Timecode& t, QWidget *parent)
+        : QAbstractSpinBox(parent),
+        m_timecode(t),
+        m_frametimecode(false),
         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()));
-}
-
-TimecodeDisplay::~TimecodeDisplay()
-{
+    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);
+
+    setValue(m_minimum);
+
+    setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
+
+    connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
 }
 
-void TimecodeDisplay::slotValueUp()
+// virtual protected
+QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
 {
-    int val = value();
-    val++;
-    if (m_maximum > -1 && val > m_maximum) val = m_maximum;
-    setValue(val);
-    lineedit->clearFocus();
-    emit editingFinished();
+    QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
+    if (m_value > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
+    if (m_maximum == -1 || m_value < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
+    return result;
 }
 
-void TimecodeDisplay::slotValueDown()
+// virtual
+void TimecodeDisplay::stepBy(int steps)
 {
-    int val = value();
-    val--;
-    if (val < m_minimum) val = m_minimum;
+    int val = m_value + steps;
     setValue(val);
-    lineedit->clearFocus();
-    emit editingFinished();
 }
 
-void TimecodeDisplay::slotPrepareTimeCodeFormat(Timecode t)
+void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
 {
-    m_timecode = t;
-    m_frametimecode = KdenliveSettings::frametimecode();
-    QString val = lineedit->text();
-    lineedit->setInputMask("");
+    if (!init && m_frametimecode == frametimecode) return;
+    m_frametimecode = frametimecode;
+    lineEdit()->clear();
     if (m_frametimecode) {
-        int frames = m_timecode.getFrameCount(lineedit->text());
-        QIntValidator *valid = new QIntValidator(lineedit);
+        QIntValidator *valid = new QIntValidator(lineEdit());
         valid->setBottom(0);
-        lineedit->setValidator(valid);
-        lineedit->setText(QString::number(frames));
+        lineEdit()->setValidator(valid);
+        lineEdit()->setInputMask(QString());
     } else {
-        int pos = lineedit->text().toInt();
-        lineedit->setValidator(m_timecode.validator());
-        lineedit->setText(m_timecode.getTimecodeFromFrames(pos));
+        lineEdit()->setValidator(0);
+        lineEdit()->setInputMask(m_timecode.mask());
     }
+    setValue(m_value);
+}
+
+void TimecodeDisplay::slotUpdateTimeCodeFormat()
+{
+    setTimeCodeFormat(KdenliveSettings::frametimecode());
+}
+
+void TimecodeDisplay::updateTimeCode(const Timecode &t)
+{
+    m_timecode = t;
+    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 || e->key() == Qt::Key_Enter) {
+        e->setAccepted(true);
+        clearFocus();
+    }
+    else
+        QAbstractSpinBox::keyPressEvent(e);
+}
+
+void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
+{
+    QAbstractSpinBox::mouseReleaseEvent(e);
+    if (!lineEdit()->underMouse()) {
+        clearFocus();
+    }
 }
 
+
 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
 {
-    if (e->delta() > 0) slotValueUp();
-    else slotValueDown();
+    QAbstractSpinBox::wheelEvent(e);
+    clearFocus();
 }
 
 
@@ -118,45 +138,64 @@ 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;
+    return m_value;
 }
 
-void TimecodeDisplay::setMinimum(int min)
+GenTime TimecodeDisplay::gentime() const
 {
-    m_minimum = min;
+    return GenTime(m_value, m_timecode.fps());
+}
+
+Timecode TimecodeDisplay::timecode() const
+{
+    return m_timecode;
 }
 
-void TimecodeDisplay::setMaximum(int max)
+void TimecodeDisplay::setRange(int min, int max)
 {
+    m_minimum = min;
     m_maximum = 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 (m_frametimecode) {
+       if (value == m_value && !lineEdit()->text().isEmpty()) return;
+       m_value = value;
+        lineEdit()->setText(QString::number(value));
+    }
+    else {
+       if (value == m_value && lineEdit()->text() != ":::") return;
+       m_value = value;
+        QString v = m_timecode.getTimecodeFromFrames(value);
+        lineEdit()->setText(v);
+    }
+}
+
+void TimecodeDisplay::setValue(const GenTime &value)
+{
+    setValue((int) value.frames(m_timecode.fps()));
+}
+
+
+void TimecodeDisplay::slotEditingFinished()
+{
+    lineEdit()->deselect();
+    if (m_frametimecode) setValue(lineEdit()->text().toInt());
+    else setValue(lineEdit()->text());
+    emit timeCodeEditingFinished();
 }
 
 #include <timecodedisplay.moc>