diff --git a/mainwindow.cpp b/mainwindow.cpp index 723a74e..c438376 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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(&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))); diff --git a/plotview.cpp b/plotview.cpp index 013255e..9174829 100644 --- a/plotview.cpp +++ b/plotview.cpp @@ -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>>(mainSampleSource)); @@ -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 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; @@ -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(); } diff --git a/plotview.h b/plotview.h index 094ec09..61398de 100644 --- a/plotview.h +++ b/plotview.h @@ -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; @@ -74,6 +74,8 @@ public slots: int powerMin; int powerMax; bool cursorsEnabled; + off_t sampleRate = 0; + bool timeScaleEnabled; void addPlot(Plot *plot); void emitTimeSelection(); @@ -81,4 +83,5 @@ public slots: int plotsHeight(); off_t samplesPerLine(); void updateView(bool reCenter = false); + void paintTimeScale(QPainter &painter, QRect &rect, range_t sampleRange); }; diff --git a/spectrogramplot.cpp b/spectrogramplot.cpp index 8af44c5..fd8ce88 100644 --- a/spectrogramplot.cpp +++ b/spectrogramplot.cpp @@ -37,7 +37,6 @@ SpectrogramPlot::SpectrogramPlot(std::shared_ptr { if (tunerEnabled()) tuner.paintFront(painter, rect, sampleRange); - - if (timeScaleIsEnabled) - paintTimeScale(painter, rect, sampleRange); -} - -void SpectrogramPlot::paintTimeScale(QPainter &painter, QRect &rect, range_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 sampleRange) @@ -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(); -} diff --git a/spectrogramplot.h b/spectrogramplot.h index a620618..44d7ee0 100644 --- a/spectrogramplot.h +++ b/spectrogramplot.h @@ -42,8 +42,6 @@ class SpectrogramPlot : public Plot void paintFront(QPainter &painter, QRect &rect, range_t sampleRange) override; void paintMid(QPainter &painter, QRect &rect, range_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); @@ -67,8 +65,6 @@ public slots: int zoomLevel; float powerMax; float powerMin; - off_t sampleRate = 0; - bool timeScaleIsEnabled; Tuner tuner; std::shared_ptr tunerTransform; @@ -81,7 +77,6 @@ public slots: std::vector getTunerTaps(); int linesPerTile(); bool tunerEnabled(); - void paintTimeScale(QPainter &painter, QRect &rect, range_t sampleRange); }; class TileCacheKey