Implemeted scratching

This commit is contained in:
2020-04-28 00:06:50 +02:00
parent 686f42265e
commit 5bdf60fc82
3 changed files with 49 additions and 2 deletions

View File

@ -2,6 +2,7 @@
#include <QPainter>
#include <QRect>
#include <QMouseEvent>
#include <QDebug>
#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)

View File

@ -3,6 +3,8 @@
#include <QWidget>
#include <QAudioBuffer>
#include <QCache>
#include <QDateTime>
#include <QTimer>
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<int, QPixmap> m_graphCache;
bool m_scratching{};
int m_mouseX;
QDateTime m_timestamp;
QTimer m_timer;
};

View File

@ -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("")); });
}