X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fkeyframehelper.cpp;h=b0bbb391d4329f3d7b51096cdc88e9a33f59326b;hb=c3302003093710ee247ad84c0fe2ef3c579d417f;hp=c5a6350263b20b345251e4437fac45a62a38a793;hpb=2d5f99d69f361ec3ba1e4b97baed2e6b1d8ffd8e;p=kdenlive diff --git a/src/keyframehelper.cpp b/src/keyframehelper.cpp index c5a63502..b0bbb391 100644 --- a/src/keyframehelper.cpp +++ b/src/keyframehelper.cpp @@ -19,72 +19,169 @@ #include "keyframehelper.h" +#include "kdenlivesettings.h" #include "definitions.h" #include #include +#include #include #include #include +const int margin = 5; +const int cursorWidth = 6; + +#define SEEK_INACTIVE (-1) KeyframeHelper::KeyframeHelper(QWidget *parent) : - QWidget(parent), - m_geom(NULL), - m_position(0), - m_scale(0), - m_movingKeyframe(false), - m_lineHeight(9) + QWidget(parent) + , frameLength(1) + , m_geom(NULL) + , m_position(0) + , m_scale(0) + , m_movingKeyframe(false) + , m_movingItem() + , m_lineHeight(9) + , m_drag(false) + , m_hoverKeyframe(-1) + , m_seekPosition(SEEK_INACTIVE) { setFont(KGlobalSettings::toolBarFont()); + setMouseTracking(true); + QPalette p = palette(); + KColorScheme scheme(p.currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme())); + m_selected = scheme.decoration(KColorScheme::HoverColor).color(); + m_keyframe = scheme.foreground(KColorScheme::LinkText).color(); + m_keyframebg = scheme.shade(KColorScheme::MidShade); } // virtual void KeyframeHelper::mousePressEvent(QMouseEvent * event) { - if (m_geom != NULL && (event->y() < m_lineHeight) && event->button() == Qt::LeftButton) { + m_hoverKeyframe = -1; + if (event->button() != Qt::LeftButton) { + QWidget::mousePressEvent(event); + return; + } + int xPos = event->x() - margin; + if (m_geom != NULL && (event->y() < m_lineHeight)) { // check if we want to move a keyframe - int mousePos = qMax((int)(event->x() / m_scale), 0); + int mousePos = qMax((int)(xPos / m_scale), 0); Mlt::GeometryItem item; if (m_geom->next_key(&item, mousePos) == 0) { - if (qAbs(item.frame() * m_scale - (int)(event->x())) < 3) { + if (qAbs(item.frame() * m_scale - xPos) < 4) { + m_drag = true; m_movingItem.x(item.x()); m_movingItem.y(item.y()); m_movingItem.w(item.w()); m_movingItem.h(item.h()); m_movingItem.mix(item.mix()); m_movingItem.frame(item.frame()); + + while (!m_extraMovingItems.isEmpty()) { + Mlt::GeometryItem *gitem = m_extraMovingItems.takeFirst(); + if (gitem) delete gitem; + } + for (int i = 0; i < m_extraGeometries.count(); ++i) { + if (m_extraGeometries.at(i)->next_key(item, mousePos) == 0) { + Mlt::GeometryItem *item2 = new Mlt::GeometryItem(); + item2->x(item.x()); + item2->frame(item.frame()); + m_extraMovingItems.append(item2); + } else { + m_extraMovingItems.append(NULL); + } + } + m_dragStart = event->pos(); m_movingKeyframe = true; return; } } } - m_position = event->x() / m_scale; - emit positionChanged(m_position); - update(); + if (event->y() >= m_lineHeight && event->y() < height()) { + int seekRequest = xPos / m_scale; + m_drag = true; + if (seekRequest != m_position) { + m_seekPosition = seekRequest; + emit requestSeek(m_seekPosition); + update(); + } + } +} + +void KeyframeHelper::leaveEvent( QEvent * event ) +{ + Q_UNUSED(event); + if (m_hoverKeyframe != -1) { + m_hoverKeyframe = -1; + update(); + } } // virtual void KeyframeHelper::mouseMoveEvent(QMouseEvent * event) { + int xPos = event->x() - margin; + if (!m_drag) { + int mousePos = qMax((int)(xPos / m_scale), 0); + if (qAbs(m_position * m_scale - xPos) < cursorWidth && event->y() >= m_lineHeight) { + // Mouse over time cursor + if (m_hoverKeyframe != -2) { + m_hoverKeyframe = -2; + update(); + } + event->accept(); + return; + } + if (m_geom != NULL && (event->y() < m_lineHeight)) { + // check if we want to move a keyframe + Mlt::GeometryItem item; + if (m_geom->next_key(&item, mousePos) == 0) { + if (qAbs(item.frame() * m_scale - xPos) < 4) { + if (m_hoverKeyframe == item.frame()) return; + m_hoverKeyframe = item.frame(); + setCursor(Qt::PointingHandCursor); + update(); + event->accept(); + return; + } + } + } + if (m_hoverKeyframe != -1) { + m_hoverKeyframe = -1; + setCursor(Qt::ArrowCursor); + update(); + } + event->accept(); + return; + } if (m_movingKeyframe) { if (!m_dragStart.isNull()) { - if ((event->pos() - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return; + if ((QPoint(xPos, event->y()) - m_dragStart).manhattanLength() < QApplication::startDragDistance()) return; m_dragStart = QPoint(); m_geom->remove(m_movingItem.frame()); + for (int i = 0; i < m_extraGeometries.count(); ++i) + m_extraGeometries[i]->remove(m_movingItem.frame()); } - int pos = qMax(0, (int)(event->x() / m_scale)); - pos = qMin(frameLength, pos); + int pos = qBound(0, (int)(xPos / m_scale), frameLength); + if (KdenliveSettings::snaptopoints() && qAbs(pos - m_position) < 5) + pos = m_position; m_movingItem.frame(pos); + for (int i = 0; i < m_extraMovingItems.count(); ++i) { + if (m_extraMovingItems.at(i)) + m_extraMovingItems[i]->frame(pos); + } update(); return; } - m_position = event->x() / m_scale; - m_position = qMax(0, m_position); - m_position = qMin(frameLength, m_position); - emit positionChanged(m_position); + m_seekPosition = (int) (xPos / m_scale); + m_seekPosition = qMax(0, m_seekPosition); + m_seekPosition = qMin(frameLength, m_seekPosition); + m_hoverKeyframe = -2; + emit requestSeek(m_seekPosition); update(); } @@ -92,7 +189,8 @@ void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event) { if (m_geom != NULL && event->button() == Qt::LeftButton) { // check if we want to move a keyframe - int mousePos = qMax((int)(event->x() / m_scale - 5), 0); + int xPos = event->x() - margin; + int mousePos = qMax((int)(xPos / m_scale - 5), 0); Mlt::GeometryItem item; if (m_geom->next_key(&item, mousePos) == 0 && item.frame() - mousePos < 10) { // There is already a keyframe close to mouse click @@ -100,16 +198,25 @@ void KeyframeHelper::mouseDoubleClickEvent(QMouseEvent * event) return; } // add new keyframe - emit addKeyframe((int)(event->x() / m_scale)); + emit addKeyframe((int)(xPos / m_scale)); } } // virtual void KeyframeHelper::mouseReleaseEvent(QMouseEvent * event) { + m_drag = false; + setCursor(Qt::ArrowCursor); + m_hoverKeyframe = -1; if (m_movingKeyframe) { m_geom->insert(m_movingItem); m_movingKeyframe = false; + + for (int i = 0; i < m_extraGeometries.count(); ++i) { + if (m_extraMovingItems.at(i)) + m_extraGeometries[i]->insert(m_extraMovingItems.at(i)); + } + emit keyframeMoved(m_position); return; } @@ -125,7 +232,7 @@ void KeyframeHelper::wheelEvent(QWheelEvent * e) ++m_position; m_position = qMax(0, m_position); m_position = qMin(frameLength, m_position); - emit positionChanged(m_position); + emit requestSeek(m_position); update(); /* int delta = 1; if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps(); @@ -140,18 +247,21 @@ void KeyframeHelper::paintEvent(QPaintEvent *e) QStylePainter p(this); const QRectF clipRect = e->rect(); p.setClipRect(clipRect); - m_scale = (double) width() / frameLength; + m_scale = (double) (width() - 2 * margin) / frameLength; if (m_geom != NULL) { int pos = 0; - p.setPen(QColor(255, 20, 20)); - p.setBrush(QColor(255, 20, 20)); + p.setPen(m_keyframe); + p.setBrush(m_keyframebg); Mlt::GeometryItem item; while (true) { if (m_geom->next_key(&item, pos) == 1) break; pos = item.frame(); - int scaledPos = pos * m_scale; + if (pos == m_hoverKeyframe) { + p.setBrush(m_selected); + } + int scaledPos = margin + pos * m_scale; // draw keyframes - p.drawLine(scaledPos, 10, scaledPos, 14); + p.drawLine(scaledPos, 9, scaledPos, 15); // draw pointer QPolygon pa(4); pa.setPoints(4, @@ -160,13 +270,19 @@ void KeyframeHelper::paintEvent(QPaintEvent *e) scaledPos, 8, scaledPos + 4, 4); p.drawPolygon(pa); + //p.drawEllipse(scaledPos - 4, 0, 8, 8); + if (pos == m_hoverKeyframe) { + p.setBrush(m_keyframebg); + } //p.fillRect(QRect(scaledPos - 1, 0, 2, 15), QBrush(QColor(255, 20, 20))); pos++; } + if (m_movingKeyframe) { - int scaledPos = (int)(m_movingItem.frame() * m_scale); + p.setBrush(m_selected); + int scaledPos = margin + (int)(m_movingItem.frame() * m_scale); // draw keyframes - p.drawLine(scaledPos, 10, scaledPos, 14); + p.drawLine(scaledPos, 9, scaledPos, 15); // draw pointer QPolygon pa(5); pa.setPoints(4, @@ -175,19 +291,26 @@ void KeyframeHelper::paintEvent(QPaintEvent *e) scaledPos, 8, scaledPos + 4, 4); p.drawPolygon(pa); + p.setBrush(m_keyframe); } } p.setPen(palette().dark().color()); - p.drawLine(clipRect.x(), m_lineHeight, clipRect.right(), m_lineHeight); + p.drawLine(margin, m_lineHeight, width() - margin - 1, m_lineHeight); + p.drawLine(margin, m_lineHeight - 3, margin, m_lineHeight + 3); + p.drawLine(width() - margin, m_lineHeight - 3, width() - margin, m_lineHeight + 3); // draw pointer + if (m_seekPosition != SEEK_INACTIVE) { + p.fillRect(margin + m_seekPosition * m_scale - 1, 0, 3, height(), palette().dark()); + } QPolygon pa(3); - const int cursor = m_position * m_scale; - pa.setPoints(3, cursor - 5, 15, cursor + 5, 15, cursor, 10); - p.setBrush(palette().dark().color()); + const int cursor = margin + m_position * m_scale; + pa.setPoints(3, cursor - cursorWidth, 16, cursor + cursorWidth, 16, cursor, 10); + if (m_hoverKeyframe == -2) + p.setBrush(palette().highlight()); + else + p.setBrush(palette().dark().color()); p.drawPolygon(pa); - - } int KeyframeHelper::value() const @@ -197,7 +320,11 @@ int KeyframeHelper::value() const void KeyframeHelper::setValue(const int pos) { - if (pos == m_position || m_geom == NULL) return; + if (pos == m_position || m_geom == NULL) + return; + if (pos == m_seekPosition) { + m_seekPosition = SEEK_INACTIVE; + } m_position = pos; update(); } @@ -205,8 +332,13 @@ void KeyframeHelper::setValue(const int pos) void KeyframeHelper::setKeyGeometry(Mlt::Geometry *geom, const int length) { m_geom = geom; - frameLength = length; + frameLength = qMax(1, length); update(); } +void KeyframeHelper::addGeometry(Mlt::Geometry *geom) +{ + m_extraGeometries.append(geom); +} + #include "keyframehelper.moc"