]> git.sesse.net Git - kdenlive/blob - src/keyframehelper.cpp
Correctly refresh transition widget when resizing a transition, move last keyframe...
[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         int seekRequest = xPos / m_scale; 
104         m_drag = true;
105         if (seekRequest != m_position) {
106             m_seekPosition = seekRequest;
107             emit requestSeek(m_seekPosition);
108             update();
109         }
110     }
111 }
112
113 void KeyframeHelper::leaveEvent( QEvent * event )
114 {
115     Q_UNUSED(event);
116     if (m_hoverKeyframe != -1) {
117         m_hoverKeyframe = -1;
118         update();
119     }
120 }
121
122 // virtual
123 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
124 {
125     int xPos = event->x() - margin;
126     if (!m_drag) {
127         int mousePos = qMax((int)(xPos / m_scale), 0);
128         if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight) {
129             // Mouse over time cursor
130             if (m_hoverKeyframe != -2) {
131                 m_hoverKeyframe = -2;
132                 update();
133             }
134             event->accept();
135             return;
136         }
137         if (m_geom != NULL && (event->y() < m_lineHeight)) {
138             // check if we want to move a keyframe
139             Mlt::GeometryItem item;
140             if (m_geom->next_key(&item, mousePos) == 0) {
141                 if (qAbs(item.frame() * m_scale - xPos) < 4) {
142                     if (m_hoverKeyframe == item.frame()) return;
143                     m_hoverKeyframe = item.frame();
144                     setCursor(Qt::PointingHandCursor);
145                     update();
146                     event->accept();
147                     return;
148                 }
149             }
150         }
151         if (m_hoverKeyframe != -1) {
152             m_hoverKeyframe = -1;
153             setCursor(Qt::ArrowCursor);
154             update();
155         }
156         event->accept();
157         return;
158     }
159     if (m_movingKeyframe) {
160         if (!m_dragStart.isNull()) {
161             if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
162             m_dragStart = QPoint();
163             m_geom->remove(m_movingItem.frame());
164             for (int i = 0; i < m_extraGeometries.count(); i++)
165                 m_extraGeometries[i]->remove(m_movingItem.frame());
166         }
167         int pos = qBound(0, (int)(xPos / m_scale), frameLength);
168         if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
169         m_movingItem.frame(pos);
170         for (int i = 0; i < m_extraMovingItems.count(); i++) {
171             m_extraMovingItems[i]->frame(pos);
172         }
173         update();
174         return;
175     }
176     m_seekPosition = (int) (xPos / m_scale);
177     m_seekPosition = qMax(0, m_seekPosition);
178     m_seekPosition = qMin(frameLength, m_seekPosition);
179     m_hoverKeyframe = -2;
180     emit requestSeek(m_seekPosition);
181     update();
182 }
183
184 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
185 {
186     if (m_geom != NULL && event->button() == Qt::LeftButton) {
187         // check if we want to move a keyframe
188         int xPos = event->x() - margin;
189         int mousePos = qMax((int)(xPos / m_scale - 5), 0);
190         Mlt::GeometryItem item;
191         if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
192             // There is already a keyframe close to mouse click
193             emit removeKeyframe(item.frame());
194             return;
195         }
196         // add new keyframe
197         emit addKeyframe((int)(xPos / m_scale));
198     }
199 }
200
201 // virtual
202 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
203 {
204     m_drag = false;
205     setCursor(Qt::ArrowCursor);
206     m_hoverKeyframe = -1;
207     if (m_movingKeyframe) {
208         m_geom->insert(m_movingItem);
209         m_movingKeyframe = false;
210
211         for (int i = 0; i < m_extraGeometries.count(); i++) {
212             m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
213         }
214         
215         emit keyframeMoved(m_position);
216         return;
217     }
218     QWidget::mouseReleaseEvent(event);
219 }
220
221 // virtual
222 void KeyframeHelper::wheelEvent(QWheelEvent * e)
223 {
224     if (e->delta() < 0)
225         --m_position;
226     else
227         ++m_position;
228     m_position = qMax(0, m_position);
229     m_position = qMin(frameLength, m_position);
230     emit requestSeek(m_position);
231     update();
232     /*    int delta = 1;
233         if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
234         if (e->delta() < 0) delta = 0 - delta;
235         m_view->moveCursorPos(delta);
236     */
237 }
238
239 // virtual
240 void KeyframeHelper::paintEvent(QPaintEvent *e)
241 {
242     QStylePainter p(this);
243     const QRectF clipRect = e->rect();
244     p.setClipRect(clipRect);
245     m_scale = (double) (width() - 2 * margin) / frameLength;
246     if (m_geom != NULL) {
247         int pos = 0;
248         p.setPen(m_keyframe);
249         p.setBrush(m_keyframebg);
250         Mlt::GeometryItem item;
251         while (true) {
252             if (m_geom->next_key(&item, pos) == 1) break;
253             pos = item.frame();
254             if (pos == m_hoverKeyframe) {
255                 p.setBrush(m_selected);
256             }
257             int scaledPos = margin + pos * m_scale;
258             // draw keyframes
259             p.drawLine(scaledPos, 9, scaledPos, 15);
260             // draw pointer
261             QPolygon pa(4);
262             pa.setPoints(4,
263                          scaledPos,     0,
264                          scaledPos - 4, 4,
265                          scaledPos,     8,
266                          scaledPos + 4, 4);
267             p.drawPolygon(pa);
268             //p.drawEllipse(scaledPos - 4, 0, 8, 8);
269             if (pos == m_hoverKeyframe) {
270                 p.setBrush(m_keyframebg);
271             }
272             //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
273             pos++;
274         }
275         
276         if (m_movingKeyframe) {
277             p.setBrush(m_selected);
278             int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
279             // draw keyframes
280             p.drawLine(scaledPos, 9, scaledPos, 15);
281             // draw pointer
282             QPolygon pa(5);
283             pa.setPoints(4,
284                          scaledPos,     0,
285                          scaledPos - 4, 4,
286                          scaledPos,     8,
287                          scaledPos + 4, 4);
288             p.drawPolygon(pa);
289             p.setBrush(m_keyframe);
290         }
291     }
292     p.setPen(palette().dark().color());
293     p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
294     p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
295     p.drawLine(width() - margin, m_lineHeight - 3, width() - margin, m_lineHeight + 3);
296
297     // draw pointer
298     if (m_seekPosition != SEEK_INACTIVE) {
299         p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark());
300     }
301     QPolygon pa(3);
302     const int cursor = margin + m_position * m_scale;
303     pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
304     if (m_hoverKeyframe == -2)
305         p.setBrush(palette().highlight());
306     else
307         p.setBrush(palette().dark().color());
308     p.drawPolygon(pa);
309 }
310
311 int KeyframeHelper::value() const
312 {
313     return m_position;
314 }
315
316 void KeyframeHelper::setValue(const int pos)
317 {
318     if (pos == m_position || m_geom == NULL) return;
319     if (pos == m_seekPosition) {
320         m_seekPosition = SEEK_INACTIVE;
321     }
322     m_position = pos;
323     update();
324 }
325
326 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
327 {
328     m_geom = geom;
329     frameLength = length;
330     update();
331 }
332
333 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
334 {
335     m_extraGeometries.append(geom);
336 }
337
338 #include "keyframehelper.moc"