]> git.sesse.net Git - kdenlive/blob - src/keyframehelper.cpp
const'ify + fix indent + minor optimization
[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) pos = m_position;
171         m_movingItem.frame(pos);
172         for (int i = 0; i < m_extraMovingItems.count(); ++i) {
173             if (m_extraMovingItems.at(i)) m_extraMovingItems[i]->frame(pos);
174         }
175         update();
176         return;
177     }
178     m_seekPosition = (int) (xPos / m_scale);
179     m_seekPosition = qMax(0, m_seekPosition);
180     m_seekPosition = qMin(frameLength, m_seekPosition);
181     m_hoverKeyframe = -2;
182     emit requestSeek(m_seekPosition);
183     update();
184 }
185
186 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
187 {
188     if (m_geom != NULL && event->button() == Qt::LeftButton) {
189         // check if we want to move a keyframe
190         int xPos = event->x() - margin;
191         int mousePos = qMax((int)(xPos / m_scale - 5), 0);
192         Mlt::GeometryItem item;
193         if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
194             // There is already a keyframe close to mouse click
195             emit removeKeyframe(item.frame());
196             return;
197         }
198         // add new keyframe
199         emit addKeyframe((int)(xPos / m_scale));
200     }
201 }
202
203 // virtual
204 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
205 {
206     m_drag = false;
207     setCursor(Qt::ArrowCursor);
208     m_hoverKeyframe = -1;
209     if (m_movingKeyframe) {
210         m_geom->insert(m_movingItem);
211         m_movingKeyframe = false;
212
213         for (int i = 0; i < m_extraGeometries.count(); ++i) {
214             if (m_extraMovingItems.at(i)) m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
215         }
216         
217         emit keyframeMoved(m_position);
218         return;
219     }
220     QWidget::mouseReleaseEvent(event);
221 }
222
223 // virtual
224 void KeyframeHelper::wheelEvent(QWheelEvent * e)
225 {
226     if (e->delta() < 0)
227         --m_position;
228     else
229         ++m_position;
230     m_position = qMax(0, m_position);
231     m_position = qMin(frameLength, m_position);
232     emit requestSeek(m_position);
233     update();
234     /*    int delta = 1;
235         if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
236         if (e->delta() < 0) delta = 0 - delta;
237         m_view->moveCursorPos(delta);
238     */
239 }
240
241 // virtual
242 void KeyframeHelper::paintEvent(QPaintEvent *e)
243 {
244     QStylePainter p(this);
245     const QRectF clipRect = e->rect();
246     p.setClipRect(clipRect);
247     m_scale = (double) (width() - 2 * margin) / frameLength;
248     if (m_geom != NULL) {
249         int pos = 0;
250         p.setPen(m_keyframe);
251         p.setBrush(m_keyframebg);
252         Mlt::GeometryItem item;
253         while (true) {
254             if (m_geom->next_key(&item, pos) == 1) break;
255             pos = item.frame();
256             if (pos == m_hoverKeyframe) {
257                 p.setBrush(m_selected);
258             }
259             int scaledPos = margin + pos * m_scale;
260             // draw keyframes
261             p.drawLine(scaledPos, 9, scaledPos, 15);
262             // draw pointer
263             QPolygon pa(4);
264             pa.setPoints(4,
265                          scaledPos,     0,
266                          scaledPos - 4, 4,
267                          scaledPos,     8,
268                          scaledPos + 4, 4);
269             p.drawPolygon(pa);
270             //p.drawEllipse(scaledPos - 4, 0, 8, 8);
271             if (pos == m_hoverKeyframe) {
272                 p.setBrush(m_keyframebg);
273             }
274             //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
275             pos++;
276         }
277         
278         if (m_movingKeyframe) {
279             p.setBrush(m_selected);
280             int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
281             // draw keyframes
282             p.drawLine(scaledPos, 9, scaledPos, 15);
283             // draw pointer
284             QPolygon pa(5);
285             pa.setPoints(4,
286                          scaledPos,     0,
287                          scaledPos - 4, 4,
288                          scaledPos,     8,
289                          scaledPos + 4, 4);
290             p.drawPolygon(pa);
291             p.setBrush(m_keyframe);
292         }
293     }
294     p.setPen(palette().dark().color());
295     p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
296     p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
297     p.drawLine(width() - margin, m_lineHeight - 3, width() - margin, m_lineHeight + 3);
298
299     // draw pointer
300     if (m_seekPosition != SEEK_INACTIVE) {
301         p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark());
302     }
303     QPolygon pa(3);
304     const int cursor = margin + m_position * m_scale;
305     pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
306     if (m_hoverKeyframe == -2)
307         p.setBrush(palette().highlight());
308     else
309         p.setBrush(palette().dark().color());
310     p.drawPolygon(pa);
311 }
312
313 int KeyframeHelper::value() const
314 {
315     return m_position;
316 }
317
318 void KeyframeHelper::setValue(const int pos)
319 {
320     if (pos == m_position || m_geom == NULL) return;
321     if (pos == m_seekPosition) {
322         m_seekPosition = SEEK_INACTIVE;
323     }
324     m_position = pos;
325     update();
326 }
327
328 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
329 {
330     m_geom = geom;
331     frameLength = qMax(1, length);
332     update();
333 }
334
335 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
336 {
337     m_extraGeometries.append(geom);
338 }
339
340 #include "keyframehelper.moc"