BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterTuningWidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Fit/ParameterTuningWidget.cpp
6 //! @brief Implements class ParameterTuningWidget
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 "GUI/Util/Error.h"
26 #include <QAction>
27 #include <QTreeView>
28 #include <QVBoxLayout>
29 
31  : SessionItemWidget(parent)
32  , m_jobModel(nullptr)
33  , m_parameterTuningModel(nullptr)
34  , m_sliderSettingsWidget(new SliderSettingsWidget(this))
35  , m_treeView(new QTreeView)
36  , m_delegate(new ParameterTuningDelegate(this))
37  , m_cautionSign(new CautionSign(m_treeView))
38 {
39  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
40 
41  m_treeView->setItemDelegate(m_delegate);
42  m_treeView->setContextMenuPolicy(Qt::CustomContextMenu);
43  m_treeView->setDragDropMode(QAbstractItemView::NoDragDrop);
44  m_treeView->setAttribute(Qt::WA_MacShowFocusRect, false);
45 
46  auto* resetValuesAction = new QAction(QIcon(":/images/undo-variant.svg"), "Reset values", this);
47  resetValuesAction->setToolTip("Reset parameter tree to initial values");
48  connect(resetValuesAction, &QAction::triggered, this,
50 
51  auto* toolbar = new StyledToolbar(this);
52  toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
53  toolbar->addAction(resetValuesAction);
54 
55  auto* mainLayout = new QVBoxLayout;
56  mainLayout->setMargin(0);
57  mainLayout->setSpacing(0);
58  mainLayout->addWidget(toolbar);
59  mainLayout->addWidget(m_sliderSettingsWidget);
60  mainLayout->addWidget(m_treeView);
61  setLayout(mainLayout);
62 
69  connect(m_treeView, &QTreeView::customContextMenuRequested, this,
71 }
72 
74 {
75  ASSERT(m_treeView);
76  return m_treeView->selectionModel();
77 }
78 
79 //! Returns list of ParameterItem's currently selected in parameter tree
80 
82 {
83  QVector<ParameterItem*> result;
84  for (auto index : selectionModel()->selectedIndexes())
86  result.push_back(parItem);
87 
88  return result;
89 }
90 
92 {
93  ASSERT(jobItem());
94 
95  if (jobItem()->isRunning())
96  return;
97 
98  if (item) {
99  // link.updateItem(); // FIXME circular dependency if uncomment
101  }
102 }
103 
105 {
107 }
108 
110 {
111  if (!jobItem())
112  return;
115 }
116 
118 {
119  ASSERT(m_jobModel);
120 
121  if (!jobItem())
122  return;
123 
124  if (!jobItem()->sampleItem() || !jobItem()->instrumentItem())
125  throw Error("ModelTuningWidget::updateParameterModel() -> Error."
126  "JobItem doesn't have sample or instrument model.");
127 
128  delete m_parameterTuningModel;
130  new ParameterTuningModel(jobItem()->parameterContainerItem()->parameterTreeRoot(), this);
131 
133  if (m_treeView->columnWidth(0) < 170)
134  m_treeView->setColumnWidth(0, 170);
135  m_treeView->expandAll();
136 }
137 
139 {
140  emit itemContextMenuRequest(m_treeView->mapToGlobal(point + QPoint(2, 22)));
141 }
142 
144 {
145  ASSERT(m_jobModel);
146  ASSERT(jobItem());
147 
148  if (jobItem()->isRunning())
149  return;
150 
152 
155 }
156 
158 {
159  QModelIndex index = m_parameterTuningModel->indexForItem(item);
160  if (index.isValid())
161  selectionModel()->select(index, QItemSelectionModel::Select);
162 }
163 
165 {
166  // reimplemented to suppress context menu from QMainWindow
167 }
168 
170 {
171  m_jobModel = dynamic_cast<JobModel*>(jobItem()->model());
172 
175 
176  connect(jobItem(), &JobItem::jobStatusChanged, this,
177  [=](const JobStatus) { updateJobStatus(); });
178 
179  updateJobStatus();
180 }
181 
183 {
184  return dynamic_cast<JobItem*>(currentItem());
185 }
186 
187 //! Disable drag-and-drop abilities, if job is in fit running state.
188 
190 {
191  ASSERT(jobItem());
192  if (jobItem()->getStatus() == JobStatus::Fitting) {
194  m_treeView->setDragDropMode(QAbstractItemView::NoDragDrop);
195  } else {
197  if (jobItem()->isValidForFitting())
198  m_treeView->setDragDropMode(QAbstractItemView::DragOnly);
199  }
200 }
201 
202 //! Sets delegate to enabled/disabled state.
203 //! In 'disabled' state the delegate is in ReadOnlyMode, if it was containing already some
204 //! editing widget, it will be forced to close.
206 {
207  if (enabled)
208  m_delegate->setReadOnly(false);
209  else {
210  m_delegate->setReadOnly(true);
212  }
213 }
214 
216 {
217  QModelIndex index = m_treeView->currentIndex();
218  QWidget* editor = m_treeView->indexWidget(index);
219  if (editor) {
220  // m_delegate->commitData(editor);
221  m_delegate->closeEditor(editor, QAbstractItemDelegate::NoHint);
222  }
223  m_treeView->selectionModel()->clearSelection();
224 }
225 
227 {
228  m_cautionSign->clear();
229 
230  if (jobItem()->isFailed()) {
231  QString message;
232  message.append("Current parameter values cause simulation failure.\n\n");
233  message.append(jobItem()->getComments());
235  }
236 
238 }
Defines class CautionSign.
Defines error class.
Defines class IntensityDataItem.
Defines class JobItem.
Defines class JobModel.
JobStatus
The JobStatus enum lists the possible states of a job.
Definition: JobStatus.h:22
@ Fitting
the job is busy fitting
Defines classes for ParameterTreeItems.
Defines class ParameterTuningDelegate.
Defines class ParameterTuningModel.
Defines class ParameterTuningWidget.
Defines class SliderSettingsWidget.
Defines class StyledToolbar.
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
void setZAxisLocked(bool state)
void jobStatusChanged(const JobStatus status)
void runJob(JobItem *jobItem)
Definition: JobModel.cpp:142
void restore(JobItem *jobItem)
restore instrument and sample model from backup for given JobItem
Definition: JobModel.cpp:83
The ParameterItem class represent a tuning value in a parameter tuning tree.
void setReadOnly(bool isReadOnly)
void setSliderRangeFactor(double value)
void currentLinkChanged(ParameterItem *item)
The ParameterTuningModel class represents parameters which can be tuned in real time in ParameterTuni...
QModelIndex indexForItem(ParameterItem *item) const
ParameterItem * getParameterItem(const QModelIndex &index) const
Returns ParameterItem from given index.
void onCustomContextMenuRequested(const QPoint &point)
void makeSelected(ParameterItem *item)
ParameterTuningModel * m_parameterTuningModel
ParameterTuningWidget(QWidget *parent=nullptr)
void contextMenuEvent(QContextMenuEvent *) override
void setTuningDelegateEnabled(bool enabled)
Sets delegate to enabled/disabled state. In 'disabled' state the delegate is in ReadOnlyMode,...
QVector< ParameterItem * > getSelectedParameters()
Returns list of ParameterItem's currently selected in parameter tree.
void itemContextMenuRequest(const QPoint &point)
void onLockZValueChanged(bool value)
QItemSelectionModel * selectionModel()
SliderSettingsWidget * m_sliderSettingsWidget
ParameterTuningDelegate * m_delegate
void onCurrentLinkChanged(ParameterItem *item)
void updateDragAndDropSettings()
Disable drag-and-drop abilities, if job is in fit running state.
void onSliderValueChanged(double value)
The SessionItemWidget class is a base for all widgets representing the content of SessionItem....
SessionItem * currentItem()
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
void sliderRangeFactorChanged(double value)
void lockzChanged(bool value)
The StyledToolbar class represents our standard narrow toolbar with the height 24 pixels.
Definition: StyledToolbar.h:22
IntensityDataItem * intensityDataItem(SessionItem *parent)
Returns IntensityDataItem contained as a child in givent parent.