]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Fix timecode widget sometimes hard to edit
[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     setTimeCodeFormat(KdenliveSettings::frametimecode(), true);
55
56     connect(lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
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         lineEdit()->setInputMask(QString());
87     } else {
88         lineEdit()->setValidator(0);
89         lineEdit()->setInputMask(m_timecode.mask());
90     }
91     setValue(val);
92 }
93
94 void TimecodeDisplay::slotUpdateTimeCodeFormat()
95 {
96     setTimeCodeFormat(KdenliveSettings::frametimecode());
97 }
98
99 void TimecodeDisplay::updateTimeCode(Timecode t)
100 {
101     m_timecode = t;
102     setTimeCodeFormat(KdenliveSettings::frametimecode());
103 }
104
105 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
106 {
107     if (e->key() == Qt::Key_Return)
108         slotEditingFinished();
109     else
110         QAbstractSpinBox::keyPressEvent(e);
111 }
112
113 void TimecodeDisplay::mouseReleaseEvent(QMouseEvent *e)
114 {
115     QAbstractSpinBox::mouseReleaseEvent(e);
116     if (!lineEdit()->underMouse()) {
117         clearFocus();
118     }
119 }
120
121 /*
122 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
123 {
124     if (e->delta() > 0)
125         slotValueUp();
126     else
127         slotValueDown();
128 }*/
129
130
131 int TimecodeDisplay::maximum() const
132 {
133     return m_maximum;
134 }
135
136 int TimecodeDisplay::minimum() const
137 {
138     return m_minimum;
139 }
140
141 int TimecodeDisplay::getValue() const
142 {
143     if (m_frametimecode) return lineEdit()->text().toInt();
144     else return m_timecode.getFrameCount(lineEdit()->text());
145 }
146
147 GenTime TimecodeDisplay::gentime() const
148 {
149     return GenTime(getValue(), m_timecode.fps());
150 }
151
152 Timecode TimecodeDisplay::timecode() const
153 {
154     return m_timecode;
155 }
156
157 void TimecodeDisplay::setRange(int min, int max)
158 {
159     m_minimum = min;
160     m_maximum = max;
161 }
162
163 void TimecodeDisplay::setValue(const QString &value)
164 {
165     setValue(m_timecode.getFrameCount(value));
166 }
167
168 void TimecodeDisplay::setValue(int value)
169 {
170     if (value < m_minimum)
171         value = m_minimum;
172     if (m_maximum > m_minimum && value > m_maximum)
173         value = m_maximum;
174
175     if (value == getValue() && !lineEdit()->text().isEmpty()) return;
176     //downarrow->setEnabled(value > m_minimum);
177     //uparrow->setEnabled(m_maximum < m_minimum || value < m_maximum);
178
179     if (m_frametimecode)
180         lineEdit()->setText(QString::number(value));
181     else {
182         QString v = m_timecode.getTimecodeFromFrames(value);
183         lineEdit()->setText(v);
184     }
185 }
186
187 void TimecodeDisplay::setValue(GenTime value)
188 {
189     setValue(m_timecode.getTimecode(value));
190 }
191
192
193 void TimecodeDisplay::slotEditingFinished()
194 {
195     clearFocus();
196     lineEdit()->deselect();
197     emit editingFinished();
198 }
199
200 #include <timecodedisplay.moc>