Mantid
Loading...
Searching...
No Matches
LocalParameterEditor.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10#include <QAction>
11#include <QDoubleValidator>
12#include <QEvent>
13#include <QHBoxLayout>
14#include <QInputDialog>
15#include <QKeyEvent>
16#include <QLineEdit>
17#include <QMenu>
18#include <QPushButton>
19#include <utility>
20
22
34LocalParameterEditor::LocalParameterEditor(QWidget *parent, int index, double value, bool fixed, const QString &tie,
35 const QString &constraint, bool othersFixed, bool allOthersFixed,
36 bool othersTied, bool logOptionsEnabled)
37 : QWidget(parent), m_index(index), m_value(QString::number(value, 'g', 16)), m_fixed(fixed), m_tie(tie),
38 m_constraint(constraint), m_othersFixed(othersFixed), m_allOthersFixed(allOthersFixed), m_othersTied(othersTied) {
39 auto layout = new QHBoxLayout(this);
40 layout->setMargin(0);
41 layout->setSpacing(0);
42 layout->setContentsMargins(0, 0, 0, 0);
43
44 m_editor = new QLineEdit(parent);
45 m_editor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
46 this->setFocusPolicy(Qt::StrongFocus);
47 this->setFocusProxy(m_editor);
48
49 m_button = new QPushButton("&Set");
50 m_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
51 m_button->setFocusPolicy(Qt::NoFocus);
52
53 layout->addWidget(m_editor);
54 layout->addWidget(m_button);
55 layout->setStretch(0, 1);
56 layout->setStretch(1, 0);
57
58 auto setMenu = new QMenu(this);
59
60 m_setAllAction = new QAction("Set to all", this);
61 m_setAllAction->setToolTip("Set all parameters to this value");
62 connect(m_setAllAction, SIGNAL(triggered()), this, SLOT(setAll()));
63 setMenu->addAction(m_setAllAction);
64
65 setMenu->addSeparator();
66 m_fixAction = new QAction(m_fixed ? "Unfix" : "Fix", this);
67 m_fixAction->setToolTip("Fix value of this parameter");
68 connect(m_fixAction, SIGNAL(triggered()), this, SLOT(fixParameter()));
69 setMenu->addAction(m_fixAction);
70
71 m_fixAllAction = new QAction("Fix all", this);
72 m_fixAllAction->setToolTip("Fix all parameters.");
73 connect(m_fixAllAction, SIGNAL(triggered()), this, SLOT(fixAll()));
74 setMenu->addAction(m_fixAllAction);
75
76 m_unfixAllAction = new QAction("Unfix all", this);
77 m_unfixAllAction->setToolTip("Unfix all parameters.");
78 connect(m_unfixAllAction, SIGNAL(triggered()), this, SLOT(unfixAll()));
79 setMenu->addAction(m_unfixAllAction);
80
81 setMenu->addSeparator();
82 m_setTieAction = new QAction("Set tie", this);
83 m_setTieAction->setToolTip("Set a tie for this parameter.");
84 connect(m_setTieAction, SIGNAL(triggered()), this, SLOT(setTie()));
85 setMenu->addAction(m_setTieAction);
86
87 m_removeTieAction = new QAction("Remove tie", this);
88 m_removeTieAction->setToolTip("Remove the tie for this parameter.");
89 connect(m_removeTieAction, SIGNAL(triggered()), this, SLOT(removeTie()));
90 setMenu->addAction(m_removeTieAction);
91
92 m_setTieToAllAction = new QAction("Set tie to all", this);
93 m_setTieToAllAction->setToolTip("Set this tie for all parameters.");
94 connect(m_setTieToAllAction, SIGNAL(triggered()), this, SLOT(setTieAll()));
95 setMenu->addAction(m_setTieToAllAction);
96
97 m_removeAllTiesAction = new QAction("Remove all ties", this);
98 m_removeAllTiesAction->setToolTip("Remove ties for all parameters.");
99 connect(m_removeAllTiesAction, SIGNAL(triggered()), this, SLOT(removeAllTies()));
100 setMenu->addAction(m_removeAllTiesAction);
101
102 setMenu->addSeparator();
103 m_setConstraintAction = new QAction("Set constraint", this);
104 m_setConstraintAction->setToolTip("Set a constraint for this parameter.");
105 connect(m_setConstraintAction, SIGNAL(triggered()), this, SLOT(setConstraint()));
106 setMenu->addAction(m_setConstraintAction);
107
108 m_removeConstraintAction = new QAction("Remove constraint", this);
109 m_removeConstraintAction->setToolTip("Remove the constraint for this parameter.");
110 connect(m_removeConstraintAction, SIGNAL(triggered()), this, SLOT(removeConstraint()));
111 setMenu->addAction(m_removeConstraintAction);
112
113 m_setConstraintToAllAction = new QAction("Set constraint to all", this);
114 m_setConstraintToAllAction->setToolTip("Set this constraint for all parameters.");
115 connect(m_setConstraintToAllAction, SIGNAL(triggered()), this, SLOT(setConstraintAll()));
116 setMenu->addAction(m_setConstraintToAllAction);
117
118 m_removeAllConstraintsAction = new QAction("Remove all constraints", this);
119 m_removeAllConstraintsAction->setToolTip("Remove constraints for all parameters.");
120 connect(m_removeAllConstraintsAction, SIGNAL(triggered()), this, SLOT(removeAllConstraints()));
121 setMenu->addAction(m_removeAllConstraintsAction);
122
123 setMenu->addSeparator();
124 m_setToLogAction = new QAction("Set to log", this);
125 m_setToLogAction->setToolTip("Set this parameter to a log value.");
126 connect(m_setToLogAction, SIGNAL(triggered()), this, SLOT(setToLog()));
127 setMenu->addAction(m_setToLogAction);
128 m_setToLogAction->setEnabled(logOptionsEnabled);
129
130 m_setAllToLogAction = new QAction("Set all to log", this);
131 m_setAllToLogAction->setToolTip("Set all parameters to log value from the relevant workspace");
132 connect(m_setAllToLogAction, SIGNAL(triggered()), this, SIGNAL(setAllValuesToLog()));
133 setMenu->addAction(m_setAllToLogAction);
134 m_setAllToLogAction->setEnabled(logOptionsEnabled);
135
136 m_button->setMenu(setMenu);
137
138 m_editor->installEventFilter(this);
139
140 connect(m_editor, SIGNAL(textEdited(const QString &)), this, SLOT(updateValue(const QString &)));
141
143}
144
147 double value = m_editor->text().toDouble();
148 emit setAllValues(value);
149}
150
153 m_fixed = !m_fixed;
156}
157
160 m_fixed = true;
161 m_allOthersFixed = true;
162 m_othersFixed = true;
164 emit setAllFixed(true);
165}
166
169 m_fixed = false;
170 m_allOthersFixed = false;
171 m_othersFixed = false;
173 emit setAllFixed(false);
174}
175
178 auto tie = setTieDialog(m_tie);
179 if (!tie.isEmpty()) {
180 m_tie = tie;
181 emit setTie(m_index, m_tie);
182 }
184}
185
188 m_tie = "";
189 emit setTie(m_index, "");
191}
192
195 auto tie = setTieDialog(m_tie);
196 if (!tie.isEmpty()) {
197 m_tie = tie;
198 m_othersTied = true;
199 emit setTieAll(m_tie);
200 }
202}
203
206 m_tie = "";
207 m_othersTied = false;
208 emit setTieAll("");
210}
211
213 auto constraint = setConstraintDialog(m_constraint);
214 if (!constraint.isEmpty()) {
215 m_constraint = constraint;
217 }
219}
220
222 m_constraint = "";
223 emit setConstraint(m_index, "");
225}
226
228 auto constraint = setConstraintDialog(m_constraint);
229 if (!constraint.isEmpty()) {
230 m_constraint = constraint;
231 m_othersConstrained = true;
233 }
235}
236
238 m_constraint = "";
239 m_othersConstrained = false;
240 emit setConstraintAll("");
242}
243
246
248bool LocalParameterEditor::eventFilter(QObject * /*unused*/, QEvent *evn) {
249 if (evn->type() == QEvent::KeyPress) {
250 auto keyEvent = static_cast<QKeyEvent *>(evn);
251 if (keyEvent->key() == Qt::Key_F && keyEvent->modifiers() == Qt::ControlModifier && m_tie.isEmpty()) {
252 fixParameter();
253 return true;
254 }
255 if (m_tie.isEmpty()) {
256 m_value = m_editor->text();
257 } else {
258 m_tie = m_editor->text();
259 emit setTie(m_index, m_tie);
260 }
261 }
262 return false;
263}
264
268 bool const isNumber = m_tie.isEmpty();
269 bool const isTie = !isNumber;
270 bool const hasConstraint = !m_constraint.isEmpty();
271
272 m_setAllAction->setEnabled(isNumber);
273 m_fixAction->setText(m_fixed ? "Unfix" : "Fix");
274 m_fixAction->setEnabled(isNumber);
275 m_unfixAllAction->setEnabled(isNumber && (m_fixed || m_othersFixed));
276 m_fixAllAction->setEnabled(isNumber && (!m_fixed || !m_allOthersFixed));
277
278 m_removeTieAction->setEnabled(isTie);
279 m_removeAllTiesAction->setEnabled(isTie || m_othersTied);
280 m_removeConstraintAction->setEnabled(hasConstraint);
281 m_removeAllConstraintsAction->setEnabled(hasConstraint);
282
283 if (isNumber) {
284 auto validator = new QDoubleValidator(this);
285 validator->setDecimals(16);
286 m_editor->setValidator(validator);
287 m_editor->setText(m_value);
288 m_editor->setToolTip("Edit local parameter value. Press Ctrl+F to fix/unfix it.");
289 } else {
290 m_editor->setValidator(nullptr);
291 m_editor->setText(m_tie);
292 m_editor->setToolTip("Edit local parameter tie.");
293 }
294}
295
297QString LocalParameterEditor::setTieDialog(const QString &tie) {
298 QInputDialog input;
299 input.setWindowTitle("Set a tie.");
300 input.setTextValue(tie);
301 if (input.exec() == QDialog::Accepted) {
302 return input.textValue();
303 }
304 return "";
305}
306
307QString LocalParameterEditor::setConstraintDialog(const QString &constraint) {
308 QInputDialog input;
309 input.setWindowTitle("Set a constraint.");
310 input.setTextValue(constraint);
311 if (input.exec() == QDialog::Accepted) {
312 return input.textValue();
313 }
314 return "";
315}
316
322
329 m_setToLogAction->setEnabled(enabled);
330 m_setAllToLogAction->setEnabled(enabled);
331}
332} // namespace MantidQt::MantidWidgets
const std::string & m_value
Definition: Algorithm.cpp:71
double value
The value of the point.
Definition: FitMW.cpp:51
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
static QString setConstraintDialog(const QString &tie)
void removeAllTies()
Remove ties from all parameters.
void removeTie()
Send a signal to remove a tie.
void setLogOptionsEnabled(bool enabled)
Slot: when log checkbox state changes, enable/disable the "set to log" and "set all to log" options.
void unfixAll()
Send a signal to unfix all parameters.
void setToLog()
Send a signal to set value to log.
static QString setTieDialog(const QString &tie)
Open an input dialog to enter a tie expression.
void setAll()
Send a signal to set all parameters to the value in the editor.
void setEditorState()
Set the state of the editor elements (the line editor and the button) according to the state of the p...
void fixAll()
Send a signal to fix all parameters.
LocalParameterEditor(QWidget *parent, int index, double value, bool fixed, const QString &tie, const QString &constraint, bool othersFixed, bool allOthersFixed, bool othersTied, bool logOptionsEnabled)
Constructor.
bool eventFilter(QObject *widget, QEvent *evn) override
Filter events in the line editor to emulate a shortcut (F to fix/unfix).
void updateValue(const QString &value)
SLOT: when user edits value, make sure m_value is updated.
EXPORT_OPT_MANTIDQT_COMMON bool isNumber(std::string const &str)
Checks if a string contains a number, or whether it contains characters.