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