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(Monitor *monitor, Render *render, QWidget *parent) :
36 ,m_cursorFramePosition(0)
40 ,m_lastSeekPosition(SEEK_INACTIVE)
41 ,m_cursorColor(palette().text())
45 KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
46 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, config).brush(this).color();
47 m_zoneColor.setAlpha(180);
49 setMouseTracking(true);
50 setMinimumHeight(QFontInfo(font()).pixelSize());
51 adjustScale(m_maxval);
55 void SmallRuler::adjustScale(int maximum)
58 m_scale = (double) width() / (double) maximum;
59 if (m_scale == 0) m_scale = 1;
64 } else if (m_scale > 0.09) {
74 void SmallRuler::setZoneStart()
76 int pos = m_render->requestedSeekPosition;
77 if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
81 void SmallRuler::setZoneEnd()
83 int pos = m_render->requestedSeekPosition;
84 if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition();
88 void SmallRuler::setZone(int start, int end)
91 if (end != -1 && start >= end) {
92 m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
94 } else if (end == -1 && start >= m_zoneEnd) {
95 m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
97 } else m_zoneStart = start;
100 if (start != -1 && end <= start) {
101 m_zoneStart = qMax(0, start - (m_zoneEnd - end));
103 } else if (start == -1 && end <= m_zoneStart) {
104 m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
106 } else m_zoneEnd = end;
111 void SmallRuler::setMarkers(QList < CommentedTime > list)
117 QPoint SmallRuler::zone()
119 return QPoint(m_zoneStart, m_zoneEnd);
123 void SmallRuler::mousePressEvent(QMouseEvent * event)
125 m_render->setActiveMonitor();
126 const int pos = event->x() / m_scale;
127 if (event->button() == Qt::RightButton) {
128 // Right button clicked, move selection zone
129 if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
130 else m_zoneEnd = pos;
131 emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
134 } else if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) {
135 m_render->seekToFrame(pos);
136 m_lastSeekPosition = pos;
143 void SmallRuler::mouseReleaseEvent(QMouseEvent * event)
150 void SmallRuler::leaveEvent(QEvent * event)
152 QWidget::leaveEvent(event);
153 if (m_cursorColor == palette().link()) {
154 m_cursorColor = palette().text();
160 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
162 const int pos = event->x() / m_scale;
163 if (event->buttons() & Qt::LeftButton) {
164 if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) {
165 m_render->seekToFrame(pos);
166 m_lastSeekPosition = pos;
171 if (m_cursorColor == palette().text() && qAbs(pos - m_cursorFramePosition) * m_scale < 7) {
172 // Mouse is over cursor
173 m_cursorColor = palette().link();
176 else if (m_cursorColor == palette().link() && qAbs(pos - m_cursorFramePosition) * m_scale >= 7) {
177 m_cursorColor = palette().text();
180 if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
181 setToolTip(i18n("Zone start: %1", m_monitor->getTimecodeFromFrames(m_zoneStart)));
182 } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
183 setToolTip(i18n("Zone end: %1", m_monitor->getTimecodeFromFrames(m_zoneEnd)));
185 for (int i = 0; i < m_markers.count(); i++) {
186 if (qAbs((pos - m_markers.at(i).time().frames(m_monitor->fps())) * m_scale) < 4) {
187 // We are on a marker
188 QString markerxt = m_monitor->getMarkerThumb(m_markers.at(i).time());
189 if (!markerxt.isEmpty()) {
190 markerxt.prepend("<img src=\"");
191 markerxt.append("\"><p align=\"center\">");
193 markerxt.append(m_markers.at(i).comment());
194 setToolTip(markerxt);
199 if (pos > m_zoneStart && pos < m_zoneEnd) {
200 setToolTip(i18n("Zone duration: %1", m_monitor->getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
202 else setToolTip(i18n("Position: %1", m_monitor->getTimecodeFromFrames(pos)));
207 void SmallRuler::refreshRuler()
209 m_lastSeekPosition = SEEK_INACTIVE;
213 bool SmallRuler::slotNewValue(int value)
215 if (value == m_cursorFramePosition) return false;
216 if (value == m_lastSeekPosition) m_lastSeekPosition = SEEK_INACTIVE;
217 m_cursorFramePosition = value;
218 /*int oldPos = m_cursorPosition;
219 m_cursorPosition = value * m_scale;
220 const int offset = 6;
221 const int x = qMin(oldPos, m_cursorPosition);
222 const int w = qAbs(oldPos - m_cursorPosition);
223 update(x - offset, 0, w + 2 * offset, height());*/
230 void SmallRuler::resizeEvent(QResizeEvent *)
232 adjustScale(m_maxval);
235 void SmallRuler::updatePixmap()
237 m_pixmap = QPixmap(width(), height());
238 m_pixmap.fill(palette().window().color());
239 m_lastSeekPosition = SEEK_INACTIVE;
240 QPainter p(&m_pixmap);
243 const int zoneStart = (int)(m_zoneStart * m_scale);
244 const int zoneEnd = (int)(m_zoneEnd * m_scale);
245 p.fillRect(zoneStart, 0, zoneEnd - zoneStart, height(), QBrush(m_zoneColor));
248 p.setPen(palette().midlight().color());
249 //p.drawLine(0, 0, width(), 0);
250 p.drawLine(0, height() - 1, width(), height() - 1);
251 p.setPen(palette().dark().color());
252 // draw the little marks
253 fend = m_scale * m_small;
254 if (fend > 2) for (f = 0; f < width(); f += fend) {
255 p.drawLine((int)f, 0, (int)f, 3);
259 fend = m_scale * m_medium;
260 if (fend > 2) for (f = 0; f < width(); f += fend) {
261 p.drawLine((int)f, 0, (int)f, 6);
264 if (!m_markers.isEmpty()) {
265 for (int i = 0; i < m_markers.count(); i++) {
266 int pos = m_markers.at(i).time().frames(m_monitor->fps()) * m_scale;
267 p.setPen(CommentedTime::markerColor(m_markers.at(i).markerType()));
268 p.drawLine(pos, 0, pos, 9);
276 void SmallRuler::paintEvent(QPaintEvent *e)
282 p.drawPixmap(QPointF(), m_pixmap);
283 p.setPen(palette().shadow().color());
284 //p.setRenderHint(QPainter::Antialiasing, true);
285 //p.drawRoundedRect(rect().adjusted(1,1,-2,-2), 3, 3);
287 int cursorPos = m_cursorFramePosition * m_scale;
290 pa.setPoints(3, cursorPos - 6, height() - 1, cursorPos + 6, height() - 1, cursorPos/*+0*/, height() - 8);
291 p.setBrush(m_cursorColor);
295 // Draw seeking pointer
296 if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_cursorFramePosition) {
297 p.fillRect(m_lastSeekPosition * m_scale - 1, 0, 3, height(), palette().linkVisited());
301 void SmallRuler::updatePalette()
303 KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme());
304 m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, config).brush(this).color();
305 m_zoneColor.setAlpha(180);
309 #include "smallruler.moc"