Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Save Canvas Layout in Plot Plugin #2924

Open
wants to merge 4 commits into
base: gazebo11
Choose a base branch
from
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
37 changes: 33 additions & 4 deletions gazebo/gui/plot/PlotCanvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#include <map>
#include <tuple>
Expand Down Expand Up @@ -268,8 +268,10 @@ unsigned int PlotCanvas::AddVariable(const std::string &_variable,

// add to container and let the signals/slots do the work on adding the
// a new plot with the curve in the overloaded AddVariable function
return this->dataPtr->yVariableContainer->AddVariablePill(_variable,
targetId);
unsigned int _retVal = this->dataPtr->yVariableContainer->AddVariablePill(
_variable, targetId);

return _retVal;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change can be reverted

}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -310,7 +312,6 @@ void PlotCanvas::AddVariable(const unsigned int _id,
this->dataPtr->plotSplitter->setVisible(true);
}


if (common::URI::Valid(_variable))
{
common::URI uri(_variable);
Expand All @@ -330,6 +331,32 @@ void PlotCanvas::AddVariable(const unsigned int _id,
}
}

/////////////////////////////////////////////////
void PlotCanvas::AddVariable(const std::string &_variable,
IncrementalPlot *plot_in)
{
if (!plot_in)
return;

if (plot_in == this->dataPtr->emptyPlot)
{
// add new variable to new plot
this->AddVariable(_variable);
}
else
{
for (const auto &it : this->dataPtr->plotData)
{
if (plot_in == it.second->plot)
{
// add to existing plot
this->AddVariable(_variable, it.second->id);
return;
}
}
}
}

/////////////////////////////////////////////////
void PlotCanvas::RemoveVariable(const unsigned int _id,
const unsigned int _plotId)
Expand Down Expand Up @@ -487,6 +514,8 @@ unsigned int PlotCanvas::PlotByVariable(const unsigned int _variableId) const
/////////////////////////////////////////////////
void PlotCanvas::OnAddVariable(const std::string &_variable)
{
// gzdbg << "PlotCanvas::OnAddVariable: " << _variable << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line can be removed


IncrementalPlot *plot =
qobject_cast<IncrementalPlot *>(QObject::sender());

Expand Down
5 changes: 5 additions & 0 deletions gazebo/gui/plot/PlotCanvas.hh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ namespace gazebo
public: unsigned int AddVariable(const std::string &_variable,
const unsigned int _plotId = EmptyPlot);

/// \brief Add a new variable to a plot.
/// \param[in] _variable Name of the variable.
/// \param[in] _plot pointer to the plot to add the variable to.
public: void AddVariable(const std::string &_variable, IncrementalPlot *plot);

/// \brief Remove a variable from a plot.
/// \param[in] _id Unique id of the variable
/// \param[in] _plotId Unique id of plot to remove the variable from.
Expand Down
183 changes: 182 additions & 1 deletion gazebo/gui/plot/PlotWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/
#include <mutex>
#include <tinyxml2.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we may need to link the GUI library against tinyxml2 with this chanege


#include "gazebo/common/Console.hh"
#include "gazebo/gui/qt.h"
#include "gazebo/gui/GuiIface.hh"
#include "gazebo/gui/MainWindow.hh"
Expand All @@ -30,6 +32,14 @@
using namespace gazebo;
using namespace gui;

using namespace tinyxml2;
#ifndef XMLCheckResult
#define XMLCheckResult(a_eResult) if (a_eResult != XML_SUCCESS) { \
printf("Error: %i\n", a_eResult); \
return a_eResult; \
}
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this macro needed? I would revert it


namespace gazebo
{
namespace gui
Expand Down Expand Up @@ -117,9 +127,37 @@ PlotWindow::PlotWindow(QWidget *_parent)
exportPlotButton->setGraphicsEffect(exportPlotShadow);
connect(exportPlotButton, SIGNAL(clicked()), this, SLOT(OnExport()));

// save button
QPushButton *saveCanvasButton = new QPushButton("Save");
saveCanvasButton->setObjectName("plotSaveCanvas");
saveCanvasButton->setDefault(false);
saveCanvasButton->setAutoDefault(false);
saveCanvasButton->setToolTip("Save canvas");
QGraphicsDropShadowEffect *saveCanvasShadow =
new QGraphicsDropShadowEffect();
saveCanvasShadow->setBlurRadius(8);
saveCanvasShadow->setOffset(0, 0);
saveCanvasButton->setGraphicsEffect(saveCanvasShadow);
connect(saveCanvasButton, SIGNAL(clicked()), this, SLOT(OnSaveCanvas()));

// load button
QPushButton *loadCanvasButton = new QPushButton("Load");
loadCanvasButton->setObjectName("plotLoadCanvas");
loadCanvasButton->setDefault(false);
loadCanvasButton->setAutoDefault(false);
loadCanvasButton->setToolTip("Load canvas");
QGraphicsDropShadowEffect *loadCanvasShadow =
new QGraphicsDropShadowEffect();
loadCanvasShadow->setBlurRadius(8);
loadCanvasShadow->setOffset(0, 0);
loadCanvasButton->setGraphicsEffect(loadCanvasShadow);
connect(loadCanvasButton, SIGNAL(clicked()), this, SLOT(OnLoadCanvas()));

QHBoxLayout *addButtonLayout = new QHBoxLayout;
addButtonLayout->addWidget(exportPlotButton);
addButtonLayout->addStretch();
addButtonLayout->addWidget(saveCanvasButton);
addButtonLayout->addWidget(loadCanvasButton);
addButtonLayout->addWidget(addCanvasButton);
addButtonLayout->setAlignment(Qt::AlignRight | Qt::AlignBottom);
addButtonLayout->setContentsMargins(0, 0, 0, 0);
Expand Down Expand Up @@ -186,6 +224,137 @@ PlotCanvas *PlotWindow::AddCanvas()
return canvas;
}

/////////////////////////////////////////////////
void PlotWindow::SavePlotLayout() {
QString _fileName = "";

XMLError _XMLError;
XMLDocument _plotLayout_XMLDocument;
XMLNode *_rootNode = nullptr;
XMLElement *_canvas_XMLElement = nullptr;
XMLElement *_plot_XMLElement = nullptr;
XMLElement *_variable_XMLElement = nullptr;

PlotCanvas *_canvas = nullptr;

_fileName = QFileDialog::getSaveFileName(this, tr("Save Plot Layout"), "~",
tr("XML File (*.xml)"));

_rootNode = _plotLayout_XMLDocument.NewElement("PlotLayout");
_plotLayout_XMLDocument.InsertFirstChild(_rootNode);

for (int _canvasIndex = 0;
_canvasIndex < this->dataPtr->canvasSplitter->count();
++_canvasIndex) {
_canvas = qobject_cast<PlotCanvas*>(
this->dataPtr->canvasSplitter->widget(_canvasIndex));
if (!_canvas) {
continue;
}

_canvas_XMLElement = _plotLayout_XMLDocument.NewElement("Canvas");
_rootNode->InsertEndChild(_canvas_XMLElement);

for (const auto &_plot : _canvas->Plots()) {
_plot_XMLElement = _plotLayout_XMLDocument.NewElement("Plot");
_canvas_XMLElement->InsertEndChild(_plot_XMLElement);

for (const auto &_curve : _plot->Curves()) {
auto _tempCurve = _curve.lock();
if (!_tempCurve)
continue;

std::string _label = _tempCurve->Label();

_variable_XMLElement = _plotLayout_XMLDocument.NewElement(
"Variable");

_variable_XMLElement->SetAttribute("Label", _label.c_str());

_plot_XMLElement->InsertEndChild(_variable_XMLElement);
} // for (const auto &_curve : _plot->Curves()) {}
} // for (const auto &_plot : _canvas->Plots()) {
} // for (int _canvasIndex = 0; ...

// todo: treat XMLError
_XMLError = _plotLayout_XMLDocument.SaveFile(_fileName.toLocal8Bit());
} // void PlotWindow::SavePlotLayout() {

/////////////////////////////////////////////////
void PlotWindow::LoadPlotLayout() {
QString _fileName = "";

XMLError _XMLError;
XMLDocument _plotLayout_XMLDocument;
XMLNode *_rootNode = nullptr;
XMLElement *_canvas_XMLElement = nullptr;
XMLElement *_plot_XMLElement = nullptr;
XMLElement *_variable_XMLElement = nullptr;

PlotCanvas *_canvas = nullptr;

std::string _label;

std::vector<IncrementalPlot*> _plotVector;
IncrementalPlot *_plot = nullptr;
int _size = 0;

_fileName = QFileDialog::getOpenFileName(this, tr("Load Plot Layout"), "~",
tr("XML File (*.xml)"));

if (_fileName == "") {
return;
}

this->Clear();

_XMLError = _plotLayout_XMLDocument.LoadFile(_fileName.toLocal8Bit());

_rootNode = _plotLayout_XMLDocument.FirstChild();
if (_rootNode == nullptr)
return;

_canvas_XMLElement = _rootNode->FirstChildElement("Canvas");
while (_canvas_XMLElement != nullptr) {
_canvas = this->AddCanvas();

if (!_canvas) {
return;
}

_plot_XMLElement = _canvas_XMLElement->FirstChildElement("Plot");
while (_plot_XMLElement != nullptr) {
_variable_XMLElement = _plot_XMLElement->FirstChildElement(
"Variable");
while (_variable_XMLElement != nullptr) {

const char *tempChars = _variable_XMLElement->Attribute(
"Label");
if (tempChars != nullptr) {
std::string _label = tempChars;

_plotVector = _canvas->Plots();
_size = _plotVector.size();

if (_size == 0) {
_canvas->AddVariable(_label);
} else { // if (_size == 0) {
_plot = _plotVector.back();
_canvas->AddVariable(_label, _plot);
} // } else { // if (_size == 0) {
} // if (tempChars != nullptr) {

_variable_XMLElement = _variable_XMLElement->NextSiblingElement(
"Variable");
} // while (_variable_XMLElement != nullptr) {

_plot_XMLElement = _plot_XMLElement->NextSiblingElement("Plot");
} // while (_plot_XMLElement != nullptr)

_canvas_XMLElement = _canvas_XMLElement->NextSiblingElement("Canvas");
} // while (_canvas_XMLElement != nullptr)
} // void PlotWindow::LoadPlotLayout() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code formatting in these two functions doesn't match the gazebo style


/////////////////////////////////////////////////
void PlotWindow::RemoveCanvas(PlotCanvas *_canvas)
{
Expand Down Expand Up @@ -221,6 +390,18 @@ void PlotWindow::OnAddCanvas()
this->AddCanvas();
}

/////////////////////////////////////////////////
void PlotWindow::OnSaveCanvas()
{
this->SavePlotLayout();
}

/////////////////////////////////////////////////
void PlotWindow::OnLoadCanvas()
{
this->LoadPlotLayout();
}

/////////////////////////////////////////////////
void PlotWindow::OnRemoveCanvas()
{
Expand Down
12 changes: 12 additions & 0 deletions gazebo/gui/plot/PlotWindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ namespace gazebo
/// \brief Add a new canvas.
public: PlotCanvas *AddCanvas();

/// \brief Save canvas layout
public: void SavePlotLayout();

/// \brief Load canvas layout
public: void LoadPlotLayout();

/// \brief Get a list of all the plots
/// \return A list of all the plots.
public: std::list<PlotCanvas *> Plots();
Expand Down Expand Up @@ -80,6 +86,12 @@ namespace gazebo
/// \brief Qt Callback when a new plot canvas should be added.
private slots: void OnAddCanvas();

/// \brief Qt Callback when the canvas setup should be saved.
private slots: void OnSaveCanvas();

/// \brief Qt Callback when the canvas setup should be loaded.
private slots: void OnLoadCanvas();

/// \brief Qt Callback when a plot canvas should be removed.
private slots: void OnRemoveCanvas();

Expand Down
2 changes: 1 addition & 1 deletion gazebo/gui/plot/VariablePillContainer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
*/

#include <iostream>
#include <map>
Expand Down