X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fcustomruler.cpp;h=fd9786e645a36dfd5fb1b26847ec52db8d201a41;hb=4f2ff860ac2157cbc10dec34eafac61a41ffc0ce;hp=8081f9ccae36517451ce7e2ea32a7fa45c2e7fdb;hpb=c6d43344e195571ecc784897310c02a7125623d0;p=kdenlive diff --git a/src/customruler.cpp b/src/customruler.cpp index 8081f9cc..fd9786e6 100644 --- a/src/customruler.cpp +++ b/src/customruler.cpp @@ -17,230 +17,337 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ -#include -#include +#include "customruler.h" #include +#include +#include +#include +#include +#include -#include "customruler.h" +static const int FIX_WIDTH = 24; /* widget width in pixel */ +static const int LINE_END = (FIX_WIDTH - 3); +static const int END_MARK_LENGTH = (FIX_WIDTH - 8); +static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4); +static const int BIG_MARK_X2 = LINE_END; +static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH); +static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2); +static const int MIDDLE_MARK_X2 = LINE_END; +static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH); +static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2); +static const int LITTLE_MARK_X2 = LINE_END; +static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH); +static const int LABEL_SIZE = 9; +static const int END_LABEL_X = 4; +static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2); -#define INIT_VALUE 0 -#define INIT_MIN_VALUE 0 -#define INIT_MAX_VALUE 100 -#define INIT_TINY_MARK_DISTANCE 1 -#define INIT_LITTLE_MARK_DISTANCE 5 -#define INIT_MIDDLE_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 2) -#define INIT_BIG_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 10) -#define INIT_SHOW_TINY_MARK false -#define INIT_SHOW_LITTLE_MARK true -#define INIT_SHOW_MEDIUM_MARK true -#define INIT_SHOW_BIG_MARK true -#define INIT_SHOW_END_MARK true -#define INIT_SHOW_POINTER true -#define INIT_SHOW_END_LABEL true - -#define INIT_PIXEL_PER_MARK (double)10.0 /* distance between 2 base marks in pixel */ -#define INIT_OFFSET (-20) -#define INIT_LENGTH_FIX true -#define INIT_END_OFFSET 0 - -#define FIX_WIDTH 20 /* widget width in pixel */ -#define LINE_END (FIX_WIDTH - 3) -#define END_MARK_LENGTH (FIX_WIDTH - 6) -#define END_MARK_X2 LINE_END -#define END_MARK_X1 (END_MARK_X2 - END_MARK_LENGTH) -#define BIG_MARK_LENGTH (END_MARK_LENGTH*3/4) -#define BIG_MARK_X2 LINE_END -#define BIG_MARK_X1 (BIG_MARK_X2 - BIG_MARK_LENGTH) -#define MIDDLE_MARK_LENGTH (END_MARK_LENGTH/2) -#define MIDDLE_MARK_X2 LINE_END -#define MIDDLE_MARK_X1 (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH) -#define LITTLE_MARK_LENGTH (MIDDLE_MARK_LENGTH/2) -#define LITTLE_MARK_X2 LINE_END -#define LITTLE_MARK_X1 (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH) -#define BASE_MARK_LENGTH (LITTLE_MARK_LENGTH/2) -#define BASE_MARK_X2 LINE_END -#define BASE_MARK_X1 (BASE_MARK_X2 - 3) //BASE_MARK_LENGTH - -#define LABEL_SIZE 8 -#define END_LABEL_X 4 -#define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2) +static int littleMarkDistance; +static int mediumMarkDistance; +static int bigMarkDistance; #include "definitions.h" -const int CustomRuler::comboScale[] = - { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, - 12000 }; +const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000}; + +CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) : + QWidget(parent), + m_timecode(tc), + m_view(parent), + m_duration(0), + m_offset(0), + m_clickedGuide(-1) +{ + setFont(KGlobalSettings::toolBarFont()); + m_scale = 3; + m_zoneColor = QColor(133, 255, 143); + littleMarkDistance = FRAME_SIZE; + mediumMarkDistance = FRAME_SIZE * m_timecode.fps(); + bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60; + m_zoneStart = 0; + m_zoneEnd = 100; + m_contextMenu = new QMenu(this); + QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide")); + connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide())); + m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide")); + connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide())); + m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide")); + connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide())); + QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides")); + connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides())); + setMouseTracking(true); + setMinimumHeight(20); +} -CustomRuler::CustomRuler(Timecode tc, QWidget *parent) - : KRuler(parent), m_timecode(tc) +void CustomRuler::updateProjectFps(Timecode t) { - slotNewOffset(0); - setRulerMetricStyle(KRuler::Pixel); - setLength(1024); - setMaximum(1024); - setPixelPerMark(3); - setLittleMarkDistance (FRAME_SIZE); - setMediumMarkDistance (FRAME_SIZE * 25); - setBigMarkDistance (FRAME_SIZE * 25 * 60); + m_timecode = t; + mediumMarkDistance = FRAME_SIZE * m_timecode.fps(); + bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60; + update(); } -// virtual -void CustomRuler::mousePressEvent ( QMouseEvent * event ) +void CustomRuler::slotEditGuide() { - int pos = event->x(); - slotMoveCursor( pos, true ); - kDebug()<slotEditGuide(m_clickedGuide); +} + +void CustomRuler::slotDeleteGuide() +{ + m_view->slotDeleteGuide(m_clickedGuide); +} + +void CustomRuler::setZone(QPoint p) +{ + int min = qMin(m_zoneStart, p.x()); + int max = qMax(m_zoneEnd, p.y()); + m_zoneStart = p.x(); + m_zoneEnd = p.y(); + update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height()); } // virtual -void CustomRuler::mouseMoveEvent ( QMouseEvent * event ) +void CustomRuler::mousePressEvent(QMouseEvent * event) { - int pos = event->x(); - slotMoveCursor( pos, true ); - kDebug()<x() + offset())); + if (event->button() == Qt::RightButton) { + m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1)); + m_editGuide->setEnabled(m_clickedGuide > 0); + m_deleteGuide->setEnabled(m_clickedGuide > 0); + m_contextMenu->exec(event->globalPos()); + return; + } + setFocus(Qt::MouseFocusReason); + m_view->activateMonitor(); + m_moveCursor = RULER_CURSOR; + if (event->y() > 10) { + if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START; + else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE; + else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END; + } + if (m_moveCursor == RULER_CURSOR) + m_view->setCursorPos((int) pos / m_factor); } -void CustomRuler::slotMoveRuler(int newPos) +// virtual +void CustomRuler::mouseMoveEvent(QMouseEvent * event) +{ + if (event->buttons() == Qt::LeftButton) { + int pos = (int)((event->x() + offset()) / m_factor); + int zoneStart = m_zoneStart; + int zoneEnd = m_zoneEnd; + if (pos < 0) pos = 0; + if (m_moveCursor == RULER_CURSOR) { + m_view->setCursorPos(pos); + m_view->slotCheckPositionScrolling(); + return; + } else if (m_moveCursor == RULER_START) m_zoneStart = pos; + else if (m_moveCursor == RULER_END) m_zoneEnd = pos; + else if (m_moveCursor == RULER_MIDDLE) { + int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2); + if (move + m_zoneStart < 0) move = - m_zoneStart; + m_zoneStart += move; + m_zoneEnd += move; + } + emit zoneMoved(m_zoneStart, m_zoneEnd); + m_view->setDocumentModified(); + + int min = qMin(m_zoneStart, zoneStart); + int max = qMax(m_zoneEnd, zoneEnd); + update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height()); + + } else { + int pos = (int)((event->x() + offset())); + if (event->y() <= 10) setCursor(Qt::ArrowCursor); + else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor)); + else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor)); + else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor); + else setCursor(Qt::ArrowCursor); + } +} + + +// virtual +void CustomRuler::wheelEvent(QWheelEvent * e) +{ + int delta = 1; + if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps(); + if (e->delta() < 0) delta = 0 - delta; + m_view->moveCursorPos(delta); +} + +int CustomRuler::inPoint() const { - int diff = offset() - newPos; - KRuler::slotNewOffset(newPos); - KRuler::slotNewValue(value() + diff); + return m_zoneStart; } -void CustomRuler::slotMoveCursor( int _value, bool emitSignal ) +int CustomRuler::outPoint() const { - KRuler::slotNewValue(_value ); - m_cursorPosition= (_value + offset()) / pixelPerMark(); - if (emitSignal) emit cursorMoved(m_cursorPosition / FRAME_SIZE); + return m_zoneEnd; } +void CustomRuler::slotMoveRuler(int newPos) +{ + m_offset = newPos; + update(); +} -void CustomRuler::slotNewValue ( int _value, bool emitSignal ) +int CustomRuler::offset() const { - kDebug()<<"-------------- SET NEW CURSOR: "<<_value; - m_cursorPosition= _value / pixelPerMark(); - if (emitSignal) emit cursorMoved(m_cursorPosition / FRAME_SIZE); - KRuler::slotNewValue(_value* pixelPerMark() - offset()); + return m_offset; } -void CustomRuler::setPixelPerMark (double rate) +void CustomRuler::slotCursorMoved(int oldpos, int newpos) +{ + if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) { + update(oldpos * m_factor - offset() - 6, 0, 17, height()); + update(newpos * m_factor - offset() - 6, 0, 17, height()); + } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height()); +} + +void CustomRuler::setPixelPerMark(double rate) { int scale = comboScale[(int) rate]; - int newPos = m_cursorPosition * (1.0 / scale); - KRuler::setPixelPerMark(1.0 / scale); - KRuler::slotNewValue( newPos ); -} - -// virtual -void CustomRuler::paintEvent(QPaintEvent * /*e*/) - { - // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical"); - - QStylePainter p(this); - - - int value = this->value(); - int minval = minimum(); - int 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; - QRect bg = QRect(offsetmin, 0, offsetmax, height()); - - QPalette palette; - //p.fillRect(bg, palette.light()); - // draw labels - QFont font = p.font(); - font.setPointSize(LABEL_SIZE); - p.setFont( font ); - p.setPen(palette.dark().color()); - // draw littlemarklabel - - // draw mediummarklabel - - // draw bigmarklabel - - // draw endlabel - /*if (d->showEndL) { - if (d->dir == Qt::Horizontal) { - p.translate( fontOffset, 0 ); - p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel ); - }*/ - - // draw the tiny marks - //if (showTinyMarks()) - /*{ - fend = pixelPerMark()*tinyMarkDistance(); - if (fend > 5) for ( f=offsetmin; f 5) for ( f=offsetmin; f 60) { - QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5)); - p.drawText( (int)f + 2, LABEL_SIZE, lab ); - } - } - } - if (showMediumMarks()) { - // draw medium marks - fend = pixelPerMark()*mediumMarkDistance(); - if (fend > 5) for ( f=offsetmin; f 60) { - QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5) ); - p.drawText( (int)f + 2, LABEL_SIZE, lab ); - } - } - } - if (showBigMarks()) { - // draw big marks - fend = pixelPerMark()*bigMarkDistance(); - if (fend > 5) for ( f=offsetmin; f 60) { - QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5) ); - p.drawText( (int)f + 2, LABEL_SIZE, lab ); - } - else if (((int) (f - offsetmin)) % ((int)(fend * 5)) == 0) { - QString lab = m_timecode.getTimecodeFromFrames((int) ((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5) ); - p.drawText( (int)f + 2, LABEL_SIZE, lab ); - } - } - } -/* if (d->showem) { - // 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() && value > 0) { - QPolygon pa(4); - pa.setPoints(3, value-6, 9, value+6, 9, value/*+0*/, 16); - p.setBrush( QBrush(Qt::yellow) ); - p.drawPolygon( pa ); - } - - } + m_factor = 1.0 / (double) scale * FRAME_SIZE; + m_scale = 1.0 / (double) scale; + double fend = m_scale * littleMarkDistance; + if (rate > 8) { + mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60; + bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300; + } else if (rate > 6) { + mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10; + bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30; + } else if (rate > 3) { + mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps(); + bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5; + } else { + mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps(); + bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60; + } + switch ((int) rate) { + case 0: + m_textSpacing = fend; + break; + case 1: + m_textSpacing = fend * 5; + break; + case 2: + case 3: + case 4: + m_textSpacing = fend * m_timecode.fps(); + break; + case 5: + m_textSpacing = fend * m_timecode.fps() * 5; + break; + case 6: + m_textSpacing = fend * m_timecode.fps() * 10; + break; + case 7: + m_textSpacing = fend * m_timecode.fps() * 30; + break; + case 8: + case 9: + m_textSpacing = fend * m_timecode.fps() * 40; + break; + case 10: + m_textSpacing = fend * m_timecode.fps() * 80; + break; + case 11: + case 12: + m_textSpacing = fend * m_timecode.fps() * 400; + break; + case 13: + m_textSpacing = fend * m_timecode.fps() * 800; + break; + } + update(); +} + +void CustomRuler::setDuration(int d) +{ + int oldduration = m_duration; + m_duration = d; + update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height()); +} + +// virtual +void CustomRuler::paintEvent(QPaintEvent *e) +{ + QStylePainter p(this); + p.setClipRect(e->rect()); + const int projectEnd = (int)(m_duration * m_factor); + p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color()); + + const int zoneStart = (int)(m_zoneStart * m_factor); + const int zoneEnd = (int)(m_zoneEnd * m_factor); + const QRect zoneRect(); + + p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor); + + const int value = m_view->cursorPos() * m_factor - m_offset; + int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1; + const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1; + if (minval < 0) minval = 0; + + double f, fend; + const int offsetmax = maxval * FRAME_SIZE; + + p.setPen(palette().dark().color()); + + // draw time labels + int offsetmin = (e->rect().left() + m_offset) / m_textSpacing; + offsetmin = offsetmin * m_textSpacing; + for (f = offsetmin; f < offsetmax; f += m_textSpacing) { + QString lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5)); + p.drawText(f - m_offset + 2, LABEL_SIZE, lab); + } + + offsetmin = (e->rect().left() + m_offset) / littleMarkDistance; + offsetmin = offsetmin * littleMarkDistance; + // draw the little marks + fend = m_scale * littleMarkDistance; + if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend) + p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2); + + offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance; + offsetmin = offsetmin * mediumMarkDistance; + // draw medium marks + fend = m_scale * mediumMarkDistance; + if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend) + p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2); + + offsetmin = (e->rect().left() + m_offset) / bigMarkDistance; + offsetmin = offsetmin * bigMarkDistance; + // draw big marks + fend = m_scale * bigMarkDistance; + if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend) + p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2); + + // draw zone cursors + int off = offset(); + if (zoneStart > 0) { + QPolygon pa(4); + pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18); + p.drawPolyline(pa); + } + + if (zoneEnd > 0) { + QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9); + p.fillRect(rec, QColor(255, 255, 255, 150)); + p.drawRect(rec); + + QPolygon pa(4); + pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18); + p.drawPolyline(pa); + } + + // draw pointer + QPolygon pa(3); + pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16); + p.setBrush(palette().highlight()); + p.drawPolygon(pa); +} #include "customruler.moc"