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"
20 #include "kdenlivesettings.h"
22 #include <QStylePainter>
23 #include <QMouseEvent>
25 #include <KGlobalSettings>
26 #include <KColorScheme>
29 SimpleTimelineWidget::SimpleTimelineWidget(QWidget* parent) :
33 m_currentKeyframe(-1),
34 m_currentKeyframeOriginal(-1),
39 setMouseTracking(true);
40 setMinimumSize(QSize(150, 20));
41 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
42 setFont(KGlobalSettings::toolBarFont());
43 QPalette p = palette();
44 KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
45 m_colSelected = scheme.decoration(KColorScheme::HoverColor).color();
46 m_colKeyframe = scheme.foreground(KColorScheme::LinkText).color();
47 m_colKeyframeBg = scheme.shade(KColorScheme::MidShade);
50 void SimpleTimelineWidget::setKeyframes(QList <int> keyframes)
52 m_keyframes = keyframes;
54 m_currentKeyframe = m_currentKeyframeOriginal = -1;
58 void SimpleTimelineWidget::slotSetPosition(int pos)
64 void SimpleTimelineWidget::slotAddKeyframe(int pos, int select)
69 m_keyframes.append(pos);
72 m_currentKeyframe = m_currentKeyframeOriginal = pos;
75 emit keyframeAdded(pos);
78 void SimpleTimelineWidget::slotAddRemove()
80 if (m_keyframes.contains(m_position))
81 slotRemoveKeyframe(m_position);
83 slotAddKeyframe(m_position);
86 void SimpleTimelineWidget::slotRemoveKeyframe(int pos)
88 m_keyframes.removeAll(pos);
89 if (m_currentKeyframe == pos)
90 m_currentKeyframe = m_currentKeyframeOriginal = -1;
92 emit keyframeRemoved(pos);
95 void SimpleTimelineWidget::setDuration(int dur)
100 void SimpleTimelineWidget::slotGoToNext()
102 foreach (const int &keyframe, m_keyframes) {
103 if (keyframe > m_position) {
104 slotSetPosition(keyframe);
105 emit positionChanged(keyframe);
110 // no keyframe after current position
111 slotSetPosition(m_duration);
112 emit positionChanged(m_duration);
115 void SimpleTimelineWidget::slotGoToPrev()
117 for (int i = m_keyframes.count() - 1; i >= 0; --i) {
118 if (m_keyframes.at(i) < m_position) {
119 slotSetPosition(m_keyframes.at(i));
120 emit positionChanged(m_keyframes.at(i));
125 // no keyframe before current position
127 emit positionChanged(0);
130 void SimpleTimelineWidget::mousePressEvent(QMouseEvent* event)
132 int pos = event->x() / m_scale;
133 if (event->y() < m_lineHeight && event->button() == Qt::LeftButton) {
134 foreach(const int &keyframe, m_keyframes) {
135 if (qAbs(keyframe - pos) < 5) {
136 m_currentKeyframeOriginal = keyframe;
137 m_keyframes[m_keyframes.indexOf(keyframe)] = pos;
138 m_currentKeyframe = pos;
145 // no keyframe next to mouse
146 m_currentKeyframe = m_currentKeyframeOriginal = -1;
148 emit positionChanged(pos);
152 void SimpleTimelineWidget::mouseMoveEvent(QMouseEvent* event)
154 int pos = qBound(0, (int)(event->x() / m_scale), m_duration);
155 if (event->buttons() & Qt::LeftButton) {
156 if (m_currentKeyframe >= 0) {
157 if (!m_keyframes.contains(pos)) {
158 // snap to position cursor
159 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5 && !m_keyframes.contains(m_position))
161 // should we maybe sort here?
162 m_keyframes[m_keyframes.indexOf(m_currentKeyframe)] = pos;
163 m_currentKeyframe = pos;
164 emit keyframeMoving(m_currentKeyframeOriginal, m_currentKeyframe);
168 emit positionChanged(pos);
173 if (event->y() < m_lineHeight) {
174 foreach(const int &keyframe, m_keyframes) {
175 if (qAbs(keyframe - pos) < 5) {
176 m_hoverKeyframe = keyframe;
177 setCursor(Qt::PointingHandCursor);
184 if (m_hoverKeyframe != -1) {
185 m_hoverKeyframe = -1;
186 setCursor(Qt::ArrowCursor);
192 void SimpleTimelineWidget::mouseReleaseEvent(QMouseEvent* event)
196 if (m_currentKeyframe >= 0) {
198 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
202 void SimpleTimelineWidget::wheelEvent(QWheelEvent* event)
204 int change = event->delta() < 0 ? -1 : 1;
205 if (m_currentKeyframe > 0) {
206 m_currentKeyframe = qBound(0, m_currentKeyframe + change, m_duration);
207 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
209 m_position = qBound(0, m_position + change, m_duration);
210 emit positionChanged(m_position);
215 void SimpleTimelineWidget::paintEvent(QPaintEvent* event)
219 QStylePainter p(this);
220 m_scale = width() / (double)(m_duration);
221 p.translate(0, m_lineHeight);
223 p.setPen(m_colKeyframe);
224 p.setBrush(m_colKeyframeBg);
229 QPolygonF keyframe = QPolygonF() << QPointF(0, -10) << QPointF(-4, -6) << QPointF(0, -2) << QPointF(4, -6);
231 foreach (const int &pos, m_keyframes) {
233 tmp.translate(pos * m_scale, 0);
234 if (pos == m_currentKeyframe || pos == m_hoverKeyframe)
235 p.setBrush(m_colSelected);
237 p.drawConvexPolygon(tmp);
238 p.drawLine(QLineF(0, -1, 0, 5).translated(pos * m_scale, 0));
240 if (pos == m_currentKeyframe || pos == m_hoverKeyframe)
241 p.setBrush(m_colKeyframeBg);
244 p.setPen(palette().dark().color());
249 p.drawLine(0, 0, width(), 0);
254 QPolygonF position = QPolygonF() << QPointF(0, 1) << QPointF(5, 6) << QPointF(-5, 6);
255 position.translate(m_position * m_scale, 0);
256 p.setBrush(palette().dark().color());
257 p.drawConvexPolygon(position);
260 #include "simpletimelinewidget.moc"