Skip to content

Commit

Permalink
Save/load snippets in layout seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Jan 11, 2019
1 parent aefbe22 commit 8ce4329
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 41 deletions.
62 changes: 33 additions & 29 deletions plotter_gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,43 +1625,47 @@ void MainWindow::onActionLoadLayoutFromFile(QString filename, bool load_data)
QByteArray snippets_saved_xml = settings.value("AddCustomPlotDialog.savedXML",
QByteArray() ).toByteArray();

auto snippets_previous = GetSnippetsFromXML(snippets_saved_xml);
auto snippets_layout = GetSnippetsFromXML(root.firstChildElement("snippets"));

bool snippets_are_different = false;
for(const auto& snippet_it : snippets_layout)
auto snippets_element = root.firstChildElement("snippets");
if( !snippets_element.isNull() )
{
auto prev_it = snippets_previous.find( snippet_it.first );
auto snippets_previous = GetSnippetsFromXML(snippets_saved_xml);
auto snippets_layout = GetSnippetsFromXML(snippets_element);

if( prev_it == snippets_previous.end() ||
prev_it->second.equation != snippet_it.second.equation ||
prev_it->second.globalVars != snippet_it.second.equation)
bool snippets_are_different = false;
for(const auto& snippet_it : snippets_layout)
{
snippets_are_different = true;
break;
auto prev_it = snippets_previous.find( snippet_it.first );

if( prev_it == snippets_previous.end() ||
prev_it->second.equation != snippet_it.second.equation ||
prev_it->second.globalVars != snippet_it.second.globalVars)
{
snippets_are_different = true;
break;
}
}
}

if( snippets_are_different )
{
QMessageBox msgBox;
msgBox.setWindowTitle("Overwrite custom transforms?");
msgBox.setText("Your layour file contains a set of custom transforms different from "
"the last one you used.\nDo you want to load these transformations?");
msgBox.addButton(QMessageBox::No);
msgBox.addButton(QMessageBox::Yes);
msgBox.setDefaultButton(QMessageBox::Yes);

if( msgBox.exec() == QMessageBox::Yes )
if( snippets_are_different )
{
for(const auto& snippet_it : snippets_layout)
QMessageBox msgBox;
msgBox.setWindowTitle("Overwrite custom transforms?");
msgBox.setText("Your layour file contains a set of custom transforms different from "
"the last one you used.\nDo you want to load these transformations?");
msgBox.addButton(QMessageBox::No);
msgBox.addButton(QMessageBox::Yes);
msgBox.setDefaultButton(QMessageBox::Yes);

if( msgBox.exec() == QMessageBox::Yes )
{
snippets_previous[snippet_it.first] = snippet_it.second;
for(const auto& snippet_it : snippets_layout)
{
snippets_previous[snippet_it.first] = snippet_it.second;
}
QDomDocument doc;
auto snippets_root_element = ExportSnippets( snippets_previous, doc);
doc.appendChild( snippets_root_element );
settings.setValue("AddCustomPlotDialog.savedXML", doc.toByteArray(2));
}
QDomDocument doc;
auto snippets_root_element = ExportSnippets( snippets_previous, doc);
doc.appendChild( snippets_root_element );
settings.setValue("AddCustomPlotDialog.savedXML", doc.toByteArray(2));
}
}

Expand Down
2 changes: 1 addition & 1 deletion plotter_gui/plotwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private slots:

TransformSelector* _transform_select_dialog;

std::map<QString, SnippetData> _snippets;
SnippetsMap _snippets;
void transformCustomCurves();
};

Expand Down
10 changes: 5 additions & 5 deletions plotter_gui/transforms/custom_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void CustomFunction::calculateAndAdd(PlotDataMapRef &plotData)

void CustomFunction::initJsEngine()
{
_jsEngine = std::make_shared<QJSEngine>();
_jsEngine = std::unique_ptr<QJSEngine>( new QJSEngine() );

QJSValue globalVarResult = _jsEngine->evaluate(_global_vars);
if(globalVarResult.isError())
Expand Down Expand Up @@ -260,7 +260,7 @@ CustomPlotPtr CustomFunction::createFromXML(QDomElement &element)
return std::make_shared<CustomFunction>(linkedPlot, name, globalVars, calcEquation );
}

std::map<QString, SnippetData> GetSnippetsFromXML(const QString& xml_text)
SnippetsMap GetSnippetsFromXML(const QString& xml_text)
{
QDomDocument doc;
QString parseErrorMsg;
Expand All @@ -279,9 +279,9 @@ std::map<QString, SnippetData> GetSnippetsFromXML(const QString& xml_text)
}
}

std::map<QString, SnippetData> GetSnippetsFromXML(const QDomElement &snippets_element)
SnippetsMap GetSnippetsFromXML(const QDomElement &snippets_element)
{
std::map<QString, SnippetData> snippets;
SnippetsMap snippets;

for (auto elem = snippets_element.firstChildElement("snippet");
!elem.isNull();
Expand All @@ -296,7 +296,7 @@ std::map<QString, SnippetData> GetSnippetsFromXML(const QDomElement &snippets_el
return snippets;
}

QDomElement ExportSnippets(const std::map<QString, SnippetData> &snippets, QDomDocument &doc)
QDomElement ExportSnippets(const SnippetsMap &snippets, QDomDocument &doc)
{
auto snippets_root = doc.createElement("snippets");

Expand Down
10 changes: 6 additions & 4 deletions plotter_gui/transforms/custom_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ struct SnippetData{
QString equation;
};

std::map<QString, SnippetData> GetSnippetsFromXML(const QString& xml_text);
typedef std::map<QString, SnippetData> SnippetsMap;

std::map<QString, SnippetData> GetSnippetsFromXML(const QDomElement& snippets_element);
SnippetsMap GetSnippetsFromXML(const QString& xml_text);

QDomElement ExportSnippets(const std::map<QString, SnippetData>& snippets,
SnippetsMap GetSnippetsFromXML(const QDomElement& snippets_element);

QDomElement ExportSnippets(const SnippetsMap& snippets,
QDomDocument& destination_doc);

class CustomFunction
Expand Down Expand Up @@ -71,7 +73,7 @@ class CustomFunction
QString _function;
std::vector<std::string> _used_channels;

std::shared_ptr<QJSEngine> _jsEngine;
std::unique_ptr<QJSEngine> _jsEngine;
double _last_updated_timestamp;
};

Expand Down
4 changes: 2 additions & 2 deletions plotter_gui/transforms/function_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private slots:
CustomPlotPtr _plot;
bool _is_new;

std::map<QString, SnippetData> _snipped_saved;
std::map<QString, SnippetData> _snipped_recent;
SnippetsMap _snipped_saved;
SnippetsMap _snipped_recent;
};

#endif // AddCustomPlotDialog_H

0 comments on commit 8ce4329

Please sign in to comment.