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),
38 setMouseTracking(true);
39 setMinimumSize(QSize(150, 20));
40 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
41 setFont(KGlobalSettings::toolBarFont());
42 QPalette p = palette();
43 KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
44 m_colSelected = scheme.decoration(KColorScheme::HoverColor).color();
45 m_colKeyframe = scheme.foreground(KColorScheme::LinkText).color();
46 m_colKeyframeBg = scheme.shade(KColorScheme::MidShade);
49 void SimpleTimelineWidget::setKeyframes(QList <int> keyframes)
51 m_keyframes = keyframes;
53 m_currentKeyframe = m_currentKeyframeOriginal = -1;
57 void SimpleTimelineWidget::slotSetPosition(int pos)
63 void SimpleTimelineWidget::slotAddKeyframe(int pos, int select)
68 m_keyframes.append(pos);
71 m_currentKeyframe = m_currentKeyframeOriginal = pos;
74 emit keyframeAdded(pos);
77 void SimpleTimelineWidget::slotAddRemove()
79 if (m_keyframes.contains(m_position))
80 slotRemoveKeyframe(m_position);
82 slotAddKeyframe(m_position);
85 void SimpleTimelineWidget::slotRemoveKeyframe(int pos)
87 m_keyframes.removeAll(pos);
88 if (m_currentKeyframe == pos)
89 m_currentKeyframe = m_currentKeyframeOriginal = -1;
91 emit keyframeRemoved(pos);
94 void SimpleTimelineWidget::setDuration(int dur)
99 void SimpleTimelineWidget::slotGoToNext()
101 foreach (const int &keyframe, m_keyframes) {
102 if (keyframe > m_position) {
103 slotSetPosition(keyframe);
104 emit positionChanged(keyframe);
109 // no keyframe after current position
110 slotSetPosition(m_duration);
111 emit positionChanged(m_duration);
114 void SimpleTimelineWidget::slotGoToPrev()
116 for (int i = m_keyframes.count() - 1; i >= 0; --i) {
117 if (m_keyframes.at(i) < m_position) {
118 slotSetPosition(m_keyframes.at(i));
119 emit positionChanged(m_keyframes.at(i));
124 // no keyframe before current position
126 emit positionChanged(0);
129 void SimpleTimelineWidget::mousePressEvent(QMouseEvent* event)
131 int pos = event->x() / m_scale;
132 if (event->y() < m_lineHeight && event->button() == Qt::LeftButton) {
133 foreach(const int &keyframe, m_keyframes) {
134 if (qAbs(keyframe - pos) < 5) {
135 m_currentKeyframeOriginal = keyframe;
136 m_keyframes[m_keyframes.indexOf(keyframe)] = pos;
137 m_currentKeyframe = pos;
144 // no keyframe next to mouse
145 m_currentKeyframe = m_currentKeyframeOriginal = -1;
147 emit positionChanged(pos);
151 void SimpleTimelineWidget::mouseMoveEvent(QMouseEvent* event)
153 if (event->buttons() & Qt::LeftButton) {
154 int pos = qBound(0, (int)(event->x() / m_scale), m_duration);
155 if (m_currentKeyframe >= 0) {
156 // should we maybe sort here?
157 m_keyframes[m_keyframes.indexOf(m_currentKeyframe)] = pos;
158 m_currentKeyframe = pos;
159 emit keyframeMoving(m_currentKeyframeOriginal, m_currentKeyframe);
162 emit positionChanged(pos);
171 void SimpleTimelineWidget::mouseReleaseEvent(QMouseEvent* event)
175 if (m_currentKeyframe >= 0) {
177 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
181 void SimpleTimelineWidget::wheelEvent(QWheelEvent* event)
183 int change = event->delta() < 0 ? -1 : 1;
184 if (m_currentKeyframe > 0) {
185 m_currentKeyframe = qBound(0, m_currentKeyframe + change, m_duration);
186 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
188 m_position = qBound(0, m_position + change, m_duration);
189 emit positionChanged(m_position);
194 void SimpleTimelineWidget::paintEvent(QPaintEvent* event)
198 QStylePainter p(this);
199 m_scale = width() / (double)(m_duration);
200 p.translate(0, m_lineHeight);
202 p.setPen(m_colKeyframe);
203 p.setBrush(m_colKeyframeBg);
208 QPolygonF keyframe = QPolygonF() << QPointF(0, -10) << QPointF(-4, -6) << QPointF(0, -2) << QPointF(4, -6);
210 foreach (const int &pos, m_keyframes) {
212 tmp.translate(pos * m_scale, 0);
213 if (pos == m_currentKeyframe)
214 p.setBrush(m_colSelected);
216 p.drawConvexPolygon(tmp);
217 p.drawLine(QLineF(0, -1, 0, 5).translated(pos * m_scale, 0));
219 if (pos == m_currentKeyframe)
220 p.setBrush(m_colKeyframeBg);
223 p.setPen(palette().dark().color());
228 p.drawLine(0, 0, width(), 0);
233 QPolygonF position = QPolygonF() << QPointF(0, 1) << QPointF(5, 6) << QPointF(-5, 6);
234 position.translate(m_position * m_scale, 0);
235 p.setBrush(palette().dark().color());
236 p.drawConvexPolygon(position);
239 #include "simpletimelinewidget.moc"