Merge pull request #48 from dnet/cursors

Added cursors with delta measurement
This commit is contained in:
Mike Walters
2016-02-29 22:20:34 +00:00
5 changed files with 104 additions and 0 deletions

View File

@@ -39,6 +39,11 @@ MainWindow::MainWindow()
connect(dock->powerMaxSlider, SIGNAL(valueChanged(int)), &spectrogram, SLOT(setPowerMax(int)));
connect(dock->powerMinSlider, SIGNAL(valueChanged(int)), &spectrogram, SLOT(setPowerMin(int)));
connect(dock->timeScaleCheckBox, SIGNAL(stateChanged(int)), &spectrogram, SLOT(setTimeScaleEnable(int)));
connect(&spectrogram, SIGNAL(cursorFrequencyChanged(QString)), dock->cursorFrequencyLabel, SLOT(setText(QString)));
connect(&spectrogram, SIGNAL(cursorTimeChanged(QString)), dock->cursorTimeLabel, SLOT(setText(QString)));
connect(dock->deltaDragCheckBox, SIGNAL(stateChanged(int)), &spectrogram, SLOT(setDeltaDragEnable(int)));
connect(&spectrogram, SIGNAL(deltaFrequencyChanged(QString)), dock->deltaFrequencyLabel, SLOT(setText(QString)));
connect(&spectrogram, SIGNAL(deltaTimeChanged(QString)), dock->deltaTimeLabel, SLOT(setText(QString)));
}
bool MainWindow::eventFilter(QObject * /*obj*/, QEvent *event)

View File

@@ -36,11 +36,14 @@ Spectrogram::Spectrogram()
powerMax = 0.0f;
powerMin = -50.0f;
timeScaleIsEnabled = true;
deltaDragIsEnabled = true;
for (int i = 0; i < 256; i++) {
float p = (float)i / 256;
colormap[i] = QColor::fromHsvF(p * 0.83f, 1.0, 1.0 - p).rgba();
}
setMouseTracking(true);
}
Spectrogram::~Spectrogram()
@@ -73,6 +76,44 @@ template <class T> const T& clamp (const T& value, const T& min, const T& max) {
return std::min(max, std::max(min, value));
}
void Spectrogram::xyToFreqTime(int x, int y, float *freq, float *time) {
*freq = labs(x - (fftSize / 2)) * sampleRate / 2 / (float)fftSize;
*time = (float)lineToSample(y) / sampleRate;
}
void Spectrogram::mouseReleaseEvent(QMouseEvent *event) {
if (deltaDragIsEnabled) {
cursorStartX = -1;
update();
}
}
void Spectrogram::mouseMoveEvent(QMouseEvent *event) {
float freq, time;
xyToFreqTime(event->x(), event->y(), &freq, &time);
emit cursorFrequencyChanged(QString::number(freq) + " Hz");
emit cursorTimeChanged(QString::number(time) + " s");
if (cursorStartX != -1) {
float s_freq, s_time;
xyToFreqTime(cursorStartX, cursorStartY, &s_freq, &s_time);
emit deltaFrequencyChanged(QString::number(fabs(s_freq - freq)) + " Hz");
emit deltaTimeChanged(QString::number(fabs(s_time - time)) + " s");
cursorEndX = event->x();
cursorEndY = event->y();
update();
}
}
void Spectrogram::mousePressEvent(QMouseEvent *event) {
if (cursorStartX == -1) {
cursorEndX = cursorStartX = event->x();
cursorEndY = cursorStartY = event->y();
} else {
cursorStartX = -1;
}
update();
}
void Spectrogram::paintEvent(QPaintEvent *event)
{
QRect rect = event->rect();
@@ -96,6 +137,7 @@ void Spectrogram::paintEvent(QPaintEvent *event)
}
paintTimeAxis(&painter, rect);
paintCursors(&painter, rect);
}
}
@@ -165,6 +207,21 @@ void Spectrogram::getLine(float *dest, off_t sample)
}
}
void Spectrogram::paintCursors(QPainter *painter, QRect rect)
{
if (cursorStartX != -1) {
painter->save();
QPen pen(Qt::white, 1, Qt::DashLine);
painter->setPen(pen);
painter->drawLine(rect.left(), cursorStartY, rect.right(), cursorStartY);
painter->drawLine(cursorStartX, rect.top(), cursorStartX, rect.bottom());
painter->drawLine(rect.left(), cursorEndY, rect.right(), cursorEndY);
painter->drawLine(cursorEndX, rect.top(), cursorEndX, rect.bottom());
painter->restore();
}
}
void Spectrogram::paintTimeAxis(QPainter *painter, QRect rect)
{
if (timeScaleIsEnabled){
@@ -232,6 +289,11 @@ void Spectrogram::setTimeScaleEnable(int state)
update();
}
void Spectrogram::setDeltaDragEnable(int state)
{
deltaDragIsEnabled = (state == Qt::Checked);
}
int Spectrogram::getHeight()
{

View File

@@ -40,6 +40,12 @@ public:
int getHeight();
int getStride();
signals:
void cursorFrequencyChanged(QString);
void cursorTimeChanged(QString);
void deltaFrequencyChanged(QString);
void deltaTimeChanged(QString);
public slots:
void openFile(QString fileName);
void setSampleRate(int rate);
@@ -48,9 +54,13 @@ public slots:
void setPowerMin(int power);
void setZoomLevel(int zoom);
void setTimeScaleEnable(int state);
void setDeltaDragEnable(int state);
protected:
void paintEvent(QPaintEvent *event);
void mouseReleaseEvent(QMouseEvent * event);
void mouseMoveEvent(QMouseEvent * event);
void mousePressEvent(QMouseEvent * event);
private:
@@ -71,15 +81,20 @@ private:
float powerMax;
float powerMin;
bool timeScaleIsEnabled;
bool deltaDragIsEnabled;
int cursorStartX = -1, cursorStartY;
int cursorEndX, cursorEndY;
QPixmap* getPixmapTile(off_t tile);
float* getFFTTile(off_t tile);
void getLine(float *dest, off_t sample);
void paintTimeAxis(QPainter *painter, QRect rect);
void paintCursors(QPainter *painter, QRect rect);
off_t lineToSample(off_t line);
int sampleToLine(off_t sample);
QString sampleToTime(off_t sample);
int linesPerTile();
void xyToFreqTime(int x, int y, float *freq, float *time);
};
class TileCacheKey {

View File

@@ -60,6 +60,22 @@ SpectrogramControls::SpectrogramControls(const QString & title, QWidget * parent
timeScaleCheckBox->setCheckState(Qt::Checked);
layout->addRow(new QLabel(tr("time overlay:")), timeScaleCheckBox);
cursorFrequencyLabel = new QLabel();
layout->addRow(new QLabel(tr("Cursor frequency:")), cursorFrequencyLabel);
cursorTimeLabel = new QLabel();
layout->addRow(new QLabel(tr("Cursor time:")), cursorTimeLabel);
deltaDragCheckBox = new QCheckBox(widget);
deltaDragCheckBox->setCheckState(Qt::Checked);
layout->addRow(new QLabel(tr("Delta dragging:")), deltaDragCheckBox);
deltaFrequencyLabel = new QLabel();
layout->addRow(new QLabel(tr("Delta frequency:")), deltaFrequencyLabel);
deltaTimeLabel = new QLabel();
layout->addRow(new QLabel(tr("Delta time:")), deltaTimeLabel);
widget->setLayout(layout);
setWidget(widget);

View File

@@ -25,6 +25,7 @@
#include <QPushButton>
#include <QSlider>
#include <QCheckBox>
#include <QLabel>
class SpectrogramControls : public QDockWidget {
Q_OBJECT
@@ -51,4 +52,9 @@ public:
QSlider *powerMaxSlider;
QSlider *powerMinSlider;
QCheckBox *timeScaleCheckBox;
QLabel *cursorFrequencyLabel;
QLabel *cursorTimeLabel;
QCheckBox *deltaDragCheckBox;
QLabel *deltaFrequencyLabel;
QLabel *deltaTimeLabel;
};