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