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