1 /***************************************************************************
2 * Copyright (C) 2011 by Till Theato (root@ttill.de) *
3 * This file is part of Kdenlive (www.kdenlive.org). *
5 * Kdenlive is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
10 * Kdenlive is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with Kdenlive. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
19 #include "simpletimelinewidget.h"
22 #include <QMouseEvent>
25 SimpleTimelineWidget::SimpleTimelineWidget(QWidget* parent) :
29 m_currentKeyframe(-1),
30 m_currentKeyframeOriginal(-1),
34 setMouseTracking(true);
35 setMinimumSize(QSize(150, 20));
36 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
39 void SimpleTimelineWidget::setKeyframes(QList <int> keyframes)
41 m_keyframes = keyframes;
43 m_currentKeyframe = m_currentKeyframeOriginal = -1;
47 void SimpleTimelineWidget::slotSetPosition(int pos)
53 void SimpleTimelineWidget::slotAddKeyframe(int pos, int select)
58 m_keyframes.append(pos);
61 m_currentKeyframe = m_currentKeyframeOriginal = pos;
64 emit keyframeAdded(pos);
67 void SimpleTimelineWidget::slotAddRemove()
69 if (m_keyframes.contains(m_position))
70 slotRemoveKeyframe(m_position);
72 slotAddKeyframe(m_position);
75 void SimpleTimelineWidget::slotRemoveKeyframe(int pos)
77 m_keyframes.removeAll(pos);
78 if (m_currentKeyframe == pos)
79 m_currentKeyframe = m_currentKeyframeOriginal = -1;
81 emit keyframeRemoved(pos);
84 void SimpleTimelineWidget::setDuration(int dur)
89 void SimpleTimelineWidget::slotGoToNext()
91 foreach (const int &keyframe, m_keyframes) {
92 if (keyframe > m_position) {
93 slotSetPosition(keyframe);
94 emit positionChanged(keyframe);
99 // no keyframe after current position
100 slotSetPosition(m_duration);
101 emit positionChanged(m_duration);
104 void SimpleTimelineWidget::slotGoToPrev()
106 for (int i = m_keyframes.count() - 1; i >= 0; --i) {
107 if (m_keyframes.at(i) < m_position) {
108 slotSetPosition(m_keyframes.at(i));
109 emit positionChanged(m_keyframes.at(i));
114 // no keyframe before current position
116 emit positionChanged(0);
119 void SimpleTimelineWidget::mousePressEvent(QMouseEvent* event)
121 int pos = (event->x() - 5) / m_scale;
122 if (qAbs(event->y() - m_lineHeight) < m_lineHeight / 5. && event->button() == Qt::LeftButton) {
123 foreach(const int &keyframe, m_keyframes) {
124 if (qAbs(keyframe - pos) < 5) {
125 m_currentKeyframeOriginal = keyframe;
126 m_currentKeyframe = pos;
133 // no keyframe next to mouse
134 m_currentKeyframe = m_currentKeyframeOriginal = -1;
136 emit positionChanged(pos);
140 void SimpleTimelineWidget::mouseMoveEvent(QMouseEvent* event)
142 if (event->buttons() & Qt::LeftButton) {
143 int pos = qBound(0, (int)((event->x() - 5) / m_scale), m_duration);
144 if (m_currentKeyframe >= 0) {
145 m_currentKeyframe = pos;
146 emit keyframeMoving(m_currentKeyframeOriginal, m_currentKeyframe);
149 emit positionChanged(pos);
158 void SimpleTimelineWidget::mouseReleaseEvent(QMouseEvent* event)
160 if (m_currentKeyframe > 0) {
161 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
165 void SimpleTimelineWidget::wheelEvent(QWheelEvent* event)
167 int change = event->delta() < 0 ? -1 : 1;
168 if (m_currentKeyframe > 0) {
169 m_currentKeyframe = qBound(0, m_currentKeyframe + change, m_duration);
170 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
172 m_position = qBound(0, m_position + change, m_duration);
173 emit positionChanged(m_position);
178 void SimpleTimelineWidget::paintEvent(QPaintEvent* event)
182 int max = width() - 6;
183 m_scale = (max - min) / (double)(m_duration);
184 p.translate(min, m_lineHeight);
186 p.setPen(QPen(palette().foreground().color(), 1, Qt::SolidLine));
191 p.drawLine(0, 0, max, 0);
196 p.fillRect(QRectF(-1, -10, 3, 20).translated(m_position * m_scale, 0), QBrush(palette().foreground().color(), Qt::SolidPattern));
201 p.setPen(QPen(palette().highlight().color(), 1, Qt::SolidLine));
202 p.setBrush(Qt::NoBrush);
203 QPolygonF keyframe = QPolygonF() << QPointF(0, -4) << QPointF(-4, 0) << QPointF(0, 4) << QPointF(4, 0);
205 foreach (const int &pos, m_keyframes) {
207 tmp.translate(pos * m_scale, 0);
208 if (pos == m_currentKeyframe)
209 p.setBrush(QBrush(palette().highlight().color(), Qt::SolidPattern));
211 p.drawConvexPolygon(tmp);
213 if (pos == m_currentKeyframe)
214 p.setBrush(Qt::NoBrush);
218 #include "simpletimelinewidget.moc"