]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Fix build with Qt < 4.6:
[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_minimum(0),
38         m_maximum(-1)
39 {
40     lineEdit()->setFont(KGlobalSettings::toolBarFont());
41     lineEdit()->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
42     QFontMetrics fm = lineEdit()->fontMetrics();
43 #if QT_VERSION >= 0x040600
44     setMinimumWidth(fm.width("88:88:88:88888888") + contentsMargins().right() + contentsMargins().right());
45 #else
46     int left, top, right, bottom;
47     getContentsMargins(&left, &top, &right, &bottom);
48     setMinimumWidth(fm.width("88:88:88:88888888") + left + right);
49 #endif
50     setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
51     setAccelerated(true);
52
53     setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
54
55     connect(lineEdit(), SIGNAL(returnPressed()), this, SLOT(slotEditingFinished()));
56     connect(lineEdit(), SIGNAL(cursorPositionChanged(int, int)), this, SLOT(slotCursorPositionChanged(int, int)));
57 }
58
59 // virtual protected
60 QAbstractSpinBox::StepEnabled TimecodeDisplay::stepEnabled () const
61 {
62     QAbstractSpinBox::StepEnabled result = QAbstractSpinBox::StepNone;
63     if (getValue() > m_minimum) result |= QAbstractSpinBox::StepDownEnabled;
64     if (m_maximum == -1 || getValue() < m_maximum) result |= QAbstractSpinBox::StepUpEnabled;
65     return result;
66 }
67
68 // virtual
69 void TimecodeDisplay::stepBy(int steps)
70 {
71     int val = getValue();
72     val += steps;
73     setValue(val);
74     emit editingFinished();
75 }
76
77 void TimecodeDisplay::setTimeCodeFormat(bool frametimecode, bool init)
78 {
79     if (!init && m_frametimecode == frametimecode) return;
80     int val = getValue();
81     m_frametimecode = frametimecode;
82     if (m_frametimecode) {
83         QIntValidator *valid = new QIntValidator(lineEdit());
84         valid->setBottom(0);
85         lineEdit()->setValidator(valid);
86     } else {
87         lineEdit()->setValidator(m_timecode.validator());
88     }
89     setValue(val);
90 }
91
92 void TimecodeDisplay::slotUpdateTimeCodeFormat()
93 {
94     setTimeCodeFormat(KdenliveSettings::frametimecode());
95 }
96
97 void TimecodeDisplay::updateTimeCode(Timecode t)
98 {
99     m_timecode = t;
100     setTimeCodeFormat(KdenliveSettings::frametimecode());
101 }
102
103 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
104 {
105     if (e->key() == Qt::Key_Return)
106         slotEditingFinished();
107     else
108         QAbstractSpinBox::keyPressEvent(e);
109 }
110
111 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
112 {
113     QAbstractSpinBox::mouseReleaseEvent(e);
114     if (!lineEdit()->underMouse()) {
115         clearFocus();
116     }
117 }
118
119 /*
120 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
121 {
122     if (e->delta() > 0)
123         slotValueUp();
124     else
125         slotValueDown();
126 }*/
127
128
129 int TimecodeDisplay::maximum() const
130 {
131     return m_maximum;
132 }
133
134 int TimecodeDisplay::minimum() const
135 {
136     return m_minimum;
137 }
138
139 int TimecodeDisplay::getValue() const
140 {
141     if (m_frametimecode) return lineEdit()->text().toInt();
142     else return m_timecode.getFrameCount(lineEdit()->text());
143 }
144
145 GenTime TimecodeDisplay::gentime() const
146 {
147     return GenTime(getValue(), m_timecode.fps());
148 }
149
150 Timecode TimecodeDisplay::timecode() const
151 {
152     return m_timecode;
153 }
154
155 void TimecodeDisplay::setRange(int min, int max)
156 {
157     m_minimum = min;
158     m_maximum = max;
159 }
160
161 void TimecodeDisplay::setValue(const QString &value)
162 {
163     setValue(m_timecode.getFrameCount(value));
164 }
165
166 void TimecodeDisplay::setValue(int value)
167 {
168     if (value < m_minimum)
169         value = m_minimum;
170     if (m_maximum > m_minimum && value > m_maximum)
171         value = m_maximum;
172
173     if (value == getValue() && !lineEdit()->text().isEmpty()) return;
174     //downarrow->setEnabled(value > m_minimum);
175     //uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
176
177     if (m_frametimecode)
178         lineEdit()->setText(QString::number(value));
179     else {
180         QString v = m_timecode.getTimecodeFromFrames(value);
181         lineEdit()->setText(v);
182     }
183 }
184
185 void TimecodeDisplay::setValue(GenTime value)
186 {
187     setValue(m_timecode.getTimecode(value));
188 }
189
190 void TimecodeDisplay::slotCursorPositionChanged(int oldPos, int newPos)
191 {
192     if (!lineEdit()->hasFocus()) return;
193     lineEdit()->blockSignals(true);
194     QString text = lineEdit()->text();
195
196     if (newPos < text.size() && !text.at(newPos).isDigit()) {
197         // char at newPos is a separator (':' or ';')
198
199         // make it possible move the cursor backwards at separators
200         if (newPos == oldPos - 1)
201             lineEdit()->setSelection(newPos, -1);
202         else
203             lineEdit()->setSelection(newPos + 2, -1);
204     } else if (newPos < text.size()) {
205         lineEdit()->setSelection(newPos + 1, -1);
206     } else {
207         lineEdit()->setSelection(newPos, -1);
208     }
209
210     lineEdit()->blockSignals(false);
211 }
212
213 void TimecodeDisplay::slotEditingFinished()
214 {
215     clearFocus();
216     lineEdit()->deselect();
217     emit editingFinished();
218 }
219
220 #include <timecodedisplay.moc>