]> git.sesse.net Git - kdenlive/blob - src/timecodedisplay.cpp
Use new timecode widget for position parameter in effects
[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 <QSize>
24 #include <QStyle>
25 #include <QStylePainter>
26 #include <QStyleOptionSlider>
27 #include <QLineEdit>
28 #include <QValidator>
29 #include <QHBoxLayout>
30 #include <QFrame>
31 #include <QMouseEvent>
32 #include <QToolButton>
33
34 #include <kglobal.h>
35 #include <klocale.h>
36 #include <kdebug.h>
37
38
39 TimecodeDisplay::TimecodeDisplay(Timecode t, QWidget *parent)
40         : QWidget(parent),
41         m_minimum(0),
42         m_maximum(-1)
43 {
44     setupUi(this);
45     lineedit->setFont(KGlobalSettings::toolBarFont());
46     QFontMetrics fm = lineedit->fontMetrics();
47     lineedit->setMaximumWidth(fm.width("88:88:88:888"));
48     slotPrepareTimeCodeFormat(t);
49     connect(uparrow, SIGNAL(clicked()), this, SLOT(slotValueUp()));
50     connect(downarrow, SIGNAL(clicked()), this, SLOT(slotValueDown()));
51     connect(lineedit, SIGNAL(editingFinished()), this, SIGNAL(editingFinished()));
52 }
53
54 TimecodeDisplay::~TimecodeDisplay()
55 {
56 }
57
58 void TimecodeDisplay::slotValueUp()
59 {
60     int val = value();
61     val++;
62     if (m_maximum > -1 && val > m_maximum) val = m_maximum;
63     setValue(val);
64     lineedit->clearFocus();
65     emit editingFinished();
66 }
67
68 void TimecodeDisplay::slotValueDown()
69 {
70     int val = value();
71     val--;
72     if (val < m_minimum) val = m_minimum;
73     setValue(val);
74     lineedit->clearFocus();
75     emit editingFinished();
76 }
77
78 void TimecodeDisplay::slotPrepareTimeCodeFormat(Timecode t)
79 {
80     m_timecode = t;
81     m_frametimecode = KdenliveSettings::frametimecode();
82     QString val = lineedit->text();
83     lineedit->setInputMask("");
84     if (m_frametimecode) {
85         int frames = m_timecode.getFrameCount(lineedit->text());
86         QIntValidator *valid = new QIntValidator(lineedit);
87         valid->setBottom(0);
88         lineedit->setValidator(valid);
89         lineedit->setText(QString::number(frames));
90     } else {
91         int pos = lineedit->text().toInt();
92         lineedit->setValidator(m_timecode.validator());
93         lineedit->setText(m_timecode.getTimecodeFromFrames(pos));
94     }
95 }
96
97 void TimecodeDisplay::keyPressEvent(QKeyEvent *e)
98 {
99     if (e->key() == Qt::Key_Up) slotValueUp();
100     else if (e->key() == Qt::Key_Down) slotValueDown();
101     else QWidget::keyPressEvent(e);
102 }
103
104 void TimecodeDisplay::wheelEvent(QWheelEvent *e)
105 {
106     if (e->delta() > 0) slotValueUp();
107     else slotValueDown();
108 }
109
110
111 int TimecodeDisplay::maximum() const
112 {
113     return m_maximum;
114 }
115
116 int TimecodeDisplay::minimum() const
117 {
118     return m_minimum;
119 }
120
121 int TimecodeDisplay::value() const
122 {
123     int frames;
124     if (m_frametimecode) frames = lineedit->text().toInt();
125     else frames = m_timecode.getFrameCount(lineedit->text());
126     return frames;
127 }
128
129 Timecode TimecodeDisplay::timecode() const
130 {
131     return m_timecode;
132 }
133
134 void TimecodeDisplay::setRange(int min, int max)
135 {
136     m_minimum = min;
137     m_maximum = max;
138 }
139
140 void TimecodeDisplay::setValue(const QString &value)
141 {
142     if (m_frametimecode) {
143         lineedit->setText(QString::number(m_timecode.getFrameCount(value)));
144     } else lineedit->setText(value);
145 }
146
147 void TimecodeDisplay::setValue(int value)
148 {
149     /*    if (value < m_minimum)
150             value = m_minimum;
151         if (value > m_maximum)
152             value = m_maximum;*/
153     if (m_frametimecode) lineedit->setText(QString::number(value));
154     else lineedit->setText(m_timecode.getTimecodeFromFrames(value));
155
156     /*    setEditText(KGlobal::locale()->formatNumber(value, d->decimals));
157         d->slider->blockSignals(true);
158         d->slider->setValue(int((value - d->minimum) * 256 / (d->maximum - d->minimum) + 0.5));
159         d->slider->blockSignals(false);*/
160     //emit valueChanged(value, true);
161 }
162
163 #include <timecodedisplay.moc>