Skip to content

Commit

Permalink
Merge pull request #215 from daniestevez/annotation-comments
Browse files Browse the repository at this point in the history
Display annotation comments as tooltips
  • Loading branch information
schneider42 authored Nov 6, 2022
2 parents 6b8a440 + 11171c8 commit 290572e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/inputsource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ void InputSource::readMetaData(const QString &filename)

auto label = sigmf_annotation["core:label"].toString();

annotationList.emplace_back(sampleRange, frequencyRange, label);
auto comment = sigmf_annotation["core:comment"].toString();

annotationList.emplace_back(sampleRange, frequencyRange, label, comment);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ MainWindow::MainWindow()
connect(dock->cursorsCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableCursors);
connect(dock->scalesCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableScales);
connect(dock->annosCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableAnnotations);
connect(dock->annosCheckBox, &QCheckBox::stateChanged, dock, &SpectrogramControls::enableAnnotations);
connect(dock->commentsCheckBox, &QCheckBox::stateChanged, plots, &PlotView::enableAnnotationCommentsTooltips);
connect(dock->cursorSymbolsSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), plots, &PlotView::setCursorSegments);

// Connect dock outputs
Expand Down
42 changes: 42 additions & 0 deletions src/plotview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QRadioButton>
#include <QScrollBar>
#include <QSpinBox>
#include <QToolTip>
#include <QVBoxLayout>
#include "plots.h"

Expand All @@ -50,6 +51,7 @@ PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0})
enableScales(true);

enableAnnotations(true);
enableAnnotationCommentsTooltips(true);

addPlot(spectrogramPlot);

Expand All @@ -62,6 +64,39 @@ void PlotView::addPlot(Plot *plot)
connect(plot, &Plot::repaint, this, &PlotView::repaint);
}

void PlotView::mouseMoveEvent(QMouseEvent *event)
{
updateAnnotationTooltip(event);
QGraphicsView::mouseMoveEvent(event);
}

void PlotView::mouseReleaseEvent(QMouseEvent *event)
{
// This is used to show the tooltip again on drag release if the mouse is
// hovering over an annotation.
updateAnnotationTooltip(event);
QGraphicsView::mouseReleaseEvent(event);
}

void PlotView::updateAnnotationTooltip(QMouseEvent *event)
{
// If there are any mouse buttons pressed, we assume
// that the plot is being dragged and hide the tooltip.
bool isDrag = event->buttons() != Qt::NoButton;
if (!annotationCommentsEnabled
|| !spectrogramPlot->isAnnotationsEnabled()
|| isDrag) {
QToolTip::hideText();
} else {
QString* comment = spectrogramPlot->mouseAnnotationComment(event);
if (comment != nullptr) {
QToolTip::showText(event->globalPos(), *comment);
} else {
QToolTip::hideText();
}
}
}

void PlotView::contextMenuEvent(QContextMenuEvent * event)
{
QMenu menu;
Expand Down Expand Up @@ -612,6 +647,13 @@ void PlotView::enableAnnotations(bool enabled)
viewport()->update();
}

void PlotView::enableAnnotationCommentsTooltips(bool enabled)
{
annotationCommentsEnabled = enabled;

viewport()->update();
}

int PlotView::sampleToColumn(size_t sample)
{
return sample / samplesPerColumn();
Expand Down
5 changes: 5 additions & 0 deletions src/plotview.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public slots:
void enableCursors(bool enabled);
void enableScales(bool enabled);
void enableAnnotations(bool enabled);
void enableAnnotationCommentsTooltips(bool enabled);
void invalidateEvent() override;
void repaint();
void setCursorSegments(int segments);
Expand All @@ -55,6 +56,8 @@ public slots:
void setPowerMax(int power);

protected:
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void contextMenuEvent(QContextMenuEvent * event) override;
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent * event) override;
Expand All @@ -80,6 +83,7 @@ public slots:
double sampleRate = 0.0;
bool timeScaleEnabled;
int scrollZoomStepsAccumulated = 0;
bool annotationCommentsEnabled;

void addPlot(Plot *plot);
void emitTimeSelection();
Expand All @@ -91,6 +95,7 @@ public slots:
void updateViewRange(bool reCenter);
void updateView(bool reCenter = false, bool expanding = false);
void paintTimeScale(QPainter &painter, QRect &rect, range_t<size_t> sampleRange);
void updateAnnotationTooltip(QMouseEvent *event);

int sampleToColumn(size_t sample);
size_t columnToSample(int col);
Expand Down
7 changes: 5 additions & 2 deletions src/samplesource.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ class Annotation
range_t<size_t> sampleRange;
range_t<double> frequencyRange;
QString label;
QString comment;

Annotation(range_t<size_t> sampleRange, range_t<double>frequencyRange, QString label)
: sampleRange(sampleRange), frequencyRange(frequencyRange), label(label) {}
Annotation(range_t<size_t> sampleRange, range_t<double>frequencyRange, QString label,
QString comment)
: sampleRange(sampleRange), frequencyRange(frequencyRange), label(label),
comment(comment) {}
};

template<typename T>
Expand Down
8 changes: 8 additions & 0 deletions src/spectrogramcontrols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ SpectrogramControls::SpectrogramControls(const QString & title, QWidget * parent

annosCheckBox = new QCheckBox(widget);
layout->addRow(new QLabel(tr("Display Annotations:")), annosCheckBox);
commentsCheckBox = new QCheckBox(widget);
layout->addRow(new QLabel(tr("Display annotation comments tooltips:")), commentsCheckBox);

widget->setLayout(layout);
setWidget(widget);
Expand Down Expand Up @@ -134,6 +136,7 @@ void SpectrogramControls::setDefaults()
cursorSymbolsSpinBox->setValue(1);

annosCheckBox->setCheckState(Qt::Checked);
commentsCheckBox->setCheckState(Qt::Checked);

// Try to set the sample rate from the last-used value
QSettings settings;
Expand Down Expand Up @@ -235,3 +238,8 @@ void SpectrogramControls::zoomOut()
{
zoomLevelSlider->setValue(zoomLevelSlider->value() - 1);
}

void SpectrogramControls::enableAnnotations(bool enabled) {
// disable annotation comments checkbox when annotations are disabled
commentsCheckBox->setEnabled(enabled);
}
2 changes: 2 additions & 0 deletions src/spectrogramcontrols.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public slots:
void timeSelectionChanged(float time);
void zoomIn();
void zoomOut();
void enableAnnotations(bool enabled);

private slots:
void fftSizeChanged(int value);
Expand Down Expand Up @@ -74,4 +75,5 @@ private slots:
QLabel *symbolPeriodLabel;
QCheckBox *scalesCheckBox;
QCheckBox *annosCheckBox;
QCheckBox *commentsCheckBox;
};
22 changes: 22 additions & 0 deletions src/spectrogramplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ void SpectrogramPlot::paintAnnotations(QPainter &painter, QRect &rect, range_t<s
painter.setPen(pen);
QFontMetrics fm(painter.font());

visibleAnnotationLocations.clear();

for (int i = 0; i < inputSource->annotationList.size(); i++) {
Annotation a = inputSource->annotationList.at(i);

Expand All @@ -195,12 +197,27 @@ void SpectrogramPlot::paintAnnotations(QPainter &painter, QRect &rect, range_t<s
// Draw the label 2 pixels above the box
painter.drawText(x, y - 2, a.label);
painter.drawRect(x, y, width, height);

visibleAnnotationLocations.emplace_back(a, x, y, width, height);
}
}

painter.restore();
}

QString *SpectrogramPlot::mouseAnnotationComment(const QMouseEvent *event) {
auto pos = event->pos();
int mouse_x = pos.x();
int mouse_y = pos.y();

for (auto& a : visibleAnnotationLocations) {
if (a.isInside(mouse_x, mouse_y)) {
return &a.annotation.comment;
}
}
return nullptr;
}

void SpectrogramPlot::paintMid(QPainter &painter, QRect &rect, range_t<size_t> sampleRange)
{
if (!inputSource || inputSource->count() == 0)
Expand Down Expand Up @@ -395,6 +412,11 @@ void SpectrogramPlot::enableAnnotations(bool enabled)
sigmfAnnotationsEnabled = enabled;
}

bool SpectrogramPlot::isAnnotationsEnabled(void)
{
return sigmfAnnotationsEnabled;
}

bool SpectrogramPlot::tunerEnabled()
{
return (tunerTransform->subscriberCount() > 0);
Expand Down
26 changes: 26 additions & 0 deletions src/spectrogramplot.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <QCache>
#include <QString>
#include <QWidget>
#include "fft.h"
#include "inputsource.h"
Expand All @@ -30,8 +31,10 @@
#include <memory>
#include <array>
#include <math.h>
#include <vector>

class TileCacheKey;
class AnnotationLocation;

class SpectrogramPlot : public Plot
{
Expand All @@ -49,6 +52,8 @@ class SpectrogramPlot : public Plot
bool tunerEnabled();
void enableScales(bool enabled);
void enableAnnotations(bool enabled);
bool isAnnotationsEnabled();
QString *mouseAnnotationComment(const QMouseEvent *event);

public slots:
void setFFTSize(int size);
Expand All @@ -62,6 +67,7 @@ public slots:
static const int tileSize = 65536; // This must be a multiple of the maximum FFT size

std::shared_ptr<SampleSource<std::complex<float>>> inputSource;
std::vector<AnnotationLocation> visibleAnnotationLocations;
std::unique_ptr<FFT> fft;
std::unique_ptr<float[]> window;
QCache<TileCacheKey, QPixmap> pixmapCache;
Expand Down Expand Up @@ -110,3 +116,23 @@ class TileCacheKey
int zoomLevel;
size_t sample;
};

class AnnotationLocation
{
public:
Annotation annotation;

AnnotationLocation(Annotation annotation, int x, int y, int width, int height)
: annotation(annotation), x(x), y(y), width(width), height(height) {}

bool isInside(int pos_x, int pos_y) {
return (x <= pos_x) && (pos_x <= x + width)
&& (y <= pos_y) && (pos_y <= y + height);
}

private:
int x;
int y;
int width;
int height;
};

0 comments on commit 290572e

Please sign in to comment.