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) 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);
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);
186 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
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());
199 emit addKeyframe((int)(xPos / m_scale));
204 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
207 setCursor(Qt::ArrowCursor);
208 m_hoverKeyframe = -1;
209 if (m_movingKeyframe) {
210 m_geom->insert(m_movingItem);
211 m_movingKeyframe = false;
213 for (int i = 0; i < m_extraGeometries.count(); ++i) {
214 if (m_extraMovingItems.at(i)) m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
217 emit keyframeMoved(m_position);
220 QWidget::mouseReleaseEvent(event);
224 void KeyframeHelper::wheelEvent(QWheelEvent * e)
230 m_position = qMax(0, m_position);
231 m_position = qMin(frameLength, m_position);
232 emit requestSeek(m_position);
235 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
236 if (e->delta() < 0) delta = 0 - delta;
237 m_view->moveCursorPos(delta);
242 void KeyframeHelper::paintEvent(QPaintEvent *e)
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) {
250 p.setPen(m_keyframe);
251 p.setBrush(m_keyframebg);
252 Mlt::GeometryItem item;
254 if (m_geom->next_key(&item, pos) == 1) break;
256 if (pos == m_hoverKeyframe) {
257 p.setBrush(m_selected);
259 int scaledPos = margin + pos * m_scale;
261 p.drawLine(scaledPos, 9, scaledPos, 15);
270 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
271 if (pos == m_hoverKeyframe) {
272 p.setBrush(m_keyframebg);
274 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
278 if (m_movingKeyframe) {
279 p.setBrush(m_selected);
280 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
282 p.drawLine(scaledPos, 9, scaledPos, 15);
291 p.setBrush(m_keyframe);
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);
300 if (m_seekPosition != SEEK_INACTIVE) {
301 p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark());
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());
309 p.setBrush(palette().dark().color());
313 int KeyframeHelper::value() const
318 void KeyframeHelper::setValue(const int pos)
320 if (pos == m_position || m_geom == NULL) return;
321 if (pos == m_seekPosition) {
322 m_seekPosition = SEEK_INACTIVE;
328 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
331 frameLength = qMax(1, length);
335 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
337 m_extraGeometries.append(geom);
340 #include "keyframehelper.moc"