BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ModelView::SelectableComboBoxEditor Class Reference

Adds multi-selection capabilities to QComboBox. More...

Inheritance diagram for ModelView::SelectableComboBoxEditor:
[legend]
Collaboration diagram for ModelView::SelectableComboBoxEditor:
[legend]

Public Slots

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

Signals

void dataChanged (QVariant value)
 Emmits signal when data was changed in an editor. More...
 

Public Member Functions

 SelectableComboBoxEditor (QWidget *parent=nullptr)
 
QVariant data () const
 
bool is_persistent () const override
 Returns true if editor should remains alive after editing finished. More...
 
QSize minimumSizeHint () const override
 
QSize sizeHint () const override
 

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 setDataIntern (const QVariant &data)
 Saves the data as given by editor's internal components and notifies the model. More...
 
void update_components () override
 Should update widget components from m_data, if necessary. More...
 

Protected Attributes

QVariant m_data
 

Properties

QVariant value
 

Private Member Functions

bool eventFilter (QObject *obj, QEvent *event) override
 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 {nullptr}
 
QStandardItemModel * m_model {nullptr}
 
WheelEventFilterm_wheelEventFilter {nullptr}
 

Detailed Description

Adds multi-selection capabilities to QComboBox.

Definition at line 29 of file selectablecomboboxeditor.h.

Constructor & Destructor Documentation

◆ SelectableComboBoxEditor()

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

Definition at line 53 of file selectablecomboboxeditor.cpp.

54  : CustomEditor(parent)
55  , m_box(new QComboBox)
57  , m_model(new QStandardItemModel(this))
58 {
59  setAutoFillBackground(true);
60  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
61 
62  m_box->installEventFilter(m_wheelEventFilter);
63  m_box->view()->viewport()->installEventFilter(this);
64 
65  // Editable mode will be used to have None/Multiple labels on top
66  m_box->setEditable(true);
67  m_box->lineEdit()->setReadOnly(true);
68  m_box->lineEdit()->installEventFilter(this);
69  connect(m_box->lineEdit(), &QLineEdit::selectionChanged, m_box->lineEdit(),
70  &QLineEdit::deselect);
71 
72  // transforms ordinary combo box into check list
73  m_box->setItemDelegate(new QCheckListStyledItemDelegate(this));
74  m_box->setModel(m_model);
75 
76  auto layout = new QVBoxLayout;
77  layout->setMargin(0);
78  layout->setSpacing(0);
79  layout->addWidget(m_box);
80  setLayout(layout);
81  setConnected(true);
82 }
CustomEditor(QWidget *parent=nullptr)
Event filter to install on combo boxes and spin boxes to ignore wheel events during scrolling.
Provides custom style delegate for QComboBox to allow checkboxes.

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

Here is the call graph for this function:

Member Function Documentation

◆ data()

QVariant CustomEditor::data ( ) const
inherited

Definition at line 21 of file customeditor.cpp.

22 {
23  return m_data;
24 }

References ModelView::CustomEditor::m_data.

Referenced by ModelView::CustomEditor::setData(), and ModelView::CustomEditor::setDataIntern().

◆ dataChanged

void ModelView::CustomEditor::dataChanged ( QVariant  value)
signalinherited

◆ eventFilter()

bool SelectableComboBoxEditor::eventFilter ( QObject *  obj,
QEvent *  event 
)
overrideprivate

Handles mouse clicks on QComboBox elements.

Definition at line 134 of file selectablecomboboxeditor.cpp.

135 {
136  if (isClickToSelect(obj, event)) {
137  // Handles mouse clicks on QListView when it is expanded from QComboBox
138  // 1) Prevents list from closing while selecting items.
139  // 2) Correctly calculates underlying model index when mouse is over check box style
140  // element.
141  const auto mouseEvent = static_cast<const QMouseEvent*>(event);
142  auto index = m_box->view()->indexAt(mouseEvent->pos());
143  onClickedList(index);
144  return true;
145 
146  } else if (isClickToExpand(obj, event)) {
147  // Expands box when clicking on None/Multiple label
148  m_box->showPopup();
149  return true;
150 
151  } else {
152  // Propagate to the parent class.
153  return QObject::eventFilter(obj, event);
154  }
155 }
bool isClickToSelect(QObject *obj, QEvent *event) const
bool isClickToExpand(QObject *obj, QEvent *event) const
void onClickedList(const QModelIndex &index)
Processes press event in QComboBox's underlying list view.

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

Here is the call graph for this function:

◆ is_persistent()

bool SelectableComboBoxEditor::is_persistent ( ) const
overridevirtual

Returns true if editor should remains alive after editing finished.

Reimplemented from ModelView::CustomEditor.

Definition at line 94 of file selectablecomboboxeditor.cpp.

95 {
96  return true;
97 }

◆ isClickToExpand()

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

Definition at line 208 of file selectablecomboboxeditor.cpp.

209 {
210  return obj == m_box->lineEdit() && event->type() == QEvent::MouseButtonRelease;
211 }

References m_box.

Referenced by eventFilter().

◆ isClickToSelect()

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

Definition at line 203 of file selectablecomboboxeditor.cpp.

204 {
205  return obj == m_box->view()->viewport() && event->type() == QEvent::MouseButtonRelease;
206 }

References m_box.

Referenced by eventFilter().

◆ minimumSizeHint()

QSize SelectableComboBoxEditor::minimumSizeHint ( ) const
override

Definition at line 89 of file selectablecomboboxeditor.cpp.

90 {
91  return m_box->minimumSizeHint();
92 }

References m_box.

◆ onClickedList

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

Processes press event in QComboBox's underlying list view.

Definition at line 124 of file selectablecomboboxeditor.cpp.

125 {
126  if (auto item = m_model->itemFromIndex(index)) {
127  auto state = item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked;
128  item->setCheckState(state);
129  }
130 }

References m_model.

Referenced by eventFilter().

◆ onModelDataChanged

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

Propagate check state from the model to ComboProperty.

Definition at line 101 of file selectablecomboboxeditor.cpp.

103 {
104 #if QT_VERSION > QT_VERSION_CHECK(5, 9, 0)
105  // for older versions this role is always empty
106  if (!roles.contains(Qt::CheckStateRole))
107  return;
108 #endif
109 
110  auto item = m_model->itemFromIndex(topLeft);
111  if (!item)
112  return;
113 
114  ComboProperty comboProperty = m_data.value<ComboProperty>();
115  auto state = item->checkState() == Qt::Checked ? true : false;
116  comboProperty.setSelected(topLeft.row(), state);
117 
118  updateBoxLabel();
119  setDataIntern(QVariant::fromValue<ComboProperty>(comboProperty));
120 }
Custom property to define list of string values with multiple selections.
Definition: comboproperty.h:27
void setSelected(int index, bool value=true)
Sets given index selection flag.
void setDataIntern(const QVariant &data)
Saves the data as given by editor's internal components and notifies the model.
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(), ModelView::CustomEditor::m_data, m_model, ModelView::CustomEditor::setDataIntern(), ModelView::ComboProperty::setSelected(), and updateBoxLabel().

Referenced by setConnected().

Here is the call graph for this function:

◆ setConnected()

void SelectableComboBoxEditor::setConnected ( bool  isConnected)
private

Definition at line 184 of file selectablecomboboxeditor.cpp.

185 {
186  if (isConnected) {
187  connect(m_model, &QStandardItemModel::dataChanged, this,
189  } else {
190  disconnect(m_model, &QStandardItemModel::dataChanged, this,
192  }
193 }
void onModelDataChanged(const QModelIndex &, const QModelIndex &, const QVector< int > &)
Propagate check state from the model to ComboProperty.

References m_model, and onModelDataChanged().

Referenced by SelectableComboBoxEditor(), and update_components().

Here is the call graph for this function:

◆ setData

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

Sets the data from model to editor.

Definition at line 35 of file customeditor.cpp.

36 {
37  m_data = data;
39 }
QVariant data() const
virtual void update_components()=0
Should update widget components from m_data, if necessary.

References ModelView::CustomEditor::data(), ModelView::CustomEditor::m_data, and ModelView::CustomEditor::update_components().

Here is the call graph for this function:

◆ setDataIntern()

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

Saves the data as given by editor's internal components and notifies the model.

Definition at line 43 of file customeditor.cpp.

44 {
45  m_data = data;
47 }
void dataChanged(QVariant value)
Emmits signal when data was changed in an editor.

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

Referenced by ModelView::ColorEditor::mousePressEvent(), ModelView::BoolEditor::onCheckBoxChange(), ModelView::DoubleEditor::onEditingFinished(), ModelView::IntegerEditor::onEditingFinished(), ModelView::ScientificDoubleEditor::onEditingFinished(), ModelView::ScientificSpinBoxEditor::onEditingFinished(), ModelView::ComboPropertyEditor::onIndexChanged(), ModelView::ExternalPropertyComboEditor::onIndexChanged(), and onModelDataChanged().

Here is the call graph for this function:

◆ sizeHint()

QSize SelectableComboBoxEditor::sizeHint ( ) const
override

Definition at line 84 of file selectablecomboboxeditor.cpp.

85 {
86  return m_box->sizeHint();
87 }

References m_box.

◆ update_components()

void SelectableComboBoxEditor::update_components ( )
overrideprotectedvirtual

Should update widget components from m_data, if necessary.

Implements ModelView::CustomEditor.

Definition at line 157 of file selectablecomboboxeditor.cpp.

158 {
159  if (!m_data.canConvert<ComboProperty>())
160  return;
161 
162  ComboProperty property = m_data.value<ComboProperty>();
163 
164  setConnected(false);
165  m_model->clear();
166 
167  auto labels = property.values();
168  auto selectedIndices = property.selectedIndices();
169 
170  for (size_t i = 0; i < labels.size(); ++i) {
171  auto item = new QStandardItem(QString::fromStdString(labels[i]));
172  m_model->invisibleRootItem()->appendRow(item);
173  item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
174  item->setCheckable(true);
175 
176  auto state = Utils::Contains(selectedIndices, i) ? Qt::Checked : Qt::Unchecked;
177  item->setData(state, Qt::CheckStateRole);
178  }
179 
180  setConnected(true);
181  updateBoxLabel();
182 }
bool Contains(const A &container, const B &element)
Returns true if container contains a given element.

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

Here is the call graph for this function:

◆ updateBoxLabel()

void SelectableComboBoxEditor::updateBoxLabel ( )
private

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

Definition at line 197 of file selectablecomboboxeditor.cpp.

198 {
199  ComboProperty combo = m_data.value<ComboProperty>();
200  m_box->setCurrentText(QString::fromStdString(combo.label()));
201 }
std::string label() const
Returns the label to show.

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

Referenced by onModelDataChanged(), and update_components().

Here is the call graph for this function:

Member Data Documentation

◆ m_box

QComboBox* ModelView::SelectableComboBoxEditor::m_box {nullptr}
private

◆ m_data

◆ m_model

QStandardItemModel* ModelView::SelectableComboBoxEditor::m_model {nullptr}
private

◆ m_wheelEventFilter

WheelEventFilter* ModelView::SelectableComboBoxEditor::m_wheelEventFilter {nullptr}
private

Definition at line 57 of file selectablecomboboxeditor.h.

Referenced by SelectableComboBoxEditor().

Property Documentation

◆ value

QVariant ModelView::CustomEditor::value
readwriteinherited

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