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(returnPressed()), this, SLOT(slotEditingFinished()));
57 connect(lineEdit(), SIGNAL(cursorPositionChanged(int, int)), this, SLOT(slotCursorPositionChanged(int, int)));
61 QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
63 QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
64 if (getValue() > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
65 if (m_maximum == -1 || getValue() < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
70 void TimecodeDisplay::stepBy(int steps)
75 emit editingFinished();
78 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
80 if (!init && m_frametimecode == frametimecode) return;
82 m_frametimecode = frametimecode;
83 if (m_frametimecode) {
84 QIntValidator *valid = new QIntValidator(lineEdit());
86 lineEdit()->setValidator(valid);
88 lineEdit()->setValidator(m_timecode.validator());
93 void TimecodeDisplay::slotUpdateTimeCodeFormat()
95 setTimeCodeFormat(KdenliveSettings::frametimecode());
98 void TimecodeDisplay::updateTimeCode(Timecode t)
101 setTimeCodeFormat(KdenliveSettings::frametimecode());
104 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
106 if (e->key() == Qt::Key_Return)
107 slotEditingFinished();
109 QAbstractSpinBox::keyPressEvent(e);
112 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
114 QAbstractSpinBox::mouseReleaseEvent(e);
115 if (!lineEdit()->underMouse()) {
121 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
130 int TimecodeDisplay::maximum() const
135 int TimecodeDisplay::minimum() const
140 int TimecodeDisplay::getValue() const
142 if (m_frametimecode) return lineEdit()->text().toInt();
143 else return m_timecode.getFrameCount(lineEdit()->text());
146 GenTime TimecodeDisplay::gentime() const
148 return GenTime(getValue(), 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)
174 if (value == getValue() && !lineEdit()->text().isEmpty()) return;
175 //downarrow->setEnabled(value > m_minimum);
176 //uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
179 lineEdit()->setText(QString::number(value));
181 QString v = m_timecode.getTimecodeFromFrames(value);
182 lineEdit()->setText(v);
186 void TimecodeDisplay::setValue(GenTime value)
188 setValue(m_timecode.getTimecode(value));
191 void TimecodeDisplay::slotCursorPositionChanged(int oldPos, int newPos)
193 if (!lineEdit()->hasFocus()) return;
194 lineEdit()->blockSignals(true);
195 QString text = lineEdit()->text();
197 if (newPos < text.size() && !text.at(newPos).isDigit()) {
198 // char at newPos is a separator (':' or ';')
200 // make it possible move the cursor backwards at separators
201 if (newPos == oldPos - 1)
202 lineEdit()->setSelection(newPos, -1);
204 lineEdit()->setSelection(newPos + 2, -1);
205 } else if (newPos < text.size()) {
206 lineEdit()->setSelection(newPos + 1, -1);
208 lineEdit()->setSelection(newPos, -1);
211 lineEdit()->blockSignals(false);
214 void TimecodeDisplay::slotEditingFinished()
217 lineEdit()->deselect();
218 emit editingFinished();
221 #include <timecodedisplay.moc>