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>
32 TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
39 lineedit->setFont(KGlobalSettings::toolBarFont());
40 QFontMetrics fm = lineedit->fontMetrics();
41 lineedit->setMaximumWidth(fm.width("88:88:88:888"));
42 setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
44 setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
46 connect(uparrow, SIGNAL(clicked()), this, SLOT(slotValueUp()));
47 connect(downarrow, SIGNAL(clicked()), this, SLOT(slotValueDown()));
48 connect(lineedit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
49 connect(lineedit, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(slotCursorPositionChanged(int, int)));
52 void TimecodeDisplay::slotValueUp()
57 lineedit->clearFocus();
58 emit editingFinished();
61 void TimecodeDisplay::slotValueDown()
66 lineedit->clearFocus();
67 emit editingFinished();
70 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
72 if (!init && m_frametimecode == frametimecode) return;
74 m_frametimecode = frametimecode;
75 if (m_frametimecode) {
76 QIntValidator *valid = new QIntValidator(lineedit);
78 lineedit->setValidator(valid);
80 lineedit->setValidator(m_timecode.validator());
85 void TimecodeDisplay::slotUpdateTimeCodeFormat()
87 setTimeCodeFormat(KdenliveSettings::frametimecode());
90 void TimecodeDisplay::updateTimeCode(Timecode t)
93 setTimeCodeFormat(KdenliveSettings::frametimecode());
96 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
98 if (e->key() == Qt::Key_Up)
100 else if (e->key() == Qt::Key_Down)
103 QWidget::keyPressEvent(e);
106 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
115 int TimecodeDisplay::maximum() const
120 int TimecodeDisplay::minimum() const
125 int TimecodeDisplay::getValue() const
127 if (m_frametimecode) return lineedit->text().toInt();
128 else return m_timecode.getFrameCount(lineedit->text());
131 GenTime TimecodeDisplay::gentime() const
133 return GenTime(getValue(), m_timecode.fps());
136 Timecode TimecodeDisplay::timecode() const
141 void TimecodeDisplay::setRange(int min, int max)
147 void TimecodeDisplay::setValue(const QString &value)
149 setValue(m_timecode.getFrameCount(value));
152 void TimecodeDisplay::setValue(int value)
154 if (value < m_minimum)
156 if (m_maximum > m_minimum && value > m_maximum)
159 if (value == getValue() && !lineedit->text().isEmpty()) return;
160 downarrow->setEnabled(value > m_minimum);
161 uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
164 lineedit->setText(QString::number(value));
166 QString v = m_timecode.getTimecodeFromFrames(value);
167 lineedit->setText(v);
171 void TimecodeDisplay::setValue(GenTime value)
173 setValue(m_timecode.getTimecode(value));
176 void TimecodeDisplay::slotCursorPositionChanged(int oldPos, int newPos)
178 lineedit->blockSignals(true);
179 QString text = lineedit->text();
181 if (newPos < text.size() && !text.at(newPos).isDigit()) {
182 // char at newPos is a separator (':' or ';')
184 // make it possible move the cursor backwards at separators
185 if (newPos == oldPos - 1)
186 lineedit->setSelection(newPos, -1);
188 lineedit->setSelection(newPos + 2, -1);
189 } else if (newPos < text.size()) {
190 lineedit->setSelection(newPos + 1, -1);
192 lineedit->setSelection(newPos, -1);
195 lineedit->blockSignals(false);
198 #include <timecodedisplay.moc>