Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/SciQLopPlots/SciQLopPlotInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@
throw std::runtime_error("Not implemented");
}

virtual void remove_plottable(SciQLopPlottableInterface* plottable)
{
delete plottable;

Check failure on line 360 in include/SciQLopPlots/SciQLopPlotInterface.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rewrite the code so that you no longer need this "delete".

See more on https://sonarcloud.io/project/issues?id=SciQLop_SciQLopPlots&issues=AZ17x-AtXISY38E6BFRH&open=AZ17x-AtXISY38E6BFRH&pullRequest=40
}

virtual void remove_plottable(int index)
{
delete plottable(index);

Check failure on line 365 in include/SciQLopPlots/SciQLopPlotInterface.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rewrite the code so that you no longer need this "delete".

See more on https://sonarcloud.io/project/issues?id=SciQLop_SciQLopPlots&issues=AZ17x-AtXISY38E6BFRI&open=AZ17x-AtXISY38E6BFRI&pullRequest=40
}

virtual void remove_plottable(const QString& name)
{
delete plottable(name);

Check failure on line 370 in include/SciQLopPlots/SciQLopPlotInterface.hpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rewrite the code so that you no longer need this "delete".

See more on https://sonarcloud.io/project/issues?id=SciQLop_SciQLopPlots&issues=AZ17x-AtXISY38E6BFRJ&open=AZ17x-AtXISY38E6BFRJ&pullRequest=40
}

inline bool selected() const noexcept { return m_selected; }

void set_selected(bool selected) noexcept;
Expand Down
2 changes: 2 additions & 0 deletions src/SciQLopPlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ void SciQLopPlot::_register_plottable_wrapper(SciQLopPlottableInterface* plottab
[this, plottable]()
{
m_plottables.removeOne(plottable);
if (m_color_map == plottable)
m_color_map = nullptr;
emit this->plotables_list_changed();
});
connect(plottable, &SciQLopGraphInterface::replot, this, [this]() { this->replot(); });
Expand Down
106 changes: 106 additions & 0 deletions tests/integration/test_remove_plottable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""Tests for SciQLopPlotInterface.remove_plottable()."""
import pytest
import numpy as np
from PySide6.QtWidgets import QApplication

from SciQLopPlots import SciQLopPlot, SciQLopMultiPlotPanel
from conftest import force_gc, process_events


class TestRemovePlottableByRef:

def test_remove_line_graph(self, qtbot, sample_data):
x, y = sample_data
p = SciQLopPlot()
qtbot.addWidget(p)
g = p.line(x, y)
assert len(p.plottables()) == 1

p.remove_plottable(g)
assert len(p.plottables()) == 0

def test_remove_one_of_many(self, qtbot, sample_data):
x, y = sample_data
p = SciQLopPlot()
qtbot.addWidget(p)
g1 = p.line(x, y, labels=["a"])
g2 = p.line(x, y, labels=["b"])
g3 = p.line(x, y, labels=["c"])
assert len(p.plottables()) == 3

p.remove_plottable(g2)
remaining = p.plottables()
assert len(remaining) == 2
assert g1 in remaining
assert g3 in remaining

def test_remove_none_is_safe(self, qtbot):
p = SciQLopPlot()
qtbot.addWidget(p)
p.remove_plottable(None)
assert len(p.plottables()) == 0


class TestRemovePlottableByIndex:

def test_remove_first(self, qtbot, sample_data):
x, y = sample_data
p = SciQLopPlot()
qtbot.addWidget(p)
p.line(x, y, labels=["a"])
g2 = p.line(x, y, labels=["b"])
assert len(p.plottables()) == 2

p.remove_plottable(0)
remaining = p.plottables()
assert len(remaining) == 1
assert remaining[0] is g2


class TestRemovePlottableByName:

def test_remove_by_object_name(self, qtbot, sample_data):
x, y = sample_data
p = SciQLopPlot()
qtbot.addWidget(p)
g = p.line(x, y)
g.name = "my_graph"
assert len(p.plottables()) == 1

p.remove_plottable("my_graph")
assert len(p.plottables()) == 0

def test_remove_nonexistent_name_is_safe(self, qtbot, sample_data):
x, y = sample_data
p = SciQLopPlot()
qtbot.addWidget(p)
p.line(x, y)
assert len(p.plottables()) == 1

p.remove_plottable("does_not_exist")
assert len(p.plottables()) == 1


class TestRemovePlottableSignal:

def test_graph_list_changed_emitted(self, qtbot, sample_data):
x, y = sample_data
p = SciQLopPlot()
qtbot.addWidget(p)
g = p.line(x, y)

with qtbot.waitSignal(p.graph_list_changed, timeout=1000):
p.remove_plottable(g)


class TestRemovePlottableFromPanel:

def test_remove_from_panel_plot(self, qtbot, sample_data):
x, y = sample_data
panel = SciQLopMultiPlotPanel()
qtbot.addWidget(panel)
plot, graph = panel.line(x, y)
assert len(plot.plottables()) == 1

plot.remove_plottable(graph)
assert len(plot.plottables()) == 0
Loading