Skip to content

Commit

Permalink
Shortcuts for adding/removing plots
Browse files Browse the repository at this point in the history
  • Loading branch information
jankae committed Oct 24, 2023
1 parent 59f3057 commit ccb71f9
Show file tree
Hide file tree
Showing 16 changed files with 397 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ void TileWidget::setPlot(TracePlot *plot)
}
}

void TileWidget::removePlot()
{
if(hasContent) {
content->setParentTile(nullptr);
removeContent();
}
}

TileWidget::TileWidget(TraceModel &model, TileWidget &parent)
: TileWidget(model)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public slots:
void splitHorizontally(bool moveContentToSecondChild = false);
void closeTile();
void setPlot(TracePlot *plot);
void removePlot();

private slots:
void on_bSmithchart_clicked();
Expand Down
14 changes: 7 additions & 7 deletions Software/PC_Application/LibreVNA-GUI/Traces/eyediagramplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,19 +659,19 @@ void EyeDiagramPlot::draw(QPainter &p)
} else {
qDebug() << "Empty eye data, displaydata:" << displayData;
}
if(dropPending) {
p.setOpacity(0.5);
p.setBrush(Qt::white);
p.setPen(Qt::white);
if(dropPending && supported(dropTrace)) {
p.setOpacity(dropOpacity);
p.setBrush(dropBackgroundColor);
p.setPen(dropForegroundColor);
// show drop area over whole plot
p.drawRect(plotRect);
p.drawRect(getDropRect());
auto font = p.font();
font.setPixelSize(20);
p.setFont(font);
p.setOpacity(1.0);
p.setPen(Qt::white);
p.setPen(dropSection == DropSection::OnPlot ? dropHighlightColor : dropForegroundColor);
auto text = "Drop here to add\n" + dropTrace->name() + "\nto eye diagram";
p.drawText(plotRect, Qt::AlignCenter, text);
p.drawText(getDropRect(), Qt::AlignCenter, text);
}
}

Expand Down
188 changes: 185 additions & 3 deletions Software/PC_Application/LibreVNA-GUI/Traces/traceplot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include "preferences.h"
#include "Util/util.h"
#include "CustomWidgets/tilewidget.h"
#include "tracexyplot.h"
#include "tracesmithchart.h"
#include "eyediagramplot.h"
#include "tracewaterfall.h"
#include "tracepolarchart.h"

#include <QPainter>
#include <QPainterPath>
Expand Down Expand Up @@ -101,6 +106,41 @@ void TracePlot::updateSpan(double min, double max)
triggerReplot();
}

QString TracePlot::TypeToString(Type t)
{
switch(t) {
case Type::EyeDiagram: return "Eye Diagram";
case Type::PolarChart: return "Polar Chart";
case Type::SmithChart: return "Smith Chart";
case Type::Waterfall: return "Waterfall";
case Type::XYPlot: return "XY Plot";
}
}

TracePlot::Type TracePlot::TypeFromString(QString s)
{
for(unsigned int i=0;i<=(int) Type::EyeDiagram;i++) {
if(TypeToString((Type) i) == s) {
return (Type) i;
}
}
// use default
return Type::XYPlot;
}

TracePlot *TracePlot::createFromType(TraceModel &model, Type t)
{
switch(t) {
case Type::EyeDiagram: return new EyeDiagramPlot(model);
case Type::PolarChart: return new TracePolarChart(model);
case Type::SmithChart: return new TraceSmithChart(model);
case Type::Waterfall: return new TraceWaterfall(model);
case Type::XYPlot: return new TraceXYPlot(model);
default:
return nullptr;
}
}

void TracePlot::initializeTraceInfo()
{
// Populate already present traces
Expand Down Expand Up @@ -295,6 +335,46 @@ void TracePlot::paintEvent(QPaintEvent *event)
p.setWindow(0, 0, w, h);

draw(p);

if(dropPending) {
p.setOpacity(dropOpacity);
p.setBrush(dropBackgroundColor);
p.setPen(dropForegroundColor);

auto dropRect = getDropRect();

p.fillRect(0, 0, dropRect.left(), h-1, p.brush());
p.fillRect(dropRect.left(), 0, dropRect.width()-1, dropRect.top(), p.brush());
p.fillRect(dropRect.left(), dropRect.bottom(), dropRect.width()-1, h-1, p.brush());
p.fillRect(dropRect.right(), 0, w-1, h-1, p.brush());

p.setOpacity(1.0);
p.drawLine(QPoint(0, 0), dropRect.topLeft());
p.drawLine(QPoint(0, h-1), dropRect.bottomLeft());
p.drawLine(QPoint(w-1, 0), dropRect.topRight());
p.drawLine(QPoint(w-1, h-1), dropRect.bottomRight());
p.drawLine(QPoint(0, 0), QPoint(0, h-1));
p.drawLine(QPoint(0, h-1), QPoint(w-1, h-1));
p.drawLine(QPoint(w-1, h-1), QPoint(w-1, 0));
p.drawLine(QPoint(w-1, 0), QPoint(0, 0));
p.drawLine(dropRect.topLeft(), dropRect.topRight());
p.drawLine(dropRect.topRight(), dropRect.bottomRight());
p.drawLine(dropRect.bottomRight(), dropRect.bottomLeft());
p.drawLine(dropRect.bottomLeft(), dropRect.topLeft());

auto font = p.font();
font.setPixelSize(20);
p.setFont(font);
p.setPen(dropSection == DropSection::Above ? dropHighlightColor : dropForegroundColor);
p.drawText(QRect(0, 0, w, dropRect.top()), Qt::AlignCenter, "Insert above");
p.setPen(dropSection == DropSection::Below ? dropHighlightColor : dropForegroundColor);
p.drawText(QRect(0, dropRect.bottom(), w, dropRect.top()), Qt::AlignCenter, "Insert below");
p.setPen(dropSection == DropSection::ToTheLeft ? dropHighlightColor : dropForegroundColor);
p.drawText(QRect(0, 0, dropRect.left(), h), Qt::AlignCenter, "Insert to\nthe left");
p.setPen(dropSection == DropSection::ToTheRight ? dropHighlightColor : dropForegroundColor);
p.drawText(QRect(dropRect.right(), 0, dropRect.left(), h), Qt::AlignCenter, "Insert to\nthe right");
}

replotTimer.start(MaxUpdateInterval);
}

Expand Down Expand Up @@ -330,6 +410,15 @@ void TracePlot::finishContextMenu()
contextmenu->addMenu(add);
}

auto removeTile = new QAction("Remove Tile", contextmenu);
contextmenu->addAction(removeTile);
connect(removeTile, &QAction::triggered, [=]() {
markedForDeletion = true;
QTimer::singleShot(0, [=](){
parentTile->closeTile();
});
});

auto close = new QAction("Close", contextmenu);
contextmenu->addAction(close);
connect(close, &QAction::triggered, [=]() {
Expand Down Expand Up @@ -511,19 +600,91 @@ void TracePlot::dragEnterEvent(QDragEnterEvent *event)
quintptr dropPtr;
stream >> dropPtr;
auto trace = (Trace*) dropPtr;
if(dropSupported(trace)) {
// if(dropSupported(trace)) {
event->acceptProposedAction();
dropPending = true;
dropTrace = trace;
}
// }
}
triggerReplot();
}

void TracePlot::dragMoveEvent(QDragMoveEvent *event)
{
if(!dropPending) {
return;
}
auto dropRect = getDropRect();
auto pos = event->position().toPoint() - QPoint(marginLeft, marginTop);
if(dropRect.contains(pos)) {
dropSection = DropSection::OnPlot;
} else {
// transform to relative coordinates from 0 to 1
auto x = (double) pos.x() / (width() - marginLeft - marginRight);
auto y = (double) pos.y() / (height() - marginTop - marginBottom);
qDebug() << "x:" << x << "y:" << y;
if(y < 0.5) {
if(x < y) {
dropSection = DropSection::ToTheLeft;
} else if(x > (1.0 - y)) {
dropSection = DropSection::ToTheRight;
} else {
dropSection = DropSection::Above;
}
} else {
if(x < (1.0 - y)) {
dropSection = DropSection::ToTheLeft;
} else if(x > y) {
dropSection = DropSection::ToTheRight;
} else {
dropSection = DropSection::Below;
}
}
}
dropPosition = pos;
replot();
}

void TracePlot::dropEvent(QDropEvent *event)
{
if(dropTrace) {
traceDropped(dropTrace, event->position().toPoint() - - QPoint(marginLeft, marginTop));
if(dropSection == DropSection::OnPlot) {
traceDropped(dropTrace, event->position().toPoint() - - QPoint(marginLeft, marginTop));
} else {
TileWidget *newTile = nullptr;
// parentTile will be modified by the split, save here
TileWidget *oldParent = parentTile;
switch(dropSection) {
case DropSection::Above:
parentTile->splitVertically(true);
newTile = oldParent->Child1();
break;
case DropSection::Below:
parentTile->splitVertically(false);
newTile = oldParent->Child2();
break;
case DropSection::ToTheLeft:
parentTile->splitHorizontally(true);
newTile = oldParent->Child1();
break;
case DropSection::ToTheRight:
parentTile->splitHorizontally(false);
newTile = oldParent->Child2();
break;
case DropSection::OnPlot:
// already handled above
break;
}
TracePlot *graph = createDefaultPlotForTrace(model, dropTrace);
if(!graph->configureForTrace(dropTrace)) {
// can't be used for the configuration the trace is in, fall back to XY-Plot
delete graph;
graph = new TraceXYPlot(model);
graph->configureForTrace(dropTrace);
}
newTile->setPlot(graph);
graph->enableTrace(dropTrace, true);
}
}
dropPending = false;
dropTrace = nullptr;
Expand All @@ -547,11 +708,32 @@ void TracePlot::traceDropped(Trace *t, QPoint position)
}
}

QRect TracePlot::getDropRect()
{
constexpr double dropBorders = 0.2;
auto w = width() - marginLeft - marginRight;
auto h = height() - marginTop - marginBottom;
return QRect(QPoint(w*dropBorders, h*dropBorders), QSize(w*(1.0-2*dropBorders), h*(1.0-2*dropBorders)));
}

std::set<TracePlot *> TracePlot::getPlots()
{
return plots;
}

TracePlot *TracePlot::createDefaultPlotForTrace(TraceModel &model, Trace *t)
{
auto &p = Preferences::getInstance();

TracePlot *ret = nullptr;
if(t->isReflection()) {
ret = createFromType(model, TypeFromString(p.Graphs.defaultGraphs.reflection));
} else {
ret = createFromType(model, TypeFromString(p.Graphs.defaultGraphs.transmission));
}
return ret;
}

void TracePlot::newTraceAvailable(Trace *t)
{
traces[t] = false;
Expand Down
22 changes: 21 additions & 1 deletion Software/PC_Application/LibreVNA-GUI/Traces/traceplot.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef TRACEPLOT_H
#ifndef TRACEPLOT_H
#define TRACEPLOT_H

#include "tracemodel.h"
Expand Down Expand Up @@ -33,6 +33,10 @@ class TracePlot : public QWidget, public Savable
void mouseDoubleClickEvent(QMouseEvent *event) override;
virtual void updateSpan(double min, double max);
virtual Type getType() = 0;
static QString TypeToString(Type t);
static Type TypeFromString(QString s);
static TracePlot* createFromType(TraceModel &model, Type t);
static TracePlot *createDefaultPlotForTrace(TraceModel &model, Trace *t);

static std::set<TracePlot *> getPlots();

Expand Down Expand Up @@ -92,10 +96,12 @@ public slots:
// handle trace drops
virtual bool dropSupported(Trace *t);
void dragEnterEvent(QDragEnterEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
void dropEvent(QDropEvent *event) override;
void dragLeaveEvent(QDragLeaveEvent *event) override;
virtual void traceDropped(Trace *t, QPoint position);
virtual QString mouseText(QPoint pos) {Q_UNUSED(pos) return QString();}
QRect getDropRect();

protected slots:
void newTraceAvailable(Trace *t);
Expand All @@ -110,6 +116,11 @@ protected slots:
static constexpr unsigned int marginLeft = 0;
static constexpr unsigned int marginRight = 0;

static constexpr double dropOpacity = 0.9;
static constexpr auto dropBackgroundColor = Qt::darkGray;
static constexpr auto dropForegroundColor = Qt::white;
static constexpr auto dropHighlightColor = Qt::red;

double sweep_fmin, sweep_fmax;
double xSweep; // current position in the sweep (NaN if no live traces are active on the plot)
TraceModel &model;
Expand All @@ -123,6 +134,15 @@ protected slots:

bool dropPending;
QPoint dropPosition;
enum class DropSection {
Above,
Below,
ToTheLeft,
ToTheRight,
OnPlot,
};
DropSection dropSection;

Trace *dropTrace;

QLabel *cursorLabel;
Expand Down
14 changes: 6 additions & 8 deletions Software/PC_Application/LibreVNA-GUI/Traces/tracepolarchart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,20 @@ void TracePolarChart::draw(QPainter &p) {
}
}

if(dropPending) {
if(dropPending && supported(dropTrace)) {
// adjust coords due to shifted restore
p.setOpacity(0.5);
p.setBrush(Qt::white);
p.setPen(Qt::white);
p.drawEllipse(-polarCoordMax, -polarCoordMax, 2*polarCoordMax, 2*polarCoordMax);
p.setOpacity(dropOpacity);
p.setBrush(dropBackgroundColor);
p.setPen(dropForegroundColor);
p.drawRect(getDropRect());
auto font = p.font();
font.setPixelSize(20);
p.setFont(font);
p.setOpacity(1.0);
p.setPen(Qt::white);
p.setPen(dropSection == DropSection::OnPlot ? dropHighlightColor : dropForegroundColor);
auto text = "Drop here to add\n" + dropTrace->name() + "\nto polar chart";
p.drawText(p.window(), Qt::AlignCenter, text);
} else {
}

}

bool TracePolarChart::dropSupported(Trace *t)
Expand Down
Loading

0 comments on commit ccb71f9

Please sign in to comment.