]> git.sesse.net Git - kdenlive/blob - src/keyframehelper.cpp
Keyframable rotation for affine transition
[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 "kdenlivesettings.h"
23 #include "definitions.h"
24
25 #include <KDebug>
26 #include <KGlobalSettings>
27 #include <KColorScheme>
28
29 #include <QMouseEvent>
30 #include <QStylePainter>
31 #include <QApplication>
32
33
34 KeyframeHelper::KeyframeHelper(QWidget *parent) :
35         QWidget(parent),
36         m_geom(NULL),
37         m_position(0),
38         m_scale(0),
39         m_movingKeyframe(false),
40         m_lineHeight(10),
41         m_drag(false),
42         m_hoverKeyframe(-1)
43 {
44     setFont(KGlobalSettings::toolBarFont());
45     setMouseTracking(true);
46     QPalette p = palette();
47     KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
48     m_selected = scheme.decoration(KColorScheme::HoverColor).color();
49     m_keyframe = scheme.foreground(KColorScheme::LinkText).color();
50     m_keyframebg = scheme.shade(KColorScheme::MidShade);
51 }
52
53 // virtual
54 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
55 {
56     m_hoverKeyframe = -1;
57     if (event->button() == Qt::LeftButton) m_drag = true;
58     else {
59         QWidget::mousePressEvent(event);
60         return;
61     }
62     if (m_geom != NULL && (event->y() < m_lineHeight)) {
63         // check if we want to move a keyframe
64         int mousePos = qMax((int)(event->x() / m_scale), 0);
65         Mlt::GeometryItem item;
66         if (m_geom->next_key(&item, mousePos) == 0) {
67             if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
68                 m_movingItem.x(item.x());
69                 m_movingItem.y(item.y());
70                 m_movingItem.w(item.w());
71                 m_movingItem.h(item.h());
72                 m_movingItem.mix(item.mix());
73                 m_movingItem.frame(item.frame());
74
75                 while (!m_extraMovingItems.isEmpty()) {
76                     Mlt::GeometryItem *gitem = m_extraMovingItems.takeFirst();
77                     delete gitem;
78                 }
79                 for (int i = 0; i < m_extraGeometries.count(); i++) {
80                     Mlt::GeometryItem *item2 = new Mlt::GeometryItem();
81                     if (m_extraGeometries.at(i)->next_key(item, mousePos) == 0) {
82                         item2->x(item.x());
83                         item2->frame(item.frame());
84                         m_extraMovingItems.append(item2);
85                     }
86                 }
87                 
88                 m_dragStart = event->pos();
89                 m_movingKeyframe = true;
90                 return;
91             }
92         }
93     }
94     m_position = event->x() / m_scale;
95     emit positionChanged(m_position);
96     update();
97 }
98
99 // virtual
100 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
101 {
102     if (!m_drag) {
103         if (m_geom != NULL && (event->y() < m_lineHeight)) {
104             // check if we want to move a keyframe
105             int mousePos = qMax((int)(event->x() / m_scale), 0);
106             Mlt::GeometryItem item;
107             if (m_geom->next_key(&item, mousePos) == 0) {
108                 if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
109                     if (m_hoverKeyframe == item.frame()) return;
110                     m_hoverKeyframe = item.frame();
111                     setCursor(Qt::PointingHandCursor);
112                     update();
113                     event->accept();
114                     return;
115                 }
116             }
117         }
118         if (m_hoverKeyframe != -1) {
119             m_hoverKeyframe = -1;
120             setCursor(Qt::ArrowCursor);
121             update();
122         }
123         event->accept();
124         return;
125     }
126     if (m_movingKeyframe) {
127         if (!m_dragStart.isNull()) {
128             if ((event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
129             m_dragStart = QPoint();
130             m_geom->remove(m_movingItem.frame());
131             for (int i = 0; i < m_extraGeometries.count(); i++)
132                 m_extraGeometries[i]->remove(m_movingItem.frame());
133         }
134         int pos = qBound(0, (int)(event->x() / m_scale), frameLength);
135         if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
136         m_movingItem.frame(pos);
137         for (int i = 0; i < m_extraMovingItems.count(); i++) {
138             m_extraMovingItems[i]->frame(pos);
139         }
140         update();
141         return;
142     }
143     m_position = event->x() / m_scale;
144     m_position = qMax(0, m_position);
145     m_position = qMin(frameLength, m_position);
146     emit positionChanged(m_position);
147     update();
148 }
149
150 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
151 {
152     if (m_geom != NULL && event->button() == Qt::LeftButton) {
153         // check if we want to move a keyframe
154         int mousePos = qMax((int)(event->x() / m_scale - 5), 0);
155         Mlt::GeometryItem item;
156         if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
157             // There is already a keyframe close to mouse click
158             emit removeKeyframe(item.frame());
159             return;
160         }
161         // add new keyframe
162         emit addKeyframe((int)(event->x() / m_scale));
163     }
164 }
165
166 // virtual
167 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
168 {
169     m_drag = false;
170     m_hoverKeyframe = -1;
171     setCursor(Qt::ArrowCursor);
172     if (m_movingKeyframe) {
173         m_geom->insert(m_movingItem);
174         m_movingKeyframe = false;
175
176         for (int i = 0; i < m_extraGeometries.count(); i++) {
177             m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
178         }
179         
180         emit keyframeMoved(m_position);
181         return;
182     }
183     QWidget::mouseReleaseEvent(event);
184 }
185
186 // virtual
187 void KeyframeHelper::wheelEvent(QWheelEvent * e)
188 {
189     if (e->delta() < 0)
190         --m_position;
191     else
192         ++m_position;
193     m_position = qMax(0, m_position);
194     m_position = qMin(frameLength, m_position);
195     emit positionChanged(m_position);
196     update();
197     /*    int delta = 1;
198         if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
199         if (e->delta() < 0) delta = 0 - delta;
200         m_view->moveCursorPos(delta);
201     */
202 }
203
204 // virtual
205 void KeyframeHelper::paintEvent(QPaintEvent *e)
206 {
207     QStylePainter p(this);
208     const QRectF clipRect = e->rect();
209     p.setClipRect(clipRect);
210     m_scale = (double) width() / frameLength;
211     if (m_geom != NULL) {
212         int pos = 0;
213         p.setPen(m_keyframe);
214         p.setBrush(m_keyframebg);
215         Mlt::GeometryItem item;
216         while (true) {
217             if (m_geom->next_key(&item, pos) == 1) break;
218             pos = item.frame();
219             if (pos == m_hoverKeyframe) {
220                 p.setBrush(m_selected);
221             }
222             int scaledPos = pos * m_scale;
223             // draw keyframes
224             p.drawLine(scaledPos, 9, scaledPos, 15);
225             // draw pointer
226             QPolygon pa(4);
227             pa.setPoints(4,
228                          scaledPos,     0,
229                          scaledPos - 4, 4,
230                          scaledPos,     8,
231                          scaledPos + 4, 4);
232             p.drawPolygon(pa);
233             //p.drawEllipse(scaledPos - 4, 0, 8, 8);
234             if (pos == m_hoverKeyframe) {
235                 p.setBrush(m_keyframebg);
236             }
237             //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
238             pos++;
239         }
240         
241         if (m_movingKeyframe) {
242             p.setBrush(m_selected);
243             int scaledPos = (int)(m_movingItem.frame() * m_scale);
244             // draw keyframes
245             p.drawLine(scaledPos, 9, scaledPos, 15);
246             // draw pointer
247             QPolygon pa(5);
248             pa.setPoints(4,
249                          scaledPos,     0,
250                          scaledPos - 4, 4,
251                          scaledPos,     8,
252                          scaledPos + 4, 4);
253             p.drawPolygon(pa);
254             p.setBrush(m_keyframe);
255         }
256     }
257     p.setPen(palette().dark().color());
258     p.drawLine(clipRect.x(), m_lineHeight, clipRect.right(), m_lineHeight);
259
260     // draw pointer
261     QPolygon pa(3);
262     const int cursor = m_position * m_scale;
263     pa.setPoints(3, cursor - 5, 16, cursor + 5, 16, cursor, 11);
264     p.setBrush(palette().dark().color());
265     p.drawPolygon(pa);
266 }
267
268 int KeyframeHelper::value() const
269 {
270     return m_position;
271 }
272
273 void KeyframeHelper::setValue(const int pos)
274 {
275     if (pos == m_position || m_geom == NULL) return;
276     m_position = pos;
277     update();
278 }
279
280 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
281 {
282     m_geom = geom;
283     frameLength = length;
284     while (!m_extraGeometries.isEmpty()) {
285         Mlt::Geometry *geom = m_extraGeometries.takeFirst();
286         delete geom;
287     }
288     update();
289 }
290
291 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
292 {
293     m_extraGeometries.append(geom);
294 }
295
296 #include "keyframehelper.moc"