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()) {
104 m_seekPosition = xPos / m_scale;
105 emit requestSeek(m_seekPosition);
110 void KeyframeHelper::leaveEvent( QEvent * event )
113 if (m_hoverKeyframe != -1) {
114 m_hoverKeyframe = -1;
120 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
122 int xPos = event->x() - margin;
124 int mousePos = qMax((int)(xPos / m_scale), 0);
125 if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight) {
126 // Mouse over time cursor
127 if (m_hoverKeyframe != -2) {
128 m_hoverKeyframe = -2;
134 if (m_geom != NULL && (event->y() < m_lineHeight)) {
135 // check if we want to move a keyframe
136 Mlt::GeometryItem item;
137 if (m_geom->next_key(&item, mousePos) == 0) {
138 if (qAbs(item.frame() * m_scale - xPos) < 4) {
139 if (m_hoverKeyframe == item.frame()) return;
140 m_hoverKeyframe = item.frame();
141 setCursor(Qt::PointingHandCursor);
148 if (m_hoverKeyframe != -1) {
149 m_hoverKeyframe = -1;
150 setCursor(Qt::ArrowCursor);
156 if (m_movingKeyframe) {
157 if (!m_dragStart.isNull()) {
158 if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
159 m_dragStart = QPoint();
160 m_geom->remove(m_movingItem.frame());
161 for (int i = 0; i < m_extraGeometries.count(); i++)
162 m_extraGeometries[i]->remove(m_movingItem.frame());
164 int pos = qBound(0, (int)(xPos / m_scale), frameLength);
165 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
166 m_movingItem.frame(pos);
167 for (int i = 0; i < m_extraMovingItems.count(); i++) {
168 m_extraMovingItems[i]->frame(pos);
173 m_seekPosition = (int) (xPos / m_scale);
174 m_seekPosition = qMax(0, m_seekPosition);
175 m_seekPosition = qMin(frameLength, m_seekPosition);
176 m_hoverKeyframe = -2;
177 emit requestSeek(m_seekPosition);
181 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
183 if (m_geom != NULL && event->button() == Qt::LeftButton) {
184 // check if we want to move a keyframe
185 int xPos = event->x() - margin;
186 int mousePos = qMax((int)(xPos / m_scale - 5), 0);
187 Mlt::GeometryItem item;
188 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
189 // There is already a keyframe close to mouse click
190 emit removeKeyframe(item.frame());
194 emit addKeyframe((int)(xPos / m_scale));
199 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
202 setCursor(Qt::ArrowCursor);
203 m_hoverKeyframe = -1;
204 if (m_movingKeyframe) {
205 m_geom->insert(m_movingItem);
206 m_movingKeyframe = false;
208 for (int i = 0; i < m_extraGeometries.count(); i++) {
209 m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
212 emit keyframeMoved(m_position);
215 QWidget::mouseReleaseEvent(event);
219 void KeyframeHelper::wheelEvent(QWheelEvent * e)
225 m_position = qMax(0, m_position);
226 m_position = qMin(frameLength, m_position);
227 emit requestSeek(m_position);
230 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
231 if (e->delta() < 0) delta = 0 - delta;
232 m_view->moveCursorPos(delta);
237 void KeyframeHelper::paintEvent(QPaintEvent *e)
239 QStylePainter p(this);
240 const QRectF clipRect = e->rect();
241 p.setClipRect(clipRect);
242 m_scale = (double) (width() - 2 * margin) / frameLength;
243 if (m_geom != NULL) {
245 p.setPen(m_keyframe);
246 p.setBrush(m_keyframebg);
247 Mlt::GeometryItem item;
249 if (m_geom->next_key(&item, pos) == 1) break;
251 if (pos == m_hoverKeyframe) {
252 p.setBrush(m_selected);
254 int scaledPos = margin + pos * m_scale;
256 p.drawLine(scaledPos, 9, scaledPos, 15);
265 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
266 if (pos == m_hoverKeyframe) {
267 p.setBrush(m_keyframebg);
269 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
273 if (m_movingKeyframe) {
274 p.setBrush(m_selected);
275 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
277 p.drawLine(scaledPos, 9, scaledPos, 15);
286 p.setBrush(m_keyframe);
289 p.setPen(palette().dark().color());
290 p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
291 p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
292 p.drawLine(width() - margin - 1, m_lineHeight - 3, width() - margin - 1, m_lineHeight + 3);
295 if (m_seekPosition != SEEK_INACTIVE) {
296 p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark());
299 const int cursor = margin + m_position * m_scale;
300 pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
301 if (m_hoverKeyframe == -2)
302 p.setBrush(palette().highlight());
304 p.setBrush(palette().dark().color());
308 int KeyframeHelper::value() const
313 void KeyframeHelper::setValue(const int pos)
315 if (pos == m_position || m_geom == NULL) return;
316 if (pos == m_seekPosition) {
317 m_seekPosition = SEEK_INACTIVE;
323 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
326 frameLength = length;
327 while (!m_extraGeometries.isEmpty()) {
328 Mlt::Geometry *geom = m_extraGeometries.takeFirst();
334 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
336 m_extraGeometries.append(geom);
339 #include "keyframehelper.moc"