X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fsmallruler.cpp;h=d5b99032b95fa3b8b4cb6461491193260fde748a;hb=e83d05274cca3326b7818fe782cad5a5656bd704;hp=62b3baa72fe77852fe45512d140b42c855dbb15b;hpb=f697b9fd5408e77412f2ee35aa406173630890bf;p=kdenlive diff --git a/src/smallruler.cpp b/src/smallruler.cpp index 62b3baa7..d5b99032 100644 --- a/src/smallruler.cpp +++ b/src/smallruler.cpp @@ -1,127 +1,309 @@ +/*************************************************************************** + * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + ***************************************************************************/ + + +#include "smallruler.h" +#include "kdenlivesettings.h" + +#include +#include +#include #include #include -#include +#define SEEK_INACTIVE (-1) -#define LITTLE_MARK_X2 15 -#define LITTLE_MARK_X1 13 -#define MIDDLE_MARK_X1 9 -#define MIDDLE_MARK_X2 15 -#define LABEL_SIZE 8 +SmallRuler::SmallRuler(Monitor *monitor, Render *render, QWidget *parent) : + QWidget(parent) + ,m_cursorFramePosition(0) + ,m_maxval(2) + ,m_monitor(monitor) + ,m_render(render) + ,m_lastSeekPosition(SEEK_INACTIVE) + ,m_cursorColor(palette().text()) +{ + m_zoneStart = 10; + m_zoneEnd = 60; + KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme()); + m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, config).brush(this).color(); + m_zoneColor.setAlpha(180); -#include "smallruler.h" + setMouseTracking(true); + setMinimumHeight(QFontInfo(font()).pixelSize()); + adjustScale(m_maxval); +} + + +void SmallRuler::adjustScale(int maximum) +{ + m_maxval = maximum; + m_scale = (double) width() / (double) maximum; + if (m_scale == 0) m_scale = 1; + + if (m_scale > 0.5) { + m_small = 25; + m_medium = 5 * 25; + } else if (m_scale > 0.09) { + m_small = 5 * 25; + m_medium = 30 * 25; + } else { + m_small = 30 * 25; + m_medium = 60 * 25; + } + updatePixmap(); +} + +void SmallRuler::setZoneStart() +{ + int pos = m_render->requestedSeekPosition; + if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition(); + setZone(pos, -1); +} + +void SmallRuler::setZoneEnd() +{ + int pos = m_render->requestedSeekPosition; + if (pos == SEEK_INACTIVE) pos = m_render->seekFramePosition(); + setZone(-1, pos); +} + +void SmallRuler::setZone(int start, int end) +{ + if (start != -1) { + if (end != -1 && start >= end) { + m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart)); + m_zoneStart = start; + } else if (end == -1 && start >= m_zoneEnd) { + m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart)); + m_zoneStart = start; + } else m_zoneStart = start; + } + if (end != -1) { + if (start != -1 && end <= start) { + m_zoneStart = qMax(0, start - (m_zoneEnd - end)); + m_zoneEnd = end; + } else if (start == -1 && end <= m_zoneStart) { + m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end)); + m_zoneEnd = end; + } else m_zoneEnd = end; + } + updatePixmap(); +} + +void SmallRuler::setMarkers(const QList &list) +{ + m_markers = list; + updatePixmap(); +} + +QPoint SmallRuler::zone() const +{ + return QPoint(m_zoneStart, m_zoneEnd); +} +// virtual +void SmallRuler::mousePressEvent(QMouseEvent * event) +{ + m_render->setActiveMonitor(); + const int pos = event->x() / m_scale; + if (event->button() == Qt::RightButton) { + // Right button clicked, move selection zone + if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos; + else m_zoneEnd = pos; + emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd)); + updatePixmap(); + + } else if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) { + m_render->seekToFrame(pos); + m_lastSeekPosition = pos; + update(); + } + event->accept(); +} -SmallRuler::SmallRuler(QWidget *parent) - : KRuler(parent) +// virtual +void SmallRuler::mouseReleaseEvent(QMouseEvent * event) { - setShowPointer(true); - setShowBigMarks(false); - setShowTinyMarks(false); - slotNewOffset(0); - setRulerMetricStyle(KRuler::Custom); - setLengthFixed(true); + event->accept(); } -void SmallRuler::setPixelPerMark ( double rate ) + +// virtual +void SmallRuler::leaveEvent(QEvent * event) { - kDebug()<<" RULER SET RATE: "< 0.5) { - setLittleMarkDistance(25); - setMediumMarkDistance(5 * 25); - } - else if (rate > 0.09) { - setLittleMarkDistance(5 * 25); - setMediumMarkDistance(30 * 25); - } - else { - setLittleMarkDistance(30 * 25); - setMediumMarkDistance(60 * 25); - } - - KRuler::setPixelPerMark( rate ); + QWidget::leaveEvent(event); + if (m_cursorColor == palette().link()) { + m_cursorColor = palette().text(); + update(); + } } -// virtual -void SmallRuler::mousePressEvent ( QMouseEvent * event ) +// virtual +void SmallRuler::mouseMoveEvent(QMouseEvent * event) { - int pos = event->x(); - //slotNewValue( pos ); - emit seekRenderer(pos); - kDebug()<x() / m_scale; + if (event->buttons() & Qt::LeftButton) { + if (pos != m_lastSeekPosition && pos != m_cursorFramePosition) { + m_render->seekToFrame(pos); + m_lastSeekPosition = pos; + update(); + } + } + else { + if (m_cursorColor == palette().text() && qAbs(pos - m_cursorFramePosition) * m_scale < 7) { + // Mouse is over cursor + m_cursorColor = palette().link(); + update(); + } + else if (m_cursorColor == palette().link() && qAbs(pos - m_cursorFramePosition) * m_scale >= 7) { + m_cursorColor = palette().text(); + update(); + } + if (qAbs((pos - m_zoneStart) * m_scale) < 4) { + setToolTip(i18n("Zone start: %1", m_monitor->getTimecodeFromFrames(m_zoneStart))); + } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) { + setToolTip(i18n("Zone end: %1", m_monitor->getTimecodeFromFrames(m_zoneEnd))); + } + for (int i = 0; i < m_markers.count(); ++i) { + if (qAbs((pos - m_markers.at(i).time().frames(m_monitor->fps())) * m_scale) < 4) { + // We are on a marker + QString markerxt = m_monitor->getMarkerThumb(m_markers.at(i).time()); + if (!markerxt.isEmpty()) { + markerxt.prepend("

"); + } + markerxt.append(m_markers.at(i).comment()); + setToolTip(markerxt); + event->accept(); + return; + } + } + if (pos > m_zoneStart && pos < m_zoneEnd) { + setToolTip(i18n("Zone duration: %1", m_monitor->getTimecodeFromFrames(m_zoneEnd - m_zoneStart))); + } + else setToolTip(i18n("Position: %1", m_monitor->getTimecodeFromFrames(pos))); + } + event->accept(); } -void SmallRuler::slotNewValue ( int _value ) +void SmallRuler::refreshRuler() +{ + m_lastSeekPosition = SEEK_INACTIVE; + update(); +} + +bool SmallRuler::slotNewValue(int value) +{ + if (value == m_cursorFramePosition) return false; + if (value == m_lastSeekPosition) m_lastSeekPosition = SEEK_INACTIVE; + m_cursorFramePosition = value; + /*int oldPos = m_cursorPosition; + m_cursorPosition = value * m_scale; + const int offset = 6; + const int x = qMin(oldPos, m_cursorPosition); + const int w = qAbs(oldPos - m_cursorPosition); + update(x - offset, 0, w + 2 * offset, height());*/ + update(); + return true; +} + + +//virtual +void SmallRuler::resizeEvent(QResizeEvent *) { - m_cursorPosition = _value / pixelPerMark(); - KRuler::slotNewValue(_value); + adjustScale(m_maxval); +} + +void SmallRuler::updatePixmap() +{ + m_pixmap = QPixmap(width(), height()); + m_pixmap.fill(palette().window().color()); + m_lastSeekPosition = SEEK_INACTIVE; + QPainter p(&m_pixmap); + double f, fend; + + const int zoneStart = (int)(m_zoneStart * m_scale); + const int zoneEnd = (int)(m_zoneEnd * m_scale); + p.fillRect(zoneStart, 0, zoneEnd - zoneStart, height(), QBrush(m_zoneColor)); + + // draw ruler + p.setPen(palette().midlight().color()); + //p.drawLine(0, 0, width(), 0); + p.drawLine(0, height() - 1, width(), height() - 1); + p.setPen(palette().dark().color()); + // draw the little marks + fend = m_scale * m_small; + if (fend > 2) for (f = 0; f < width(); f += fend) { + p.drawLine((int)f, 0, (int)f, 3); + } + + // draw medium marks + fend = m_scale * m_medium; + if (fend > 2) for (f = 0; f < width(); f += fend) { + p.drawLine((int)f, 0, (int)f, 6); + } + // draw markers + if (!m_markers.isEmpty()) { + for (int i = 0; i < m_markers.count(); ++i) { + int pos = m_markers.at(i).time().frames(m_monitor->fps()) * m_scale; + p.setPen(CommentedTime::markerColor(m_markers.at(i).markerType())); + p.drawLine(pos, 0, pos, 9); + } + } + p.end(); + update(); } // virtual -void SmallRuler::paintEvent(QPaintEvent * /*e*/) - { - // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical"); - - QStylePainter p(this); - - - int value = this->value(), - minval = minimum(), - maxval; - maxval = maximum() - + offset() - endOffset(); - - //ioffsetval = value-offset; - // pixelpm = (int)ppm; - // left = clip.left(), - // right = clip.right(); - double f, fend, - offsetmin=(double)(minval-offset()), - offsetmax=(double)(maxval-offset()), - fontOffset = (((double)minval)>offsetmin)?(double)minval:offsetmin; - - // draw labels - QFont font = p.font(); - font.setPointSize(LABEL_SIZE); - p.setFont( font ); - - if (showLittleMarks()) { - // draw the little marks - fend = pixelPerMark()*littleMarkDistance(); - if (fend > 2) for ( f=offsetmin; f 2) for ( f=offsetmin; fshowem) { - // draw end marks - if (d->dir == Qt::Horizontal) { - p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2); - p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2); - } - else { - p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset); - p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset); - } - }*/ - - // draw pointer - if (showPointer()) { - QPolygon pa(4); - pa.setPoints(3, value-6, 9, value+6, 9, value/*+0*/, 16); - p.setBrush( QBrush(Qt::yellow) ); - p.drawPolygon( pa ); - } - - } +void SmallRuler::paintEvent(QPaintEvent *e) +{ + + QPainter p(this); + QRect r = e->rect(); + p.setClipRect(r); + p.drawPixmap(QPointF(), m_pixmap); + p.setPen(palette().shadow().color()); + //p.setRenderHint(QPainter::Antialiasing, true); + //p.drawRoundedRect(rect().adjusted(1,1,-2,-2), 3, 3); + + int cursorPos = m_cursorFramePosition * m_scale; + // draw pointer + QPolygon pa(3); + pa.setPoints(3, cursorPos - 6, height() - 1, cursorPos + 6, height() - 1, cursorPos/*+0*/, height() - 8); + p.setBrush(m_cursorColor); + p.setPen(Qt::NoPen); + p.drawPolygon(pa); + + // Draw seeking pointer + if (m_lastSeekPosition != SEEK_INACTIVE && m_lastSeekPosition != m_cursorFramePosition) { + p.fillRect(m_lastSeekPosition * m_scale - 1, 0, 3, height(), palette().linkVisited()); + } +} + +void SmallRuler::updatePalette() +{ + KSharedConfigPtr config = KSharedConfig::openConfig(KdenliveSettings::colortheme()); + m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor, config).brush(this).color(); + m_zoneColor.setAlpha(180); + updatePixmap(); +} #include "smallruler.moc"