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 "definitions.h"
25 #include <KGlobalSettings>
27 #include <QMouseEvent>
28 #include <QStylePainter>
29 #include <QApplication>
32 KeyframeHelper::KeyframeHelper(QWidget *parent) :
37 m_movingKeyframe(false)
39 setFont(KGlobalSettings::toolBarFont());
43 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
45 if (m_geom != NULL && (event->y() < height() / 2) && event->button() == Qt::LeftButton) {
46 // check if we want to move a keyframe
47 int mousePos = qMax((int)(event->x() / m_scale - 5), 0);
48 Mlt::GeometryItem item;
49 if (m_geom->next_key(&item, mousePos) == 0) {
50 if (item.frame() - mousePos < 10) {
51 m_movingItem.x(item.x());
52 m_movingItem.y(item.y());
53 m_movingItem.w(item.w());
54 m_movingItem.h(item.h());
55 m_movingItem.mix(item.mix());
56 m_geom->remove(item.frame());
57 m_dragStart = event->pos();
58 m_movingKeyframe = true;
63 m_position = event->x() / m_scale;
64 emit positionChanged(m_position);
69 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
71 if (m_movingKeyframe) {
72 if (!m_dragStart.isNull() && (event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
73 m_dragStart = QPoint();
74 int pos = qMax(0, (int)(event->x() / m_scale));
75 pos = qMin(m_length, pos);
76 m_movingItem.frame(pos);
80 m_position = event->x() / m_scale;
81 m_position = qMax(0, m_position);
82 m_position = qMin(m_length, m_position);
83 emit positionChanged(m_position);
88 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
90 if (m_movingKeyframe) {
91 m_geom->insert(m_movingItem);
92 m_movingKeyframe = false;
93 emit keyframeMoved(m_position);
96 QWidget::mouseReleaseEvent(event);
100 void KeyframeHelper::wheelEvent(QWheelEvent * e)
102 if (e->delta() < 0) m_position = m_position - 1;
103 else m_position = m_position + 1;
104 m_position = qMax(0, m_position);
105 m_position = qMin(m_length, m_position);
106 emit positionChanged(m_position);
109 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
110 if (e->delta() < 0) delta = 0 - delta;
111 m_view->moveCursorPos(delta);
116 void KeyframeHelper::paintEvent(QPaintEvent *e)
118 QStylePainter p(this);
119 const QRectF clipRect = e->rect();
120 p.setClipRect(clipRect);
121 m_scale = (double) width() / m_length;
122 if (m_geom != NULL) {
124 p.setPen(QColor(255, 20, 20));
125 p.setBrush(QColor(255, 20, 20));
126 Mlt::GeometryItem item;
128 if (m_geom->next_key(&item, pos) == 1) break;
130 int scaledPos = pos * m_scale;
132 p.drawLine(scaledPos, 6, scaledPos, 10);
135 pa.setPoints(3, scaledPos - 4, 0, scaledPos + 4, 0, scaledPos, 4);
137 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
140 if (m_movingKeyframe) {
141 int scaledPos = (int)(m_movingItem.frame() * m_scale);
143 p.drawLine(scaledPos, 6, scaledPos, 10);
146 pa.setPoints(3, scaledPos - 4, 0, scaledPos + 4, 0, scaledPos, 4);
150 p.setPen(palette().dark().color());
151 p.drawLine(clipRect.x(), 5, clipRect.right(), 5);
155 const int cursor = m_position * m_scale;
156 pa.setPoints(3, cursor - 5, 11, cursor + 5, 11, cursor, 6);
157 p.setBrush(palette().dark().color());
163 int KeyframeHelper::value() const
168 void KeyframeHelper::setValue(const int pos)
170 if (pos == m_position || m_geom == NULL) return;
175 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
182 #include "keyframehelper.moc"