21 #include <QMouseEvent> 24 #include <QAbstractItemView> 28 : QStandardItemModel( 0, 1, parent )
34 return QStandardItemModel::flags( index ) | Qt::ItemIsUserCheckable;
39 QVariant value = QStandardItemModel::data( index, role );
41 if ( index.isValid() && role == Qt::CheckStateRole && !value.isValid() )
43 value = Qt::Unchecked;
51 bool ok = QStandardItemModel::setData( index, value, role );
53 if ( ok && role == Qt::CheckStateRole )
55 emit dataChanged( index, index );
64 : QStyledItemDelegate( parent )
70 QStyleOptionViewItem &nonConstOpt =
const_cast<QStyleOptionViewItem &
>( option );
71 nonConstOpt.showDecorationSelected =
false;
72 QStyledItemDelegate::paint( painter, nonConstOpt, index );
78 , mSeparator( QStringLiteral(
", " ) )
83 QLineEdit *lineEdit =
new QLineEdit(
this );
84 lineEdit->setReadOnly(
true );
85 setLineEdit( lineEdit );
87 mContextMenu =
new QMenu(
this );
88 mSelectAllAction = mContextMenu->addAction( tr(
"Select all" ) );
89 mDeselectAllAction = mContextMenu->addAction( tr(
"Deselect all" ) );
93 view()->viewport()->installEventFilter(
this );
94 view()->setContextMenuPolicy( Qt::CustomContextMenu );
99 connect( model(), &QStandardItemModel::rowsInserted,
this, [ = ](
const QModelIndex &,
int,
int ) { updateCheckedItems(); } );
100 connect( model(), &QStandardItemModel::rowsRemoved,
this, [ = ](
const QModelIndex &,
int,
int ) { updateCheckedItems(); } );
112 if ( mSeparator != separator )
126 if ( mDefaultText != text )
139 QModelIndex index = model()->index( 0, modelColumn(), rootModelIndex() );
140 QModelIndexList indexes = model()->match( index, Qt::CheckStateRole, Qt::Checked, -1, Qt::MatchExactly );
141 Q_FOREACH (
const QModelIndex &index, indexes )
143 items += index.data().toString();
152 return static_cast<Qt::CheckState
>( itemData( index, Qt::CheckStateRole ).toInt() );
157 setItemData( index, state, Qt::CheckStateRole );
162 QVariant value = itemData( index, Qt::CheckStateRole );
163 if ( value.isValid() )
165 Qt::CheckState state =
static_cast<Qt::CheckState
>( value.toInt() );
166 setItemData( index, ( state == Qt::Unchecked ? Qt::Checked : Qt::Unchecked ), Qt::CheckStateRole );
174 QComboBox::hidePopup();
183 mContextMenu->exec( QCursor::pos() );
188 blockSignals(
true );
189 for (
int i = 0; i < count(); i++ )
191 setItemData( i, Qt::Checked, Qt::CheckStateRole );
193 blockSignals(
false );
194 updateCheckedItems();
199 blockSignals(
true );
200 for (
int i = 0; i < count(); i++ )
202 setItemData( i, Qt::Unchecked, Qt::CheckStateRole );
204 blockSignals(
false );
205 updateCheckedItems();
210 if ( ( event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease )
211 &&
object == view()->viewport() )
216 if ( event->type() == QEvent::MouseButtonRelease )
218 if ( static_cast<QMouseEvent *>( event )->button() == Qt::RightButton )
228 Q_FOREACH (
const QString &text, items )
230 const int index = findText( text );
237 QComboBox::resizeEvent( event );
241 void QgsCheckableComboBox::updateCheckedItems()
248 void QgsCheckableComboBox::updateDisplayText()
252 if ( items.isEmpty() )
258 text = items.join( mSeparator );
261 QRect rect = lineEdit()->rect();
262 QFontMetrics fontMetrics( font() );
263 text = fontMetrics.elidedText( text, Qt::ElideRight, rect.width() );
void setDefaultText(const QString &text)
Set default text which will be displayed in the widget when no items selected.
QgsCheckableItemModel(QObject *parent=nullptr)
Constructor for QgsCheckableItemModel.
void selectAllOptions()
Selects all items.
void setCheckedItems(const QStringList &items)
Set items which should be checked/selected.
QStyledItemDelegate subclass for QgsCheckableComboBox.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Sets the role data for the item at index to value.
void showContextMenu(QPoint pos)
Display context menu which allows selecting/deselecting all items at once.
bool eventFilter(QObject *object, QEvent *event) override
Filters events to enable context menu.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Returns the data stored under the given role for the item referred to by the index.
QString separator() const
Returns separator used to separate items in the display text.
void deselectAllOptions()
Removes selection from all items.
QString defaultText() const
Returns default text which will be displayed in the widget when no items selected.
void hidePopup() override
Hides the list of items in the combobox if it is currently visible and resets the internal state...
QgsCheckableComboBox(QWidget *parent=nullptr)
Constructor for QgsCheckableComboBox.
void toggleItemCheckState(int index)
Toggles the item check state.
void setItemCheckState(int index, Qt::CheckState state)
Sets the item check state to state.
void itemCheckStateChanged()
This signal is emitted whenever the items checkstate has changed.
void resizeEvent(QResizeEvent *event) override
Handler for widget resizing.
Qt::CheckState itemCheckState(int index) const
Returns the checked state of the item identified by index.
QStringList checkedItems() const
Returns currently checked items.
Qt::ItemFlags flags(const QModelIndex &index) const override
Returns a combination of the item flags: items are enabled (ItemIsEnabled), selectable (ItemIsSelecta...
void setSeparator(const QString &separator)
Set separator used to separate items in the display text.
void checkedItemsChanged(const QStringList &items)
This signal is emitted whenever the checked items list changed.
QStandardItemModel subclass which makes all items checkable by default.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Renders the delegate using the given painter and style option for the item specified by index...
QgsCheckBoxDelegate(QObject *parent=nullptr)
Constructor for QgsCheckBoxDelegate.