BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RunFitControlWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Fit/RunFitControlWidget.cpp
6 //! @brief Implements class RunFitControlWidget
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include "GUI/Model/Job/JobItem.h"
21 #include <QFont>
22 #include <QHBoxLayout>
23 #include <QLabel>
24 #include <QPushButton>
25 #include <QSlider>
26 
27 namespace {
28 
29 const int default_interval = 10;
30 const std::vector<int> slider_to_interval = {1, 2, 3, 4, 5, 10, 15, 20,
31  25, 30, 50, 100, 200, 500, 1000};
32 const QString slider_tooltip = "Updates fit progress every Nth iteration";
33 
34 } // namespace
35 
37  : SessionItemWidget(parent)
38  , m_startButton(new QPushButton)
39  , m_stopButton(new QPushButton)
40  , m_intervalSlider(new QSlider)
41  , m_updateIntervalLabel(new QLabel)
42  , m_iterationsCountLabel(new QLabel)
43  , m_cautionSign(new CautionSign(this))
44 {
45  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
47 
48  m_startButton->setText("Run");
49  m_startButton->setToolTip("Run fitting");
50  m_startButton->setMaximumWidth(80);
51 
52  m_stopButton->setText("Stop");
53  m_stopButton->setToolTip("Interrupt fitting");
54  m_stopButton->setMaximumWidth(80);
55 
56  m_intervalSlider->setToolTip(slider_tooltip);
57  m_intervalSlider->setOrientation(Qt::Horizontal);
58  m_intervalSlider->setRange(0, static_cast<int>(slider_to_interval.size()) - 1);
59  m_intervalSlider->setMaximumWidth(120);
60  m_intervalSlider->setMinimumWidth(120);
61  m_intervalSlider->setFocusPolicy(Qt::NoFocus);
62  m_intervalSlider->setValue(5);
63 
64  QFont font("Monospace", DesignerHelper::getLabelFontSize(), QFont::Normal);
65  font.setPointSize(DesignerHelper::getPortFontSize());
66  m_updateIntervalLabel->setToolTip(slider_tooltip);
67  m_updateIntervalLabel->setFont(font);
68  m_updateIntervalLabel->setText(QString::number(sliderUpdateInterval()));
69 
70  auto* layout = new QHBoxLayout;
71  layout->setSpacing(0);
72  layout->addWidget(m_startButton);
73  layout->addSpacing(5);
74  layout->addWidget(m_stopButton);
75  layout->addSpacing(5);
76  layout->addWidget(m_intervalSlider);
77  layout->addSpacing(2);
78  layout->addWidget(m_updateIntervalLabel);
79  layout->addSpacing(5);
80  layout->addStretch();
81  layout->addWidget(m_iterationsCountLabel);
82  setLayout(layout);
83 
84  connect(m_startButton, &QPushButton::clicked, [&]() { startFittingPushed(); });
85  connect(m_stopButton, &QPushButton::clicked, this, [&]() { stopFittingPushed(); });
86  connect(m_intervalSlider, &QSlider::valueChanged, this,
88 
89  setEnabled(false);
90 }
91 
93 {
95 }
96 
97 void RunFitControlWidget::onFittingError(const QString& what)
98 {
100  m_iterationsCountLabel->setText("");
102 }
103 
105 {
106  int interval = sliderValueToUpdateInterval(value);
107  m_updateIntervalLabel->setText(QString::number(interval));
108  if (fitSuiteItem())
109  fitSuiteItem()->setUpdateInterval(interval);
110 }
111 
113 {
116 }
117 
119 {
121 
123 
125  [this](const QString& name) { onFitSuitePropertyChange(name); }, this);
126 
128 
129  connect(jobItem(), &JobItem::jobStatusChanged, this,
130  [=](const JobStatus) { updateControlElements(); });
131 }
132 
134 {
135  setEnabled(false);
137 }
138 
140 {
142 }
143 
144 //! converts slider value (1-15) to update interval to be propagated to FitSuiteWidget
145 
147 {
148  auto svalue = static_cast<size_t>(value);
149  return svalue < slider_to_interval.size() ? slider_to_interval[svalue] : default_interval;
150 }
151 
152 //! Updates button "enabled" status and caution status depending on current job conditions.
153 
155 {
156  setEnabled(isValidJobItem());
157 
158  if (jobItem()->getStatus() == JobStatus::Fitting) {
159  m_startButton->setEnabled(false);
160  m_stopButton->setEnabled(true);
161  m_cautionSign->clear();
162  } else {
163  m_startButton->setEnabled(true);
164  m_stopButton->setEnabled(false);
165  }
166 }
167 
169 {
170  return dynamic_cast<JobItem*>(currentItem());
171 }
172 
174 {
175  return jobItem() ? jobItem()->fitSuiteItem() : nullptr;
176 }
177 
179 {
180  return jobItem() ? jobItem()->isValidForFitting() : false;
181 }
182 
184 {
185  if (fitSuiteItem())
186  fitSuiteItem()->mapper()->unsubscribe(this);
187 }
188 
190 {
191  int niter = fitSuiteItem()->iterationCount();
192  m_iterationsCountLabel->setText(QString::number(niter));
193 }
Defines class CautionSign.
Defines class DesignerHelper.
Defines class FitSuiteItem.
Defines class JobItem.
JobStatus
The JobStatus enum lists the possible states of a job.
Definition: JobStatus.h:22
@ Fitting
the job is busy fitting
Defines class RunFitControlWidget.
The CautionSign controls appearance of CautionSignWidget on top of parent widget.
Definition: CautionSign.h:25
void clear()
Clears caution message;.
Definition: CautionSign.cpp:42
void setCautionMessage(const QString &cautionMessage)
Shows caution sign on the screen. If clear of previous caution sign had happened just few msec ago,...
Definition: CautionSign.cpp:60
static int getLabelFontSize()
static int getPortFontSize()
int iterationCount() const
static bool isIterationCountPropertyName(const QString &name)
void setUpdateInterval(int interval)
void jobStatusChanged(const JobStatus status)
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:257
bool isValidForFitting()
Definition: JobItem.cpp:154
void unsubscribe(const void *caller)
Cancels all subscriptions of given caller.
Definition: ModelMapper.cpp:78
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=nullptr)
Definition: ModelMapper.cpp:39
void onFittingError(const QString &what)
CautionSign * m_cautionSign
void unsubscribeFromItem() override
QPushButton * m_stopButton
QPushButton * m_startButton
FitSuiteItem * fitSuiteItem()
RunFitControlWidget(QWidget *parent=nullptr)
void updateControlElements()
Updates button "enabled" status and caution status depending on current job conditions.
int sliderValueToUpdateInterval(int value)
converts slider value (1-15) to update interval to be propagated to FitSuiteWidget
void onFitSuitePropertyChange(const QString &name)
void onSliderValueChanged(int value)
void subscribeToItem() override
The SessionItemWidget class is a base for all widgets representing the content of SessionItem....
SessionItem * currentItem()
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
Defines namespace GUI::Constants.
const unsigned int RUN_FIT_CONTROL_WIDGET_HEIGHT
QString const & name(EShape k)
Definition: particles.cpp:20