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) :
30 m_currentKeyframe(-1),
31 m_currentKeyframeOriginal(-1),
35 setMouseTracking(true);
36 setMinimumSize(QSize(150, 20));
37 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
40 void SimpleTimelineWidget::setKeyframes(QList <int> keyframes)
42 m_keyframes = keyframes;
44 m_currentKeyframe = m_currentKeyframeOriginal = -1;
48 void SimpleTimelineWidget::slotSetPosition(int pos)
54 void SimpleTimelineWidget::slotAddKeyframe(int pos, int select)
59 m_keyframes.append(pos);
62 m_currentKeyframe = m_currentKeyframeOriginal = pos;
65 emit keyframeAdded(pos);
68 void SimpleTimelineWidget::slotAddRemove()
70 if (m_keyframes.contains(m_position))
71 slotRemoveKeyframe(m_position);
73 slotAddKeyframe(m_position);
76 void SimpleTimelineWidget::slotRemoveKeyframe(int pos)
78 m_keyframes.removeAll(pos);
79 if (m_currentKeyframe == pos)
80 m_currentKeyframe = m_currentKeyframeOriginal = -1;
82 emit keyframeRemoved(pos);
85 void SimpleTimelineWidget::setRange(int min, int max)
91 void SimpleTimelineWidget::slotGoToNext()
93 foreach (const int &keyframe, m_keyframes) {
94 if (keyframe > m_position) {
95 slotSetPosition(keyframe);
96 emit positionChanged(keyframe);
101 // no keyframe after current position
102 slotSetPosition(m_max);
103 emit positionChanged(m_max);
106 void SimpleTimelineWidget::slotGoToPrev()
108 for (int i = m_keyframes.count() - 1; i >= 0; --i) {
109 if (m_keyframes.at(i) < m_position) {
110 slotSetPosition(m_keyframes.at(i));
111 emit positionChanged(m_keyframes.at(i));
116 // no keyframe before current position
117 slotSetPosition(m_min);
118 emit positionChanged(m_min);
121 void SimpleTimelineWidget::mousePressEvent(QMouseEvent* event)
123 if (qAbs(event->y() - m_lineHeight) < m_lineHeight / 5. && event->button() == Qt::LeftButton) {
124 int pos = (event->x() - 5) / m_scale;
125 foreach(const int &keyframe, m_keyframes) {
126 if (qAbs(keyframe - pos) < 5) {
127 m_currentKeyframeOriginal = keyframe;
128 m_currentKeyframe = pos;
135 // no keyframe next to mouse
136 m_currentKeyframe = m_currentKeyframeOriginal = -1;
137 m_position = (event->x() - 5) / m_scale;
138 emit positionChanged(m_position);
142 void SimpleTimelineWidget::mouseMoveEvent(QMouseEvent* event)
144 if (event->buttons() & Qt::LeftButton) {
145 int pos = qBound(m_min, (int)((event->x() - 5) / m_scale), m_max);
146 if (m_currentKeyframe >= 0) {
147 m_currentKeyframe = pos;
148 emit keyframeMoving(m_currentKeyframeOriginal, m_currentKeyframe);
151 emit positionChanged(pos);
160 void SimpleTimelineWidget::mouseReleaseEvent(QMouseEvent* event)
162 if (m_currentKeyframe > 0) {
163 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
167 void SimpleTimelineWidget::wheelEvent(QWheelEvent* event)
169 int change = event->delta() < 0 ? -1 : 1;
170 if (m_currentKeyframe > 0) {
171 m_currentKeyframe = qBound(m_min, m_currentKeyframe + change, m_max);
172 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
174 m_position = qBound(m_min, m_position + change, m_max);
175 emit positionChanged(m_position);
180 void SimpleTimelineWidget::paintEvent(QPaintEvent* event)
184 int max = width() - 6;
185 m_scale = (max - min) / (double)(m_max - m_min);
186 p.translate(min, m_lineHeight);
188 p.setPen(QPen(palette().foreground().color(), 1, Qt::SolidLine));
193 p.drawLine(0, 0, max, 0);
198 p.fillRect(QRectF(-1, -10, 3, 20).translated(m_position * m_scale, 0), QBrush(palette().foreground().color(), Qt::SolidPattern));
203 p.setPen(QPen(palette().highlight().color(), 1, Qt::SolidLine));
204 p.setBrush(Qt::NoBrush);
205 QPolygonF keyframe = QPolygonF() << QPointF(0, -4) << QPointF(-4, 0) << QPointF(0, 4) << QPointF(4, 0);
207 foreach (const int &pos, m_keyframes) {
209 tmp.translate(pos * m_scale, 0);
210 if (pos == m_currentKeyframe)
211 p.setBrush(QBrush(palette().highlight().color(), Qt::SolidPattern));
213 p.drawConvexPolygon(tmp);
215 if (pos == m_currentKeyframe)
216 p.setBrush(Qt::NoBrush);
220 #include "simpletimelinewidget.moc"