Mantid
Loading...
Searching...
No Matches
LocalParameterItemDelegate.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 +
10
11#include <QPainter>
12
14
17 : QStyledItemDelegate(parent), m_currentEditor(nullptr) {}
18
20QWidget *LocalParameterItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem & /*option*/,
21 const QModelIndex &index) const {
22 auto row = index.row();
23 m_currentEditor = new LocalParameterEditor(parent, row, owner()->getValue(row), owner()->isFixed(row),
24 owner()->getTie(row), owner()->getConstraint(row),
25 owner()->areOthersFixed(row), owner()->areAllOthersFixed(row),
26 owner()->areOthersTied(row), owner()->isLogCheckboxTicked());
27 connect(m_currentEditor, SIGNAL(setAllValues(double)), this, SIGNAL(setAllValues(double)));
28 connect(m_currentEditor, SIGNAL(fixParameter(int, bool)), this, SIGNAL(fixParameter(int, bool)));
29 connect(m_currentEditor, SIGNAL(setAllFixed(bool)), this, SIGNAL(setAllFixed(bool)));
30 connect(m_currentEditor, SIGNAL(setTie(int, QString)), this, SIGNAL(setTie(int, QString)));
31 connect(m_currentEditor, SIGNAL(setTieAll(QString)), this, SIGNAL(setTieAll(QString)));
32 connect(m_currentEditor, SIGNAL(setConstraint(int, QString)), this, SIGNAL(setConstraint(int, QString)));
33 connect(m_currentEditor, SIGNAL(setConstraintAll(QString)), this, SIGNAL(setConstraintAll(QString)));
34 connect(m_currentEditor, SIGNAL(setValueToLog(int)), this, SLOT(doSetValueToLog(int)));
35 connect(m_currentEditor, SIGNAL(setAllValuesToLog()), this, SLOT(doSetAllValuesToLog()));
36 connect(owner(), SIGNAL(logOptionsChecked(bool)), m_currentEditor, SLOT(setLogOptionsEnabled(bool)));
37 m_currentEditor->installEventFilter(const_cast<LocalParameterItemDelegate *>(this));
38 return m_currentEditor;
39}
40
42void LocalParameterItemDelegate::setEditorData(QWidget * /*editor*/, const QModelIndex & /*index*/) const {
43 // Needs to be empty to prevent Qt's default behaviour.
44}
45
47void LocalParameterItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
48 const QModelIndex &index) const {
49 QStyledItemDelegate::setModelData(editor->layout()->itemAt(0)->widget(), model, index);
50}
51
54bool LocalParameterItemDelegate::eventFilter(QObject *obj, QEvent *ev) {
55 if (ev->type() == QEvent::WindowDeactivate) {
56 // Force to save the changes to the underlying model.
57 emit commitData(m_currentEditor);
58 return true;
59 }
60 return QStyledItemDelegate::eventFilter(obj, ev);
61}
62
64void LocalParameterItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
65 const QModelIndex &index) const {
66 auto const tie = owner()->getTie(index.row());
67
68 if (!tie.isEmpty()) {
69 auto rect = option.rect;
70 auto dHeight = (option.rect.height() - option.fontMetrics.height()) / 2;
71 rect.adjust(0, dHeight, 0, -dHeight);
72 painter->drawText(rect, tie);
73 } else {
74 QStyledItemDelegate::paint(painter, option, index);
75 }
76}
77
81 return static_cast<EditLocalParameterDialog *>(parent());
82}
83
89 if (m_currentEditor) {
90 closeEditor(m_currentEditor);
91 m_currentEditor = nullptr;
92 }
93 emit setValueToLog(i);
94}
95
100 if (m_currentEditor) {
101 closeEditor(m_currentEditor);
102 m_currentEditor = nullptr;
103 }
104 emit setAllValuesToLog();
105}
106
113 if (m_currentEditor) {
114 closeEditor(m_currentEditor);
115 m_currentEditor = nullptr;
116 }
117}
118
119} // namespace MantidQt::MantidWidgets
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
double obj
the value of the quadratic function
A dialog for displaying and editing values of local parameters.
An editor widget for editing a local parameter value.
A custom item delegate - an object controlling display and editing of a cell in a table widget.
void doSetValueToLog(int)
Slot: close the editor and re-emit the signal.
void prepareForPastedData()
Data is about to be pasted into the table.
void doSetAllValuesToLog()
Slot: close the editor and re-emit the signal.
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Initialize the editor with the current data in the cell.
EditLocalParameterDialog * owner() const
Cast the parent to EditLocalParameterDialog.
LocalParameterItemDelegate(EditLocalParameterDialog *parent=nullptr)
Constructor.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Create a custom editor LocalParameterEditor.
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Update the data in the cell with the text in the editor.
bool eventFilter(QObject *obj, QEvent *ev) override
Re-implemented to resolve an issue: if the parent dialog closes with the editor is active any changes...
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Paint the table cell.