diff --git a/scratchwidget.cpp b/scratchwidget.cpp index c906053..4245620 100644 --- a/scratchwidget.cpp +++ b/scratchwidget.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "graphrenderer.h" @@ -11,6 +12,9 @@ constexpr auto theWidth = 100; ScratchWidget::ScratchWidget(QWidget *parent) : QWidget{parent} { + connect(&m_timer, &QTimer::timeout, this, &ScratchWidget::timeout); + m_timer.setSingleShot(true); + m_timer.setInterval(100); } void ScratchWidget::paintEvent(QPaintEvent *event) @@ -58,17 +62,46 @@ void ScratchWidget::paintEvent(QPaintEvent *event) void ScratchWidget::mousePressEvent(QMouseEvent *event) { - + if (event->button() == Qt::LeftButton) + { + m_scratching = true; + m_mouseX = event->x(); + m_timestamp = QDateTime::currentDateTime(); + setMouseTracking(true); + } } void ScratchWidget::mouseReleaseEvent(QMouseEvent *event) { - + if (event->button() == Qt::LeftButton) + { + m_timer.stop(); + emit scratchSpeed(1.f); + m_scratching = false; + setMouseTracking(false); + } } void ScratchWidget::mouseMoveEvent(QMouseEvent *event) { + if (m_scratching) + { + const auto now = QDateTime::currentDateTime(); + int dx = m_mouseX - event->x(); + int dt = m_timestamp.msecsTo(now); + + emit scratchSpeed(float(dx) / dt * 5.f); + + m_mouseX = event->x(); + m_timestamp = now; + m_timer.start(); + } +} + +void ScratchWidget::timeout() +{ + emit scratchSpeed(0.f); } QPixmap ScratchWidget::getPixmap(int index) diff --git a/scratchwidget.h b/scratchwidget.h index 544af16..fcae5d9 100644 --- a/scratchwidget.h +++ b/scratchwidget.h @@ -3,6 +3,8 @@ #include #include #include +#include +#include class ScratchWidget : public QWidget { @@ -17,16 +19,27 @@ public: std::size_t position() const { return m_position; } void setPosition(std::size_t position) { m_position = position; repaint(); } +signals: + void scratchSpeed(float speed); + protected: void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; +private slots: + void timeout(); + private: QPixmap getPixmap(int index); QAudioBuffer m_buffer; std::size_t m_position{}; QCache m_graphCache; + + bool m_scratching{}; + int m_mouseX; + QDateTime m_timestamp; + QTimer m_timer; }; diff --git a/trackdeck.cpp b/trackdeck.cpp index 3e4e0ea..ac33a28 100644 --- a/trackdeck.cpp +++ b/trackdeck.cpp @@ -29,6 +29,7 @@ TrackDeck::TrackDeck(QWidget *parent) : connect(m_ui->previewWidget, &PreviewWidget::positionSelected, &m_player, &AudioPlayer::setPosition); connect(&m_player, &AudioPlayer::positionChanged, m_ui->previewWidget, &PreviewWidget::setPosition); + connect(m_ui->scratchWidget, &ScratchWidget::scratchSpeed, &m_player, &AudioPlayer::setSpeed); connect(&m_player, &AudioPlayer::positionChanged, m_ui->scratchWidget, &ScratchWidget::setPosition); connect(&m_player, &AudioPlayer::playingChanged, m_ui->pushButtonPlay, [&button=*m_ui->pushButtonPlay](bool playing){ button.setText(playing ? tr("▮▮") : tr("▶")); }); }