7 #include <QDomDocument> 9 #include <QFontDatabase> 14 #include <QFileDialog> 18 #include <QInputDialog> 24 _plot_map_data(plotMapData),
25 _custom_plots(mapped_custom_plots),
26 ui(new
Ui::FunctionEditor),
31 this->setWindowTitle(
"Create a custom timeseries");
32 ui->mathEquation->setPlainText(
"return value*2");
34 const QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
35 ui->globalVarsTextField->setFont(fixedFont);
36 ui->mathEquation->setFont(fixedFont);
37 ui->snippetTextEdit->setFont(fixedFont);
39 QStringList numericPlotNames;
42 QString
name = QString::fromStdString(p.first);
43 numericPlotNames.push_back(name);
45 numericPlotNames.sort(Qt::CaseInsensitive);
46 for(
const QString&
name : numericPlotNames)
48 ui->combo_linkedChannel->addItem(
name);
49 ui->curvesListWidget->addItem(
name);
53 QByteArray saved_xml = settings.value(
"AddCustomPlotDialog.savedXML", QByteArray() ).toByteArray();
54 restoreGeometry(settings.value(
"AddCustomPlotDialog.geometry").toByteArray());
56 if( saved_xml.isEmpty() )
58 QFile file(
"://resources/default.snippets.xml");
59 if(!file.open(QIODevice::ReadOnly))
61 throw std::runtime_error(
"problem with default.snippets.xml" );
63 saved_xml = file.readAll();
68 ui->snippetsListRecent->setContextMenuPolicy(Qt::CustomContextMenu);
70 connect(
ui->snippetsListRecent, &QListWidget::customContextMenuRequested,
73 ui->snippetsListSaved->setContextMenuPolicy(Qt::CustomContextMenu);
75 connect(
ui->snippetsListSaved, &QListWidget::customContextMenuRequested,
78 ui->splitter->setStretchFactor(0,3);
79 ui->splitter->setStretchFactor(1,2);
85 settings.setValue(
"AddCustomPlotDialog.savedXML",
exportSnippets() );
86 settings.setValue(
"AddCustomPlotDialog.geometry", saveGeometry());
93 int idx =
ui->combo_linkedChannel->findText(linkedPlotName);
97 ui->combo_linkedChannel->insertItem(idx, linkedPlotName);
99 ui->combo_linkedChannel->setCurrentIndex(idx);
112 return ui->combo_linkedChannel->currentText();
117 return ui->globalVarsTextField->toPlainText();
122 return ui->mathEquation->toPlainText();
127 return ui->nameLineEdit->text();
132 ui->globalVarsTextField->setPlainText(data->globalVars());
133 ui->mathEquation->setPlainText(data->function());
135 ui->pushButtonCreate->setText(
"Update");
136 ui->nameLineEdit->setText(QString::fromStdString(data->name()));
137 ui->nameLineEdit->setEnabled(
false);
149 QString appendString = QString(
"V%1 = $$%2$$\n")
151 .arg(
ui->curvesListWidget->item(index.row())->text());
153 QPlainTextEdit* edit =
ui->mathEquation;
155 if(
ui->globalVarsTextField->hasFocus())
157 edit =
ui->globalVarsTextField;
160 if( !edit->toPlainText().endsWith(
"\n") )
162 edit->insertPlainText(
"\n");
164 edit->insertPlainText(appendString);
169 ui->snippetsListSaved->clear();
175 ui->snippetsListSaved->addItem( it.first );
180 const auto& math_plot = custom_it.second;
182 snippet.
name = QString::fromStdString(math_plot->name());
184 if( _snipped_saved.count( snippet.
name ) > 0 )
190 snippet.
equation = math_plot->function();
192 ui->snippetsListRecent->addItem( snippet.
name );
194 ui->snippetsListRecent->sortItems();
195 ui->snippetsListSaved->sortItems();
202 doc.appendChild( root );
203 return doc.toByteArray(2);
209 if( current_row < 0 )
211 ui->snippetTextEdit->setPlainText(
"");
214 const auto&
name =
ui->snippetsListSaved->currentItem()->text();
216 QString desc = QString(
"%1\n\nfunction calc(time,value)\n{\n%2\n}").arg(snippet.
globalVars).arg(snippet.
equation);
217 ui->snippetTextEdit->setPlainText(desc);
222 const auto&
name =
ui->snippetsListSaved->item( index.row() )->text();
225 ui->globalVarsTextField->setPlainText(snippet.
globalVars);
226 ui->mathEquation->setPlainText(snippet.
equation);
231 if( current_row < 0 )
233 ui->snippetTextEdit->setPlainText(
"");
236 const auto&
name =
ui->snippetsListRecent->currentItem()->text();
239 QString desc = QString(
"%1\n\nfunction calc(time,value)\n{\n%2\n}")
242 ui->snippetTextEdit->setPlainText(desc);
247 const auto&
name =
ui->snippetsListRecent->item( index.row() )->text();
250 ui->globalVarsTextField->setPlainText(snippet.
globalVars);
251 ui->mathEquation->setPlainText(snippet.
equation);
256 auto list_recent =
ui->snippetsListRecent;
258 if( list_recent->selectedItems().size() != 1)
263 auto list_saved =
ui->snippetsListSaved;
265 auto item = list_recent->selectedItems().first();
266 const auto&
name = item->text();
269 QAction* move_item =
new QAction(
"Move to Saved",
this);
270 menu.addAction( move_item );
272 connect(move_item, &QAction::triggered,
280 delete list_recent->takeItem( list_recent->row(item) );
284 menu.exec( list_recent->mapToGlobal(pos) );
289 auto list_saved =
ui->snippetsListSaved;
291 if( list_saved->selectedItems().size() != 1)
298 QAction* rename_item =
new QAction(
"Rename...",
this);
299 menu.addAction( rename_item );
303 QAction* remove_item =
new QAction(
"Remove",
this);
304 menu.addAction( remove_item );
306 connect(remove_item, &QAction::triggered,
307 this, [list_saved,
this]()
309 const auto& item = list_saved->selectedItems().first();
311 delete list_saved->takeItem( list_saved->row(item) );
314 menu.exec( list_saved->mapToGlobal(pos) );
319 ui->pushButtonCreate->setEnabled( !name.isEmpty() );
320 ui->pushButtonSave->setEnabled( !name.isEmpty() );
324 ui->pushButtonCreate->setText(
"Create New Timeseries");
327 ui->pushButtonCreate->setText(
"Modify Timeseries");
334 QString directory_path = settings.value(
"AddCustomPlotDialog.loadDirectory",
335 QDir::currentPath() ).toString();
337 QString fileName = QFileDialog::getOpenFileName(
this, tr(
"Open Snippet Library"),
339 tr(
"Snippets (*.snippets.xml)"));
340 if (fileName.isEmpty()){
344 QFile file(fileName);
346 if ( !file.open(QIODevice::ReadOnly)) {
347 QMessageBox::critical(
this,
"Error", QString(
"Failed to open the file [%1]").
arg(fileName) );
351 directory_path = QFileInfo(fileName).absolutePath();
352 settings.setValue(
"AddCustomPlotDialog.loadDirectory", directory_path );
360 QString directory_path = settings.value(
"AddCustomPlotDialog.loadDirectory",
361 QDir::currentPath() ).toString();
363 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Open Snippet Library"),
365 tr(
"Snippets (*.snippets.xml)"));
367 if (fileName.isEmpty()){
370 if( !fileName.endsWith(
".snippets.xml") )
372 fileName.append(
".snippets.xml");
375 QFile file(fileName);
376 if ( !file.open(QIODevice::WriteOnly)) {
377 QMessageBox::critical(
this,
"Error", QString(
"Failed to open the file [%1]").
arg(fileName));
388 QString
name =
ui->nameLineEdit->text();
392 snippet.
globalVars =
ui->globalVarsTextField->toPlainText();
393 snippet.
equation =
ui->mathEquation->toPlainText();
404 QMessageBox msgBox(
this);
405 msgBox.setWindowTitle(
"Warning");
406 msgBox.setText(tr(
"A function with the same name exists already in the list of saved functions.\n"));
407 msgBox.addButton(QMessageBox::Cancel);
408 QPushButton* button = msgBox.addButton(tr(
"Overwrite"), QMessageBox::YesRole);
409 msgBox.setDefaultButton(button);
411 int res = msgBox.exec();
413 if( res < 0 || res == QMessageBox::Cancel) {
418 ui->snippetsListSaved->addItem(name);
419 ui->snippetsListSaved->sortItems();
427 auto list_saved =
ui->snippetsListSaved;
428 auto item = list_saved->selectedItems().first();
429 const auto&
name = item->text();
432 QString new_name = QInputDialog::getText(
this, tr(
"Change the name of the function"),
433 tr(
"New name:"), QLineEdit::Normal,
436 if (!ok || new_name.isEmpty() || new_name ==
name)
443 snippet.
name = new_name;
446 item->setText( new_name );
447 ui->snippetsListSaved->sortItems();
454 std::string plotName =
getName().toStdString();
460 throw std::runtime_error(
"plot name already exists and can't be modified");
469 catch (
const std::runtime_error &e) {
471 QMessageBox::critical(
this,
"Error",
"Failed to create math plot : " 472 + QString::fromStdString(e.what()));
479 QStringList spaced_items = search_string.split(
' ');
481 for (
int row=0; row <
ui->curvesListWidget->count(); row++)
483 auto item =
ui->curvesListWidget->item(row);
484 QString
name = item->text();
487 for (
const auto& item: spaced_items)
489 if( !name.contains(item) )
495 ui->curvesListWidget->setRowHidden(row, toHide );
QByteArray exportSnippets() const
std::unordered_map< std::string, PlotData > numeric
void on_snippetsListSaved_currentRowChanged(int currentRow)
SnippetsMap _snipped_recent
void setLinkedPlotName(const QString &linkedPlotName)
void on_pushButtonSave_clicked()
void setEditorMode(EditorMode mode)
QDomElement ExportSnippets(const SnippetsMap &snippets, QDomDocument &doc)
void on_snippetsListSaved_doubleClicked(const QModelIndex &index)
void on_buttonSaveFunctions_clicked()
const CustomPlotMap & _custom_plots
void recentContextMenu(const QPoint &pos)
void on_lineEditFilter_textChanged(const QString &arg1)
void on_nameLineEdit_textChanged(const QString &arg1)
CustomPlotPtr getCustomPlotData() const
QString getGlobalVars() const
std::shared_ptr< CustomFunction > CustomPlotPtr
QString getLinkedData() const
AddCustomPlotDialog(PlotDataMapRef &plotMapData, const CustomPlotMap &mapped_custom_plots, QWidget *parent)
bool addToSaved(const QString &name, const SnippetData &snippet)
void on_buttonLoadFunctions_clicked()
SnippetsMap GetSnippetsFromXML(const QString &xml_text)
void on_pushButtonCreate_clicked()
PlotDataMapRef & _plot_map_data
QString getEquation() const
std::unordered_map< std::string, CustomPlotPtr > CustomPlotMap
void editExistingPlot(CustomPlotPtr data)
void on_curvesListWidget_doubleClicked(const QModelIndex &index)
void on_snippetsListRecent_currentRowChanged(int currentRow)
void importSnippets(const QByteArray &xml_text)
virtual ~AddCustomPlotDialog() override
SnippetsMap _snipped_saved
empty_struct data[sizeof(T)/sizeof(empty_struct)]
void on_snippetsListRecent_doubleClicked(const QModelIndex &index)
void savedContextMenu(const QPoint &pos)