]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Try to fix timecode error:
[kdenlive] / src / timecodedisplay.cpp
1 /* This file is part of the KDE project
2    Copyright (c) 2010 Jean-Baptiste Mardelle <jb@kdenlive.org>
3
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.
8
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.
13
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.
18 */
19
20 #include "timecodedisplay.h"
21 #include "kdenlivesettings.h"
22
23 #include <QLineEdit>
24 #include <QValidator>
25 #include <QMouseEvent>
26
27 #include <kglobal.h>
28 #include <klocale.h>
29 #include <kdebug.h>
30
31
32 TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
33         : QWidget(parent),
34         m_timecode(t),
35         m_minimum(0),
36         m_maximum(-1)
37 {
38     setupUi(this);
39     lineedit->setFont(KGlobalSettings::toolBarFont());
40     QFontMetrics fm = lineedit->fontMetrics();
41     lineedit->setMaximumWidth(fm.width("88:88:88:888"));
42     setSizePolicy(QSizePolicy::Maximum, QSizePolicy::MinimumExpanding);
43
44     setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
45
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 }
50
51 void TimecodeDisplay::slotValueUp()
52 {
53     int val = getValue();
54     val++;
55     setValue(val);
56     lineedit->clearFocus();
57     emit editingFinished();
58 }
59
60 void TimecodeDisplay::slotValueDown()
61 {
62     int val = getValue();
63     val--;
64     setValue(val);
65     lineedit->clearFocus();
66     emit editingFinished();
67 }
68
69 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
70 {
71     if (!init && m_frametimecode == frametimecode) return;
72     int val = getValue();
73     m_frametimecode = frametimecode;
74     if (m_frametimecode) {
75         QIntValidator *valid = new QIntValidator(lineedit);
76         valid->setBottom(0);
77         lineedit->setValidator(valid);
78     } else {
79         lineedit->setValidator(m_timecode.validator());
80     }
81     setValue(val);
82 }
83
84 void TimecodeDisplay::slotUpdateTimeCodeFormat()
85 {
86     setTimeCodeFormat(KdenliveSettings::frametimecode());
87 }
88
89 void TimecodeDisplay::updateTimeCode(Timecode t)
90 {
91     m_timecode = t;
92     setTimeCodeFormat(KdenliveSettings::frametimecode());
93 }
94
95 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
96 {
97     if (e->key() == Qt::Key_Up)
98         slotValueUp();
99     else if (e->key() == Qt::Key_Down)
100         slotValueDown();
101     else
102         QWidget::keyPressEvent(e);
103 }
104
105 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
106 {
107     if (e->delta() > 0)
108         slotValueUp();
109     else
110         slotValueDown();
111 }
112
113
114 int TimecodeDisplay::maximum() const
115 {
116     return m_maximum;
117 }
118
119 int TimecodeDisplay::minimum() const
120 {
121     return m_minimum;
122 }
123
124 int TimecodeDisplay::getValue() const
125 {
126     if (m_frametimecode) return lineedit->text().toInt();
127     else return m_timecode.getFrameCount(lineedit->text());
128 }
129
130 GenTime TimecodeDisplay::gentime() const
131 {
132     return GenTime(getValue(), m_timecode.fps());
133 }
134
135 Timecode TimecodeDisplay::timecode() const
136 {
137     return m_timecode;
138 }
139
140 void TimecodeDisplay::setRange(int min, int max)
141 {
142     m_minimum = min;
143     m_maximum = max;
144 }
145
146 void TimecodeDisplay::setValue(const QString &value)
147 {
148     setValue(m_timecode.getFrameCount(value));
149 }
150
151 void TimecodeDisplay::setValue(int value)
152 {
153     if (value < m_minimum)
154         value = m_minimum;
155     if (m_maximum > m_minimum && value > m_maximum)
156         value = m_maximum;
157
158     if (value == getValue()) return;
159     downarrow->setEnabled(value > m_minimum);
160     uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
161
162     if (m_frametimecode)
163         lineedit->setText(QString::number(value));
164     else {
165         QString v = m_timecode.getTimecodeFromFrames(value);
166         kDebug() << "// SETTING TO: " << value << " = " << v << "( " << m_timecode.fps();
167         lineedit->setText(v);
168     }
169 }
170
171 void TimecodeDisplay::setValue(GenTime value)
172 {
173     setValue(m_timecode.getTimecode(value));
174 }
175
176 #include <timecodedisplay.moc>