-
Notifications
You must be signed in to change notification settings - Fork 122
/
MDFLocalParameterItemDelegate.cpp
93 lines (79 loc) · 3.18 KB
/
MDFLocalParameterItemDelegate.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
#include "MantidQtCustomInterfaces/MultiDatasetFit/MDFLocalParameterItemDelegate.h"
#include "MantidQtCustomInterfaces/MultiDatasetFit/MDFEditLocalParameterDialog.h"
#include "MantidQtCustomInterfaces/MultiDatasetFit/MDFLocalParameterEditor.h"
#include "MantidQtCustomInterfaces/MultiDatasetFit/MultiDatasetFit.h"
#include <QPainter>
namespace MantidQt
{
namespace CustomInterfaces
{
namespace MDF
{
/// Constructor.
LocalParameterItemDelegate::LocalParameterItemDelegate(EditLocalParameterDialog *parent):
QStyledItemDelegate(parent),
m_currentEditor(NULL)
{
}
/// Create a custom editor LocalParameterEditor.
QWidget* LocalParameterItemDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem &, const QModelIndex & index) const
{
m_currentEditor = new LocalParameterEditor(parent,index.row(), owner()->isFixed(index.row()));
connect(m_currentEditor,SIGNAL(setAllValues(double)),this,SIGNAL(setAllValues(double)));
connect(m_currentEditor,SIGNAL(fixParameter(int,bool)),this,SIGNAL(fixParameter(int,bool)));
connect(m_currentEditor,SIGNAL(setAllFixed(bool)),this,SIGNAL(setAllFixed(bool)));
m_currentEditor->installEventFilter(const_cast<LocalParameterItemDelegate*>(this));
return m_currentEditor;
}
/// Initialize the editor with the current data in the cell.
void LocalParameterItemDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
{
QStyledItemDelegate::setEditorData(editor->layout()->itemAt(0)->widget(), index);
}
/// Update the data in the cell with the text in the editor.
void LocalParameterItemDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
{
QStyledItemDelegate::setModelData(editor->layout()->itemAt(0)->widget(), model, index);
}
/// Re-implemented to resolve an issue: if the parent dialog closes with
/// the editor is active any changes in it get ignored.
bool LocalParameterItemDelegate::eventFilter(QObject * obj, QEvent * ev)
{
if ( ev->type() == QEvent::WindowDeactivate )
{
// Force to save the changes to the underlying model.
emit commitData(m_currentEditor);
return true;
}
return QStyledItemDelegate::eventFilter(obj,ev);
}
/// Paint the table cell.
void LocalParameterItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QStyledItemDelegate::paint(painter, option, index);
if ( owner()->isFixed(index.row()) )
{
auto rect = option.rect;
auto text = index.model()->data(index).asString();
int textWidth = option.fontMetrics.width(text);
QString fixedStr(" (fixed)");
int fWidth = option.fontMetrics.width(fixedStr);
if ( textWidth + fWidth > rect.width() )
{
fixedStr = "(f)";
fWidth = option.fontMetrics.width(fixedStr);
}
auto dHeight = (option.rect.height() - option.fontMetrics.height()) / 2;
rect.adjust(rect.width() - fWidth, dHeight, 0 ,-dHeight);
painter->drawText(rect,fixedStr);
}
}
/// Cast the parent to EditLocalParameterDialog. Get access to parameter
/// values and fixes.
EditLocalParameterDialog *LocalParameterItemDelegate::owner() const
{
return static_cast<EditLocalParameterDialog*>(parent());
}
} // MDF
} // CustomInterfaces
} // MantidQt