-
Notifications
You must be signed in to change notification settings - Fork 486
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
base: gazebo11
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
|
||
|
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() | ||
{ | ||
|
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