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;
55 emit atKeyframe(m_keyframes.contains(m_position));
59 void SimpleTimelineWidget::slotSetPosition(int pos)
61 if (pos != m_position) {
63 emit atKeyframe(m_keyframes.contains(m_position));
68 void SimpleTimelineWidget::slotAddKeyframe(int pos, int select)
73 m_keyframes.append(pos);
76 m_currentKeyframe = m_currentKeyframeOriginal = pos;
79 emit keyframeAdded(pos);
80 if (pos == m_position)
81 emit atKeyframe(true);
84 void SimpleTimelineWidget::slotAddRemove()
86 if (m_keyframes.contains(m_position))
87 slotRemoveKeyframe(m_position);
89 slotAddKeyframe(m_position);
92 void SimpleTimelineWidget::slotRemoveKeyframe(int pos)
94 m_keyframes.removeAll(pos);
95 if (m_currentKeyframe == pos)
96 m_currentKeyframe = m_currentKeyframeOriginal = -1;
98 emit keyframeRemoved(pos);
99 if (pos == m_position)
100 emit atKeyframe(false);
103 void SimpleTimelineWidget::setDuration(int dur)
108 void SimpleTimelineWidget::slotGoToNext()
110 foreach (const int &keyframe, m_keyframes) {
111 if (keyframe > m_position) {
112 slotSetPosition(keyframe);
113 emit positionChanged(keyframe);
114 emit atKeyframe(true);
119 // no keyframe after current position
120 slotSetPosition(m_duration);
121 emit positionChanged(m_duration);
122 emit atKeyframe(false);
125 void SimpleTimelineWidget::slotGoToPrev()
127 for (int i = m_keyframes.count() - 1; i >= 0; --i) {
128 if (m_keyframes.at(i) < m_position) {
129 slotSetPosition(m_keyframes.at(i));
130 emit positionChanged(m_keyframes.at(i));
131 emit atKeyframe(true);
136 // no keyframe before current position
138 emit positionChanged(0);
139 emit atKeyframe(false);
142 void SimpleTimelineWidget::mousePressEvent(QMouseEvent* event)
144 int pos = event->x() / m_scale;
145 if (event->y() < m_lineHeight && event->button() == Qt::LeftButton) {
146 foreach(const int &keyframe, m_keyframes) {
147 if (qAbs(keyframe - pos) < 5) {
148 m_currentKeyframeOriginal = keyframe;
149 m_keyframes[m_keyframes.indexOf(keyframe)] = pos;
150 m_currentKeyframe = pos;
157 // no keyframe next to mouse
158 m_currentKeyframe = m_currentKeyframeOriginal = -1;
160 emit positionChanged(pos);
161 emit atKeyframe(m_keyframes.contains(pos));
165 void SimpleTimelineWidget::mouseMoveEvent(QMouseEvent* event)
167 int pos = qBound(0, (int)(event->x() / m_scale), m_duration);
168 if (event->buttons() & Qt::LeftButton) {
169 if (m_currentKeyframe >= 0) {
170 if (!m_keyframes.contains(pos)) {
171 // snap to position cursor
172 if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5 && !m_keyframes.contains(m_position))
174 // should we maybe sort here?
175 m_keyframes[m_keyframes.indexOf(m_currentKeyframe)] = pos;
176 m_currentKeyframe = pos;
177 emit keyframeMoving(m_currentKeyframeOriginal, m_currentKeyframe);
178 emit atKeyframe(m_keyframes.contains(m_position));
182 emit positionChanged(pos);
183 emit atKeyframe(m_keyframes.contains(pos));
188 if (event->y() < m_lineHeight) {
189 foreach(const int &keyframe, m_keyframes) {
190 if (qAbs(keyframe - pos) < 5) {
191 m_hoverKeyframe = keyframe;
192 setCursor(Qt::PointingHandCursor);
199 if (m_hoverKeyframe != -1) {
200 m_hoverKeyframe = -1;
201 setCursor(Qt::ArrowCursor);
207 void SimpleTimelineWidget::mouseReleaseEvent(QMouseEvent* event)
211 if (m_currentKeyframe >= 0) {
213 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
217 void SimpleTimelineWidget::mouseDoubleClickEvent(QMouseEvent* event)
219 if (event->button() == Qt::LeftButton && event->y() < m_lineHeight) {
220 int pos = qBound(0, (int)(event->x() / m_scale), m_duration);
221 foreach(const int &keyframe, m_keyframes) {
222 if (qAbs(keyframe - pos) < 5) {
223 m_keyframes.removeAll(keyframe);
224 if (keyframe == m_currentKeyframe)
225 m_currentKeyframe = m_currentKeyframeOriginal = -1;
226 emit keyframeRemoved(keyframe);
227 if (keyframe == m_position)
228 emit atKeyframe(false);
234 m_keyframes.append(pos);
236 emit keyframeAdded(pos);
237 if (pos == m_position)
238 emit atKeyframe(true);
240 QWidget::mouseDoubleClickEvent(event);
244 void SimpleTimelineWidget::wheelEvent(QWheelEvent* event)
246 int change = event->delta() < 0 ? -1 : 1;
247 if (m_currentKeyframe > 0) {
248 m_currentKeyframe = qBound(0, m_currentKeyframe + change, m_duration);
249 emit keyframeMoved(m_currentKeyframeOriginal, m_currentKeyframe);
251 m_position = qBound(0, m_position + change, m_duration);
252 emit positionChanged(m_position);
254 emit atKeyframe(m_keyframes.contains(m_position));
258 void SimpleTimelineWidget::paintEvent(QPaintEvent* event)
262 QStylePainter p(this);
263 m_scale = width() / (double)(m_duration);
264 p.translate(0, m_lineHeight);
266 p.setPen(m_colKeyframe);
267 p.setBrush(m_colKeyframeBg);
272 QPolygonF keyframe = QPolygonF() << QPointF(0, -10) << QPointF(-4, -6) << QPointF(0, -2) << QPointF(4, -6);
274 foreach (const int &pos, m_keyframes) {
276 tmp.translate(pos * m_scale, 0);
277 if (pos == m_currentKeyframe || pos == m_hoverKeyframe)
278 p.setBrush(m_colSelected);
280 p.drawConvexPolygon(tmp);
281 p.drawLine(QLineF(0, -1, 0, 5).translated(pos * m_scale, 0));
283 if (pos == m_currentKeyframe || pos == m_hoverKeyframe)
284 p.setBrush(m_colKeyframeBg);
287 p.setPen(palette().dark().color());
292 p.drawLine(0, 0, width(), 0);
297 QPolygonF position = QPolygonF() << QPointF(0, 1) << QPointF(5, 6) << QPointF(-5, 6);
298 position.translate(m_position * m_scale, 0);
299 p.setBrush(palette().dark().color());
300 p.drawConvexPolygon(position);
303 #include "simpletimelinewidget.moc"