BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
ParameterTuningDelegate Class Reference

Description

Definition at line 28 of file ParameterTuningDelegate.h.

Inheritance diagram for ParameterTuningDelegate:
[legend]
Collaboration diagram for ParameterTuningDelegate:
[legend]

Classes

class  TuningData
 

Signals

void currentLinkChanged (ParameterItem *item)
 

Public Member Functions

 ParameterTuningDelegate (QObject *parent=nullptr)
 
 ~ParameterTuningDelegate () override
 
QWidget * createEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 
void setEditorData (QWidget *editor, const QModelIndex &index) const override
 
void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
 
void setReadOnly (bool isReadOnly)
 
void setSliderRangeFactor (double value)
 
void setValueColumn (int valueColumn)
 
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &) const override
 

Private Slots

void editorValueChanged (double value)
 
void sliderValueChanged (int position)
 

Private Member Functions

void emitSignals (double value)
 
void updateSlider (double value) const
 

Private Attributes

QHBoxLayout * m_contentLayout
 
QWidget * m_contentWidget
 
ParameterItemm_currentItem
 
bool m_isReadOnly
 
QSlider * m_slider
 
TuningData m_tuning_info
 
ScientificSpinBoxm_valueBox
 
int m_valueColumn
 

Constructor & Destructor Documentation

◆ ParameterTuningDelegate()

ParameterTuningDelegate::ParameterTuningDelegate ( QObject *  parent = nullptr)

Definition at line 89 of file ParameterTuningDelegate.cpp.

90  : QItemDelegate(parent)
91  , m_valueColumn(1)
92  , m_slider(nullptr)
93  , m_valueBox(nullptr)
94  , m_contentWidget(nullptr)
95  , m_contentLayout(nullptr)
96  , m_currentItem(nullptr)
97  , m_isReadOnly(false)
98 {
99 }
ScientificSpinBox * m_valueBox

◆ ~ParameterTuningDelegate()

ParameterTuningDelegate::~ParameterTuningDelegate ( )
overridedefault

Member Function Documentation

◆ createEditor()

QWidget * ParameterTuningDelegate::createEditor ( QWidget *  parent,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
override

Definition at line 126 of file ParameterTuningDelegate.cpp.

128 {
129  if (m_isReadOnly)
130  return nullptr;
131 
132  if (index.column() != m_valueColumn)
133  return QItemDelegate::createEditor(parent, option, index);
134 
135  if (!index.parent().isValid())
136  return nullptr;
137 
138  QVariant data = index.model()->data(index, Qt::EditRole);
139  if (!data.isValid())
140  return nullptr;
141 
143  if (!m_currentItem)
144  return nullptr;
145 
146  double value = data.toDouble();
147  RealLimits limits = m_currentItem->limitsOfLink();
150 
151  // initializing value box
153  m_valueBox->setKeyboardTracking(false);
154  m_valueBox->setFixedWidth(105);
157 
158  if (limits.hasLowerLimit())
159  m_valueBox->setMinimum(limits.lowerLimit());
160  else
161  m_valueBox->setMinimum(minimum_doublespin_value);
162 
163  if (limits.hasUpperLimit())
164  m_valueBox->setMaximum(limits.upperLimit());
165  else
166  m_valueBox->setMaximum(maximum_doublespin_value);
167 
168  m_valueBox->setValue(value);
171 
172  // initializing slider
173  m_slider = new QSlider(Qt::Horizontal);
174  m_slider->setFocusPolicy(Qt::StrongFocus);
175  m_slider->setTickPosition(QSlider::NoTicks);
176  m_slider->setTickInterval(1);
177  m_slider->setSingleStep(1);
179 
180  updateSlider(value);
181 
182  m_contentWidget = new QWidget(parent);
183  m_contentLayout = new QHBoxLayout(parent);
184  m_contentLayout->setMargin(0);
185  m_contentLayout->setSpacing(0);
186  m_contentLayout->addWidget(m_valueBox);
187  m_contentLayout->addWidget(m_slider);
188 
189  // FIXME there is an issue with time of life of editor .vs. item
190  // m_currentItem->mapper()->setOnValueChange(
191  // [this](){
192  // if(m_valueBox && m_currentItem)
193  // m_valueBox->setValue(m_currentItem->value().toDouble());
194  // }, this);
195 
196  // m_currentItem->mapper()->setOnItemDestroy(
197  // [this](SessionItem *) {
198  // m_currentItem = 0;
199  // }, this);
200 
201  m_contentWidget->setLayout(m_contentLayout);
202 
203  return m_contentWidget;
204 }
int decimalsOfLink() const
RealLimits limitsOfLink() const
void setItemLimits(const RealLimits &item_limits)
void updateSlider(double value) const
static ParameterItem * toParameterItem(const QModelIndex &index)
void setMaximum(double max)
void setMinimum(double min)
void setValue(double val)
void setSingleStep(double step)
void valueChanged(double value)

References ParameterItem::decimalsOfLink(), editorValueChanged(), ParameterItem::limitsOfLink(), m_contentLayout, m_contentWidget, m_currentItem, m_isReadOnly, m_slider, ParameterTuningDelegate::TuningData::m_smax, ParameterTuningDelegate::TuningData::m_smin, m_tuning_info, m_valueBox, m_valueColumn, ScientificSpinBox::setDecimals(), ParameterTuningDelegate::TuningData::setItemLimits(), ScientificSpinBox::setMaximum(), ScientificSpinBox::setMinimum(), ScientificSpinBox::setSingleStep(), ScientificSpinBox::setValue(), ParameterTuningDelegate::TuningData::step(), ParameterTuningModel::toParameterItem(), updateSlider(), ParameterTuningDelegate::TuningData::value_to_slider(), and ScientificSpinBox::valueChanged().

Here is the call graph for this function:

◆ currentLinkChanged

void ParameterTuningDelegate::currentLinkChanged ( ParameterItem item)
signal

◆ editorValueChanged

void ParameterTuningDelegate::editorValueChanged ( double  value)
privateslot

Definition at line 229 of file ParameterTuningDelegate.cpp.

230 {
231  disconnect(m_slider, &QSlider::valueChanged, this,
233 
234  updateSlider(value);
235 
236  connect(m_slider, &QSlider::valueChanged, this, &ParameterTuningDelegate::sliderValueChanged);
237  emitSignals(value);
238 }

References emitSignals(), m_slider, sliderValueChanged(), and updateSlider().

Referenced by createEditor(), and sliderValueChanged().

Here is the call graph for this function:

◆ emitSignals()

void ParameterTuningDelegate::emitSignals ( double  value)
private

Definition at line 258 of file ParameterTuningDelegate.cpp.

259 {
260  if (m_currentItem) {
263  }
264 }
void propagateValueToLink(double newValue)
Sets current value to the original PropertyItem of MultiLayerItem/InstrumentItem.
void currentLinkChanged(ParameterItem *item)

References currentLinkChanged(), m_currentItem, and ParameterItem::propagateValueToLink().

Referenced by editorValueChanged(), and sliderValueChanged().

Here is the call graph for this function:

◆ paint()

void ParameterTuningDelegate::paint ( QPainter *  painter,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
override

Definition at line 103 of file ParameterTuningDelegate.cpp.

105 {
106  if (index.column() == m_valueColumn) {
107  if (!index.parent().isValid())
108  return;
109 
110  QVariant valueAsVariant = index.model()->data(index, Qt::EditRole);
111  if (valueAsVariant.isValid() && valueAsVariant.type() == QVariant::Double) {
112  double value = valueAsVariant.toDouble();
113  QString text = ScientificSpinBox::toString(value, 3);
114 
115  QStyleOptionViewItem myOption = option;
116  myOption.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
117 
118  drawDisplay(painter, myOption, myOption.rect, text);
119  drawFocus(painter, myOption, myOption.rect);
120  return;
121  }
122  }
123  QItemDelegate::paint(painter, option, index);
124 }
static QString toString(double val, int decimal_points)

References m_valueColumn, and ScientificSpinBox::toString().

Here is the call graph for this function:

◆ setEditorData()

void ParameterTuningDelegate::setEditorData ( QWidget *  editor,
const QModelIndex &  index 
) const
override

Definition at line 240 of file ParameterTuningDelegate.cpp.

241 {
242  if (index.column() == m_valueColumn) {
243  // as using custom widget, doing nothing here
244  } else {
245  QItemDelegate::setEditorData(editor, index);
246  }
247 }

References m_valueColumn.

◆ setModelData()

void ParameterTuningDelegate::setModelData ( QWidget *  editor,
QAbstractItemModel *  model,
const QModelIndex &  index 
) const
override

Definition at line 249 of file ParameterTuningDelegate.cpp.

251 {
252  if (index.column() == m_valueColumn)
253  model->setData(index, m_valueBox->value());
254  else
255  QItemDelegate::setModelData(editor, model, index);
256 }

References m_valueBox, m_valueColumn, and ScientificSpinBox::value.

◆ setReadOnly()

void ParameterTuningDelegate::setReadOnly ( bool  isReadOnly)

Definition at line 271 of file ParameterTuningDelegate.cpp.

272 {
273  m_isReadOnly = isReadOnly;
274 }

References m_isReadOnly.

Referenced by ParameterTuningWidget::setTuningDelegateEnabled().

◆ setSliderRangeFactor()

void ParameterTuningDelegate::setSliderRangeFactor ( double  value)

Definition at line 266 of file ParameterTuningDelegate.cpp.

267 {
269 }

References m_tuning_info, and ParameterTuningDelegate::TuningData::setRangeFactor().

Referenced by ParameterTuningWidget::onSliderValueChanged().

Here is the call graph for this function:

◆ setValueColumn()

void ParameterTuningDelegate::setValueColumn ( int  valueColumn)
inline

Definition at line 66 of file ParameterTuningDelegate.h.

66 { m_valueColumn = valueColumn; }

References m_valueColumn.

◆ sizeHint()

QSize ParameterTuningDelegate::sizeHint ( const QStyleOptionViewItem &  option,
const QModelIndex &   
) const
inlineoverride

Definition at line 51 of file ParameterTuningDelegate.h.

53  {
54  return QSize(option.rect.width(), 25);
55  }

◆ sliderValueChanged

void ParameterTuningDelegate::sliderValueChanged ( int  position)
privateslot

Definition at line 216 of file ParameterTuningDelegate.cpp.

217 {
218  disconnect(m_valueBox, &ScientificSpinBox::valueChanged, this,
220 
221  double value = m_tuning_info.slider_to_value(position);
222  m_valueBox->setValue(value);
223 
226  emitSignals(value);
227 }

References editorValueChanged(), emitSignals(), m_tuning_info, m_valueBox, ScientificSpinBox::setValue(), ParameterTuningDelegate::TuningData::slider_to_value(), and ScientificSpinBox::valueChanged().

Referenced by editorValueChanged(), and updateSlider().

Here is the call graph for this function:

◆ updateSlider()

void ParameterTuningDelegate::updateSlider ( double  value) const
private

Definition at line 206 of file ParameterTuningDelegate.cpp.

207 {
208  disconnect(m_slider, &QSlider::valueChanged, this,
210 
211  m_slider->setValue(m_tuning_info.value_to_slider(value));
212 
213  connect(m_slider, &QSlider::valueChanged, this, &ParameterTuningDelegate::sliderValueChanged);
214 }

References m_slider, m_tuning_info, sliderValueChanged(), and ParameterTuningDelegate::TuningData::value_to_slider().

Referenced by createEditor(), and editorValueChanged().

Here is the call graph for this function:

Member Data Documentation

◆ m_contentLayout

QHBoxLayout* ParameterTuningDelegate::m_contentLayout
mutableprivate

Definition at line 85 of file ParameterTuningDelegate.h.

Referenced by createEditor().

◆ m_contentWidget

QWidget* ParameterTuningDelegate::m_contentWidget
mutableprivate

Definition at line 84 of file ParameterTuningDelegate.h.

Referenced by createEditor().

◆ m_currentItem

ParameterItem* ParameterTuningDelegate::m_currentItem
mutableprivate

Definition at line 86 of file ParameterTuningDelegate.h.

Referenced by createEditor(), and emitSignals().

◆ m_isReadOnly

bool ParameterTuningDelegate::m_isReadOnly
private

Definition at line 88 of file ParameterTuningDelegate.h.

Referenced by createEditor(), and setReadOnly().

◆ m_slider

QSlider* ParameterTuningDelegate::m_slider
mutableprivate

Definition at line 82 of file ParameterTuningDelegate.h.

Referenced by createEditor(), editorValueChanged(), and updateSlider().

◆ m_tuning_info

TuningData ParameterTuningDelegate::m_tuning_info
mutableprivate

◆ m_valueBox

ScientificSpinBox* ParameterTuningDelegate::m_valueBox
mutableprivate

Definition at line 83 of file ParameterTuningDelegate.h.

Referenced by createEditor(), setModelData(), and sliderValueChanged().

◆ m_valueColumn

int ParameterTuningDelegate::m_valueColumn
private

The documentation for this class was generated from the following files: