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->setSpacing(0);
41 layout->setContentsMargins(0, 0, 0, 0);
42
43 m_editor = new QLineEdit(parent);
44 m_editor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
45 this->setFocusPolicy(Qt::StrongFocus);
46 this->setFocusProxy(m_editor);
47
48 m_button = new QPushButton("&Set");
49 m_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
50 m_button->setFocusPolicy(Qt::NoFocus);
51
52 layout->addWidget(m_editor);
53 layout->addWidget(m_button);
54 layout->setStretch(0, 1);
55 layout->setStretch(1, 0);
56
57 auto setMenu = new QMenu(this);
58
59 m_setAllAction = new QAction("Set to all", this);
60 m_setAllAction->setToolTip("Set all parameters to this value");
61 connect(m_setAllAction, SIGNAL(triggered()), this, SLOT(setAll()));
62 setMenu->addAction(m_setAllAction);
63
64 setMenu->addSeparator();
65 m_fixAction = new QAction(m_fixed ? "Unfix" : "Fix", this);
66 m_fixAction->setToolTip("Fix value of this parameter");
67 connect(m_fixAction, SIGNAL(triggered()), this, SLOT(fixParameter()));
68 setMenu->addAction(m_fixAction);
69
70 m_fixAllAction = new QAction("Fix all", this);
71 m_fixAllAction->setToolTip("Fix all parameters.");
72 connect(m_fixAllAction, SIGNAL(triggered()), this, SLOT(fixAll()));
73 setMenu->addAction(m_fixAllAction);
74
75 m_unfixAllAction = new QAction("Unfix all", this);
76 m_unfixAllAction->setToolTip("Unfix all parameters.");
77 connect(m_unfixAllAction, SIGNAL(triggered()), this, SLOT(unfixAll()));
78 setMenu->addAction(m_unfixAllAction);
79
80 setMenu->addSeparator();
81 m_setTieAction = new QAction("Set tie", this);
82 m_setTieAction->setToolTip("Set a tie for this parameter.");
83 connect(m_setTieAction, SIGNAL(triggered()), this, SLOT(setTie()));
84 setMenu->addAction(m_setTieAction);
85
86 m_removeTieAction = new QAction("Remove tie", this);
87 m_removeTieAction->setToolTip("Remove the tie for this parameter.");
88 connect(m_removeTieAction, SIGNAL(triggered()), this, SLOT(removeTie()));
89 setMenu->addAction(m_removeTieAction);
90
91 m_setTieToAllAction = new QAction("Set tie to all", this);
92 m_setTieToAllAction->setToolTip("Set this tie for all parameters.");
93 connect(m_setTieToAllAction, SIGNAL(triggered()), this, SLOT(setTieAll()));
94 setMenu->addAction(m_setTieToAllAction);
95
96 m_removeAllTiesAction = new QAction("Remove all ties", this);
97 m_removeAllTiesAction->setToolTip("Remove ties for all parameters.");
98 connect(m_removeAllTiesAction, SIGNAL(triggered()), this, SLOT(removeAllTies()));
99 setMenu->addAction(m_removeAllTiesAction);
100
101 setMenu->addSeparator();
102 m_setConstraintAction = new QAction("Set constraint", this);
103 m_setConstraintAction->setToolTip("Set a constraint for this parameter.");
104 connect(m_setConstraintAction, SIGNAL(triggered()), this, SLOT(setConstraint()));
105 setMenu->addAction(m_setConstraintAction);
106
107 m_removeConstraintAction = new QAction("Remove constraint", this);
108 m_removeConstraintAction->setToolTip("Remove the constraint for this parameter.");
109 connect(m_removeConstraintAction, SIGNAL(triggered()), this, SLOT(removeConstraint()));
110 setMenu->addAction(m_removeConstraintAction);
111
112 m_setConstraintToAllAction = new QAction("Set constraint to all", this);
113 m_setConstraintToAllAction->setToolTip("Set this constraint for all parameters.");
114 connect(m_setConstraintToAllAction, SIGNAL(triggered()), this, SLOT(setConstraintAll()));
115 setMenu->addAction(m_setConstraintToAllAction);
116
117 m_removeAllConstraintsAction = new QAction("Remove all constraints", this);
118 m_removeAllConstraintsAction->setToolTip("Remove constraints for all parameters.");
119 connect(m_removeAllConstraintsAction, SIGNAL(triggered()), this, SLOT(removeAllConstraints()));
120 setMenu->addAction(m_removeAllConstraintsAction);
121
122 setMenu->addSeparator();
123 m_setToLogAction = new QAction("Set to log", this);
124 m_setToLogAction->setToolTip("Set this parameter to a log value.");
125 connect(m_setToLogAction, SIGNAL(triggered()), this, SLOT(setToLog()));
126 setMenu->addAction(m_setToLogAction);
127 m_setToLogAction->setEnabled(logOptionsEnabled);
128
129 m_setAllToLogAction = new QAction("Set all to log", this);
130 m_setAllToLogAction->setToolTip("Set all parameters to log value from the relevant workspace");
131 connect(m_setAllToLogAction, SIGNAL(triggered()), this, SIGNAL(setAllValuesToLog()));
132 setMenu->addAction(m_setAllToLogAction);
133 m_setAllToLogAction->setEnabled(logOptionsEnabled);
134
135 m_button->setMenu(setMenu);
136
137 m_editor->installEventFilter(this);
138
139 connect(m_editor, SIGNAL(textEdited(const QString &)), this, SLOT(updateValue(const QString &)));
140
142}
143
146 double value = m_editor->text().toDouble();
147 emit setAllValues(value);
148}
149
156
159 m_fixed = true;
160 m_allOthersFixed = true;
161 m_othersFixed = true;
163 emit setAllFixed(true);
164}
165
168 m_fixed = false;
169 m_allOthersFixed = false;
170 m_othersFixed = false;
172 emit setAllFixed(false);
173}
174
177 auto tie = setTieDialog(m_tie);
178 if (!tie.isEmpty()) {
179 m_tie = tie;
180 emit setTie(m_index, m_tie);
181 }
183}
184
187 m_tie = "";
188 emit setTie(m_index, "");
190}
191
194 auto tie = setTieDialog(m_tie);
195 if (!tie.isEmpty()) {
196 m_tie = tie;
197 m_othersTied = true;
198 emit setTieAll(m_tie);
199 }
201}
202
205 m_tie = "";
206 m_othersTied = false;
207 emit setTieAll("");
209}
210
212 auto constraint = setConstraintDialog(m_constraint);
213 if (!constraint.isEmpty()) {
214 m_constraint = constraint;
216 }
218}
219
225
227 auto constraint = setConstraintDialog(m_constraint);
228 if (!constraint.isEmpty()) {
229 m_constraint = constraint;
230 m_othersConstrained = true;
232 }
234}
235
242
245
247bool LocalParameterEditor::eventFilter(QObject * /*unused*/, QEvent *evn) {
248 if (evn->type() == QEvent::KeyPress) {
249 auto keyEvent = static_cast<QKeyEvent *>(evn);
250 if (keyEvent->key() == Qt::Key_F && keyEvent->modifiers() == Qt::ControlModifier && m_tie.isEmpty()) {
251 fixParameter();
252 return true;
253 }
254 if (m_tie.isEmpty()) {
255 m_value = m_editor->text();
256 } else {
257 m_tie = m_editor->text();
258 emit setTie(m_index, m_tie);
259 }
260 }
261 return false;
262}
263
267 bool const isNumber = m_tie.isEmpty();
268 bool const isTie = !isNumber;
269 bool const hasConstraint = !m_constraint.isEmpty();
270
271 m_setAllAction->setEnabled(isNumber);
272 m_fixAction->setText(m_fixed ? "Unfix" : "Fix");
273 m_fixAction->setEnabled(isNumber);
274 m_unfixAllAction->setEnabled(isNumber && (m_fixed || m_othersFixed));
275 m_fixAllAction->setEnabled(isNumber && (!m_fixed || !m_allOthersFixed));
276
277 m_removeTieAction->setEnabled(isTie);
278 m_removeAllTiesAction->setEnabled(isTie || m_othersTied);
279 m_removeConstraintAction->setEnabled(hasConstraint);
280 m_removeAllConstraintsAction->setEnabled(hasConstraint);
281
282 if (isNumber) {
283 auto validator = new QDoubleValidator(this);
284 validator->setDecimals(16);
285 m_editor->setValidator(validator);
286 m_editor->setText(m_value);
287 m_editor->setToolTip("Edit local parameter value. Press Ctrl+F to fix/unfix it.");
288 } else {
289 m_editor->setValidator(nullptr);
290 m_editor->setText(m_tie);
291 m_editor->setToolTip("Edit local parameter tie.");
292 }
293}
294
296QString LocalParameterEditor::setTieDialog(const QString &tie) {
297 QInputDialog input;
298 input.setWindowTitle("Set a tie.");
299 input.setTextValue(tie);
300 if (input.exec() == QDialog::Accepted) {
301 return input.textValue();
302 }
303 return "";
304}
305
306QString LocalParameterEditor::setConstraintDialog(const QString &constraint) {
307 QInputDialog input;
308 input.setWindowTitle("Set a constraint.");
309 input.setTextValue(constraint);
310 if (input.exec() == QDialog::Accepted) {
311 return input.textValue();
312 }
313 return "";
314}
315
321
328 m_setToLogAction->setEnabled(enabled);
329 m_setAllToLogAction->setEnabled(enabled);
330}
331} // 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
static QString setConstraintDialog(const QString &tie)
void removeAllTies()
Remove ties from all parameters.
void setTieAll()
Set all ties for 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 setTie()
Send a signal to tie a parameter.
void fixParameter()
Toggle the fix state of the current parameter.
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.