From 1964560774abc96764d78c4f379d045bf958522e Mon Sep 17 00:00:00 2001 From: Neil Horne Date: Tue, 7 Jun 2022 17:53:03 +1000 Subject: [PATCH] feat(cpn): Add two more new model actions: template and prompt (#1963) * Add new model actions to application settings * Add new model prompt action * Tweak wording * Add new model action template * Open individual model yml files * Tidy up after importing template * Add create default and edit to prompt options * Fix edit mode to display the edit dialog after model creation when selected in settings --- companion/src/apppreferencesdialog.cpp | 7 +- companion/src/apppreferencesdialog.ui | 39 ++--------- companion/src/mdichild.cpp | 90 +++++++++++++++++++++++++- companion/src/mdichild.h | 2 + companion/src/storage/appdata.h | 6 +- 5 files changed, 102 insertions(+), 42 deletions(-) diff --git a/companion/src/apppreferencesdialog.cpp b/companion/src/apppreferencesdialog.cpp index 2728c79b4..9877b3f22 100644 --- a/companion/src/apppreferencesdialog.cpp +++ b/companion/src/apppreferencesdialog.cpp @@ -80,7 +80,7 @@ void AppPreferencesDialog::accept() g.promptProfile(ui->chkPromptProfile->isChecked()); g.simuSW(ui->simuSW->isChecked()); g.removeModelSlots(ui->opt_removeBlankSlots->isChecked()); - g.newModelAction(ui->opt_newMdl_useWizard->isChecked() ? AppData::MODEL_ACT_WIZARD : ui->opt_newMdl_useEditor->isChecked() ? AppData::MODEL_ACT_EDITOR : AppData::MODEL_ACT_NONE); + g.newModelAction((AppData::NewModelAction)ui->cboNewModelAction->currentIndex()); g.historySize(ui->historySize->value()); g.backLight(ui->backLightColor->currentIndex()); profile.volumeGain(round(ui->volumeGain->value() * 10.0)); @@ -207,9 +207,8 @@ void AppPreferencesDialog::initSettings() ui->simuSW->setChecked(g.simuSW()); ui->opt_removeBlankSlots->setChecked(g.removeModelSlots()); - ui->opt_newMdl_useNone->setChecked(g.newModelAction() == AppData::MODEL_ACT_NONE); - ui->opt_newMdl_useWizard->setChecked(g.newModelAction() == AppData::MODEL_ACT_WIZARD); - ui->opt_newMdl_useEditor->setChecked(g.newModelAction() == AppData::MODEL_ACT_EDITOR); + ui->cboNewModelAction->addItems(AppData::newModelActionsList()); + ui->cboNewModelAction->setCurrentIndex(g.newModelAction()); ui->libraryPath->setText(g.libDir()); ui->ge_lineedit->setText(g.gePath()); diff --git a/companion/src/apppreferencesdialog.ui b/companion/src/apppreferencesdialog.ui index 888d6d1c6..4ada1faed 100644 --- a/companion/src/apppreferencesdialog.ui +++ b/companion/src/apppreferencesdialog.ui @@ -45,7 +45,7 @@ - 0 + 1 @@ -887,40 +887,6 @@ Mode 4: - - - - - - Use model wizard - - - btnGrp_newModelActions - - - - - - - Open model editor - - - btnGrp_newModelActions - - - - - - - Just create the model - - - btnGrp_newModelActions - - - - - @@ -1190,6 +1156,9 @@ Mode 4: + + + diff --git a/companion/src/mdichild.cpp b/companion/src/mdichild.cpp index 3cce72a78..c622f73ed 100644 --- a/companion/src/mdichild.cpp +++ b/companion/src/mdichild.cpp @@ -903,8 +903,16 @@ int MdiChild::newModel(int modelIndex, int categoryIndex) setSelectedModel(modelIndex); //qDebug() << modelIndex << categoryIndex << isNewModel; - if (isNewModel && g.newModelAction() == AppData::MODEL_ACT_WIZARD) - openModelWizard(modelIndex); + if (isNewModel) { + if (g.newModelAction() == AppData::MODEL_ACT_WIZARD) + openModelWizard(modelIndex); + else if (g.newModelAction() == AppData::MODEL_ACT_TEMPLATE) + openModelTemplate(modelIndex); + else if (g.newModelAction() == AppData::MODEL_ACT_PROMPT) + openModelPrompt(modelIndex); + else if (g.newModelAction() == AppData::MODEL_ACT_EDITOR) + openModelEditWindow(modelIndex); + } else if (g.newModelAction() == AppData::MODEL_ACT_EDITOR) openModelEditWindow(modelIndex); @@ -1706,3 +1714,81 @@ void MdiChild::onInternalModuleChanged() delete fim; } + +void MdiChild::openModelTemplate(int row) +{ + if (row < 0 && (row = getCurrentModel()) < 0) + return; + + QString filename = QFileDialog::getOpenFileName(this, tr("Select a model template file"), QDir::toNativeSeparators(g.profile[g.id()].sdPath() + "/TEMPLATES"), YML_FILES_FILTER); + + if (filename.isEmpty()) + return; + + // validate like read single model + // if okay copy into current model slot + + RadioData data; + + Storage storage(filename); + if (!storage.load(data)) { + QMessageBox::critical(this, CPN_STR_TTL_ERROR, storage.error()); + return; + } + + QString warning = storage.warning(); + if (!warning.isEmpty()) { + QMessageBox::warning(this, CPN_STR_TTL_WARNING, warning); + } + + radioData.models[row] = data.models[0]; + + // reset module bindings + for (int i = 0; i < CPN_MAX_MODULES; i++) { + radioData.models[row].moduleData[i].modelId = row + 1; + } + + setModified(); + setSelectedModel(row); + + openModelEditWindow(row); +} + +void MdiChild::openModelPrompt(int row) +{ + if (row < 0 && (row = getCurrentModel()) < 0) + return; + + QMessageBox msgBox; + msgBox.setWindowTitle(CPN_STR_APP_NAME); + msgBox.setIcon(QMessageBox::Question); + msgBox.setText(tr("Add a new model using")); + QPushButton *defaultsButton = msgBox.addButton(tr("Defaults"),QMessageBox::ActionRole); + QPushButton *editButton = msgBox.addButton(tr("Edit"),QMessageBox::ActionRole); + QPushButton *wizardButton = msgBox.addButton(tr("Wizard"),QMessageBox::ActionRole); + QPushButton *templateButton = msgBox.addButton(tr("Template"),QMessageBox::ActionRole); + QPushButton *cancelButton = msgBox.addButton(QMessageBox::Cancel); + + msgBox.exec(); + + if (msgBox.clickedButton() == cancelButton) { + if (!deleteModel(row)) + QMessageBox::critical(this, CPN_STR_APP_NAME, tr("Failed to remove temporary model!")); + return; + } + else if (msgBox.clickedButton() == defaultsButton) { + // nothing to do here + return; + } + else if (msgBox.clickedButton() == editButton) { + openModelEditWindow(row); + } + else if (msgBox.clickedButton() == wizardButton) { + openModelWizard(row); + } + else if (msgBox.clickedButton() == templateButton) { + openModelTemplate(row); + } + + return; +} diff --git a/companion/src/mdichild.h b/companion/src/mdichild.h index 47da91ce3..16e0281d3 100644 --- a/companion/src/mdichild.h +++ b/companion/src/mdichild.h @@ -138,6 +138,8 @@ class MdiChild : public QWidget void openModelWizard(int row = -1); void openModelEditWindow(int row = -1); + void openModelTemplate(int row = -1); + void openModelPrompt(int row = -1); void setDefault(); void modelSimulate(); diff --git a/companion/src/storage/appdata.h b/companion/src/storage/appdata.h index 53ffb71d9..c383be2e5 100644 --- a/companion/src/storage/appdata.h +++ b/companion/src/storage/appdata.h @@ -451,10 +451,14 @@ class AppData: public CompStoreObj enum NewModelAction { MODEL_ACT_NONE, MODEL_ACT_WIZARD, - MODEL_ACT_EDITOR + MODEL_ACT_EDITOR, + MODEL_ACT_TEMPLATE, + MODEL_ACT_PROMPT }; Q_ENUM(NewModelAction) + static QStringList newModelActionsList() { return { tr("None"), tr("Wizard"), tr("Editor"), tr("Template"), tr("Prompt") } ; } + explicit AppData(); void init() override; void initAll();