16#include <QStringListModel>
19#include "ui_categoryselector.h"
27 QSet<int> SelectedRows_;
32 : QStringListModel { &selector }
33 , Selector_ { selector }
37 QVariant
headerData (
int section, Qt::Orientation orientation,
int role)
const override
39 if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section)
45 Qt::ItemFlags
flags (
const QModelIndex& index)
const override
47 return (QStringListModel::flags (index) & ~Qt::ItemIsEditable) | Qt::ItemIsUserCheckable;
50 QVariant
data (
const QModelIndex& index,
int role)
const override
52 if (role == Qt::CheckStateRole)
53 return SelectedRows_.contains (index.row ()) ? Qt::Checked : Qt::Unchecked;
55 return QStringListModel::data (index, role);
58 bool setData (
const QModelIndex& index,
const QVariant& value,
int role)
override
60 if (role != Qt::CheckStateRole)
63 if (value.value<Qt::CheckState> () == Qt::Checked)
64 SelectedRows_ << index.row ();
66 SelectedRows_.remove (index.row ());
67 emit dataChanged (index, index, { Qt::CheckStateRole });
68 Selector_.NotifyTagsSelection ();
74 const int size = stringList ().size ();
78 SelectedRows_.reserve (size);
79 for (
int i = 0; i < size; ++i)
82 emit dataChanged (index (0), index (size - 1), { Qt::CheckStateRole });
84 Selector_.NotifyTagsSelection ();
89 const int size = stringList ().size ();
93 SelectedRows_.clear ();
94 emit dataChanged (index (0), index (size - 1), { Qt::CheckStateRole });
96 Selector_.NotifyTagsSelection ();
101 if (header == Header_)
104 Header_ = std::move (header);
105 emit headerDataChanged (Qt::Horizontal, 0, 0);
115 setWindowTitle (tr (
"Tags selector"));
116 setWindowFlags (Qt::Dialog | Qt::WindowStaysOnTopHint);
119 Ui_->Tree_->setModel (&Model_);
121 const auto& avail = screen ()->availableGeometry ();
122 setMinimumHeight (avail.height () / 3 * 2);
124 const auto all =
new QAction (tr (
"Select all"),
this);
125 all->setProperty (
"ActionIcon",
"edit-select-all");
131 const auto none =
new QAction (tr (
"Select none"),
this);
132 none->setProperty (
"ActionIcon",
"edit-select-none");
138 Ui_->Tree_->addAction (all);
139 Ui_->Tree_->addAction (none);
141 Ui_->Tree_->setContextMenuPolicy (Qt::ActionsContextMenu);
148 Model_.SetHeader (caption);
153 auto guard = DisableNotifications ();
157 Model_.setStringList (tags);
158 Model_.SelectNone ();
163 return Model_.stringList ();
168 const auto& allTags = Model_.stringList ();
170 QStringList selected;
171 selected.reserve (selectedIdxes.size ());
172 for (
const auto idx : selectedIdxes)
173 selected << allTags [idx];
181 const auto& rowCount = Model_.stringList ().size ();
182 for (
int i = 0; i < rowCount; ++i)
184 const auto state = Model_.index (i).data (Qt::CheckStateRole).value<Qt::CheckState> ();
185 if (state == Qt::Checked)
194 auto guard = DisableNotifications (
false);
196 const auto& allTags = Model_.stringList ();
197 const auto rowCount = allTags.size ();
198 for (
int i = 0; i < rowCount; ++i)
200 const auto state = tags.contains (allTags [i]) ?
203 Model_.setData (Model_.index (i), state, Qt::CheckStateRole);
222 Ui_->ButtonsBox_->setVisible (
false);
225 Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Close);
226 Ui_->ButtonsBox_->setVisible (
true);
229 Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
230 Ui_->ButtonsBox_->setVisible (
true);
237 QWidget::moveEvent (e);
238 QPoint pos = e->pos ();
239 QRect avail = screen ()->availableGeometry ();
241 if (pos.x () + width () > avail.width ())
242 dx = width () + pos.x () - avail.width ();
243 if (pos.y () + height () > avail.height () &&
244 height () < avail.height ())
245 dy = height () + pos.y () - avail.height ();
248 move (pos - QPoint (dx, dy));
258 Model_.SelectNone ();
263 auto guard = DisableNotifications (
false);
267 void CategorySelector::NotifyTagsSelection ()
269 if (NotificationsEnabled_)
275 auto prevValue = NotificationsEnabled_;
276 NotificationsEnabled_ =
false;
279 NotificationsEnabled_ = prevValue;
281 NotifyTagsSelection ();
void SetSelections(const QStringList &subset)
Selects some of the items.
void SetButtonsMode(ButtonsMode)
Sets the buttons mode.
QList< int > GetSelectedIndexes() const
Gets the indexes of the selected items.
QStringList GetSelections() const
Gets selected items.
CategorySelector(QWidget *parent=nullptr)
Constructor.
QStringList GetPossibleSelections() const
QString GetSeparator() const
Returns the separator for the tags.
void moveEvent(QMoveEvent *) override
Checks whether after the move event the selector won't be beoynd the screen. if it would,...
void SelectNone()
Deselects all variants.
void SetCaption(const QString &caption)
Sets the caption of this selector.
void SetSeparator(const QString &)
Sets the separator for the tags.
virtual void SetPossibleSelections(QStringList selections, bool sort=true)
Sets possible selections.
void SetSelectionsFromString(const QString &newText)
Notifies CategorySelector about logical selection changes.
void SelectAll()
Selects all variants.
void tagsSelectionChanged(const QStringList &newSelections)
Indicates that selections have changed.
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
QString GetDefaultTagsSeparator()
detail::ScopeGuard< detail::DefaultScopeGuardDeleter > DefaultScopeGuard