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