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 const auto constIndexes = indexes;
142 for (
const QModelIndex &index : constIndexes )
144 items += index.data().toString();
153 return static_cast<Qt::CheckState
>( itemData( index, Qt::CheckStateRole ).toInt() );
158 setItemData( index, state, Qt::CheckStateRole );
163 QVariant value = itemData( index, Qt::CheckStateRole );
164 if ( value.isValid() )
166 Qt::CheckState state =
static_cast<Qt::CheckState
>( value.toInt() );
167 setItemData( index, ( state == Qt::Unchecked ? Qt::Checked : Qt::Unchecked ), Qt::CheckStateRole );
175 QComboBox::hidePopup();
184 mContextMenu->exec( QCursor::pos() );
189 blockSignals(
true );
190 for (
int i = 0; i < count(); i++ )
192 setItemData( i, Qt::Checked, Qt::CheckStateRole );
194 blockSignals(
false );
195 updateCheckedItems();
200 blockSignals(
true );
201 for (
int i = 0; i < count(); i++ )
203 setItemData( i, Qt::Unchecked, Qt::CheckStateRole );
205 blockSignals(
false );
206 updateCheckedItems();
211 if ( ( event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease )
212 &&
object == view()->viewport() )
217 if ( event->type() == QEvent::MouseButtonRelease )
219 if ( static_cast<QMouseEvent *>( event )->button() == Qt::RightButton )
224 return QComboBox::eventFilter(
object, event );
229 const auto constItems = items;
230 for (
const QString &text : constItems )
232 const int index = findText( text );
239 QComboBox::resizeEvent( event );
243 void QgsCheckableComboBox::updateCheckedItems()
250 void QgsCheckableComboBox::updateDisplayText()
254 if ( items.isEmpty() )
260 text = items.join( mSeparator );
263 QRect rect = lineEdit()->rect();
264 QFontMetrics fontMetrics( font() );
265 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()
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)
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.