]> git.sesse.net Git - kdenlive/blob - src/keyframehelper.cpp
04a2c02b06da671f3dc41d15292428b603a4b197
[kdenlive] / src / keyframehelper.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program 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         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "keyframehelper.h"
22 #include "definitions.h"
23
24 #include <KDebug>
25 #include <KGlobalSettings>
26
27 #include <QMouseEvent>
28 #include <QStylePainter>
29
30
31 KeyframeHelper::KeyframeHelper(QWidget *parent)
32         : QWidget(parent), m_geom(NULL), m_position(0), m_scale(0)
33 {
34     setFont(KGlobalSettings::toolBarFont());
35 }
36
37 // virtual
38 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
39 {
40     m_position = event->x() / m_scale;
41     emit positionChanged(m_position);
42     update();
43 }
44
45 // virtual
46 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
47 {
48     m_position = event->x() / m_scale;
49     m_position = qMax(0, m_position);
50     m_position = qMin(m_length, m_position);
51     emit positionChanged(m_position);
52     update();
53 }
54
55
56 // virtual
57 void KeyframeHelper::wheelEvent(QWheelEvent * e)
58 {
59     if (e->delta() < 0) m_position = m_position - 1;
60     else m_position = m_position + 1;
61     m_position = qMax(0, m_position);
62     m_position = qMin(m_length, m_position);
63     emit positionChanged(m_position);
64     update();
65     /*    int delta = 1;
66         if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
67         if (e->delta() < 0) delta = 0 - delta;
68         m_view->moveCursorPos(delta);
69     */
70 }
71
72 // virtual
73 void KeyframeHelper::paintEvent(QPaintEvent *e)
74 {
75     QStylePainter p(this);
76     const QRectF clipRect = e->rect();
77     p.setClipRect(clipRect);
78     m_scale = (double) width() / m_length;
79     if (m_geom != NULL) {
80         int pos = 0;
81         p.setPen(QColor(255, 20, 20));
82         Mlt::GeometryItem item;
83         while (true) {
84             if (m_geom->next_key(&item, pos) == 1) break;
85             pos = item.frame();
86             int scaledPos = pos * m_scale;
87             // draw keyframes
88             p.drawLine(scaledPos, 6, scaledPos, 10);
89             // draw pointer
90             QPolygon pa(3);
91             pa.setPoints(3, scaledPos - 4, 0, scaledPos + 4, 0, scaledPos, 4);
92             p.setBrush(QColor(255, 20, 20));
93             p.drawPolygon(pa);
94             //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
95             pos++;
96         }
97     }
98     p.setPen(palette().dark().color());
99     p.drawLine(clipRect.x(), 5, clipRect.right(), 5);
100
101     // draw pointer
102     QPolygon pa(3);
103     const int cursor = m_position * m_scale;
104     pa.setPoints(3, cursor - 5, 11, cursor + 5, 11, cursor, 6);
105     p.setBrush(palette().dark().color());
106     p.drawPolygon(pa);
107
108
109 }
110
111 int KeyframeHelper::value() const
112 {
113     return m_position;
114 }
115
116 void KeyframeHelper::setValue(const int pos)
117 {
118     if (pos == m_position || m_geom == NULL) return;
119     m_position = pos;
120     update();
121 }
122
123 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
124 {
125     m_geom = geom;
126     m_length = length;
127     update();
128 }
129
130 #include "keyframehelper.moc"