1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Tuning/ParameterBackupWidget.cpp
//! @brief Implements class ParameterBackupWidget.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2023
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/Tuning/ParameterBackupWidget.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Par/ParameterTreeItems.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/View/Base/SlotFactory.h"
#include <QBoxLayout>
#include <QDateTime>
#include <QDialog>
#include <QLineEdit>
namespace {
void resizeLineEditToContents(QLineEdit* lineedit)
{
QSize text_size = lineedit->fontMetrics().size(0, lineedit->text());
lineedit->setMinimumSize(text_size + QSize(15, 0));
}
class NewSnapshotDialog : public QDialog {
public:
explicit NewSnapshotDialog(QString& newName, QWidget* parent = nullptr)
: QDialog(parent)
{
setWindowTitle("New snapshot");
setMinimumWidth(240);
auto* mainLayout = new QVBoxLayout(this);
QString dateTime = QDateTime::currentDateTime().toString("<dd.MM.yyyy | hh:mm:ss>");
auto* lineEdit = new QLineEdit("State at " + dateTime);
lineEdit->selectAll();
mainLayout->addWidget(lineEdit);
connect(lineEdit, &QLineEdit::textEdited, [this, lineEdit] {
::resizeLineEditToContents(lineEdit);
adjustSize();
});
::resizeLineEditToContents(lineEdit);
auto* okButton = new QPushButton("OK");
okButton->setFixedWidth(50);
mainLayout->addWidget(okButton, 0, Qt::AlignCenter);
okButton->setDefault(true);
connect(okButton, &QPushButton::clicked, [this, &newName, lineEdit] {
newName = lineEdit->text();
accept();
});
}
};
} // namespace
ParameterBackupWidget::ParameterBackupWidget(QWidget* parent)
: QWidget(parent)
, m_combo(new QComboBox)
{
auto* mainLayout = new QVBoxLayout(this);
auto* h1 = new QHBoxLayout;
h1->setAlignment(Qt::AlignLeft);
m_combo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
h1->addWidget(m_combo);
m_remove = new QPushButton(QIcon(":/images/delete.svg"), "Remove");
m_remove->setToolTip("Remove selected snapshot");
h1->addWidget(m_remove);
auto* h2 = new QHBoxLayout;
h2->setAlignment(Qt::AlignLeft);
m_create = new QPushButton(QIcon(":/images/shape-square-plus.svg"), "New snapshot");
m_create->setToolTip("Add new snapshot of parameter tree");
h2->addWidget(m_create);
m_reset = new QPushButton(QIcon(":/images/undo.svg"), "Reset");
m_reset->setToolTip("Reset parameter tree to selected snapshot");
connect(m_reset, &QPushButton::clicked, [this] { backupSwitched(m_combo->currentIndex()); });
h2->addWidget(m_reset);
mainLayout->addLayout(h1);
mainLayout->addLayout(h2);
}
void ParameterBackupWidget::setParameterContainer(ParameterContainerItem* container)
{
ASSERT(container);
m_container = container;
// new snapshot
m_create->disconnect();
connect(m_create, &QPushButton::clicked, [this] {
QString newName;
NewSnapshotDialog dialog(newName);
if (dialog.exec() == QDialog::Accepted)
m_container->addBackupValues(newName);
fillCombo();
QSignalBlocker b(m_combo);
m_combo->setCurrentIndex(m_combo->count() - 1);
m_remove->setEnabled(true);
gDoc->setModified();
});
// delete snapshot
m_remove->disconnect();
connect(m_remove, &QPushButton::clicked, [this] {
if (m_combo->currentIndex() >= 0) {
m_container->deleteBackupValues(m_combo->currentIndex());
fillCombo();
gDoc->setModified();
}
});
// update combo
fillCombo();
}
void ParameterBackupWidget::onComboChange(int index)
{
m_remove->setDisabled(m_combo->currentIndex() <= 0);
m_container->setCurrentIndex(index);
emit backupSwitched(index);
}
void ParameterBackupWidget::fillCombo()
{
QSignalBlocker b(m_combo);
int index = m_container->currentIndex();
m_combo->clear();
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);
}
m_remove->setDisabled(m_combo->currentIndex() <= 0);
connect(m_combo, &QComboBox::currentIndexChanged, this, &ParameterBackupWidget::onComboChange,
Qt::UniqueConnection);
}
|