]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Fix monitor timecode display with NTSC profiles:
[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     slotUpdateTimeCodeFormat();
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 = value();
54     val++;
55     setValue(val);
56     lineedit->clearFocus();
57     emit editingFinished();
58 }
59
60 void TimecodeDisplay::slotValueDown()
61 {
62     int val = value();
63     val--;
64     setValue(val);
65     lineedit->clearFocus();
66     emit editingFinished();
67 }
68
69 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode)
70 {
71     int val = value();
72     m_frametimecode = frametimecode;
73     lineedit->setInputMask("");
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::value() const
125 {
126     int frames;
127     if (m_frametimecode) frames = lineedit->text().toInt();
128     else frames = m_timecode.getFrameCount(lineedit->text());
129     return frames;
130 }
131
132 GenTime TimecodeDisplay::gentime() const
133 {
134     return GenTime(value(), m_timecode.fps());
135 }
136
137 Timecode TimecodeDisplay::timecode() const
138 {
139     return m_timecode;
140 }
141
142 void TimecodeDisplay::setRange(int min, int max)
143 {
144     m_minimum = min;
145     m_maximum = max;
146 }
147
148 void TimecodeDisplay::setValue(const QString &value)
149 {
150     setValue(m_timecode.getFrameCount(value));
151 }
152
153 void TimecodeDisplay::setValue(int value)
154 {
155     if (value < m_minimum)
156         value = m_minimum;
157     if (m_maximum > m_minimum && value > m_maximum)
158         value = m_maximum;
159
160     downarrow->setEnabled(value > m_minimum);
161     uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
162
163     if (m_frametimecode)
164         lineedit->setText(QString::number(value));
165     else
166         lineedit->setText(m_timecode.getTimecodeFromFrames(value));
167
168     //emit valueChanged(value, true);
169 }
170
171 void TimecodeDisplay::setValue(GenTime value)
172 {
173     setValue(m_timecode.getTimecode(value));
174 }
175
176 #include <timecodedisplay.moc>