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"
25 #include <QMouseEvent>
26 #include <QStylePainter>
28 SmallRuler::SmallRuler(QWidget *parent) :
35 m_zoneColor = QColor(133, 255, 143);
38 void SmallRuler::adjustScale(int maximum)
41 m_scale = (double) width() / (double) maximum;
42 if (m_scale == 0) m_scale = 1;
47 } else if (m_scale > 0.09) {
54 m_cursorPosition = m_cursorFramePosition * m_scale;
58 void SmallRuler::setZone(int start, int end)
61 if (end != -1 && start >= end) {
62 m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
64 } else if (end == -1 && start >= m_zoneEnd) {
65 m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
67 } else m_zoneStart = start;
70 if (start != -1 && end <= start) {
71 m_zoneStart = qMax(0, start - (m_zoneEnd - end));
73 } else if (start == -1 && end <= m_zoneStart) {
74 m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
76 } else m_zoneEnd = end;
81 void SmallRuler::setMarkers(QList < int > list)
87 QPoint SmallRuler::zone()
89 return QPoint(m_zoneStart, m_zoneEnd);
93 void SmallRuler::mousePressEvent(QMouseEvent * event)
95 const int pos = event->x() / m_scale;
96 if (event->button() == Qt::RightButton) {
97 // Right button clicked, move selection zone
98 if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
100 emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
103 } else emit seekRenderer((int) pos);
107 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
109 const int pos = event->x() / m_scale;
110 if (event->buttons() & Qt::LeftButton) emit seekRenderer((int) pos);
113 void SmallRuler::slotNewValue(int value)
115 m_cursorFramePosition = value;
116 int oldPos = m_cursorPosition;
117 m_cursorPosition = value * m_scale;
118 if (oldPos == m_cursorPosition) return;
119 const int offset = 6;
120 const int x = qMin(oldPos, m_cursorPosition);
121 const int w = qAbs(oldPos - m_cursorPosition);
122 update(x - offset, 4, w + 2 * offset, 6);
126 void SmallRuler::resizeEvent(QResizeEvent *)
128 adjustScale(m_maxval);
131 void SmallRuler::updatePixmap()
133 m_pixmap = QPixmap(width(), height());
134 m_pixmap.fill(palette().window().color());
135 QPainter p(&m_pixmap);
138 const int zoneStart = (int)(m_zoneStart * m_scale);
139 const int zoneEnd = (int)(m_zoneEnd * m_scale);
140 p.fillRect(zoneStart, height() / 2 - 1, zoneEnd - zoneStart, height() / 2, m_zoneColor);
143 if (!m_markers.isEmpty()) {
145 for (int i = 0; i < m_markers.count(); i++) {
146 p.drawLine(m_markers.at(i) * m_scale, 0, m_markers.at(i) * m_scale, 9);
149 p.setPen(palette().dark().color());
150 // draw the little marks
151 fend = m_scale * m_small;
152 if (fend > 2) for (f = 0; f < width(); f += fend) {
153 p.drawLine((int)f, 0, (int)f, 3);
157 fend = m_scale * m_medium;
158 if (fend > 2) for (f = 0; f < width(); f += fend) {
159 p.drawLine((int)f, 0, (int)f, 6);
166 void SmallRuler::paintEvent(QPaintEvent *e)
172 p.drawPixmap(QPointF(), m_pixmap);
176 pa.setPoints(3, m_cursorPosition - 5, 10, m_cursorPosition + 5, 10, m_cursorPosition/*+0*/, 5);
177 p.setBrush(palette().dark().color());
182 #include "smallruler.moc"