BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MultiComboPropertyEditor Class Reference

Provides custom editor for ComboProperty with multi-select option. More...

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

Public Slots

void setData (const QVariant &data)
 Sets the data from the model to editor. More...
 

Signals

void dataChanged (const QVariant &data)
 Signal emit then user changed the data through the editor. More...
 

Public Member Functions

 MultiComboPropertyEditor (QWidget *parent=nullptr)
 
QVariant editorData ()
 
QSize minimumSizeHint () const
 
QSize sizeHint () const
 

Protected Slots

void onClickedList (const QModelIndex &index)
 Processes press event in QComboBox's underlying list view. More...
 
void onModelDataChanged (const QModelIndex &, const QModelIndex &, const QVector< int > &)
 Propagate check state from the model to ComboProperty. More...
 

Protected Member Functions

void initEditor ()
 Inits editor widgets from m_data. More...
 
void setDataIntern (const QVariant &data)
 Saves the data from the editor and informs external delegates. More...
 

Protected Attributes

QVariant m_data
 

Private Member Functions

bool eventFilter (QObject *obj, QEvent *event)
 Handles mouse clicks on QComboBox elements. More...
 
bool isClickToExpand (QObject *obj, QEvent *event) const
 
bool isClickToSelect (QObject *obj, QEvent *event) const
 
void setConnected (bool isConnected)
 
void updateBoxLabel ()
 Update text on QComboBox with the label provided by combo property. More...
 

Private Attributes

QComboBox * m_box
 
QStandardItemModel * m_model
 
class WheelEventEaterm_wheel_event_filter
 

Detailed Description

Provides custom editor for ComboProperty with multi-select option.

Definition at line 28 of file MultiComboPropertyEditor.h.

Constructor & Destructor Documentation

◆ MultiComboPropertyEditor()

MultiComboPropertyEditor::MultiComboPropertyEditor ( QWidget *  parent = nullptr)
explicit

Definition at line 46 of file MultiComboPropertyEditor.cpp.

47  : CustomEditor(parent)
48  , m_box(new QComboBox)
50  , m_model(new QStandardItemModel(this))
51 {
52  setAutoFillBackground(true);
53  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
54 
55  m_box->installEventFilter(m_wheel_event_filter);
56  m_box->view()->viewport()->installEventFilter(this);
57 
58  // Editable mode will be used to have None/Multiple labels on top
59  m_box->setEditable(true);
60  m_box->lineEdit()->setReadOnly(true);
61  m_box->lineEdit()->installEventFilter(this);
62  connect(m_box->lineEdit(), &QLineEdit::selectionChanged, m_box->lineEdit(),
63  &QLineEdit::deselect);
64 
65  // transforms ordinary combo box into check list
66  m_box->setItemDelegate(new QCheckListStyledItemDelegate(this));
67  m_box->setModel(m_model);
68 
69  auto layout = new QVBoxLayout;
70  layout->setMargin(0);
71  layout->setSpacing(0);
72  layout->addWidget(m_box);
73  setLayout(layout);
74  setConnected(true);
75 }
CustomEditor(QWidget *parent=nullptr)
Definition: CustomEditors.h:31
class WheelEventEater * m_wheel_event_filter
void setConnected(bool isConnected)
Provides custom style delegate for QComboBox to allow checkboxes.
Event filter to install on combo boxes and spin boxes to not to react on wheel events during scrollin...

References m_box, m_model, m_wheel_event_filter, and setConnected().

Here is the call graph for this function:

Member Function Documentation

◆ dataChanged

void CustomEditor::dataChanged ( const QVariant &  data)
signalinherited

Signal emit then user changed the data through the editor.

Referenced by PropertyWidgetItem::connectEditor(), SessionModelDelegate::createEditor(), and CustomEditor::setDataIntern().

◆ editorData()

QVariant CustomEditor::editorData ( )
inlineinherited

Definition at line 33 of file CustomEditors.h.

33 { return m_data; }
QVariant m_data
Definition: CustomEditors.h:45

References CustomEditor::m_data.

◆ eventFilter()

bool MultiComboPropertyEditor::eventFilter ( QObject *  obj,
QEvent *  event 
)
private

Handles mouse clicks on QComboBox elements.

Definition at line 120 of file MultiComboPropertyEditor.cpp.

121 {
122  if (isClickToSelect(obj, event)) {
123  // Handles mouse clicks on QListView when it is expanded from QComboBox
124  // 1) Prevents list from closing while selecting items.
125  // 2) Correctly calculates underlying model index when mouse is over check box style
126  // element.
127  const auto mouseEvent = static_cast<const QMouseEvent*>(event);
128  auto index = m_box->view()->indexAt(mouseEvent->pos());
129  onClickedList(index);
130  return true;
131 
132  } else if (isClickToExpand(obj, event)) {
133  // Expands box when clicking on None/Multiple label
134  m_box->showPopup();
135  return true;
136 
137  } else {
138  // Propagate to the parent class.
139  return QObject::eventFilter(obj, event);
140  }
141 }
bool isClickToExpand(QObject *obj, QEvent *event) const
void onClickedList(const QModelIndex &index)
Processes press event in QComboBox's underlying list view.
bool isClickToSelect(QObject *obj, QEvent *event) const

References isClickToExpand(), isClickToSelect(), m_box, and onClickedList().

Here is the call graph for this function:

◆ initEditor()

void MultiComboPropertyEditor::initEditor ( )
protectedvirtual

Inits editor widgets from m_data.

Reimplemented from CustomEditor.

Definition at line 143 of file MultiComboPropertyEditor.cpp.

144 {
145  if (!m_data.canConvert<ComboProperty>())
146  return;
147 
148  ComboProperty property = m_data.value<ComboProperty>();
149 
150  setConnected(false);
151  m_model->clear();
152 
153  auto labels = property.getValues();
154  auto selectedIndices = property.selectedIndices();
155 
156  for (int i = 0; i < labels.size(); ++i) {
157  auto item = new QStandardItem(labels[i]);
158  m_model->invisibleRootItem()->appendRow(item);
159  item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
160  item->setCheckable(true);
161 
162  auto state = selectedIndices.contains(i) ? Qt::Checked : Qt::Unchecked;
163  item->setData(state, Qt::CheckStateRole);
164  }
165 
166  setConnected(true);
167  updateBoxLabel();
168 }
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
void updateBoxLabel()
Update text on QComboBox with the label provided by combo property.
QVariant CheckStateRole(const SessionItem &item)
Returns check state for given item.

References SessionItemUtils::CheckStateRole(), CustomEditor::m_data, m_model, setConnected(), and updateBoxLabel().

Here is the call graph for this function:

◆ isClickToExpand()

bool MultiComboPropertyEditor::isClickToExpand ( QObject *  obj,
QEvent *  event 
) const
private

Definition at line 194 of file MultiComboPropertyEditor.cpp.

195 {
196  return obj == m_box->lineEdit() && event->type() == QEvent::MouseButtonRelease;
197 }

References m_box.

Referenced by eventFilter().

◆ isClickToSelect()

bool MultiComboPropertyEditor::isClickToSelect ( QObject *  obj,
QEvent *  event 
) const
private

Definition at line 189 of file MultiComboPropertyEditor.cpp.

190 {
191  return obj == m_box->view()->viewport() && event->type() == QEvent::MouseButtonRelease;
192 }

References m_box.

Referenced by eventFilter().

◆ minimumSizeHint()

QSize MultiComboPropertyEditor::minimumSizeHint ( ) const

Definition at line 82 of file MultiComboPropertyEditor.cpp.

83 {
84  return m_box->minimumSizeHint();
85 }

References m_box.

◆ onClickedList

void MultiComboPropertyEditor::onClickedList ( const QModelIndex &  index)
protectedslot

Processes press event in QComboBox's underlying list view.

Definition at line 110 of file MultiComboPropertyEditor.cpp.

111 {
112  if (auto item = m_model->itemFromIndex(index)) {
113  auto state = item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked;
114  item->setCheckState(state);
115  }
116 }

References m_model.

Referenced by eventFilter().

◆ onModelDataChanged

void MultiComboPropertyEditor::onModelDataChanged ( const QModelIndex &  topLeft,
const QModelIndex &  ,
const QVector< int > &   
)
protectedslot

Propagate check state from the model to ComboProperty.

Definition at line 89 of file MultiComboPropertyEditor.cpp.

91 {
92  // on Qt 5.9 roles remains empty for checked state. It will stop working if uncomment.
93  // if (!roles.contains(Qt::CheckStateRole))
94  // return;
95 
96  auto item = m_model->itemFromIndex(topLeft);
97  if (!item)
98  return;
99 
100  ComboProperty comboProperty = m_data.value<ComboProperty>();
101  auto state = item->checkState() == Qt::Checked;
102  comboProperty.setSelected(topLeft.row(), state);
103 
104  updateBoxLabel();
105  setDataIntern(QVariant::fromValue<ComboProperty>(comboProperty));
106 }
void setSelected(int index, bool value=true)
Sets given index selection flag.
void setDataIntern(const QVariant &data)
Saves the data from the editor and informs external delegates.

References CustomEditor::m_data, m_model, CustomEditor::setDataIntern(), ComboProperty::setSelected(), and updateBoxLabel().

Referenced by setConnected().

Here is the call graph for this function:

◆ setConnected()

void MultiComboPropertyEditor::setConnected ( bool  isConnected)
private

Definition at line 170 of file MultiComboPropertyEditor.cpp.

171 {
172  if (isConnected) {
173  connect(m_model, &QStandardItemModel::dataChanged, this,
175  } else {
176  disconnect(m_model, &QStandardItemModel::dataChanged, this,
178  }
179 }
void onModelDataChanged(const QModelIndex &, const QModelIndex &, const QVector< int > &)
Propagate check state from the model to ComboProperty.

References m_model, and onModelDataChanged().

Referenced by MultiComboPropertyEditor(), and initEditor().

Here is the call graph for this function:

◆ setData

void CustomEditor::setData ( const QVariant &  data)
slotinherited

Sets the data from the model to editor.

Definition at line 48 of file CustomEditors.cpp.

49 {
50  m_data = data;
51  initEditor();
52 }
virtual void initEditor()
Inits editor widgets from m_data.

References CustomEditor::initEditor(), and CustomEditor::m_data.

Here is the call graph for this function:

◆ setDataIntern()

void CustomEditor::setDataIntern ( const QVariant &  data)
protectedinherited

Saves the data from the editor and informs external delegates.

Definition at line 60 of file CustomEditors.cpp.

61 {
62  m_data = data;
64 }
void dataChanged(const QVariant &data)
Signal emit then user changed the data through the editor.

References CustomEditor::dataChanged(), and CustomEditor::m_data.

Referenced by ExternalPropertyEditor::buttonClicked(), BoolEditor::onCheckBoxChange(), ScientificDoublePropertyEditor::onEditingFinished(), DoubleEditor::onEditingFinished(), ScientificSpinBoxEditor::onEditingFinished(), IntEditor::onEditingFinished(), ComboPropertyEditor::onIndexChanged(), and onModelDataChanged().

◆ sizeHint()

QSize MultiComboPropertyEditor::sizeHint ( ) const

Definition at line 77 of file MultiComboPropertyEditor.cpp.

78 {
79  return m_box->sizeHint();
80 }

References m_box.

◆ updateBoxLabel()

void MultiComboPropertyEditor::updateBoxLabel ( )
private

Update text on QComboBox with the label provided by combo property.

Definition at line 183 of file MultiComboPropertyEditor.cpp.

184 {
185  ComboProperty combo = m_data.value<ComboProperty>();
186  m_box->setCurrentText(combo.label());
187 }
QString label() const
Returns the label to show.

References ComboProperty::label(), m_box, and CustomEditor::m_data.

Referenced by initEditor(), and onModelDataChanged().

Here is the call graph for this function:

Member Data Documentation

◆ m_box

QComboBox* MultiComboPropertyEditor::m_box
private

◆ m_data

◆ m_model

QStandardItemModel* MultiComboPropertyEditor::m_model
private

◆ m_wheel_event_filter

class WheelEventEater* MultiComboPropertyEditor::m_wheel_event_filter
private

Definition at line 53 of file MultiComboPropertyEditor.h.

Referenced by MultiComboPropertyEditor().


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