soundboard/src/soundview.cpp

84 lines
1.8 KiB
C++

#include "soundview.h"
#include "sound.h"
#include <Log.h>
#include <QResizeEvent>
SoundView::SoundView(QWidget* parent) : QGraphicsView(parent) {
setScene(&scene);
redraw();
}
SoundView::~SoundView() {
delete samples;
}
void SoundView::loadFile(const std::string& file) {
samples = Sound::instance().openFile(file);
}
void SoundView::redraw() {
//Log::debug << "redraw SoundView";
scene.clear();
scene.setBackgroundBrush(QBrush(Qt::GlobalColor::black));
scene.addRect(0, 0, width()-4, height()-4, QPen(Qt::green));
uint32_t h = scene.height()-4;
//Log::info << "scene Size: " << scene.width() << " " << scene.height() << " " << width() << " " << height();
// red center line
scene.addLine(0, h/2, scene.width()-4, h/2, QPen(Qt::red));
// cursor
if(samples) {
drawCursor(currentPosition, QPen(Qt::blue));
drawCursor(startPos, QPen(Qt::yellow));
drawCursor(endPos == 0 ? samples->getLength() : endPos, QPen(Qt::yellow));
/*
for(uint32_t x = 0; x < width(); ++x) {
float s = samples->readSample(x);
scene.addLine(x, h/2, x, s*h, QPen(Qt::white));
//Log::info << "Line: " << x << " " << h/2;
} */
}
}
void SoundView::paintEvent(QPaintEvent* event) {
QGraphicsView::paintEvent(event);
redraw();
}
void SoundView::resizeEvent(QResizeEvent* event) {
QGraphicsView::resizeEvent(event);
if(samples) {
samples->setWidth(event->size().width());
}
}
void SoundView::updatePosition(unsigned long newpos) {
currentPosition = newpos;
update();
}
void SoundView::updateStartPosition(unsigned long newpos) {
startPos = newpos;
update();
}
void SoundView::updateEndPosition(unsigned long newpos) {
endPos = newpos;
update();
}
void SoundView::drawCursor(uint64_t pos, QPen pen) {
uint32_t cursorXpos = (pos * (scene.width()-4)) / samples->getLength();
scene.addLine(cursorXpos, 0, cursorXpos, scene.height() -4, pen);
}