This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
Implemented Save Canvas Layout in Plot Plugin #2924
Open
m-melchior
wants to merge
4
commits into
gazebosim:gazebo11
Choose a base branch
from
m-melchior:feature_PlotSaveCanvasLayout
base: gazebo11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
*/ | ||
|
||
#include <map> | ||
#include <tuple> | ||
|
@@ -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; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
|
@@ -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); | ||
|
@@ -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) | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
*/ | ||
#include <mutex> | ||
#include <tinyxml2.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this macro needed? I would revert it |
||
|
||
namespace gazebo | ||
{ | ||
namespace gui | ||
|
@@ -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); | ||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
{ | ||
|
@@ -221,6 +390,18 @@ void PlotWindow::OnAddCanvas() | |
this->AddCanvas(); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
void PlotWindow::OnSaveCanvas() | ||
{ | ||
this->SavePlotLayout(); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
void PlotWindow::OnLoadCanvas() | ||
{ | ||
this->LoadPlotLayout(); | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
void PlotWindow::OnRemoveCanvas() | ||
{ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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