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());
75 while (!m_extraMovingItems.isEmpty()) {
76 Mlt::GeometryItem *gitem = m_extraMovingItems.takeFirst();
79 for (int i = 0; i < m_extraGeometries.count(); i++) {
80 Mlt::GeometryItem *item2 = new Mlt::GeometryItem();
81 if (m_extraGeometries.at(i)->next_key(item, mousePos) == 0) {
83 item2->frame(item.frame());
84 m_extraMovingItems.append(item2);
88 m_dragStart = event->pos();
89 m_movingKeyframe = true;
94 m_position = event->x() / m_scale;
95 emit positionChanged(m_position);
100 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
103 if (m_geom != NULL && (event->y() < m_lineHeight)) {
104 // check if we want to move a keyframe
105 int mousePos = qMax((int)(event->x() / m_scale), 0);
106 Mlt::GeometryItem item;
107 if (m_geom->next_key(&item, mousePos) == 0) {
108 if (qAbs(item.frame() * m_scale - (int)(event->x())) < 4) {
109 if (m_hoverKeyframe == item.frame()) return;
110 m_hoverKeyframe = item.frame();
111 setCursor(Qt::PointingHandCursor);
118 if (m_hoverKeyframe != -1) {
119 m_hoverKeyframe = -1;
120 setCursor(Qt::ArrowCursor);
126 if (m_movingKeyframe) {
127 if (!m_dragStart.isNull()) {
128 if ((event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
129 m_dragStart = QPoint();
130 m_geom->remove(m_movingItem.frame());
131 for (int i = 0; i < m_extraGeometries.count(); i++)
132 m_extraGeometries[i]->remove(m_movingItem.frame());
134 int pos = qBound(0, (int)(event->x() / m_scale), frameLength);
135 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
136 m_movingItem.frame(pos);
137 for (int i = 0; i < m_extraMovingItems.count(); i++) {
138 m_extraMovingItems[i]->frame(pos);
143 m_position = event->x() / m_scale;
144 m_position = qMax(0, m_position);
145 m_position = qMin(frameLength, m_position);
146 emit positionChanged(m_position);
150 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
152 if (m_geom != NULL && event->button() == Qt::LeftButton) {
153 // check if we want to move a keyframe
154 int mousePos = qMax((int)(event->x() / m_scale - 5), 0);
155 Mlt::GeometryItem item;
156 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
157 // There is already a keyframe close to mouse click
158 emit removeKeyframe(item.frame());
162 emit addKeyframe((int)(event->x() / m_scale));
167 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
170 m_hoverKeyframe = -1;
171 setCursor(Qt::ArrowCursor);
172 if (m_movingKeyframe) {
173 m_geom->insert(m_movingItem);
174 m_movingKeyframe = false;
176 for (int i = 0; i < m_extraGeometries.count(); i++) {
177 m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
180 emit keyframeMoved(m_position);
183 QWidget::mouseReleaseEvent(event);
187 void KeyframeHelper::wheelEvent(QWheelEvent * e)
193 m_position = qMax(0, m_position);
194 m_position = qMin(frameLength, m_position);
195 emit positionChanged(m_position);
198 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
199 if (e->delta() < 0) delta = 0 - delta;
200 m_view->moveCursorPos(delta);
205 void KeyframeHelper::paintEvent(QPaintEvent *e)
207 QStylePainter p(this);
208 const QRectF clipRect = e->rect();
209 p.setClipRect(clipRect);
210 m_scale = (double) width() / frameLength;
211 if (m_geom != NULL) {
213 p.setPen(m_keyframe);
214 p.setBrush(m_keyframebg);
215 Mlt::GeometryItem item;
217 if (m_geom->next_key(&item, pos) == 1) break;
219 if (pos == m_hoverKeyframe) {
220 p.setBrush(m_selected);
222 int scaledPos = pos * m_scale;
224 p.drawLine(scaledPos, 9, scaledPos, 15);
233 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
234 if (pos == m_hoverKeyframe) {
235 p.setBrush(m_keyframebg);
237 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
241 if (m_movingKeyframe) {
242 p.setBrush(m_selected);
243 int scaledPos = (int)(m_movingItem.frame() * m_scale);
245 p.drawLine(scaledPos, 9, scaledPos, 15);
254 p.setBrush(m_keyframe);
257 p.setPen(palette().dark().color());
258 p.drawLine(clipRect.x(), m_lineHeight, clipRect.right(), m_lineHeight);
262 const int cursor = m_position * m_scale;
263 pa.setPoints(3, cursor - 5, 16, cursor + 5, 16, cursor, 11);
264 p.setBrush(palette().dark().color());
268 int KeyframeHelper::value() const
273 void KeyframeHelper::setValue(const int pos)
275 if (pos == m_position || m_geom == NULL) return;
280 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
283 frameLength = length;
284 while (!m_extraGeometries.isEmpty()) {
285 Mlt::Geometry *geom = m_extraGeometries.takeFirst();
291 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
293 m_extraGeometries.append(geom);
296 #include "keyframehelper.moc"