]> git.sesse.net Git - kdenlive/blob - src/keyframehelper.cpp
Keyframe in composite transitions can be moved:
[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 #include <QApplication>
30
31
32 KeyframeHelper::KeyframeHelper(QWidget *parent) :
33         QWidget(parent),
34         m_geom(NULL),
35         m_position(0),
36         m_scale(0),
37         m_movingKeyframe(false)
38 {
39     setFont(KGlobalSettings::toolBarFont());
40 }
41
42 // virtual
43 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
44 {
45     if (m_geom != NULL && (event->y() < height() / 2) && event->button() == Qt::LeftButton) {
46         // check if we want to move a keyframe
47         int mousePos = qMax((int)(event->x() / m_scale - 5), 0);
48         Mlt::GeometryItem item;
49         if (m_geom->next_key(&item, mousePos) == 0) {
50             if (item.frame() - mousePos < 10) {
51                 m_movingItem.x(item.x());
52                 m_movingItem.y(item.y());
53                 m_movingItem.w(item.w());
54                 m_movingItem.h(item.h());
55                 m_movingItem.mix(item.mix());
56                 m_geom->remove(item.frame());
57                 m_dragStart = event->pos();
58                 m_movingKeyframe = true;
59                 return;
60             }
61         }
62     }
63     m_position = event->x() / m_scale;
64     emit positionChanged(m_position);
65     update();
66 }
67
68 // virtual
69 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
70 {
71     if (m_movingKeyframe) {
72         if (!m_dragStart.isNull() && (event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
73         m_dragStart = QPoint();
74         int pos = qMax(0, (int)(event->x() / m_scale));
75         pos = qMin(m_length, pos);
76         m_movingItem.frame(pos);
77         update();
78         return;
79     }
80     m_position = event->x() / m_scale;
81     m_position = qMax(0, m_position);
82     m_position = qMin(m_length, m_position);
83     emit positionChanged(m_position);
84     update();
85 }
86
87 // virtual
88 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
89 {
90     if (m_movingKeyframe) {
91         m_geom->insert(m_movingItem);
92         m_movingKeyframe = false;
93         emit keyframeMoved(m_position);
94         return;
95     }
96     QWidget::mouseReleaseEvent(event);
97 }
98
99 // virtual
100 void KeyframeHelper::wheelEvent(QWheelEvent * e)
101 {
102     if (e->delta() < 0) m_position = m_position - 1;
103     else m_position = m_position + 1;
104     m_position = qMax(0, m_position);
105     m_position = qMin(m_length, m_position);
106     emit positionChanged(m_position);
107     update();
108     /*    int delta = 1;
109         if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
110         if (e->delta() < 0) delta = 0 - delta;
111         m_view->moveCursorPos(delta);
112     */
113 }
114
115 // virtual
116 void KeyframeHelper::paintEvent(QPaintEvent *e)
117 {
118     QStylePainter p(this);
119     const QRectF clipRect = e->rect();
120     p.setClipRect(clipRect);
121     m_scale = (double) width() / m_length;
122     if (m_geom != NULL) {
123         int pos = 0;
124         p.setPen(QColor(255, 20, 20));
125         p.setBrush(QColor(255, 20, 20));
126         Mlt::GeometryItem item;
127         while (true) {
128             if (m_geom->next_key(&item, pos) == 1) break;
129             pos = item.frame();
130             int scaledPos = pos * m_scale;
131             // draw keyframes
132             p.drawLine(scaledPos, 6, scaledPos, 10);
133             // draw pointer
134             QPolygon pa(3);
135             pa.setPoints(3, scaledPos - 4, 0, scaledPos + 4, 0, scaledPos, 4);
136             p.drawPolygon(pa);
137             //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
138             pos++;
139         }
140         if (m_movingKeyframe) {
141             int scaledPos = (int)(m_movingItem.frame() * m_scale);
142             // draw keyframes
143             p.drawLine(scaledPos, 6, scaledPos, 10);
144             // draw pointer
145             QPolygon pa(3);
146             pa.setPoints(3, scaledPos - 4, 0, scaledPos + 4, 0, scaledPos, 4);
147             p.drawPolygon(pa);
148         }
149     }
150     p.setPen(palette().dark().color());
151     p.drawLine(clipRect.x(), 5, clipRect.right(), 5);
152
153     // draw pointer
154     QPolygon pa(3);
155     const int cursor = m_position * m_scale;
156     pa.setPoints(3, cursor - 5, 11, cursor + 5, 11, cursor, 6);
157     p.setBrush(palette().dark().color());
158     p.drawPolygon(pa);
159
160
161 }
162
163 int KeyframeHelper::value() const
164 {
165     return m_position;
166 }
167
168 void KeyframeHelper::setValue(const int pos)
169 {
170     if (pos == m_position || m_geom == NULL) return;
171     m_position = pos;
172     update();
173 }
174
175 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
176 {
177     m_geom = geom;
178     m_length = length;
179     update();
180 }
181
182 #include "keyframehelper.moc"