Files
DrumMachine/widgets/scratchwidget.h

58 lines
1.5 KiB
C
Raw Permalink Normal View History

2020-04-27 22:59:30 +02:00
#pragma once
#include <QWidget>
#include <QAudioBuffer>
2020-04-27 23:39:49 +02:00
#include <QCache>
2020-04-28 00:06:50 +02:00
#include <QDateTime>
#include <QTimer>
2020-04-27 22:59:30 +02:00
class ScratchWidget : public QWidget
{
Q_OBJECT
public:
explicit ScratchWidget(QWidget *parent = nullptr);
const QAudioBuffer &buffer() const { return m_buffer; }
void setBuffer(const QAudioBuffer &buffer) { m_buffer = buffer; m_graphCache.clear(); repaint(); }
2020-04-29 19:48:41 +02:00
std::ptrdiff_t position() const { return m_position; }
void setPosition(std::ptrdiff_t position) { m_position = position; repaint(); }
int beatWidth() const { return m_beatWidth; };
void setBeatWidth(int beatWidth) { m_beatWidth = beatWidth; m_graphCache.clear(); repaint(); };
int framesPerBeat() const { return m_framesPerBeat; }
void setFramesPerBeat(int framesPerBeat) { m_framesPerBeat = framesPerBeat; m_graphCache.clear(); repaint(); }
2020-04-27 22:59:30 +02:00
2020-04-28 00:06:50 +02:00
signals:
2020-04-28 00:26:40 +02:00
void scratchBegin();
2020-04-28 00:06:50 +02:00
void scratchSpeed(float speed);
2020-04-28 00:26:40 +02:00
void scratchEnd();
2020-04-28 00:06:50 +02:00
2020-04-27 22:59:30 +02:00
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
2020-04-28 00:06:50 +02:00
private slots:
void timeout();
2020-04-27 22:59:30 +02:00
private:
2020-04-27 23:39:49 +02:00
QPixmap getPixmap(int index);
2020-04-27 22:59:30 +02:00
QAudioBuffer m_buffer;
2020-04-29 19:48:41 +02:00
std::ptrdiff_t m_position{};
2020-04-27 23:39:49 +02:00
QCache<int, QPixmap> m_graphCache;
2020-04-28 00:06:50 +02:00
2020-04-29 19:48:41 +02:00
int m_beatWidth{100};
2022-12-27 21:19:21 +01:00
int m_framesPerBeat;
2020-04-29 19:48:41 +02:00
2020-04-28 00:06:50 +02:00
bool m_scratching{};
2020-04-28 00:26:40 +02:00
bool m_dragging{};
2020-04-28 00:06:50 +02:00
int m_mouseX;
QDateTime m_timestamp;
QTimer m_timer;
2020-04-27 22:59:30 +02:00
};