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)
45 KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
46 m_zoneBrush = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, config);
48 setMouseTracking(true);
53 void SmallRuler::adjustScale(int maximum)
56 m_scale = (double) width() / (double) maximum;
57 if (m_scale == 0) m_scale = 1;
62 } else if (m_scale > 0.09) {
72 void SmallRuler::setZoneStart()
74 int pos = m_render->requestedSeekPosition;
75 if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
79 void SmallRuler::setZoneEnd()
81 int pos = m_render->requestedSeekPosition;
82 if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
86 void SmallRuler::setZone(int start, int end)
89 if (end != -1 && start >= end) {
90 m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
92 } else if (end == -1 && start >= m_zoneEnd) {
93 m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
95 } else m_zoneStart = start;
98 if (start != -1 && end <= start) {
99 m_zoneStart = qMax(0, start - (m_zoneEnd - end));
101 } else if (start == -1 && end <= m_zoneStart) {
102 m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
104 } else m_zoneEnd = end;
109 void SmallRuler::setMarkers(QList < int > list)
115 QPoint SmallRuler::zone()
117 return QPoint(m_zoneStart, m_zoneEnd);
121 void SmallRuler::mousePressEvent(QMouseEvent * event)
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));
132 emit seekRenderer((int) pos);
136 void SmallRuler::leaveEvent( QEvent * event )
140 m_overCursor = false;
146 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
148 const int pos = event->x() / m_scale;
149 if (event->button() == Qt::NoButton) {
150 if (qAbs(pos * m_scale - m_render->seekFramePosition()) < 6) {
156 else if (m_overCursor) {
157 m_overCursor = false;
161 if (event->buttons() & Qt::LeftButton) {
163 emit seekRenderer((int) pos);
167 if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
168 setToolTip(i18n("Zone start: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneStart)));
169 } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
170 setToolTip(i18n("Zone end: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd)));
171 } else if (pos > m_zoneStart && pos < m_zoneEnd) {
172 setToolTip(i18n("Zone duration: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
173 } else setToolTip(i18n("Position: %1", m_manager->timecode().getTimecodeFromFrames(pos)));
177 bool SmallRuler::slotNewValue(int value)
179 /*if (value == m_cursorFramePosition) return false;
180 m_cursorFramePosition = value;
181 int oldPos = m_cursorPosition;
182 m_cursorPosition = value * m_scale;
183 const int offset = 6;
184 const int x = qMin(oldPos, m_cursorPosition);
185 const int w = qAbs(oldPos - m_cursorPosition);
186 update(x - offset, 0, w + 2 * offset, height());*/
193 void SmallRuler::resizeEvent(QResizeEvent *)
195 adjustScale(m_maxval);
198 void SmallRuler::updatePixmap()
200 m_pixmap = QPixmap(width(), height());
201 m_pixmap.fill(palette().window().color());
202 QPainter p(&m_pixmap);
205 const int zoneStart = (int)(m_zoneStart * m_scale);
206 const int zoneEnd = (int)(m_zoneEnd * m_scale);
208 p.setBrush(m_zoneBrush.brush(this));
209 p.drawRect(zoneStart, height() / 2 - 1, zoneEnd - zoneStart, height() / 2);
212 p.setPen(palette().text().color());
213 // draw the little marks
214 fend = m_scale * m_small;
215 if (fend > 2) for (f = 0; f < width(); f += fend) {
216 p.drawLine((int)f, 0, (int)f, 3);
220 fend = m_scale * m_medium;
221 if (fend > 2) for (f = 0; f < width(); f += fend) {
222 p.drawLine((int)f, 0, (int)f, 6);
225 if (!m_markers.isEmpty()) {
227 for (int i = 0; i < m_markers.count(); i++) {
228 p.drawLine(m_markers.at(i) * m_scale, 0, m_markers.at(i) * m_scale, 9);
236 void SmallRuler::paintEvent(QPaintEvent *e)
242 p.drawPixmap(QPointF(), m_pixmap);
245 int cursorPos = m_render->seekFramePosition() * m_scale;
246 if (m_render->requestedSeekPosition != SEEK_INACTIVE) {
247 seekPos = m_render->requestedSeekPosition * m_scale - 1;
249 else seekPos = cursorPos - 1;
251 // Draw seeking pointer
252 p.fillRect(seekPos, 0, 3, height(), palette().text());
256 pa.setPoints(3, cursorPos - 6, 10, cursorPos + 6, 10, cursorPos/*+0*/, 4);
257 if (m_overCursor) p.setBrush(palette().highlight());
258 else p.setBrush(palette().text().color());
263 void SmallRuler::updatePalette()
265 KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
266 m_zoneBrush = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, config);
270 #include "smallruler.moc"