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);
56 setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
58 connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
62 QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
64 QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
65 if (m_value > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
66 if (m_maximum == -1 || m_value < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
71 void TimecodeDisplay::stepBy(int steps)
73 int val = m_value + steps;
77 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
79 if (!init && m_frametimecode == frametimecode) return;
80 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 || e->key() == Qt::Key_Enter) {
108 e->setAccepted(true);
112 QAbstractSpinBox::keyPressEvent(e);
115 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
117 QAbstractSpinBox::mouseReleaseEvent(e);
118 if (!lineEdit()->underMouse()) {
124 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
126 QAbstractSpinBox::wheelEvent(e);
131 int TimecodeDisplay::maximum() const
136 int TimecodeDisplay::minimum() const
141 int TimecodeDisplay::getValue() const
146 GenTime TimecodeDisplay::gentime() const
148 return GenTime(m_value, m_timecode.fps());
151 Timecode TimecodeDisplay::timecode() const
156 void TimecodeDisplay::setRange(int min, int max)
162 void TimecodeDisplay::setValue(const QString &value)
164 setValue(m_timecode.getFrameCount(value));
167 void TimecodeDisplay::setValue(int value)
169 if (value < m_minimum)
171 if (m_maximum > m_minimum && value > m_maximum)
173 if (value == m_value && !lineEdit()->text().isEmpty()) return;
177 lineEdit()->setText(QString::number(value));
179 QString v = m_timecode.getTimecodeFromFrames(value);
180 lineEdit()->setText(v);
184 void TimecodeDisplay::setValue(GenTime value)
186 setValue((int) value.frames(m_timecode.fps()));
190 void TimecodeDisplay::slotEditingFinished()
192 lineEdit()->deselect();
193 if (m_frametimecode) setValue(lineEdit()->text().toInt());
194 else setValue(lineEdit()->text());
195 emit timeCodeEditingFinished();
198 #include <timecodedisplay.moc>