]> git.sesse.net Git - kdenlive/blob - src/keyframehelper.cpp
small improvements to keyframe display in effectstack
[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
34 KeyframeHelper::KeyframeHelper(QWidget *parent) :
35         QWidget(parent),
36         m_geom(NULL),
37         m_position(0),
38         m_scale(0),
39         m_movingKeyframe(false),
40         m_lineHeight(10),
41         m_drag(false),
42         m_hoverKeyframe(-1)
43 {
44     setFont(KGlobalSettings::toolBarFont());
45     setMouseTracking(true);
46     QPalette p = palette();
47     KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
48     m_selected = scheme.decoration(KColorScheme::HoverColor).color();
49     m_keyframe = scheme.foreground(KColorScheme::LinkText).color();
50     m_keyframebg = scheme.shade(KColorScheme::MidShade);
51 }
52
53 // virtual
54 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
55 {
56     m_hoverKeyframe = -1;
57     if (event->button() == Qt::LeftButton) m_drag = true;
58     else {
59         QWidget::mousePressEvent(event);
60         return;
61     }
62     if (m_geom != NULL && (event->y() < m_lineHeight)) {
63         // check if we want to move a keyframe
64         int mousePos = qMax((int)(event->x() / m_scale), 0);
65         Mlt::GeometryItem item;
66         if (m_geom->next_key(&item, mousePos) == 0) {
67             if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
68                 m_movingItem.x(item.x());
69                 m_movingItem.y(item.y());
70                 m_movingItem.w(item.w());
71                 m_movingItem.h(item.h());
72                 m_movingItem.mix(item.mix());
73                 m_movingItem.frame(item.frame());
74                 m_dragStart = event->pos();
75                 m_movingKeyframe = true;
76                 return;
77             }
78         }
79     }
80     m_position = event->x() / m_scale;
81     emit positionChanged(m_position);
82     update();
83 }
84
85 // virtual
86 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
87 {
88     if (!m_drag) {
89         if (m_geom != NULL && (event->y() < m_lineHeight)) {
90             // check if we want to move a keyframe
91             int mousePos = qMax((int)(event->x() / m_scale), 0);
92             Mlt::GeometryItem item;
93             if (m_geom->next_key(&item, mousePos) == 0) {
94                 if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
95                     if (m_hoverKeyframe == item.frame()) return;
96                     m_hoverKeyframe = item.frame();
97                     setCursor(Qt::PointingHandCursor);
98                     update();
99                     event->accept();
100                     return;
101                 }
102             }
103         }
104         if (m_hoverKeyframe != -1) {
105             m_hoverKeyframe = -1;
106             setCursor(Qt::ArrowCursor);
107             update();
108         }
109         event->accept();
110         return;
111     }
112     if (m_movingKeyframe) {
113         if (!m_dragStart.isNull()) {
114             if ((event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
115             m_dragStart = QPoint();
116             m_geom->remove(m_movingItem.frame());
117         }
118         int pos = qBound(0, (int)(event->x() / m_scale), frameLength);
119         if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
120         m_movingItem.frame(pos);
121         update();
122         return;
123     }
124     m_position = event->x() / m_scale;
125     m_position = qMax(0, m_position);
126     m_position = qMin(frameLength, m_position);
127     emit positionChanged(m_position);
128     update();
129 }
130
131 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
132 {
133     if (m_geom != NULL && event->button() == Qt::LeftButton) {
134         // check if we want to move a keyframe
135         int mousePos = qMax((int)(event->x() / m_scale - 5), 0);
136         Mlt::GeometryItem item;
137         if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
138             // There is already a keyframe close to mouse click
139             emit removeKeyframe(item.frame());
140             return;
141         }
142         // add new keyframe
143         emit addKeyframe((int)(event->x() / m_scale));
144     }
145 }
146
147 // virtual
148 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
149 {
150     m_drag = false;
151     m_hoverKeyframe = -1;
152     setCursor(Qt::ArrowCursor);
153     if (m_movingKeyframe) {
154         m_geom->insert(m_movingItem);
155         m_movingKeyframe = false;
156         emit keyframeMoved(m_position);
157         return;
158     }
159     QWidget::mouseReleaseEvent(event);
160 }
161
162 // virtual
163 void KeyframeHelper::wheelEvent(QWheelEvent * e)
164 {
165     if (e->delta() < 0)
166         --m_position;
167     else
168         ++m_position;
169     m_position = qMax(0, m_position);
170     m_position = qMin(frameLength, m_position);
171     emit positionChanged(m_position);
172     update();
173     /*    int delta = 1;
174         if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
175         if (e->delta() < 0) delta = 0 - delta;
176         m_view->moveCursorPos(delta);
177     */
178 }
179
180 // virtual
181 void KeyframeHelper::paintEvent(QPaintEvent *e)
182 {
183     QStylePainter p(this);
184     const QRectF clipRect = e->rect();
185     p.setClipRect(clipRect);
186     m_scale = (double) width() / frameLength;
187     if (m_geom != NULL) {
188         int pos = 0;
189         p.setPen(m_keyframe);
190         p.setBrush(m_keyframebg);
191         Mlt::GeometryItem item;
192         while (true) {
193             if (m_geom->next_key(&item, pos) == 1) break;
194             pos = item.frame();
195             if (pos == m_hoverKeyframe) {
196                 p.setBrush(m_selected);
197             }
198             int scaledPos = pos * m_scale;
199             // draw keyframes
200             p.drawLine(scaledPos, 9, scaledPos, 15);
201             // draw pointer
202             QPolygon pa(4);
203             pa.setPoints(4,
204                          scaledPos,     0,
205                          scaledPos - 4, 4,
206                          scaledPos,     8,
207                          scaledPos + 4, 4);
208             p.drawPolygon(pa);
209             //p.drawEllipse(scaledPos - 4, 0, 8, 8);
210             if (pos == m_hoverKeyframe) {
211                 p.setBrush(m_keyframebg);
212             }
213             //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
214             pos++;
215         }
216         
217         if (m_movingKeyframe) {
218             p.setBrush(m_selected);
219             int scaledPos = (int)(m_movingItem.frame() * m_scale);
220             // draw keyframes
221             p.drawLine(scaledPos, 9, scaledPos, 15);
222             // draw pointer
223             QPolygon pa(5);
224             pa.setPoints(4,
225                          scaledPos,     0,
226                          scaledPos - 4, 4,
227                          scaledPos,     8,
228                          scaledPos + 4, 4);
229             p.drawPolygon(pa);
230             p.setBrush(m_keyframe);
231         }
232     }
233     p.setPen(palette().dark().color());
234     p.drawLine(clipRect.x(), m_lineHeight, clipRect.right(), m_lineHeight);
235
236     // draw pointer
237     QPolygon pa(3);
238     const int cursor = m_position * m_scale;
239     pa.setPoints(3, cursor - 5, 16, cursor + 5, 16, cursor, 11);
240     p.setBrush(palette().dark().color());
241     p.drawPolygon(pa);
242 }
243
244 int KeyframeHelper::value() const
245 {
246     return m_position;
247 }
248
249 void KeyframeHelper::setValue(const int pos)
250 {
251     if (pos == m_position || m_geom == NULL) return;
252     m_position = pos;
253     update();
254 }
255
256 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
257 {
258     m_geom = geom;
259     frameLength = length;
260     update();
261 }
262
263 #include "keyframehelper.moc"