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>
32 SmallRuler::SmallRuler(MonitorManager *manager, QWidget *parent) :
34 ,m_cursorFramePosition(0)
41 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
42 setMouseTracking(true);
46 void SmallRuler::adjustScale(int maximum)
49 m_scale = (double) width() / (double) maximum;
50 if (m_scale == 0) m_scale = 1;
55 } else if (m_scale > 0.09) {
62 m_cursorPosition = m_cursorFramePosition * m_scale;
66 void SmallRuler::setZone(int start, int end)
69 if (end != -1 && start >= end) {
70 m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
72 } else if (end == -1 && start >= m_zoneEnd) {
73 m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
75 } else m_zoneStart = start;
78 if (start != -1 && end <= start) {
79 m_zoneStart = qMax(0, start - (m_zoneEnd - end));
81 } else if (start == -1 && end <= m_zoneStart) {
82 m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
84 } else m_zoneEnd = end;
89 void SmallRuler::setMarkers(QList < int > list)
95 QPoint SmallRuler::zone()
97 return QPoint(m_zoneStart, m_zoneEnd);
101 void SmallRuler::mousePressEvent(QMouseEvent * event)
103 const int pos = event->x() / m_scale;
104 if (event->button() == Qt::RightButton) {
105 // Right button clicked, move selection zone
106 if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
107 else m_zoneEnd = pos;
108 emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
111 } else emit seekRenderer((int) pos);
115 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
117 const int pos = event->x() / m_scale;
118 if (event->buttons() & Qt::LeftButton) emit seekRenderer((int) pos);
120 if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
121 setToolTip(i18n("Zone start: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneStart)));
122 } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
123 setToolTip(i18n("Zone end: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd)));
124 } else if (pos > m_zoneStart && pos < m_zoneEnd) {
125 setToolTip(i18n("Zone duration: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
126 } else setToolTip(i18n("Position: %1", m_manager->timecode().getTimecodeFromFrames(pos)));
130 bool SmallRuler::slotNewValue(int value)
132 if (value == m_cursorFramePosition) return false;
133 m_cursorFramePosition = value;
134 int oldPos = m_cursorPosition;
135 m_cursorPosition = value * m_scale;
136 const int offset = 6;
137 const int x = qMin(oldPos, m_cursorPosition);
138 const int w = qAbs(oldPos - m_cursorPosition);
139 update(x - offset, 4, w + 2 * offset, 6);
144 void SmallRuler::resizeEvent(QResizeEvent *)
146 adjustScale(m_maxval);
149 void SmallRuler::updatePixmap()
151 m_pixmap = QPixmap(width(), height());
152 m_pixmap.fill(palette().window().color());
153 QPainter p(&m_pixmap);
156 const int zoneStart = (int)(m_zoneStart * m_scale);
157 const int zoneEnd = (int)(m_zoneEnd * m_scale);
158 p.fillRect(zoneStart, height() / 2 - 1, zoneEnd - zoneStart, height() / 2, m_zoneColor);
161 p.setPen(palette().text().color());
162 // draw the little marks
163 fend = m_scale * m_small;
164 if (fend > 2) for (f = 0; f < width(); f += fend) {
165 p.drawLine((int)f, 0, (int)f, 3);
169 fend = m_scale * m_medium;
170 if (fend > 2) for (f = 0; f < width(); f += fend) {
171 p.drawLine((int)f, 0, (int)f, 6);
174 if (!m_markers.isEmpty()) {
176 for (int i = 0; i < m_markers.count(); i++) {
177 p.drawLine(m_markers.at(i) * m_scale, 0, m_markers.at(i) * m_scale, 9);
185 void SmallRuler::paintEvent(QPaintEvent *e)
191 p.drawPixmap(QPointF(), m_pixmap);
195 pa.setPoints(3, m_cursorPosition - 5, 10, m_cursorPosition + 5, 10, m_cursorPosition/*+0*/, 5);
196 p.setBrush(palette().text().color());
201 #include "smallruler.moc"