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);
105 void KeyframeHelper::leaveEvent( QEvent * event )
108 if (m_hoverKeyframe != -1) {
109 m_hoverKeyframe = -1;
115 void KeyframeHelper::mouseMoveEvent(QMouseEvent * event)
117 int xPos = event->x() - margin;
119 int mousePos = qMax((int)(xPos / m_scale), 0);
120 if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight) {
121 // Mouse over time cursor
122 if (m_hoverKeyframe != -2) {
123 m_hoverKeyframe = -2;
129 if (m_geom != NULL && (event->y() < m_lineHeight)) {
130 // check if we want to move a keyframe
131 Mlt::GeometryItem item;
132 if (m_geom->next_key(&item, mousePos) == 0) {
133 if (qAbs(item.frame() * m_scale - xPos) < 4) {
134 if (m_hoverKeyframe == item.frame()) return;
135 m_hoverKeyframe = item.frame();
136 setCursor(Qt::PointingHandCursor);
143 if (m_hoverKeyframe != -1) {
144 m_hoverKeyframe = -1;
145 setCursor(Qt::ArrowCursor);
151 if (m_movingKeyframe) {
152 if (!m_dragStart.isNull()) {
153 if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return;
154 m_dragStart = QPoint();
155 m_geom->remove(m_movingItem.frame());
156 for (int i = 0; i < m_extraGeometries.count(); i++)
157 m_extraGeometries[i]->remove(m_movingItem.frame());
159 int pos = qBound(0, (int)(xPos / m_scale), frameLength);
160 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) pos = m_position;
161 m_movingItem.frame(pos);
162 for (int i = 0; i < m_extraMovingItems.count(); i++) {
163 m_extraMovingItems[i]->frame(pos);
168 m_position = xPos / m_scale;
169 m_position = qMax(0, m_position);
170 m_position = qMin(frameLength, m_position);
171 m_hoverKeyframe = -2;
172 emit positionChanged(m_position);
176 void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event)
178 if (m_geom != NULL && event->button() == Qt::LeftButton) {
179 // check if we want to move a keyframe
180 int xPos = event->x() - margin;
181 int mousePos = qMax((int)(xPos / m_scale - 5), 0);
182 Mlt::GeometryItem item;
183 if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) {
184 // There is already a keyframe close to mouse click
185 emit removeKeyframe(item.frame());
189 emit addKeyframe((int)(xPos / m_scale));
194 void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event)
197 setCursor(Qt::ArrowCursor);
198 m_hoverKeyframe = -1;
199 if (m_movingKeyframe) {
200 m_geom->insert(m_movingItem);
201 m_movingKeyframe = false;
203 for (int i = 0; i < m_extraGeometries.count(); i++) {
204 m_extraGeometries[i]->insert(m_extraMovingItems.at(i));
207 emit keyframeMoved(m_position);
210 QWidget::mouseReleaseEvent(event);
214 void KeyframeHelper::wheelEvent(QWheelEvent * e)
220 m_position = qMax(0, m_position);
221 m_position = qMin(frameLength, m_position);
222 emit positionChanged(m_position);
225 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
226 if (e->delta() < 0) delta = 0 - delta;
227 m_view->moveCursorPos(delta);
232 void KeyframeHelper::paintEvent(QPaintEvent *e)
234 QStylePainter p(this);
235 const QRectF clipRect = e->rect();
236 p.setClipRect(clipRect);
237 m_scale = (double) (width() - 2 * margin) / frameLength;
238 if (m_geom != NULL) {
240 p.setPen(m_keyframe);
241 p.setBrush(m_keyframebg);
242 Mlt::GeometryItem item;
244 if (m_geom->next_key(&item, pos) == 1) break;
246 if (pos == m_hoverKeyframe) {
247 p.setBrush(m_selected);
249 int scaledPos = margin + pos * m_scale;
251 p.drawLine(scaledPos, 9, scaledPos, 15);
260 //p.drawEllipse(scaledPos - 4, 0, 8, 8);
261 if (pos == m_hoverKeyframe) {
262 p.setBrush(m_keyframebg);
264 //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20)));
268 if (m_movingKeyframe) {
269 p.setBrush(m_selected);
270 int scaledPos = margin + (int)(m_movingItem.frame() * m_scale);
272 p.drawLine(scaledPos, 9, scaledPos, 15);
281 p.setBrush(m_keyframe);
284 p.setPen(palette().dark().color());
285 p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight);
286 p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3);
287 p.drawLine(width() - margin - 1, m_lineHeight - 3, width() - margin - 1, m_lineHeight + 3);
291 const int cursor = margin + m_position * m_scale;
292 pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10);
293 if (m_hoverKeyframe == -2)
294 p.setBrush(palette().highlight());
296 p.setBrush(palette().dark().color());
300 int KeyframeHelper::value() const
305 void KeyframeHelper::setValue(const int pos)
307 if (pos == m_position || m_geom == NULL) return;
312 void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length)
315 frameLength = length;
316 while (!m_extraGeometries.isEmpty()) {
317 Mlt::Geometry *geom = m_extraGeometries.takeFirst();
323 void KeyframeHelper::addGeometry(Mlt::Geometry *geom)
325 m_extraGeometries.append(geom);
328 #include "keyframehelper.moc"