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 (getValue() > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
66 if (m_maximum == -1 || getValue() < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
71 void TimecodeDisplay::stepBy(int steps)
76 emit editingFinished();
79 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
81 if (!init && m_frametimecode == frametimecode) return;
83 m_frametimecode = frametimecode;
84 if (m_frametimecode) {
85 QIntValidator *valid = new QIntValidator(lineEdit());
87 lineEdit()->setValidator(valid);
88 lineEdit()->setInputMask(QString());
90 lineEdit()->setValidator(0);
91 lineEdit()->setInputMask(m_timecode.mask());
96 void TimecodeDisplay::slotUpdateTimeCodeFormat()
98 setTimeCodeFormat(KdenliveSettings::frametimecode());
101 void TimecodeDisplay::updateTimeCode(Timecode t)
104 setTimeCodeFormat(KdenliveSettings::frametimecode());
107 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
109 if (e->key() == Qt::Key_Return)
110 slotEditingFinished();
112 QAbstractSpinBox::keyPressEvent(e);
115 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
117 QAbstractSpinBox::mouseReleaseEvent(e);
118 if (!lineEdit()->underMouse()) {
124 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
133 int TimecodeDisplay::maximum() const
138 int TimecodeDisplay::minimum() const
143 int TimecodeDisplay::getValue() const
145 if (m_frametimecode) return lineEdit()->text().toInt();
146 else return m_timecode.getFrameCount(lineEdit()->text());
149 GenTime TimecodeDisplay::gentime() const
151 return GenTime(getValue(), m_timecode.fps());
154 Timecode TimecodeDisplay::timecode() const
159 void TimecodeDisplay::setRange(int min, int max)
165 void TimecodeDisplay::setValue(const QString &value)
167 setValue(m_timecode.getFrameCount(value));
170 void TimecodeDisplay::setValue(int value)
172 if (value < m_minimum)
174 if (m_maximum > m_minimum && value > m_maximum)
177 if (value == getValue() && !lineEdit()->text().isEmpty()) return;
178 //downarrow->setEnabled(value > m_minimum);
179 //uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
182 lineEdit()->setText(QString::number(value));
184 QString v = m_timecode.getTimecodeFromFrames(value);
185 lineEdit()->setText(v);
189 void TimecodeDisplay::setValue(GenTime value)
191 setValue(m_timecode.getTimecode(value));
195 void TimecodeDisplay::slotEditingFinished()
198 lineEdit()->deselect();
199 emit editingFinished();
202 #include <timecodedisplay.moc>