]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Fix missing initiation of timecode widget.
[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 #include <krestrictedline.h>
31 #include <KColorScheme>
32 #include <KRestrictedLine>
33
34 TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
35         : QAbstractSpinBox(parent),
36         m_timecode(t),
37         m_frametimecode(false),
38         m_minimum(0),
39         m_maximum(-1)
40 {
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());
46 #else
47     int left, top, right, bottom;
48     getContentsMargins(&left, &top, &right, &bottom);
49     setMinimumWidth(fm.width("88:88:88:88888888") + left + right);
50 #endif
51     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
52     setAccelerated(true);
53
54     setValue(m_minimum);
55
56     setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
57
58     connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
59 }
60
61 // virtual protected
62 QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
63 {
64     QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
65     if (getValue() > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
66     if (m_maximum == -1 || getValue() < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
67     return result;
68 }
69
70 // virtual
71 void TimecodeDisplay::stepBy(int steps)
72 {
73     int val = getValue();
74     val += steps;
75     setValue(val);
76     emit editingFinished();
77 }
78
79 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
80 {
81     if (!init && m_frametimecode == frametimecode) return;
82     int val = getValue();
83     m_frametimecode = frametimecode;
84     if (m_frametimecode) {
85         QIntValidator *valid = new QIntValidator(lineEdit());
86         valid->setBottom(0);
87         lineEdit()->setValidator(valid);
88         lineEdit()->setInputMask(QString());
89     } else {
90         lineEdit()->setValidator(0);
91         lineEdit()->setInputMask(m_timecode.mask());
92     }
93     setValue(val);
94 }
95
96 void TimecodeDisplay::slotUpdateTimeCodeFormat()
97 {
98     setTimeCodeFormat(KdenliveSettings::frametimecode());
99 }
100
101 void TimecodeDisplay::updateTimeCode(Timecode t)
102 {
103     m_timecode = t;
104     setTimeCodeFormat(KdenliveSettings::frametimecode());
105 }
106
107 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
108 {
109     if (e->key() == Qt::Key_Return)
110         slotEditingFinished();
111     else
112         QAbstractSpinBox::keyPressEvent(e);
113 }
114
115 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
116 {
117     QAbstractSpinBox::mouseReleaseEvent(e);
118     if (!lineEdit()->underMouse()) {
119         clearFocus();
120     }
121 }
122
123 /*
124 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
125 {
126     if (e->delta() > 0)
127         slotValueUp();
128     else
129         slotValueDown();
130 }*/
131
132
133 int TimecodeDisplay::maximum() const
134 {
135     return m_maximum;
136 }
137
138 int TimecodeDisplay::minimum() const
139 {
140     return m_minimum;
141 }
142
143 int TimecodeDisplay::getValue() const
144 {
145     if (m_frametimecode) return lineEdit()->text().toInt();
146     else return m_timecode.getFrameCount(lineEdit()->text());
147 }
148
149 GenTime TimecodeDisplay::gentime() const
150 {
151     return GenTime(getValue(), m_timecode.fps());
152 }
153
154 Timecode TimecodeDisplay::timecode() const
155 {
156     return m_timecode;
157 }
158
159 void TimecodeDisplay::setRange(int min, int max)
160 {
161     m_minimum = min;
162     m_maximum = max;
163 }
164
165 void TimecodeDisplay::setValue(const QString &value)
166 {
167     setValue(m_timecode.getFrameCount(value));
168 }
169
170 void TimecodeDisplay::setValue(int value)
171 {
172     if (value < m_minimum)
173         value = m_minimum;
174     if (m_maximum > m_minimum && value > m_maximum)
175         value = m_maximum;
176
177     if (value == getValue() && !lineEdit()->text().isEmpty()) return;
178     //downarrow->setEnabled(value > m_minimum);
179     //uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
180
181     if (m_frametimecode)
182         lineEdit()->setText(QString::number(value));
183     else {
184         QString v = m_timecode.getTimecodeFromFrames(value);
185         lineEdit()->setText(v);
186     }
187 }
188
189 void TimecodeDisplay::setValue(GenTime value)
190 {
191     setValue(m_timecode.getTimecode(value));
192 }
193
194
195 void TimecodeDisplay::slotEditingFinished()
196 {
197     clearFocus();
198     lineEdit()->deselect();
199     emit editingFinished();
200 }
201
202 #include <timecodedisplay.moc>