16#include <QApplication>
17#include <QDesktopWidget>
20#include "ui_categoryselector.h"
32 setWindowTitle (tr (
"Tags selector"));
33 setWindowFlags (Qt::Dialog | Qt::WindowStaysOnTopHint);
37 Ui_->Tree_->setRootIsDecorated (
false);
38 Ui_->Tree_->setUniformRowHeights (
true);
40 QRect avail = QApplication::desktop ()->availableGeometry (
this);
41 setMinimumHeight (avail.height () / 3 * 2);
44 &QTreeWidget::itemChanged,
46 &CategorySelector::NotifyTagsSelection);
48 const auto all =
new QAction (tr (
"Select all"),
this);
54 const auto none =
new QAction (tr (
"Select none"),
this);
60 Ui_->Tree_->addAction (all);
61 Ui_->Tree_->addAction (none);
63 Ui_->Tree_->setContextMenuPolicy (Qt::ActionsContextMenu);
70 Ui_->Tree_->setHeaderLabel (caption);
76 auto guard = DisableNotifications ();
84 for (
const auto& tag : mytags)
89 auto item =
new QTreeWidgetItem ({ tag });
90 item->setCheckState (0, Qt::Unchecked);
91 item->setData (0,
RoleTag, tag);
94 Ui_->Tree_->addTopLevelItems (items);
96 Ui_->Tree_->setHeaderLabel (Caption_);
103 for (
int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
105 const auto item = Ui_->Tree_->topLevelItem (i);
106 if (item->checkState (0) == Qt::Checked)
107 tags += item->data (0,
RoleTag).toString ();
117 for (
int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
119 const auto item = Ui_->Tree_->topLevelItem (i);
120 if (item->checkState (0) == Qt::Checked)
129 auto guard = DisableNotifications (
false);
131 for (
int i = 0; i < Ui_->Tree_->topLevelItemCount (); ++i)
133 const auto& tagVar = Ui_->Tree_->topLevelItem (i)->data (0,
RoleTag);
134 const auto state = tags.contains (tagVar.toString ()) ?
137 Ui_->Tree_->topLevelItem (i)->setCheckState (0, state);
156 Ui_->ButtonsBox_->setVisible (
false);
159 Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Close);
160 Ui_->ButtonsBox_->setVisible (
true);
163 Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
164 Ui_->ButtonsBox_->setVisible (
true);
171 QWidget::moveEvent (e);
172 QPoint pos = e->pos ();
173 QRect avail = QApplication::desktop ()->availableGeometry (
this);
175 if (pos.x () + width () > avail.width ())
176 dx = width () + pos.x () - avail.width ();
177 if (pos.y () + height () > avail.height () &&
178 height () < avail.height ())
179 dy = height () + pos.y () - avail.height ();
182 move (pos - QPoint (dx, dy));
187 auto guard = DisableNotifications ();
189 for (
int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
190 Ui_->Tree_->topLevelItem (i)->setCheckState (0, Qt::Checked);
195 auto guard = DisableNotifications ();
197 for (
int i = 0; i < Ui_->Tree_->topLevelItemCount (); ++i)
198 Ui_->Tree_->topLevelItem (i)->setCheckState (0, Qt::Unchecked);
203 auto guard = DisableNotifications (
false);
207 void CategorySelector::NotifyTagsSelection ()
209 if (NotificationsEnabled_)
215 auto prevValue = NotificationsEnabled_;
216 NotificationsEnabled_ =
false;
219 NotificationsEnabled_ = prevValue;
221 NotifyTagsSelection ();
The CategorySelector widget provides a way to select amongst a group of items.
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.
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.
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.
detail::ScopeGuard< detail::DefaultScopeGuardDeleter > DefaultScopeGuard
QString GetDefaultTagsSeparator()