1 /* This file is part of the KDE project
2 Copyright (c) 2010 Jean-Baptiste Mardelle <jb@kdenlive.org>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #include "timecodedisplay.h"
21 #include "kdenlivesettings.h"
25 #include <QMouseEvent>
30 #include <krestrictedline.h>
31 #include <KColorScheme>
32 #include <KRestrictedLine>
34 TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
35 : QAbstractSpinBox(parent),
37 m_frametimecode(false),
41 lineEdit()->setFont(KGlobalSettings::toolBarFont());
42 lineEdit()->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
43 QFontMetrics fm = lineEdit()->fontMetrics();
44 #if QT_VERSION >= 0x040600
45 setMinimumWidth(fm.width("88:88:88:88888888") + contentsMargins().right() + contentsMargins().right());
47 int left, top, right, bottom;
48 getContentsMargins(&left, &top, &right, &bottom);
49 setMinimumWidth(fm.width("88:88:88:88888888") + left + right);
51 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
54 setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
56 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
60 QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
62 QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
63 if (getValue() > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
64 if (m_maximum == -1 || getValue() < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
69 void TimecodeDisplay::stepBy(int steps)
74 emit editingFinished();
77 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
79 if (!init && m_frametimecode == frametimecode) return;
81 m_frametimecode = frametimecode;
82 if (m_frametimecode) {
83 QIntValidator *valid = new QIntValidator(lineEdit());
85 lineEdit()->setValidator(valid);
86 lineEdit()->setInputMask(QString());
88 lineEdit()->setValidator(0);
89 lineEdit()->setInputMask(m_timecode.mask());
94 void TimecodeDisplay::slotUpdateTimeCodeFormat()
96 setTimeCodeFormat(KdenliveSettings::frametimecode());
99 void TimecodeDisplay::updateTimeCode(Timecode t)
102 setTimeCodeFormat(KdenliveSettings::frametimecode());
105 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
107 if (e->key() == Qt::Key_Return)
108 slotEditingFinished();
110 QAbstractSpinBox::keyPressEvent(e);
113 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
115 QAbstractSpinBox::mouseReleaseEvent(e);
116 if (!lineEdit()->underMouse()) {
122 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
131 int TimecodeDisplay::maximum() const
136 int TimecodeDisplay::minimum() const
141 int TimecodeDisplay::getValue() const
143 if (m_frametimecode) return lineEdit()->text().toInt();
144 else return m_timecode.getFrameCount(lineEdit()->text());
147 GenTime TimecodeDisplay::gentime() const
149 return GenTime(getValue(), m_timecode.fps());
152 Timecode TimecodeDisplay::timecode() const
157 void TimecodeDisplay::setRange(int min, int max)
163 void TimecodeDisplay::setValue(const QString &value)
165 setValue(m_timecode.getFrameCount(value));
168 void TimecodeDisplay::setValue(int value)
170 if (value < m_minimum)
172 if (m_maximum > m_minimum && value > m_maximum)
175 if (value == getValue() && !lineEdit()->text().isEmpty()) return;
176 //downarrow->setEnabled(value > m_minimum);
177 //uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
180 lineEdit()->setText(QString::number(value));
182 QString v = m_timecode.getTimecodeFromFrames(value);
183 lineEdit()->setText(v);
187 void TimecodeDisplay::setValue(GenTime value)
189 setValue(m_timecode.getTimecode(value));
193 void TimecodeDisplay::slotEditingFinished()
196 lineEdit()->deselect();
197 emit editingFinished();
200 #include <timecodedisplay.moc>