diff --git a/cursors.cpp b/cursors.cpp index 5a9e38a..6995ddb 100644 --- a/cursors.cpp +++ b/cursors.cpp @@ -17,29 +17,75 @@ * along with this program. If not, see . */ -#include +#include +#include #include "cursors.h" -Cursors::Cursors(QWidget * parent) - : QWidget::QWidget(parent) +Cursors::Cursors(QObject * parent) : QObject::QObject(parent) { } -void Cursors::paintEvent(QPaintEvent *event) +// Return true if point is over a cursor, put cursor ID in `cursor` +bool Cursors::pointOverCursor(QPoint point, int &cursor) { - QPainter painter(this); + int margin = 5; + for (int i = 0; i < 2; i++) { + range_t range = {cursorPositions[i] - margin, cursorPositions[i] + margin}; + if (range.contains(point.x())) { + cursor = i; + return true; + } + } + return false; +} +bool Cursors::eventFilter(QObject *obj, QEvent *event) +{ + // Start dragging on left mouse button press, if over a cursor + if (event->type() == QEvent::MouseButtonPress) { + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->button() == Qt::LeftButton) { + if (pointOverCursor(mouseEvent->pos(), selectedCursor)) { + dragging = true; + return true; + } + } + + // Update current cursor positon if we're dragging + } else if (event->type() == QEvent::MouseMove) { + QMouseEvent *mouseEvent = static_cast(event); + if (dragging) { + cursorPositions[selectedCursor] = mouseEvent->pos().x(); + emit cursorsMoved(); + } + + // Stop dragging on left mouse button release + } else if (event->type() == QEvent::MouseButtonRelease) { + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->button() == Qt::LeftButton) { + dragging = false; + return true; + } + } + return false; +} + +void Cursors::paintFront(QPainter &painter, QRect &rect, range_t sampleRange) +{ painter.save(); // Draw translucent white fill for highlight - painter.fillRect(rect(), QBrush(QColor(255, 255, 255, 50))); + painter.fillRect( + QRect(cursorPositions[0], rect.top(), cursorPositions[1] - cursorPositions[0], rect.bottom()), + QBrush(QColor(255, 255, 255, 50)) + ); // Draw vertical edges QPen pen(Qt::white, 1, Qt::DashLine); painter.setPen(pen); - painter.drawLine(rect().left(), rect().top(), rect().left(), rect().bottom()); - painter.drawLine(rect().right(), rect().top(), rect().right(), rect().bottom()); + painter.drawLine(cursorPositions[0], rect.top(), cursorPositions[0], rect.bottom()); + painter.drawLine(cursorPositions[1], rect.top(), cursorPositions[1], rect.bottom()); painter.restore(); } diff --git a/cursors.h b/cursors.h index 6d0ef6c..ef02964 100644 --- a/cursors.h +++ b/cursors.h @@ -19,16 +19,30 @@ #pragma once -#include +#include +#include +#include +#include "util.h" -class Cursors : public QWidget +class Cursors : public QObject { Q_OBJECT public: - Cursors(QWidget * parent); + Cursors(QObject * parent); + void paintFront(QPainter &painter, QRect &rect, range_t sampleRange); + +signals: + void cursorsMoved(); protected: - void paintEvent(QPaintEvent *event); + bool eventFilter(QObject *obj, QEvent *event) override; + +private: + bool pointOverCursor(QPoint point, int &cursor); + + bool dragging = false; + int selectedCursor = 0; + int cursorPositions[2] = {0, 50}; }; diff --git a/plotview.cpp b/plotview.cpp index 585cc8b..683ecdd 100644 --- a/plotview.cpp +++ b/plotview.cpp @@ -35,6 +35,8 @@ PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0}) mainSampleSource = input; setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); enableCursors(false); + viewport()->installEventFilter(&cursors); + connect(&cursors, SIGNAL(cursorsMoved()), this, SLOT(cursorsMoved())); spectrogramPlot = new SpectrogramPlot(mainSampleSource); plots.emplace_back(spectrogramPlot); @@ -86,12 +88,15 @@ TracePlot* PlotView::createQuadratureDemodPlot(SampleSource> ); } +void PlotView::cursorsMoved() +{ + viewport()->update(); +} + void PlotView::enableCursors(bool enabled) { - if (enabled) - cursors.show(); - else - cursors.hide(); + cursorsEnabled = enabled; + viewport()->update(); } void PlotView::invalidateEvent() @@ -168,6 +173,8 @@ void PlotView::paintEvent(QPaintEvent *event) PLOT_LAYER(paintBack); PLOT_LAYER(paintMid); PLOT_LAYER(paintFront); + if (cursorsEnabled) + cursors.paintFront(painter, rect, {viewRange.first, viewRange.second}); #undef PLOT_LAYER } @@ -179,7 +186,7 @@ void PlotView::resizeEvent(QResizeEvent * event) // Resize cursors // TODO: don't hardcode this int margin = rect.width() / 3; - cursors.setGeometry(QRect(rect.left() + margin, rect.top(), rect.right() - rect.left() - 2 * margin, rect.height())); + //cursors.setGeometry(QRect(rect.left() + margin, rect.top(), rect.right() - rect.left() - 2 * margin, rect.height())); updateView(); } diff --git a/plotview.h b/plotview.h index d46814d..55a46a7 100644 --- a/plotview.h +++ b/plotview.h @@ -37,6 +37,7 @@ public: PlotView(InputSource *input); public slots: + void cursorsMoved(); void enableCursors(bool enable); void invalidateEvent(); void selectionChanged(std::pair selectionTime, std::pair selectionFreq); @@ -66,6 +67,7 @@ private: int zoomLevel; int powerMin; int powerMax; + bool cursorsEnabled; TracePlot* createIQPlot(SampleSource> *src); TracePlot* createQuadratureDemodPlot(SampleSource> *src); diff --git a/spectrogramplot.cpp b/spectrogramplot.cpp index 09d90b6..f2fc330 100644 --- a/spectrogramplot.cpp +++ b/spectrogramplot.cpp @@ -64,42 +64,6 @@ void SpectrogramPlot::xyToFreqTime(int x, int y, float *freq, float *time) *time = (float)lineToSample(y) / sampleRate; } -void SpectrogramPlot::mouseReleaseEvent(QMouseEvent *event) -{ - if (deltaDragIsEnabled) { - cursorStartX = -1; - update(); - } -} - -void SpectrogramPlot::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 SpectrogramPlot::mousePressEvent(QMouseEvent *event) -{ - if (cursorStartX == -1) { - cursorEndX = cursorStartX = event->x(); - cursorEndY = cursorStartY = event->y(); - } else { - cursorStartX = -1; - } - update(); -} - void SpectrogramPlot::paintMid(QPainter &painter, QRect &rect, range_t sampleRange) { if (!inputSource || inputSource->count() == 0)