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 KeyframeHelper::KeyframeHelper(QWidget *parent) :
39 m_movingKeyframe(false),
44 setFont(KGlobalSettings::toolBarFont());
45 setMouseTracking(true);
46 QPalette p = palette();
47 KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
48 m_selected = scheme.decoration(KColorScheme::HoverColor).color();
49 m_keyframe = scheme.foreground(KColorScheme::LinkText).color();
50 m_keyframebg = scheme.shade(KColorScheme::MidShade);
54 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
57 if (event->button() == Qt::LeftButton) m_drag = true;
59 QWidget::mousePressEvent(event);
62 if (m_geom != NULL && (event->y() < m_lineHeight)) {
63 // check if we want to move a keyframe
64 int mousePos = qMax((int)(event->x() / m_scale), 0);
65 Mlt::GeometryItem item;
66 if (m_geom->next_key(&item, mousePos) == 0) {
67 if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
68 m_movingItem.x(item.x());
69 m_movingItem.y(item.y());
70 m_movingItem.w(item.w());
71 m_movingItem.h(item.h());
72 m_movingItem.mix(item.mix());
73 m_movingItem.frame(item.frame());
74 m_dragStart = event->pos();
75 m_movingKeyframe = true;
80 m_position = event->x() / m_scale;
81 emit positionChanged(m_position);
86 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
89 if (m_geom != NULL && (event->y() < m_lineHeight)) {
90 // check if we want to move a keyframe
91 int mousePos = qMax((int)(event->x() / m_scale), 0);
92 Mlt::GeometryItem item;
93 if (m_geom->next_key(&item, mousePos) == 0) {
94 if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
95 if (m_hoverKeyframe == item.frame()) return;
96 m_hoverKeyframe = item.frame();
97 setCursor(Qt::PointingHandCursor);
104 if (m_hoverKeyframe != -1) {
105 m_hoverKeyframe = -1;
106 setCursor(Qt::ArrowCursor);
112 if (m_movingKeyframe) {
113 if (!m_dragStart.isNull()) {
114 if ((event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
115 m_dragStart = QPoint();
116 m_geom->remove(m_movingItem.frame());
118 int pos = qBound(0, (int)(event->x() / m_scale), frameLength);
119 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
120 m_movingItem.frame(pos);
124 m_position = event->x() / m_scale;
125 m_position = qMax(0, m_position);
126 m_position = qMin(frameLength, m_position);
127 emit positionChanged(m_position);
131 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
133 if (m_geom != NULL && event->button() == Qt::LeftButton) {
134 // check if we want to move a keyframe
135 int mousePos = qMax((int)(event->x() / m_scale - 5), 0);
136 Mlt::GeometryItem item;
137 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
138 // There is already a keyframe close to mouse click
139 emit removeKeyframe(item.frame());
143 emit addKeyframe((int)(event->x() / m_scale));
148 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
151 m_hoverKeyframe = -1;
152 setCursor(Qt::ArrowCursor);
153 if (m_movingKeyframe) {
154 m_geom->insert(m_movingItem);
155 m_movingKeyframe = false;
156 emit keyframeMoved(m_position);
159 QWidget::mouseReleaseEvent(event);
163 void KeyframeHelper::wheelEvent(QWheelEvent * e)
169 m_position = qMax(0, m_position);
170 m_position = qMin(frameLength, m_position);
171 emit positionChanged(m_position);
174 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
175 if (e->delta() < 0) delta = 0 - delta;
176 m_view->moveCursorPos(delta);
181 void KeyframeHelper::paintEvent(QPaintEvent *e)
183 QStylePainter p(this);
184 const QRectF clipRect = e->rect();
185 p.setClipRect(clipRect);
186 m_scale = (double) width() / frameLength;
187 if (m_geom != NULL) {
189 p.setPen(m_keyframe);
190 p.setBrush(m_keyframebg);
191 Mlt::GeometryItem item;
193 if (m_geom->next_key(&item, pos) == 1) break;
195 if (pos == m_hoverKeyframe) {
196 p.setBrush(m_selected);
198 int scaledPos = pos * m_scale;
200 p.drawLine(scaledPos, 9, scaledPos, 15);
209 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
210 if (pos == m_hoverKeyframe) {
211 p.setBrush(m_keyframebg);
213 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
217 if (m_movingKeyframe) {
218 p.setBrush(m_selected);
219 int scaledPos = (int)(m_movingItem.frame() * m_scale);
221 p.drawLine(scaledPos, 9, scaledPos, 15);
230 p.setBrush(m_keyframe);
233 p.setPen(palette().dark().color());
234 p.drawLine(clipRect.x(), m_lineHeight, clipRect.right(), m_lineHeight);
238 const int cursor = m_position * m_scale;
239 pa.setPoints(3, cursor - 5, 16, cursor + 5, 16, cursor, 11);
240 p.setBrush(palette().dark().color());
244 int KeyframeHelper::value() const
249 void KeyframeHelper::setValue(const int pos)
251 if (pos == m_position || m_geom == NULL) return;
256 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
259 frameLength = length;
263 #include "keyframehelper.moc"