]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
Don't send several seek requests for same frame when seeking with monitor ruler
[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(MonitorManager *manager, Render *render, QWidget *parent) :
35         QWidget(parent)
36         ,m_cursorFramePosition(0)
37         ,m_maxval(2)
38         ,m_manager(manager)
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_zoneBrush = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, config);
47
48     setMouseTracking(true);
49     setMinimumHeight(10);
50     adjustScale(m_maxval);
51 }
52
53
54 void SmallRuler::adjustScale(int maximum)
55 {
56     m_maxval = maximum;
57     m_scale = (double) width() / (double) maximum;
58     if (m_scale == 0) m_scale = 1;
59
60     if (m_scale > 0.5) {
61         m_small = 25;
62         m_medium = 5 * 25;
63     } else if (m_scale > 0.09) {
64         m_small = 5 * 25;
65         m_medium = 30 * 25;
66     } else {
67         m_small = 30 * 25;
68         m_medium = 60 * 25;
69     }
70     updatePixmap();
71 }
72
73 void SmallRuler::setZoneStart()
74 {
75     int pos = m_render->requestedSeekPosition;
76     if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
77     setZone(pos, -1);
78 }
79
80 void SmallRuler::setZoneEnd()
81 {
82     int pos = m_render->requestedSeekPosition;
83     if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
84     setZone(-1, pos);
85 }
86
87 void SmallRuler::setZone(int start, int end)
88 {
89     if (start != -1) {
90         if (end != -1 && start >= end) {
91             m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
92             m_zoneStart = start;
93         } else if (end == -1 && start >= m_zoneEnd) {
94             m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
95             m_zoneStart = start;
96         } else m_zoneStart = start;
97     }
98     if (end != -1) {
99         if (start != -1 && end <= start) {
100             m_zoneStart = qMax(0, start - (m_zoneEnd - end));
101             m_zoneEnd = end;
102         } else if (start == -1 && end <= m_zoneStart) {
103             m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
104             m_zoneEnd = end;
105         } else m_zoneEnd = end;
106     }
107     updatePixmap();
108 }
109
110 void SmallRuler::setMarkers(QList < CommentedTime > list)
111 {
112     m_markers = list;
113     updatePixmap();
114 }
115
116 QPoint SmallRuler::zone()
117 {
118     return QPoint(m_zoneStart, m_zoneEnd);
119 }
120
121 // virtual
122 void SmallRuler::mousePressEvent(QMouseEvent * event)
123 {
124     const int pos = event->x() / m_scale;
125     if (event->button() == Qt::RightButton) {
126         // Right button clicked, move selection zone
127         if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
128         else m_zoneEnd = pos;
129         emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
130         updatePixmap();
131
132     } else if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) {
133         m_render->seekToFrame(pos);
134         m_lastSeekPosition = pos;
135         update();
136     }
137     event->accept();
138 }
139
140 // virtual
141 void SmallRuler::mouseReleaseEvent(QMouseEvent * event)
142 {
143     event->accept();
144 }
145
146
147 // virtual
148 void SmallRuler::leaveEvent(QEvent * event)
149 {
150     QWidget::leaveEvent(event);
151     if (m_cursorColor == palette().highlight()) {
152         m_cursorColor = palette().text();
153         update();
154     }
155 }
156
157 // virtual
158 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
159 {
160     const int pos = event->x() / m_scale;
161     if (event->buttons() & Qt::LeftButton) {
162         if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) {
163             m_render->seekToFrame(pos);
164             m_lastSeekPosition = pos;
165             update();
166         }
167     }
168     else {
169         if (m_cursorColor == palette().text() && qAbs(pos - m_cursorFramePosition) * m_scale < 7) {
170             // Mouse is over cursor
171             m_cursorColor = palette().highlight();
172             update();
173         }
174         else if (m_cursorColor == palette().highlight() && qAbs(pos - m_cursorFramePosition) * m_scale >= 7) {
175             m_cursorColor = palette().text();
176             update();
177         }
178         if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
179             setToolTip(i18n("Zone start: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneStart)));
180         } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
181             setToolTip(i18n("Zone end: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd)));
182         } else if (pos > m_zoneStart && pos < m_zoneEnd) {
183             setToolTip(i18n("Zone duration: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
184         } else setToolTip(i18n("Position: %1", m_manager->timecode().getTimecodeFromFrames(pos)));
185     }
186     event->accept();
187 }
188
189 void SmallRuler::refreshRuler()
190 {
191     m_lastSeekPosition = SEEK_INACTIVE;
192     update();
193 }
194
195 bool SmallRuler::slotNewValue(int value)
196 {
197     if (value == m_cursorFramePosition) return false;
198     if (value == m_lastSeekPosition) m_lastSeekPosition = SEEK_INACTIVE;
199     m_cursorFramePosition = value;
200     /*int oldPos = m_cursorPosition;
201     m_cursorPosition = value * m_scale;
202     const int offset = 6;
203     const int x = qMin(oldPos, m_cursorPosition);
204     const int w = qAbs(oldPos - m_cursorPosition);
205     update(x - offset, 0, w + 2 * offset, height());*/
206     update();
207     return true;
208 }
209
210
211 //virtual
212 void SmallRuler::resizeEvent(QResizeEvent *)
213 {
214     adjustScale(m_maxval);
215 }
216
217 void SmallRuler::updatePixmap()
218 {
219     m_pixmap = QPixmap(width(), height());
220     m_pixmap.fill(palette().window().color());
221     m_lastSeekPosition = SEEK_INACTIVE;
222     QPainter p(&m_pixmap);
223     double f, fend;
224
225     const int zoneStart = (int)(m_zoneStart * m_scale);
226     const int zoneEnd = (int)(m_zoneEnd * m_scale);
227     p.setPen(Qt::NoPen);
228     p.setBrush(m_zoneBrush.brush(this));
229     p.drawRect(zoneStart, height() / 2 - 1, zoneEnd - zoneStart, height() / 2);
230
231     // draw ruler
232     p.setPen(palette().text().color());
233     // draw the little marks
234     fend = m_scale * m_small;
235     if (fend > 2) for (f = 0; f < width(); f += fend) {
236         p.drawLine((int)f, 0, (int)f, 3);
237     }
238
239     // draw medium marks
240     fend = m_scale * m_medium;
241     if (fend > 2) for (f = 0; f < width(); f += fend) {
242         p.drawLine((int)f, 0, (int)f, 6);
243     }
244     // draw markers
245     if (!m_markers.isEmpty()) {
246         for (int i = 0; i < m_markers.count(); i++) {
247             int pos = m_markers.at(i).time().frames(m_manager->timecode().fps()) * m_scale;
248             p.setPen(CommentedTime::markerColor(m_markers.at(i).markerType()));
249             p.drawLine(pos, 0, pos, 9);
250         }
251     }
252     p.end();
253     update();
254 }
255
256 // virtual
257 void SmallRuler::paintEvent(QPaintEvent *e)
258 {
259
260     QPainter p(this);
261     QRect r = e->rect();
262     p.setClipRect(r);
263     p.drawPixmap(QPointF(), m_pixmap);
264
265     int cursorPos = m_cursorFramePosition * m_scale;
266     // draw pointer
267     QPolygon pa(3);
268     pa.setPoints(3, cursorPos - 6, 10, cursorPos + 6, 10, cursorPos/*+0*/, 4);
269     p.setBrush(m_cursorColor);
270     p.setPen(Qt::NoPen);
271     p.drawPolygon(pa);
272
273     // Draw seeking pointer
274     if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_cursorFramePosition) {
275         p.fillRect(m_lastSeekPosition * m_scale - 1, 0, 3, height(), palette().highlight());
276     }
277 }
278
279 void SmallRuler::updatePalette()
280 {
281     KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
282     m_zoneBrush = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, config);
283     updatePixmap();
284 }
285
286 #include "smallruler.moc"