Skip to content

Commit

Permalink
refact(plotview): Move the time scale into PlotView
Browse files Browse the repository at this point in the history
  • Loading branch information
schneider42 committed Aug 9, 2016
1 parent dd461ba commit e2d066c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 77 deletions.
2 changes: 1 addition & 1 deletion mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ MainWindow::MainWindow()
connect(dock->powerMaxSlider, SIGNAL(valueChanged(int)), plots, SLOT(setPowerMax(int)));
connect(dock->powerMinSlider, SIGNAL(valueChanged(int)), plots, SLOT(setPowerMin(int)));
connect(dock->cursorsCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableCursors);
connect(dock->timeScaleCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableTimeScale);
connect(dock->cursorSymbolsSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), plots, &PlotView::setCursorSegments);
connect(dock->timeScaleCheckBox, SIGNAL(stateChanged(int)), plots, SLOT(setTimeScaleEnable(int)));

// Connect dock outputs
connect(plots, SIGNAL(timeSelectionChanged(float)), dock, SLOT(timeSelectionChanged(float)));
Expand Down
62 changes: 59 additions & 3 deletions plotview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0})
mainSampleSource = input;
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
enableCursors(false);
enableTimeScale(true);
connect(&cursors, SIGNAL(cursorsMoved()), this, SLOT(cursorsMoved()));

spectrogramPlot = new SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float>>>(mainSampleSource));
Expand Down Expand Up @@ -273,9 +274,63 @@ void PlotView::paintEvent(QPaintEvent *event)
if (cursorsEnabled)
cursors.paintFront(painter, rect, viewRange);

if (timeScaleEnabled)
paintTimeScale(painter, rect, viewRange);

#undef PLOT_LAYER
}

void PlotView::paintTimeScale(QPainter &painter, QRect &rect, range_t<off_t> sampleRange)
{
float startTime = (float)sampleRange.minimum / sampleRate;
float stopTime = (float)sampleRange.maximum / sampleRate;
float duration = stopTime - startTime;

painter.save();

QPen pen(Qt::white, 1, Qt::SolidLine);
painter.setPen(pen);
QFontMetrics fm(painter.font());

int tickWidth = 80;
int maxTicks = rect.width() / tickWidth;

double durationPerTick = 10 * pow(10, floor(log(duration / maxTicks) / log(10)));

double firstTick = int(startTime / durationPerTick) * durationPerTick;

double tick = firstTick;

while (tick <= stopTime) {

off_t tickSample = tick * sampleRate;
int tickLine = (tickSample - sampleRange.minimum) / samplesPerLine();

char buf[128];
snprintf(buf, sizeof(buf), "%.06f", tick);
painter.drawLine(tickLine, 0, tickLine, 30);
painter.drawText(tickLine + 2, 25, buf);

tick += durationPerTick;
}

// Draw small ticks
durationPerTick /= 10;
firstTick = int(startTime / durationPerTick) * durationPerTick;
tick = firstTick;
while (tick <= stopTime) {

off_t tickSample = tick * sampleRate;
int tickLine = (tickSample - sampleRange.minimum) / samplesPerLine();

painter.drawLine(tickLine, 0, tickLine, 10);
tick += durationPerTick;
}

painter.restore();
}


int PlotView::plotsHeight()
{
int height = 0;
Expand Down Expand Up @@ -336,12 +391,13 @@ void PlotView::updateView(bool reCenter)

void PlotView::setSampleRate(off_t rate)
{
sampleRate = rate;
spectrogramPlot->setSampleRate(rate);
}

void PlotView::setTimeScaleEnable(int state)
void PlotView::enableTimeScale(bool enabled)
{
bool timeScaleIsEnabled = (state == Qt::Checked);
spectrogramPlot->setTimeScaleEnable(timeScaleIsEnabled);
timeScaleEnabled = enabled;
viewport()->update();
}

7 changes: 5 additions & 2 deletions plotview.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class PlotView : public QAbstractScrollArea, Subscriber

public slots:
void cursorsMoved();
void enableCursors(bool enable);
void enableCursors(bool enabled);
void enableTimeScale(bool enabled);
void invalidateEvent();
void repaint();
void setCursorSegments(int segments);
void setFFTAndZoom(int fftSize, int zoomLevel);
void setPowerMin(int power);
void setPowerMax(int power);
void setTimeScaleEnable(int state);

protected:
void contextMenuEvent(QContextMenuEvent * event) override;
Expand All @@ -74,11 +74,14 @@ public slots:
int powerMin;
int powerMax;
bool cursorsEnabled;
off_t sampleRate = 0;
bool timeScaleEnabled;

void addPlot(Plot *plot);
void emitTimeSelection();
void extractSymbols(std::shared_ptr<AbstractSampleSource> src);
int plotsHeight();
off_t samplesPerLine();
void updateView(bool reCenter = false);
void paintTimeScale(QPainter &painter, QRect &rect, range_t<off_t> sampleRange);
};
66 changes: 0 additions & 66 deletions spectrogramplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ SpectrogramPlot::SpectrogramPlot(std::shared_ptr<SampleSource<std::complex<float
zoomLevel = 0;
powerMax = 0.0f;
powerMin = -50.0f;
timeScaleIsEnabled = true;

for (int i = 0; i < 256; i++) {
float p = (float)i / 256;
Expand All @@ -53,59 +52,6 @@ void SpectrogramPlot::paintFront(QPainter &painter, QRect &rect, range_t<off_t>
{
if (tunerEnabled())
tuner.paintFront(painter, rect, sampleRange);

if (timeScaleIsEnabled)
paintTimeScale(painter, rect, sampleRange);
}

void SpectrogramPlot::paintTimeScale(QPainter &painter, QRect &rect, range_t<off_t> sampleRange)
{
float startTime = (float)sampleRange.minimum / sampleRate;
float stopTime = (float)sampleRange.maximum / sampleRate;
float duration = stopTime - startTime;

painter.save();

QPen pen(Qt::white, 1, Qt::SolidLine);
painter.setPen(pen);
QFontMetrics fm(painter.font());

int tickWidth = 80;
int maxTicks = rect.width() / tickWidth;

double durationPerTick = 10 * pow(10, floor(log(duration / maxTicks) / log(10)));

double firstTick = int(startTime / durationPerTick) * durationPerTick;

double tick = firstTick;

while (tick <= stopTime) {

off_t tickSample = tick * sampleRate;
int tickLine = (tickSample - sampleRange.minimum) / getStride();

char buf[128];
snprintf(buf, sizeof(buf), "%.06f", tick);
painter.drawLine(tickLine, 0, tickLine, 30);
painter.drawText(tickLine + 2, 25, buf);

tick += durationPerTick;
}

// Draw small ticks
durationPerTick /= 10;
firstTick = int(startTime / durationPerTick) * durationPerTick;
tick = firstTick;
while (tick <= stopTime) {

off_t tickSample = tick * sampleRate;
int tickLine = (tickSample - sampleRange.minimum) / getStride();

painter.drawLine(tickLine, 0, tickLine, 10);
tick += durationPerTick;
}

painter.restore();
}

void SpectrogramPlot::paintMid(QPainter &painter, QRect &rect, range_t<off_t> sampleRange)
Expand Down Expand Up @@ -293,15 +239,3 @@ uint qHash(const TileCacheKey &key, uint seed)
{
return key.fftSize ^ key.zoomLevel ^ key.sample ^ seed;
}

void SpectrogramPlot::setSampleRate(off_t rate)
{
sampleRate = rate;
emit repaint();
}

void SpectrogramPlot::setTimeScaleEnable(bool enabled)
{
timeScaleIsEnabled = enabled;
emit repaint();
}
5 changes: 0 additions & 5 deletions spectrogramplot.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class SpectrogramPlot : public Plot
void paintFront(QPainter &painter, QRect &rect, range_t<off_t> sampleRange) override;
void paintMid(QPainter &painter, QRect &rect, range_t<off_t> sampleRange) override;
bool mouseEvent(QEvent::Type type, QMouseEvent event) override;
void setSampleRate(off_t rate);
void setTimeScaleEnable(bool enabled);

public slots:
void setFFTSize(int size);
Expand All @@ -67,8 +65,6 @@ public slots:
int zoomLevel;
float powerMax;
float powerMin;
off_t sampleRate = 0;
bool timeScaleIsEnabled;

Tuner tuner;
std::shared_ptr<TunerTransform> tunerTransform;
Expand All @@ -81,7 +77,6 @@ public slots:
std::vector<float> getTunerTaps();
int linesPerTile();
bool tunerEnabled();
void paintTimeScale(QPainter &painter, QRect &rect, range_t<off_t> sampleRange);
};

class TileCacheKey
Expand Down

0 comments on commit e2d066c

Please sign in to comment.