-
Notifications
You must be signed in to change notification settings - Fork 122
/
MDFLocalParameterEditor.cpp
120 lines (101 loc) · 3.22 KB
/
MDFLocalParameterEditor.cpp
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
#include "MantidQtCustomInterfaces/MultiDatasetFit/MDFLocalParameterEditor.h"
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QMenu>
#include <QAction>
#include <QDoubleValidator>
#include <QEvent>
#include <QKeyEvent>
namespace MantidQt
{
namespace CustomInterfaces
{
namespace MDF
{
/// Constructor
/// @param parent :: Parent widget.
/// @param index :: Index of the spectrum which parameter is edited.
/// @param fixed :: Is the parameter fixed initially?
LocalParameterEditor::LocalParameterEditor(QWidget *parent, int index, bool fixed):
QWidget(parent), m_index(index),m_fixed(fixed)
{
auto layout = new QHBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
layout->setContentsMargins(0,0,0,0);
m_editor = new QLineEdit(parent);
m_editor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
auto validator = new QDoubleValidator(this);
validator->setDecimals(16);
m_editor->setValidator(validator);
m_editor->setToolTip("Edit local parameter value. Press F to fix/unfix it.");
auto button = new QPushButton("Set");
button->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Expanding);
this->setFocusPolicy(Qt::NoFocus);
layout->addWidget(m_editor);
layout->addWidget(button);
layout->setStretch(0,1);
layout->setStretch(1,0);
this->setFocusProxy(m_editor);
this->setFocusPolicy(Qt::StrongFocus);
auto setMenu = new QMenu(this);
QAction *action = new QAction("Set to all",this);
action->setToolTip("Set all parameters to this value");
connect(action,SIGNAL(activated()),this,SLOT(setAll()));
setMenu->addAction(action);
m_fixAction = new QAction(m_fixed? "Unfix" : "Fix", this);
m_fixAction->setToolTip("Fix value of this parameter");
connect(m_fixAction,SIGNAL(activated()),this,SLOT(fixParameter()));
setMenu->addAction(m_fixAction);
action = new QAction("Fix all",this);
action->setToolTip("Fix all parameters.");
connect(action,SIGNAL(activated()),this,SLOT(fixAll()));
setMenu->addAction(action);
action = new QAction("Unix all",this);
action->setToolTip("Unfix all parameters.");
connect(action,SIGNAL(activated()),this,SLOT(unfixAll()));
setMenu->addAction(action);
button->setMenu(setMenu);
m_editor->installEventFilter(this);
}
/// Send a signal to set all parameters to the value in the editor.
void LocalParameterEditor::setAll()
{
double value = m_editor->text().toDouble();
emit setAllValues(value);
}
/// Toggle the fix state of the current parameter.
void LocalParameterEditor::fixParameter()
{
m_fixed = !m_fixed;
m_fixAction->setText( m_fixed? "Unfix" : "Fix" );
emit fixParameter(m_index, m_fixed);
}
/// Send a signal to fix all parameters.
void LocalParameterEditor::fixAll()
{
emit setAllFixed(true);
}
/// Send a signal to unfix all parameters.
void LocalParameterEditor::unfixAll()
{
emit setAllFixed(false);
}
/// Filter events in the line editor to emulate a shortcut (F to fix/unfix).
bool LocalParameterEditor::eventFilter(QObject *widget, QEvent *evn)
{
if ( evn->type() == QEvent::KeyPress )
{
auto keyEvent = static_cast<QKeyEvent*>(evn);
if ( keyEvent->key() == Qt::Key_F )
{
fixParameter();
return true;
}
}
return false;
}
} // MDF
} // CustomInterfaces
} // MantidQt