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) :
43 m_movingKeyframe(false),
47 m_seekPosition(SEEK_INACTIVE)
49 setFont(KGlobalSettings::toolBarFont());
50 setMouseTracking(true);
51 QPalette p = palette();
52 KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
53 m_selected = scheme.decoration(KColorScheme::HoverColor).color();
54 m_keyframe = scheme.foreground(KColorScheme::LinkText).color();
55 m_keyframebg = scheme.shade(KColorScheme::MidShade);
59 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
62 if (event->button() != Qt::LeftButton) {
63 QWidget::mousePressEvent(event);
66 int xPos = event->x() - margin;
67 if (m_geom != NULL && (event->y() < m_lineHeight)) {
68 // check if we want to move a keyframe
69 int mousePos = qMax((int)(xPos / m_scale), 0);
70 Mlt::GeometryItem item;
71 if (m_geom->next_key(&item, mousePos) == 0) {
72 if (qAbs(item.frame() * m_scale - xPos) < 4) {
74 m_movingItem.x(item.x());
75 m_movingItem.y(item.y());
76 m_movingItem.w(item.w());
77 m_movingItem.h(item.h());
78 m_movingItem.mix(item.mix());
79 m_movingItem.frame(item.frame());
81 while (!m_extraMovingItems.isEmpty()) {
82 Mlt::GeometryItem *gitem = m_extraMovingItems.takeFirst();
85 for (int i = 0; i < m_extraGeometries.count(); i++) {
86 Mlt::GeometryItem *item2 = new Mlt::GeometryItem();
87 if (m_extraGeometries.at(i)->next_key(item, mousePos) == 0) {
89 item2->frame(item.frame());
90 m_extraMovingItems.append(item2);
96 m_dragStart = event->pos();
97 m_movingKeyframe = true;
102 if (event->y() >= m_lineHeight && event->y() < height()) {
103 int seekRequest = xPos / m_scale;
105 if (seekRequest != m_position) {
106 m_seekPosition = seekRequest;
107 emit requestSeek(m_seekPosition);
113 void KeyframeHelper::leaveEvent( QEvent * event )
116 if (m_hoverKeyframe != -1) {
117 m_hoverKeyframe = -1;
123 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
125 int xPos = event->x() - margin;
127 int mousePos = qMax((int)(xPos / m_scale), 0);
128 if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight) {
129 // Mouse over time cursor
130 if (m_hoverKeyframe != -2) {
131 m_hoverKeyframe = -2;
137 if (m_geom != NULL && (event->y() < m_lineHeight)) {
138 // check if we want to move a keyframe
139 Mlt::GeometryItem item;
140 if (m_geom->next_key(&item, mousePos) == 0) {
141 if (qAbs(item.frame() * m_scale - xPos) < 4) {
142 if (m_hoverKeyframe == item.frame()) return;
143 m_hoverKeyframe = item.frame();
144 setCursor(Qt::PointingHandCursor);
151 if (m_hoverKeyframe != -1) {
152 m_hoverKeyframe = -1;
153 setCursor(Qt::ArrowCursor);
159 if (m_movingKeyframe) {
160 if (!m_dragStart.isNull()) {
161 if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
162 m_dragStart = QPoint();
163 m_geom->remove(m_movingItem.frame());
164 for (int i = 0; i < m_extraGeometries.count(); i++)
165 m_extraGeometries[i]->remove(m_movingItem.frame());
167 int pos = qBound(0, (int)(xPos / m_scale), frameLength);
168 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
169 m_movingItem.frame(pos);
170 for (int i = 0; i < m_extraMovingItems.count(); i++) {
171 m_extraMovingItems[i]->frame(pos);
176 m_seekPosition = (int) (xPos / m_scale);
177 m_seekPosition = qMax(0, m_seekPosition);
178 m_seekPosition = qMin(frameLength, m_seekPosition);
179 m_hoverKeyframe = -2;
180 emit requestSeek(m_seekPosition);
184 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
186 if (m_geom != NULL && event->button() == Qt::LeftButton) {
187 // check if we want to move a keyframe
188 int xPos = event->x() - margin;
189 int mousePos = qMax((int)(xPos / m_scale - 5), 0);
190 Mlt::GeometryItem item;
191 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
192 // There is already a keyframe close to mouse click
193 emit removeKeyframe(item.frame());
197 emit addKeyframe((int)(xPos / m_scale));
202 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
205 setCursor(Qt::ArrowCursor);
206 m_hoverKeyframe = -1;
207 if (m_movingKeyframe) {
208 m_geom->insert(m_movingItem);
209 m_movingKeyframe = false;
211 for (int i = 0; i < m_extraGeometries.count(); i++) {
212 m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
215 emit keyframeMoved(m_position);
218 QWidget::mouseReleaseEvent(event);
222 void KeyframeHelper::wheelEvent(QWheelEvent * e)
228 m_position = qMax(0, m_position);
229 m_position = qMin(frameLength, m_position);
230 emit requestSeek(m_position);
233 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
234 if (e->delta() < 0) delta = 0 - delta;
235 m_view->moveCursorPos(delta);
240 void KeyframeHelper::paintEvent(QPaintEvent *e)
242 QStylePainter p(this);
243 const QRectF clipRect = e->rect();
244 p.setClipRect(clipRect);
245 m_scale = (double) (width() - 2 * margin) / frameLength;
246 if (m_geom != NULL) {
248 p.setPen(m_keyframe);
249 p.setBrush(m_keyframebg);
250 Mlt::GeometryItem item;
252 if (m_geom->next_key(&item, pos) == 1) break;
254 if (pos == m_hoverKeyframe) {
255 p.setBrush(m_selected);
257 int scaledPos = margin + pos * m_scale;
259 p.drawLine(scaledPos, 9, scaledPos, 15);
268 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
269 if (pos == m_hoverKeyframe) {
270 p.setBrush(m_keyframebg);
272 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
276 if (m_movingKeyframe) {
277 p.setBrush(m_selected);
278 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
280 p.drawLine(scaledPos, 9, scaledPos, 15);
289 p.setBrush(m_keyframe);
292 p.setPen(palette().dark().color());
293 p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
294 p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
295 p.drawLine(width() - margin, m_lineHeight - 3, width() - margin, m_lineHeight + 3);
298 if (m_seekPosition != SEEK_INACTIVE) {
299 p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark());
302 const int cursor = margin + m_position * m_scale;
303 pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
304 if (m_hoverKeyframe == -2)
305 p.setBrush(palette().highlight());
307 p.setBrush(palette().dark().color());
311 int KeyframeHelper::value() const
316 void KeyframeHelper::setValue(const int pos)
318 if (pos == m_position || m_geom == NULL) return;
319 if (pos == m_seekPosition) {
320 m_seekPosition = SEEK_INACTIVE;
326 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
329 frameLength = length;
333 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
335 m_extraGeometries.append(geom);
338 #include "keyframehelper.moc"