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