]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Insert Space Dialog:
[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::keyPressEvent(QKeyEvent *e)
90 {
91     if (e->key() == Qt::Key_Up)
92         slotValueUp();
93     else if (e->key() == Qt::Key_Down)
94         slotValueDown();
95     else
96         QWidget::keyPressEvent(e);
97 }
98
99 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
100 {
101     if (e->delta() > 0)
102         slotValueUp();
103     else
104         slotValueDown();
105 }
106
107
108 int TimecodeDisplay::maximum() const
109 {
110     return m_maximum;
111 }
112
113 int TimecodeDisplay::minimum() const
114 {
115     return m_minimum;
116 }
117
118 int TimecodeDisplay::value() const
119 {
120     int frames;
121     if (m_frametimecode) frames = lineedit->text().toInt();
122     else frames = m_timecode.getFrameCount(lineedit->text());
123     return frames;
124 }
125
126 GenTime TimecodeDisplay::gentime() const
127 {
128     return GenTime(value(), m_timecode.fps());
129 }
130
131 Timecode TimecodeDisplay::timecode() const
132 {
133     return m_timecode;
134 }
135
136 void TimecodeDisplay::setRange(int min, int max)
137 {
138     m_minimum = min;
139     m_maximum = max;
140 }
141
142 void TimecodeDisplay::setValue(const QString &value)
143 {
144     setValue(m_timecode.getFrameCount(value));
145 }
146
147 void TimecodeDisplay::setValue(int value)
148 {
149     if (value < m_minimum)
150         value = m_minimum;
151     if (m_maximum > m_minimum && value > m_maximum)
152         value = m_maximum;
153
154     downarrow->setEnabled(value > m_minimum);
155     uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
156
157     if (m_frametimecode)
158         lineedit->setText(QString::number(value));
159     else
160         lineedit->setText(m_timecode.getTimecodeFromFrames(value));
161
162     //emit valueChanged(value, true);
163 }
164
165 void TimecodeDisplay::setValue(GenTime value)
166 {
167     setValue(m_timecode.getTimecode(value));
168 }
169
170 #include <timecodedisplay.moc>