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);
93 m_dragStart = event->pos();
94 m_movingKeyframe = true;
99 if (event->y() >= m_lineHeight && event->y() < height()) {
101 m_position = xPos / m_scale;
102 emit positionChanged(m_position);
107 void KeyframeHelper::leaveEvent( QEvent * event )
110 if (m_hoverKeyframe != -1) {
111 m_hoverKeyframe = -1;
117 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
119 int xPos = event->x() - margin;
121 int mousePos = qMax((int)(xPos / m_scale), 0);
122 if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight) {
123 // Mouse over time cursor
124 if (m_hoverKeyframe != -2) {
125 m_hoverKeyframe = -2;
131 if (m_geom != NULL && (event->y() < m_lineHeight)) {
132 // check if we want to move a keyframe
133 Mlt::GeometryItem item;
134 if (m_geom->next_key(&item, mousePos) == 0) {
135 if (qAbs(item.frame() * m_scale - xPos) < 4) {
136 if (m_hoverKeyframe == item.frame()) return;
137 m_hoverKeyframe = item.frame();
138 setCursor(Qt::PointingHandCursor);
145 if (m_hoverKeyframe != -1) {
146 m_hoverKeyframe = -1;
147 setCursor(Qt::ArrowCursor);
153 if (m_movingKeyframe) {
154 if (!m_dragStart.isNull()) {
155 if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
156 m_dragStart = QPoint();
157 m_geom->remove(m_movingItem.frame());
158 for (int i = 0; i < m_extraGeometries.count(); i++)
159 m_extraGeometries[i]->remove(m_movingItem.frame());
161 int pos = qBound(0, (int)(xPos / m_scale), frameLength);
162 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
163 m_movingItem.frame(pos);
164 for (int i = 0; i < m_extraMovingItems.count(); i++) {
165 m_extraMovingItems[i]->frame(pos);
170 m_position = xPos / m_scale;
171 m_position = qMax(0, m_position);
172 m_position = qMin(frameLength, m_position);
173 m_hoverKeyframe = -2;
174 emit positionChanged(m_position);
178 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
180 if (m_geom != NULL && event->button() == Qt::LeftButton) {
181 // check if we want to move a keyframe
182 int xPos = event->x() - margin;
183 int mousePos = qMax((int)(xPos / m_scale - 5), 0);
184 Mlt::GeometryItem item;
185 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
186 // There is already a keyframe close to mouse click
187 emit removeKeyframe(item.frame());
191 emit addKeyframe((int)(xPos / m_scale));
196 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
199 setCursor(Qt::ArrowCursor);
200 m_hoverKeyframe = -1;
201 if (m_movingKeyframe) {
202 m_geom->insert(m_movingItem);
203 m_movingKeyframe = false;
205 for (int i = 0; i < m_extraGeometries.count(); i++) {
206 m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
209 emit keyframeMoved(m_position);
212 QWidget::mouseReleaseEvent(event);
216 void KeyframeHelper::wheelEvent(QWheelEvent * e)
222 m_position = qMax(0, m_position);
223 m_position = qMin(frameLength, m_position);
224 emit positionChanged(m_position);
227 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
228 if (e->delta() < 0) delta = 0 - delta;
229 m_view->moveCursorPos(delta);
234 void KeyframeHelper::paintEvent(QPaintEvent *e)
236 QStylePainter p(this);
237 const QRectF clipRect = e->rect();
238 p.setClipRect(clipRect);
239 m_scale = (double) (width() - 2 * margin) / frameLength;
240 if (m_geom != NULL) {
242 p.setPen(m_keyframe);
243 p.setBrush(m_keyframebg);
244 Mlt::GeometryItem item;
246 if (m_geom->next_key(&item, pos) == 1) break;
248 if (pos == m_hoverKeyframe) {
249 p.setBrush(m_selected);
251 int scaledPos = margin + pos * m_scale;
253 p.drawLine(scaledPos, 9, scaledPos, 15);
262 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
263 if (pos == m_hoverKeyframe) {
264 p.setBrush(m_keyframebg);
266 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
270 if (m_movingKeyframe) {
271 p.setBrush(m_selected);
272 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
274 p.drawLine(scaledPos, 9, scaledPos, 15);
283 p.setBrush(m_keyframe);
286 p.setPen(palette().dark().color());
287 p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
288 p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
289 p.drawLine(width() - margin - 1, m_lineHeight - 3, width() - margin - 1, m_lineHeight + 3);
293 const int cursor = margin + m_position * m_scale;
294 pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
295 if (m_hoverKeyframe == -2)
296 p.setBrush(palette().highlight());
298 p.setBrush(palette().dark().color());
302 int KeyframeHelper::value() const
307 void KeyframeHelper::setValue(const int pos)
309 if (pos == m_position || m_geom == NULL) return;
314 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
317 frameLength = length;
318 while (!m_extraGeometries.isEmpty()) {
319 Mlt::Geometry *geom = m_extraGeometries.takeFirst();
325 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
327 m_extraGeometries.append(geom);
330 #include "keyframehelper.moc"