]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
Fix timeline seek sometimes not refreshing on last frame of the seek, small update...
[kdenlive] / src / smallruler.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "smallruler.h"
22 #include "kdenlivesettings.h"
23
24 #include <KDebug>
25 #include <KColorScheme>
26 #include <KLocale>
27
28 #include <QMouseEvent>
29 #include <QStylePainter>
30
31 #define SEEK_INACTIVE (-1)
32
33
34 SmallRuler::SmallRuler(Monitor *monitor, Render *render, QWidget *parent) :
35         QWidget(parent)
36         ,m_cursorFramePosition(0)
37         ,m_maxval(2)
38         ,m_monitor(monitor)
39         ,m_render(render)
40         ,m_lastSeekPosition(SEEK_INACTIVE)
41         ,m_cursorColor(palette().text())
42 {
43     m_zoneStart = 10;
44     m_zoneEnd = 60;
45     KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
46     m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, config).brush(this).color();
47     m_zoneColor.setAlpha(180);
48
49     setMouseTracking(true);
50     setMinimumHeight(8);
51     adjustScale(m_maxval);
52 }
53
54
55 void SmallRuler::adjustScale(int maximum)
56 {
57     m_maxval = maximum;
58     m_scale = (double) width() / (double) maximum;
59     if (m_scale == 0) m_scale = 1;
60
61     if (m_scale > 0.5) {
62         m_small = 25;
63         m_medium = 5 * 25;
64     } else if (m_scale > 0.09) {
65         m_small = 5 * 25;
66         m_medium = 30 * 25;
67     } else {
68         m_small = 30 * 25;
69         m_medium = 60 * 25;
70     }
71     updatePixmap();
72 }
73
74 void SmallRuler::setZoneStart()
75 {
76     int pos = m_render->requestedSeekPosition;
77     if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
78     setZone(pos, -1);
79 }
80
81 void SmallRuler::setZoneEnd()
82 {
83     int pos = m_render->requestedSeekPosition;
84     if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
85     setZone(-1, pos);
86 }
87
88 void SmallRuler::setZone(int start, int end)
89 {
90     if (start != -1) {
91         if (end != -1 && start >= end) {
92             m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
93             m_zoneStart = start;
94         } else if (end == -1 && start >= m_zoneEnd) {
95             m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
96             m_zoneStart = start;
97         } else m_zoneStart = start;
98     }
99     if (end != -1) {
100         if (start != -1 && end <= start) {
101             m_zoneStart = qMax(0, start - (m_zoneEnd - end));
102             m_zoneEnd = end;
103         } else if (start == -1 && end <= m_zoneStart) {
104             m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
105             m_zoneEnd = end;
106         } else m_zoneEnd = end;
107     }
108     updatePixmap();
109 }
110
111 void SmallRuler::setMarkers(QList < CommentedTime > list)
112 {
113     m_markers = list;
114     updatePixmap();
115 }
116
117 QPoint SmallRuler::zone()
118 {
119     return QPoint(m_zoneStart, m_zoneEnd);
120 }
121
122 // virtual
123 void SmallRuler::mousePressEvent(QMouseEvent * event)
124 {
125     const int pos = event->x() / m_scale;
126     if (event->button() == Qt::RightButton) {
127         // Right button clicked, move selection zone
128         if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
129         else m_zoneEnd = pos;
130         emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
131         updatePixmap();
132
133     } else if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) {
134         m_render->seekToFrame(pos);
135         m_lastSeekPosition = pos;
136         update();
137     }
138     event->accept();
139 }
140
141 // virtual
142 void SmallRuler::mouseReleaseEvent(QMouseEvent * event)
143 {
144     event->accept();
145 }
146
147
148 // virtual
149 void SmallRuler::leaveEvent(QEvent * event)
150 {
151     QWidget::leaveEvent(event);
152     if (m_cursorColor == palette().link()) {
153         m_cursorColor = palette().text();
154         update();
155     }
156 }
157
158 // virtual
159 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
160 {
161     const int pos = event->x() / m_scale;
162     if (event->buttons() & Qt::LeftButton) {
163         if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) {
164             m_render->seekToFrame(pos);
165             m_lastSeekPosition = pos;
166             update();
167         }
168     }
169     else {
170         if (m_cursorColor == palette().text() && qAbs(pos - m_cursorFramePosition) * m_scale < 7) {
171             // Mouse is over cursor
172             m_cursorColor = palette().link();
173             update();
174         }
175         else if (m_cursorColor == palette().link() && qAbs(pos - m_cursorFramePosition) * m_scale >= 7) {
176             m_cursorColor = palette().text();
177             update();
178         }
179         if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
180             setToolTip(i18n("Zone start: %1", m_monitor->getTimecodeFromFrames(m_zoneStart)));
181         } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
182             setToolTip(i18n("Zone end: %1", m_monitor->getTimecodeFromFrames(m_zoneEnd)));
183         } 
184         for (int i = 0; i < m_markers.count(); i++) {
185             if (qAbs((pos - m_markers.at(i).time().frames(m_monitor->fps())) * m_scale) < 4) {
186                 // We are on a marker
187                 QString markerxt = m_monitor->getMarkerThumb(m_markers.at(i).time());
188                 if (!markerxt.isEmpty()) {
189                     markerxt.prepend("<img src=\"");
190                     markerxt.append("\"><p align=\"center\">");
191                 }
192                 markerxt.append(m_markers.at(i).comment());
193                 setToolTip(markerxt);
194                 event->accept();
195                 return;
196             }
197         }
198         if (pos > m_zoneStart && pos < m_zoneEnd) {
199             setToolTip(i18n("Zone duration: %1", m_monitor->getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
200         }
201         else setToolTip(i18n("Position: %1", m_monitor->getTimecodeFromFrames(pos)));
202     }
203     event->accept();
204 }
205
206 void SmallRuler::refreshRuler()
207 {
208     m_lastSeekPosition = SEEK_INACTIVE;
209     update();
210 }
211
212 bool SmallRuler::slotNewValue(int value)
213 {
214     if (value == m_cursorFramePosition) return false;
215     if (value == m_lastSeekPosition) m_lastSeekPosition = SEEK_INACTIVE;
216     m_cursorFramePosition = value;
217     /*int oldPos = m_cursorPosition;
218     m_cursorPosition = value * m_scale;
219     const int offset = 6;
220     const int x = qMin(oldPos, m_cursorPosition);
221     const int w = qAbs(oldPos - m_cursorPosition);
222     update(x - offset, 0, w + 2 * offset, height());*/
223     update();
224     return true;
225 }
226
227
228 //virtual
229 void SmallRuler::resizeEvent(QResizeEvent *)
230 {
231     adjustScale(m_maxval);
232 }
233
234 void SmallRuler::updatePixmap()
235 {
236     m_pixmap = QPixmap(width(), height());
237     m_pixmap.fill(palette().window().color());
238     m_lastSeekPosition = SEEK_INACTIVE;
239     QPainter p(&m_pixmap);
240     double f, fend;
241
242     const int zoneStart = (int)(m_zoneStart * m_scale);
243     const int zoneEnd = (int)(m_zoneEnd * m_scale);
244     p.fillRect(zoneStart, 0, zoneEnd - zoneStart, height(), QBrush(m_zoneColor));
245
246     // draw ruler
247     p.setPen(palette().midlight().color());
248     //p.drawLine(0, 0, width(), 0);
249     p.drawLine(0, height() - 1, width(), height() - 1);
250     p.setPen(palette().dark().color());
251     // draw the little marks
252     fend = m_scale * m_small;
253     if (fend > 2) for (f = 0; f < width(); f += fend) {
254         p.drawLine((int)f, 0, (int)f, 3);
255     }
256
257     // draw medium marks
258     fend = m_scale * m_medium;
259     if (fend > 2) for (f = 0; f < width(); f += fend) {
260         p.drawLine((int)f, 0, (int)f, 6);
261     }
262     // draw markers
263     if (!m_markers.isEmpty()) {
264         for (int i = 0; i < m_markers.count(); i++) {
265             int pos = m_markers.at(i).time().frames(m_monitor->fps()) * m_scale;
266             p.setPen(CommentedTime::markerColor(m_markers.at(i).markerType()));
267             p.drawLine(pos, 0, pos, 9);
268         }
269     }
270     p.end();
271     update();
272 }
273
274 // virtual
275 void SmallRuler::paintEvent(QPaintEvent *e)
276 {
277
278     QPainter p(this);
279     QRect r = e->rect();
280     p.setClipRect(r);
281     p.drawPixmap(QPointF(), m_pixmap);
282     p.setPen(palette().shadow().color());
283     //p.setRenderHint(QPainter::Antialiasing, true);
284     //p.drawRoundedRect(rect().adjusted(1,1,-2,-2), 3, 3);
285
286     int cursorPos = m_cursorFramePosition * m_scale;
287     // draw pointer
288     QPolygon pa(3);
289     pa.setPoints(3, cursorPos - 6, 7, cursorPos + 6, 7, cursorPos/*+0*/, 0);
290     p.setBrush(m_cursorColor);
291     p.setPen(Qt::NoPen);
292     p.drawPolygon(pa);
293
294     // Draw seeking pointer
295     if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_cursorFramePosition) {
296         p.fillRect(m_lastSeekPosition * m_scale - 1, 0, 3, height(), palette().linkVisited());
297     }
298 }
299
300 void SmallRuler::updatePalette()
301 {
302     KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
303     m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, config).brush(this).color();
304     m_zoneColor.setAlpha(180);
305     updatePixmap();
306 }
307
308 #include "smallruler.moc"