diff --git a/GUI/Model/Job/ParameterTreeItems.cpp b/GUI/Model/Job/ParameterTreeItems.cpp index 15eb107035485b86bdff7bbb3fcea0e898fa4fc6..85093ef9f3c4abc8360c0e4d34485379e8cf43e1 100644 --- a/GUI/Model/Job/ParameterTreeItems.cpp +++ b/GUI/Model/Job/ParameterTreeItems.cpp @@ -24,6 +24,7 @@ const QString Backups("Backups"); const QString Backup("Backup"); const QString ParameterLabels("ParameterLabels"); const QString ParameterLabel("ParameterLabel"); +const QString SelectedIndex("SelectedIndex"); } // namespace Tag @@ -151,6 +152,11 @@ void ParameterContainerItem::writeTo(QXmlStreamWriter* w) const { XML::writeAttribute(w, XML::Attrib::version, uint(2)); + // selected index + w->writeStartElement(Tag::SelectedIndex); + XML::writeAttribute(w, XML::Attrib::value, m_selectedIndex); + w->writeEndElement(); + // backups w->writeStartElement(Tag::Backups); for (const auto& backup : m_backupValues) { @@ -175,8 +181,13 @@ void ParameterContainerItem::readFrom(QXmlStreamReader* r) while (r->readNextStartElement()) { QString tag = r->name().toString(); - // backups - if (tag == Tag::Backups) { + // selected index + if (tag == Tag::SelectedIndex) { + XML::readAttribute(r, XML::Attrib::value, &m_selectedIndex); + XML::gotoEndElementOfTag(r, tag); + + // backups + } else if (tag == Tag::Backups) { m_backupValues.clear(); while (r->readNextStartElement()) { QString backupTag = r->name().toString(); diff --git a/GUI/Model/Job/ParameterTreeItems.h b/GUI/Model/Job/ParameterTreeItems.h index ff93a71ac8d7480e53de29a3ec7b6466a72fb578..ead125bf52d1f19164e0d462fa25141c65eb635c 100644 --- a/GUI/Model/Job/ParameterTreeItems.h +++ b/GUI/Model/Job/ParameterTreeItems.h @@ -108,10 +108,14 @@ public: QObject* parameterTreeRoot(); + int selectedIndex() const { return m_selectedIndex; } + void setSelectedIndex(int i) { m_selectedIndex = i; } + private: void addBackupValue(QObject* item); void restoreBackupValue(QObject* item, int index); + int m_selectedIndex = 0; OwningVector<ParameterBackup> m_backupValues; std::unique_ptr<QObject> m_parameterTreeRoot; }; diff --git a/GUI/Model/Sample/SampleModel.h b/GUI/Model/Sample/SampleModel.h index 3322d586c09d01d6e4699d1f9494d857dbeeef7d..2551637da496cd28a3d7d6f5b481273e2a276f28 100644 --- a/GUI/Model/Sample/SampleModel.h +++ b/GUI/Model/Sample/SampleModel.h @@ -42,7 +42,7 @@ public: private: OwningVector<SampleItem> m_samples; - int m_selectedIndex = -1; + int m_selectedIndex = 0; }; #endif // BORNAGAIN_GUI_MODEL_SAMPLE_SAMPLEMODEL_H diff --git a/GUI/View/Tuning/ParameterBackupWidget.cpp b/GUI/View/Tuning/ParameterBackupWidget.cpp index 234e468175e7c56c0a32d5bf3c7ebeb0dec3b79c..e366e8e327dfeafd7d6241a3fd73cea085f6108b 100644 --- a/GUI/View/Tuning/ParameterBackupWidget.cpp +++ b/GUI/View/Tuning/ParameterBackupWidget.cpp @@ -95,15 +95,16 @@ ParameterBackupWidget::ParameterBackupWidget(QWidget* parent) void ParameterBackupWidget::setParameterContainer(ParameterContainerItem* container) { ASSERT(container); + m_container = container; // new snapshot disconnect(m_create, nullptr, nullptr, nullptr); - connect(m_create, &QPushButton::clicked, [this, container] { + connect(m_create, &QPushButton::clicked, [this] { QString newName; NewSnapshotDialog dialog(newName); if (dialog.exec() == QDialog::Accepted) - container->addBackupValues(newName); - fillCombo(container); + m_container->addBackupValues(newName); + fillCombo(); QSignalBlocker b(m_combo); m_combo->setCurrentIndex(m_combo->count() - 1); m_remove->setEnabled(true); @@ -111,33 +112,34 @@ void ParameterBackupWidget::setParameterContainer(ParameterContainerItem* contai // delete snapshot disconnect(m_remove, nullptr, nullptr, nullptr); - connect(m_remove, &QPushButton::clicked, [this, container] { + connect(m_remove, &QPushButton::clicked, [this] { if (m_combo->currentIndex() >= 0) { - container->deleteBackupValues(m_combo->currentIndex()); - fillCombo(container); + m_container->deleteBackupValues(m_combo->currentIndex()); + fillCombo(); } }); // update combo - fillCombo(container); + fillCombo(); } void ParameterBackupWidget::onComboChange(int index) { m_remove->setDisabled(m_combo->currentIndex() <= 0); + m_container->setSelectedIndex(index); emit backupSwitched(index); } -void ParameterBackupWidget::fillCombo(ParameterContainerItem* container) +void ParameterBackupWidget::fillCombo() { disconnect(m_combo, nullptr, nullptr, nullptr); - int currentIndex = m_combo->currentIndex(); + int index = m_container->selectedIndex(); m_combo->clear(); - m_combo->addItems(container->backupTitles()); - if (currentIndex >= 0) { - if (currentIndex < m_combo->count()) - m_combo->setCurrentIndex(currentIndex); + m_combo->addItems(m_container->backupTitles()); + if (index >= 0) { + if (index < m_combo->count()) + m_combo->setCurrentIndex(index); else m_combo->setCurrentIndex(m_combo->count() - 1); } diff --git a/GUI/View/Tuning/ParameterBackupWidget.h b/GUI/View/Tuning/ParameterBackupWidget.h index 210f13fdb0f6f1bf7cc0673cbb8eb6c478b521f0..7ee45b54d38f6096781614159363356ddb8d38e3 100644 --- a/GUI/View/Tuning/ParameterBackupWidget.h +++ b/GUI/View/Tuning/ParameterBackupWidget.h @@ -37,12 +37,13 @@ private slots: void onComboChange(int index); private: - void fillCombo(ParameterContainerItem* container); + void fillCombo(); QPushButton* m_create; QComboBox* m_combo; QPushButton* m_remove; QPushButton* m_reset; + ParameterContainerItem* m_container; }; #endif // BORNAGAIN_GUI_VIEW_TUNING_PARAMETERBACKUPWIDGET_H