From 015ae8f63e9d3d02ea283a55fde019d97e577fb8 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Mon, 27 Feb 2023 16:28:06 +0100 Subject: [PATCH] Allow the ScriptlogDialog to return multiple scripts; Make use of the Dialog in the Properties panel (closes #2253) --- .../launcher/include/profile/actiondialog.h | 2 +- .../include/profile/additionalscriptsdialog.h | 2 +- .../include/profile/scriptlogdialog.h | 6 +- .../ext/launcher/src/profile/actiondialog.cpp | 11 +- .../src/profile/additionalscriptsdialog.cpp | 6 +- .../launcher/src/profile/propertiesdialog.cpp | 139 +++++++----------- .../launcher/src/profile/scriptlogdialog.cpp | 18 +-- 7 files changed, 77 insertions(+), 107 deletions(-) diff --git a/apps/OpenSpace/ext/launcher/include/profile/actiondialog.h b/apps/OpenSpace/ext/launcher/include/profile/actiondialog.h index 9b0bd5f717..fe5a17c41b 100644 --- a/apps/OpenSpace/ext/launcher/include/profile/actiondialog.h +++ b/apps/OpenSpace/ext/launcher/include/profile/actiondialog.h @@ -58,7 +58,7 @@ private: void clearActionFields(); void actionRejected(); void chooseScripts(); - void appendScriptsToTextfield(std::string scripts); + void appendScriptsToTextfield(std::vector scripts); openspace::Profile::Keybinding* selectedKeybinding(); void keybindingAdd(); diff --git a/apps/OpenSpace/ext/launcher/include/profile/additionalscriptsdialog.h b/apps/OpenSpace/ext/launcher/include/profile/additionalscriptsdialog.h index 7382c12280..bfde6ff809 100644 --- a/apps/OpenSpace/ext/launcher/include/profile/additionalscriptsdialog.h +++ b/apps/OpenSpace/ext/launcher/include/profile/additionalscriptsdialog.h @@ -52,7 +52,7 @@ private: * * \param scripts #std::string scripts to be appended */ - void appendScriptsToTextfield(std::string scripts); + void appendScriptsToTextfield(std::vector scripts); std::vector* _scripts = nullptr; std::vector _scriptsData; diff --git a/apps/OpenSpace/ext/launcher/include/profile/scriptlogdialog.h b/apps/OpenSpace/ext/launcher/include/profile/scriptlogdialog.h index c8a1218647..b3014f0e31 100644 --- a/apps/OpenSpace/ext/launcher/include/profile/scriptlogdialog.h +++ b/apps/OpenSpace/ext/launcher/include/profile/scriptlogdialog.h @@ -34,10 +34,10 @@ class QPushButton; class ScriptlogDialog final : public QDialog { Q_OBJECT public: - ScriptlogDialog(QWidget* parent); + ScriptlogDialog(QWidget* parent, std::string filter = ""); signals: - void scriptsSelected(std::string script); + void scriptsSelected(std::vector script); private: void createWidgets(); @@ -52,6 +52,8 @@ private: QPushButton* _reloadFile = nullptr; std::string _scriptLogFile; std::vector _scripts; + + std::string _fixedFilter; }; #endif // __OPENSPACE_UI_LAUNCHER___SCRIPTLOGDIALOG___H__ diff --git a/apps/OpenSpace/ext/launcher/src/profile/actiondialog.cpp b/apps/OpenSpace/ext/launcher/src/profile/actiondialog.cpp index b90fa9a125..03a8e4781d 100644 --- a/apps/OpenSpace/ext/launcher/src/profile/actiondialog.cpp +++ b/apps/OpenSpace/ext/launcher/src/profile/actiondialog.cpp @@ -649,12 +649,17 @@ void ActionDialog::actionRejected() { void ActionDialog::chooseScripts() { ScriptlogDialog d(this); - connect(&d, &ScriptlogDialog::scriptsSelected, this, &ActionDialog::appendScriptsToTextfield); + connect( + &d, &ScriptlogDialog::scriptsSelected, + this, &ActionDialog::appendScriptsToTextfield + ); d.exec(); } -void ActionDialog::appendScriptsToTextfield(std::string scripts) { - _actionWidgets.script->append(QString::fromStdString(std::move(scripts))); +void ActionDialog::appendScriptsToTextfield(std::vector scripts) { + for (std::string script : scripts) { + _actionWidgets.script->append(QString::fromStdString(std::move(script))); + } } Profile::Keybinding* ActionDialog::selectedKeybinding() { diff --git a/apps/OpenSpace/ext/launcher/src/profile/additionalscriptsdialog.cpp b/apps/OpenSpace/ext/launcher/src/profile/additionalscriptsdialog.cpp index 90d50a4fb1..9dcb181983 100644 --- a/apps/OpenSpace/ext/launcher/src/profile/additionalscriptsdialog.cpp +++ b/apps/OpenSpace/ext/launcher/src/profile/additionalscriptsdialog.cpp @@ -109,6 +109,8 @@ void AdditionalScriptsDialog::chooseScripts() { d.exec(); } -void AdditionalScriptsDialog::appendScriptsToTextfield(std::string scripts) { - _textScripts->append(QString::fromStdString(std::move(scripts))); +void AdditionalScriptsDialog::appendScriptsToTextfield(std::vector scripts) { + for (std::string script : scripts) { + _textScripts->append(QString::fromStdString(std::move(script))); + } } diff --git a/apps/OpenSpace/ext/launcher/src/profile/propertiesdialog.cpp b/apps/OpenSpace/ext/launcher/src/profile/propertiesdialog.cpp index 4ada1a6408..65c78ea0a8 100644 --- a/apps/OpenSpace/ext/launcher/src/profile/propertiesdialog.cpp +++ b/apps/OpenSpace/ext/launcher/src/profile/propertiesdialog.cpp @@ -25,6 +25,7 @@ #include "profile/propertiesdialog.h" #include "profile/line.h" +#include "profile/scriptlogdialog.h" #include #include #include @@ -92,17 +93,17 @@ void PropertiesDialog::createWidgets() { box->addStretch(); - layout->addLayout(box); - } - layout->addWidget(new Line); - { _fillFromScriptLog = new QPushButton("Fill from ScriptLog"); connect( _fillFromScriptLog, &QPushButton::clicked, this, &PropertiesDialog::selectLineFromScriptLog ); - layout->addWidget(_fillFromScriptLog); + box->addWidget(_fillFromScriptLog); + layout->addLayout(box); + } + layout->addWidget(new Line); + { _commandLabel = new QLabel("Property Set Command"); layout->addWidget(_commandLabel); @@ -339,7 +340,6 @@ void PropertiesDialog::transitionFromEditMode() { } void PropertiesDialog::editBoxDisabled(bool disabled) { - _fillFromScriptLog->setDisabled(disabled); _commandLabel->setDisabled(disabled); _commandCombo->setDisabled(disabled); _propertyLabel->setDisabled(disabled); @@ -374,91 +374,52 @@ void PropertiesDialog::keyPressEvent(QKeyEvent* evt) { } void PropertiesDialog::selectLineFromScriptLog() { - QComboBox* comboBox = new QComboBox; - - QFile file(QString::fromStdString(absPath("${LOGS}/scriptLog.txt").string())); - if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { - QTextStream in(&file); - while (!in.atEnd()) { - QString line = in.readLine(); - // removing return from a few statments - // these are usually generated by gui panels - line.remove(QRegularExpression("^return ")); - - if (line.isEmpty()) { - continue; - } - - if (!line.startsWith("openspace.setPropertyValue")) { - continue; - } - - comboBox->addItem(line); - } - } - - QDialog dialog; - + ScriptlogDialog d(this, "openspace.setPropertyValue"); connect( - &dialog, &QDialog::finished, - [this, comboBox](int result) { - if (result == QDialog::Rejected) { - return; + &d, &ScriptlogDialog::scriptsSelected, + [this](std::vector scripts) { + for (const std::string& script : scripts) { + listItemAdded(); + + QString text = QString::fromStdString(script); + if (!text.startsWith("openspace.setPropertyValue")) { + return; + } + + // We have a string that is of the form: + // openspace.setPropertyValue('prop', value); + if (text.startsWith("openspace.setPropertyValueSingle")) { + _commandCombo->setCurrentIndex(0); + std::string_view prefix = "openspace.setPropertyValueSingle"; + text = text.mid(static_cast(prefix.size()) + 1); // +1 for ( + } + else { + // command == "openspace.setPropertyValue" + _commandCombo->setCurrentIndex(1); + std::string_view prefix = "openspace.setPropertyValue"; + text = text.mid(static_cast(prefix.size()) + 1); // +1 for ( + } + + // Remove everything past the closing brace + text = text.left(text.indexOf(")")); + QStringList textList = text.split(","); + + if (textList.size() < 2) { + return; + } + + // Remove the string markers around the property + QString property = textList[0].mid(1, textList[0].size() - 2); + + textList.removeFirst(); + QString value = textList.join(","); + + + _propertyEdit->setText(property.trimmed()); + _valueEdit->setText(value.trimmed()); + listItemSave(); } - - QString text = comboBox->currentText(); - if (!text.startsWith("openspace.setPropertyValue")) { - return; - } - - // We have a string that is of the form: - // openspace.setPropertyValue('prop', value); - - if (text.startsWith("openspace.setPropertyValueSingle")) { - _commandCombo->setCurrentIndex(0); - std::string_view prefix = "openspace.setPropertyValueSingle"; - text = text.mid(static_cast(prefix.size()) + 1); // +1 for ( - } - else { - // command == "openspace.setPropertyValue" - _commandCombo->setCurrentIndex(1); - std::string_view prefix = "openspace.setPropertyValue"; - text = text.mid(static_cast(prefix.size()) + 1); // +1 for ( - } - - // Remove everything past the closing brace - text = text.left(text.indexOf(")")); - QStringList textList = text.split(","); - - if (textList.size() < 2) { - return; - } - - // Remove the string markers around the property - QString property = textList[0].mid(1, textList[0].size() - 2); - - textList.removeFirst(); - QString value = textList.join(","); - - - _propertyEdit->setText(property.trimmed()); - _valueEdit->setText(value.trimmed()); } ); - - QLayout* layout = new QVBoxLayout; - QLabel* label = new QLabel("Select a line from the Script Log to add"); - layout->addWidget(label); - - layout->addWidget(comboBox); - - QDialogButtonBox* bb = new QDialogButtonBox( - QDialogButtonBox::Ok | QDialogButtonBox::Cancel - ); - connect(bb, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); - connect(bb, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); - layout->addWidget(bb); - - dialog.setLayout(layout); - dialog.exec(); + d.exec(); } diff --git a/apps/OpenSpace/ext/launcher/src/profile/scriptlogdialog.cpp b/apps/OpenSpace/ext/launcher/src/profile/scriptlogdialog.cpp index 0089fd518d..80524b5b18 100644 --- a/apps/OpenSpace/ext/launcher/src/profile/scriptlogdialog.cpp +++ b/apps/OpenSpace/ext/launcher/src/profile/scriptlogdialog.cpp @@ -40,9 +40,10 @@ #include #include -ScriptlogDialog::ScriptlogDialog(QWidget* parent) +ScriptlogDialog::ScriptlogDialog(QWidget* parent, std::string filter) : QDialog(parent) , _scriptLogFile(openspace::global::configuration->scriptLog) + , _fixedFilter(std::move(filter)) { setWindowTitle("Scriptlog"); createWidgets(); @@ -151,7 +152,10 @@ void ScriptlogDialog::updateScriptList() { int index = -1; _scriptlogList->clear(); for (const std::string& script : _scripts) { - if (script.find(filter) != std::string::npos) { + bool foundDynamic = script.find(filter) != std::string::npos; + bool foundStatic = + _fixedFilter.empty() ? true : script.find(_fixedFilter) != std::string::npos; + if (foundDynamic && foundStatic) { if (script == selection && index == -1) { index = _scriptlogList->count(); } @@ -161,15 +165,11 @@ void ScriptlogDialog::updateScriptList() { } void ScriptlogDialog::saveChosenScripts() { - std::string chosenScripts; + std::vector chosenScripts; QList itemList = _scriptlogList->selectedItems(); - for (int i = 0; i < itemList.size(); ++i) { - chosenScripts += itemList.at(i)->text().toStdString(); - if (i < itemList.size()) { - chosenScripts += "\n"; - } + for (QListWidgetItem* item : _scriptlogList->selectedItems()) { + chosenScripts.push_back(item->text().toStdString()); } emit scriptsSelected(chosenScripts); - accept(); }