12#include <QSortFilterProxyModel>
21 QSet<QString> Selected_;
25 : QSortFilterProxyModel { &selector }
26 , Selector_ { selector }
28 setSortCaseSensitivity (Qt::CaseInsensitive);
32 QVariant
headerData (
int section, Qt::Orientation orientation,
int role)
const override
34 if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section)
40 Qt::ItemFlags
flags (
const QModelIndex& index)
const override
42 const auto flags = QSortFilterProxyModel::flags (index);
43 if (!index.isValid () || index.column ())
46 return (
flags & ~Qt::ItemIsEditable) | Qt::ItemIsUserCheckable;
49 QVariant
data (
const QModelIndex& index,
int role)
const override
51 if (role != Qt::CheckStateRole)
52 return QSortFilterProxyModel::data (index, role);
54 if (!index.isValid () || index.column ())
57 return Selected_.contains (Name (index)) ? Qt::Checked : Qt::Unchecked;
60 bool setData (
const QModelIndex& index,
const QVariant& value,
int role)
override
62 if (role != Qt::CheckStateRole || !index.isValid () || index.column ())
65 const auto& name = Name (index);
66 if (value.value<Qt::CheckState> () == Qt::Checked)
69 Selected_.remove (name);
70 emit dataChanged (index, index, { Qt::CheckStateRole });
72 emit Selector_.selectionEdited (Selector_.GetSelections ());
79 for (
int row = 0; row < rowCount (); ++row)
80 if (
const auto& name = Name (index (row, 0)); Selected_.contains (name))
87 QModelIndexList result;
88 for (
int row = 0; row < rowCount (); ++row)
89 if (
const auto& idx = index (row, 0); Selected_.contains (Name (idx)))
90 result << mapToSource (idx);
96 Selected_ = { names.begin (), names.end () };
103 for (
int row = 0; row < rowCount (); ++row)
104 Selected_ << Name (index (row, 0));
116 if (caption == Caption_)
120 emit headerDataChanged (Qt::Horizontal, 0, 0);
123 QString Name (
const QModelIndex& index)
const
125 return index.siblingAtColumn (0).data ().toString ();
130 if (
const auto rows = rowCount ())
131 emit dataChanged (index (0, 0), index (rows - 1, 0), { Qt::CheckStateRole });
138 , View_ { *new QTreeView { this } }
140 View_.setModel (&Model_);
141 View_.setUniformRowHeights (
true);
142 View_.setRootIsDecorated (
false);
143 View_.setHeaderHidden (
true);
144 View_.setContextMenuPolicy (Qt::ActionsContextMenu);
146 const auto all =
new QAction { tr (
"Select all"),
this };
147 all->setProperty (
"ActionIcon",
"edit-select-all");
157 const auto none =
new QAction { tr (
"Select none"),
this };
158 none->setProperty (
"ActionIcon",
"edit-select-none");
164 Model_.SelectNone ();
168 View_.addActions ({ all, none });
170 const auto layout =
new QVBoxLayout {
this };
171 layout->setContentsMargins (0, 0, 0, 0);
172 layout->addWidget (&View_);
177 Model_.setSourceModel (&model);
182 Model_.SetCaption (caption);
183 View_.setHeaderHidden (caption.isEmpty ());
188 return Model_.Selections ();
193 return Model_.SelectedIndexes ();
198 Model_.SetSelected (names);
208 Model_.SelectNone ();
QModelIndexList SelectedIndexes() const
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Qt::ItemFlags flags(const QModelIndex &index) const override
void SetSelected(const QStringList &names)
void SetCaption(const QString &caption)
CheckableModel(CategorySelector &selector)
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QStringList Selections() const
QVariant data(const QModelIndex &index, int role) const override
void selectionEdited(const QStringList &selected)
Emitted after the user has changed the selection, with GetSelections().
QStringList GetSelections() const
Returns the selected names present in the model.
void SetModel(QAbstractItemModel &model)
Lists the names of model, which is not owned.
CategorySelector(QWidget *parent=nullptr)
void SelectNone()
Clears the selection.
void SetCaption(const QString &caption)
Sets the header above the list; there is none by default.
void SetSelections(const QStringList &names)
Replaces the selection.
void SelectAll()
Selects exactly the names in the model.
QModelIndexList GetSelectedIndexes() const
Returns the model's indexes of the selected names.