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 KeyframeHelper::KeyframeHelper(QWidget *parent) :
41 m_movingKeyframe(false),
46 setFont(KGlobalSettings::toolBarFont());
47 setMouseTracking(true);
48 QPalette p = palette();
49 KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
50 m_selected = scheme.decoration(KColorScheme::HoverColor).color();
51 m_keyframe = scheme.foreground(KColorScheme::LinkText).color();
52 m_keyframebg = scheme.shade(KColorScheme::MidShade);
56 void KeyframeHelper::mousePressEvent(QMouseEvent * event)
59 if (event->button() != Qt::LeftButton) {
60 QWidget::mousePressEvent(event);
63 int xPos = event->x() - margin;
64 if (m_geom != NULL && (event->y() < m_lineHeight)) {
65 // check if we want to move a keyframe
66 int mousePos = qMax((int)(xPos / m_scale), 0);
67 Mlt::GeometryItem item;
68 if (m_geom->next_key(&item, mousePos) == 0) {
69 if (qAbs(item.frame() * m_scale - xPos) < 4) {
71 m_movingItem.x(item.x());
72 m_movingItem.y(item.y());
73 m_movingItem.w(item.w());
74 m_movingItem.h(item.h());
75 m_movingItem.mix(item.mix());
76 m_movingItem.frame(item.frame());
78 while (!m_extraMovingItems.isEmpty()) {
79 Mlt::GeometryItem *gitem = m_extraMovingItems.takeFirst();
82 for (int i = 0; i < m_extraGeometries.count(); i++) {
83 Mlt::GeometryItem *item2 = new Mlt::GeometryItem();
84 if (m_extraGeometries.at(i)->next_key(item, mousePos) == 0) {
86 item2->frame(item.frame());
87 m_extraMovingItems.append(item2);
91 m_dragStart = event->pos();
92 m_movingKeyframe = true;
97 if (event->y() >= m_lineHeight && event->y() < height()) {
99 m_position = xPos / m_scale;
100 emit positionChanged(m_position);
106 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
108 int xPos = event->x() - margin;
110 int mousePos = qMax((int)(xPos / m_scale), 0);
111 if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight && event->y() < 17) {
112 // Mouse over time cursor
113 m_hoverKeyframe = -2;
118 if (m_geom != NULL && (event->y() < m_lineHeight)) {
119 // check if we want to move a keyframe
120 Mlt::GeometryItem item;
121 if (m_geom->next_key(&item, mousePos) == 0) {
122 if (qAbs(item.frame() * m_scale - xPos) < 4) {
123 if (m_hoverKeyframe == item.frame()) return;
124 m_hoverKeyframe = item.frame();
125 setCursor(Qt::PointingHandCursor);
132 if (m_hoverKeyframe != -1) {
133 m_hoverKeyframe = -1;
134 setCursor(Qt::ArrowCursor);
140 if (m_movingKeyframe) {
141 if (!m_dragStart.isNull()) {
142 if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
143 m_dragStart = QPoint();
144 m_geom->remove(m_movingItem.frame());
145 for (int i = 0; i < m_extraGeometries.count(); i++)
146 m_extraGeometries[i]->remove(m_movingItem.frame());
148 int pos = qBound(0, (int)(xPos / m_scale), frameLength);
149 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
150 m_movingItem.frame(pos);
151 for (int i = 0; i < m_extraMovingItems.count(); i++) {
152 m_extraMovingItems[i]->frame(pos);
157 m_position = xPos / m_scale;
158 m_position = qMax(0, m_position);
159 m_position = qMin(frameLength, m_position);
160 m_hoverKeyframe = -2;
161 emit positionChanged(m_position);
165 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
167 if (m_geom != NULL && event->button() == Qt::LeftButton) {
168 // check if we want to move a keyframe
169 int xPos = event->x() - margin;
170 int mousePos = qMax((int)(xPos / m_scale - 5), 0);
171 Mlt::GeometryItem item;
172 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
173 // There is already a keyframe close to mouse click
174 emit removeKeyframe(item.frame());
178 emit addKeyframe((int)(xPos / m_scale));
183 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
186 setCursor(Qt::ArrowCursor);
187 m_hoverKeyframe = -1;
188 if (m_movingKeyframe) {
189 m_geom->insert(m_movingItem);
190 m_movingKeyframe = false;
192 for (int i = 0; i < m_extraGeometries.count(); i++) {
193 m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
196 emit keyframeMoved(m_position);
199 QWidget::mouseReleaseEvent(event);
203 void KeyframeHelper::wheelEvent(QWheelEvent * e)
209 m_position = qMax(0, m_position);
210 m_position = qMin(frameLength, m_position);
211 emit positionChanged(m_position);
214 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
215 if (e->delta() < 0) delta = 0 - delta;
216 m_view->moveCursorPos(delta);
221 void KeyframeHelper::paintEvent(QPaintEvent *e)
223 QStylePainter p(this);
224 const QRectF clipRect = e->rect();
225 p.setClipRect(clipRect);
226 m_scale = (double) (width() - 2 * margin) / frameLength;
227 if (m_geom != NULL) {
229 p.setPen(m_keyframe);
230 p.setBrush(m_keyframebg);
231 Mlt::GeometryItem item;
233 if (m_geom->next_key(&item, pos) == 1) break;
235 if (pos == m_hoverKeyframe) {
236 p.setBrush(m_selected);
238 int scaledPos = margin + pos * m_scale;
240 p.drawLine(scaledPos, 9, scaledPos, 15);
249 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
250 if (pos == m_hoverKeyframe) {
251 p.setBrush(m_keyframebg);
253 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
257 if (m_movingKeyframe) {
258 p.setBrush(m_selected);
259 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
261 p.drawLine(scaledPos, 9, scaledPos, 15);
270 p.setBrush(m_keyframe);
273 p.setPen(palette().dark().color());
274 p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
275 p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
276 p.drawLine(width() - margin - 1, m_lineHeight - 3, width() - margin - 1, m_lineHeight + 3);
280 const int cursor = margin + m_position * m_scale;
281 pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
282 if (m_hoverKeyframe == -2)
283 p.setBrush(m_selected);
285 p.setBrush(palette().dark().color());
289 int KeyframeHelper::value() const
294 void KeyframeHelper::setValue(const int pos)
296 if (pos == m_position || m_geom == NULL) return;
301 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
304 frameLength = length;
305 while (!m_extraGeometries.isEmpty()) {
306 Mlt::Geometry *geom = m_extraGeometries.takeFirst();
312 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
314 m_extraGeometries.append(geom);
317 #include "keyframehelper.moc"