1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "smallruler.h"
22 #include "kdenlivesettings.h"
25 #include <KColorScheme>
28 #include <QMouseEvent>
29 #include <QStylePainter>
31 #define SEEK_INACTIVE (-1)
34 SmallRuler::SmallRuler(MonitorManager *manager, Render *render, QWidget *parent) :
36 ,m_cursorFramePosition(0)
41 ,m_lastSeekPosition(SEEK_INACTIVE)
42 ,m_cursorColor(palette().text())
46 KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
47 m_zoneBrush = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, config);
49 setMouseTracking(true);
54 void SmallRuler::adjustScale(int maximum)
57 m_scale = (double) width() / (double) maximum;
58 if (m_scale == 0) m_scale = 1;
63 } else if (m_scale > 0.09) {
73 void SmallRuler::setZoneStart()
75 int pos = m_render->requestedSeekPosition;
76 if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
80 void SmallRuler::setZoneEnd()
82 int pos = m_render->requestedSeekPosition;
83 if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
87 void SmallRuler::setZone(int start, int end)
90 if (end != -1 && start >= end) {
91 m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
93 } else if (end == -1 && start >= m_zoneEnd) {
94 m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
96 } else m_zoneStart = start;
99 if (start != -1 && end <= start) {
100 m_zoneStart = qMax(0, start - (m_zoneEnd - end));
102 } else if (start == -1 && end <= m_zoneStart) {
103 m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
105 } else m_zoneEnd = end;
110 void SmallRuler::setMarkers(QList < CommentedTime > list)
116 QPoint SmallRuler::zone()
118 return QPoint(m_zoneStart, m_zoneEnd);
122 void SmallRuler::mousePressEvent(QMouseEvent * event)
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));
133 m_render->seekToFrame(pos);
134 m_lastSeekPosition = pos;
140 void SmallRuler::leaveEvent(QEvent * event)
142 QWidget::leaveEvent(event);
143 if (m_cursorColor == palette().highlight()) {
144 m_cursorColor = palette().text();
150 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
152 QWidget::mouseMoveEvent(event);
153 const int pos = event->x() / m_scale;
154 if (event->buttons() & Qt::LeftButton) {
155 m_render->seekToFrame(pos);
156 m_lastSeekPosition = pos;
160 if (m_cursorColor == palette().text() && qAbs(pos - m_cursorFramePosition) * m_scale < 7) {
161 // Mouse is over cursor
162 m_cursorColor = palette().highlight();
165 else if (m_cursorColor == palette().highlight() && qAbs(pos - m_cursorFramePosition) * m_scale >= 7) {
166 m_cursorColor = palette().text();
169 if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
170 setToolTip(i18n("Zone start: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneStart)));
171 } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
172 setToolTip(i18n("Zone end: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd)));
173 } else if (pos > m_zoneStart && pos < m_zoneEnd) {
174 setToolTip(i18n("Zone duration: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
175 } else setToolTip(i18n("Position: %1", m_manager->timecode().getTimecodeFromFrames(pos)));
179 void SmallRuler::refreshRuler()
181 m_lastSeekPosition = SEEK_INACTIVE;
185 bool SmallRuler::slotNewValue(int value)
187 if (value == m_cursorFramePosition) return false;
188 if (value == m_lastSeekPosition) m_lastSeekPosition = SEEK_INACTIVE;
189 m_cursorFramePosition = value;
190 /*int oldPos = m_cursorPosition;
191 m_cursorPosition = value * m_scale;
192 const int offset = 6;
193 const int x = qMin(oldPos, m_cursorPosition);
194 const int w = qAbs(oldPos - m_cursorPosition);
195 update(x - offset, 0, w + 2 * offset, height());*/
202 void SmallRuler::resizeEvent(QResizeEvent *)
204 adjustScale(m_maxval);
207 void SmallRuler::updatePixmap()
209 m_pixmap = QPixmap(width(), height());
210 m_pixmap.fill(palette().window().color());
211 m_lastSeekPosition = SEEK_INACTIVE;
212 QPainter p(&m_pixmap);
215 const int zoneStart = (int)(m_zoneStart * m_scale);
216 const int zoneEnd = (int)(m_zoneEnd * m_scale);
218 p.setBrush(m_zoneBrush.brush(this));
219 p.drawRect(zoneStart, height() / 2 - 1, zoneEnd - zoneStart, height() / 2);
222 p.setPen(palette().text().color());
223 // draw the little marks
224 fend = m_scale * m_small;
225 if (fend > 2) for (f = 0; f < width(); f += fend) {
226 p.drawLine((int)f, 0, (int)f, 3);
230 fend = m_scale * m_medium;
231 if (fend > 2) for (f = 0; f < width(); f += fend) {
232 p.drawLine((int)f, 0, (int)f, 6);
235 if (!m_markers.isEmpty()) {
236 for (int i = 0; i < m_markers.count(); i++) {
237 int pos = m_markers.at(i).time().frames(m_manager->timecode().fps()) * m_scale;
238 p.setPen(CommentedTime::markerColor(m_markers.at(i).markerType()));
239 p.drawLine(pos, 0, pos, 9);
247 void SmallRuler::paintEvent(QPaintEvent *e)
253 p.drawPixmap(QPointF(), m_pixmap);
255 int cursorPos = m_cursorFramePosition * m_scale;
258 pa.setPoints(3, cursorPos - 6, 10, cursorPos + 6, 10, cursorPos/*+0*/, 4);
259 p.setBrush(m_cursorColor);
263 // Draw seeking pointer
264 if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_cursorFramePosition) {
265 p.fillRect(m_lastSeekPosition * m_scale - 1, 0, 3, height(), palette().highlight());
269 void SmallRuler::updatePalette()
271 KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
272 m_zoneBrush = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, config);
276 #include "smallruler.moc"