1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "keyframehelper.h"
22 #include "kdenlivesettings.h"
23 #include "definitions.h"
26 #include <KGlobalSettings>
27 #include <KColorScheme>
29 #include <QMouseEvent>
30 #include <QStylePainter>
31 #include <QApplication>
34 const int cursorWidth = 6;
36 #define SEEK_INACTIVE (-1)
38 KeyframeHelper::KeyframeHelper(QWidget *parent) :
44 , m_movingKeyframe(false)
49 , m_seekPosition(SEEK_INACTIVE)
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);
61 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
64 if (event->button() != Qt::LeftButton) {
65 QWidget::mousePressEvent(event);
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) {
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());
83 while (!m_extraMovingItems.isEmpty()) {
84 Mlt::GeometryItem *gitem = m_extraMovingItems.takeFirst();
85 if (gitem) delete gitem;
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();
91 item2->frame(item.frame());
92 m_extraMovingItems.append(item2);
94 m_extraMovingItems.append(NULL);
98 m_dragStart = event->pos();
99 m_movingKeyframe = true;
104 if (event->y() >= m_lineHeight && event->y() < height()) {
105 int seekRequest = xPos / m_scale;
107 if (seekRequest != m_position) {
108 m_seekPosition = seekRequest;
109 emit requestSeek(m_seekPosition);
115 void KeyframeHelper::leaveEvent( QEvent * event )
118 if (m_hoverKeyframe != -1) {
119 m_hoverKeyframe = -1;
125 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
127 int xPos = event->x() - margin;
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;
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);
153 if (m_hoverKeyframe != -1) {
154 m_hoverKeyframe = -1;
155 setCursor(Qt::ArrowCursor);
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());
169 int pos = qBound(0, (int)(xPos / m_scale), frameLength);
170 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5)
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);
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);
188 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
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());
201 emit addKeyframe((int)(xPos / m_scale));
206 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
209 setCursor(Qt::ArrowCursor);
210 m_hoverKeyframe = -1;
211 if (m_movingKeyframe) {
212 m_geom->insert(m_movingItem);
213 m_movingKeyframe = false;
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));
220 emit keyframeMoved(m_position);
223 QWidget::mouseReleaseEvent(event);
227 void KeyframeHelper::wheelEvent(QWheelEvent * e)
233 m_position = qMax(0, m_position);
234 m_position = qMin(frameLength, m_position);
235 emit requestSeek(m_position);
238 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
239 if (e->delta() < 0) delta = 0 - delta;
240 m_view->moveCursorPos(delta);
245 void KeyframeHelper::paintEvent(QPaintEvent *e)
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) {
253 p.setPen(m_keyframe);
254 p.setBrush(m_keyframebg);
255 Mlt::GeometryItem item;
257 if (m_geom->next_key(&item, pos) == 1) break;
259 if (pos == m_hoverKeyframe) {
260 p.setBrush(m_selected);
262 int scaledPos = margin + pos * m_scale;
264 p.drawLine(scaledPos, 9, scaledPos, 15);
273 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
274 if (pos == m_hoverKeyframe) {
275 p.setBrush(m_keyframebg);
277 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
281 if (m_movingKeyframe) {
282 p.setBrush(m_selected);
283 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
285 p.drawLine(scaledPos, 9, scaledPos, 15);
294 p.setBrush(m_keyframe);
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);
303 if (m_seekPosition != SEEK_INACTIVE) {
304 p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark());
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());
312 p.setBrush(palette().dark().color());
316 int KeyframeHelper::value() const
321 void KeyframeHelper::setValue(const int pos)
323 if (pos == m_position || m_geom == NULL)
325 if (pos == m_seekPosition) {
326 m_seekPosition = SEEK_INACTIVE;
332 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
335 frameLength = qMax(1, length);
339 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
341 m_extraGeometries.append(geom);
344 #include "keyframehelper.moc"