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