LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
categoryselector.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "categoryselector.h"
10#include <algorithm>
11#include <QStringList>
12#include <QCheckBox>
13#include <QVariant>
14#include <QVBoxLayout>
15#include <QMoveEvent>
16#include <QApplication>
17#include <QDesktopWidget>
18#include <QAction>
19#include <QtDebug>
20#include "ui_categoryselector.h"
21#include "util.h"
22
23namespace LC::Util
24{
25 const int RoleTag = Qt::UserRole;
26
28 : QDialog (parent)
29 , Ui_ (new Ui::CategorySelector)
30 , Separator_ (GetDefaultTagsSeparator ())
31 {
32 setWindowTitle (tr ("Tags selector"));
33 setWindowFlags (Qt::Dialog | Qt::WindowStaysOnTopHint);
34
35 Ui_->setupUi (this);
36
37 Ui_->Tree_->setRootIsDecorated (false);
38 Ui_->Tree_->setUniformRowHeights (true);
39
40 QRect avail = QApplication::desktop ()->availableGeometry (this);
41 setMinimumHeight (avail.height () / 3 * 2);
42
43 connect (Ui_->Tree_,
44 &QTreeWidget::itemChanged,
45 this,
46 &CategorySelector::NotifyTagsSelection);
47
48 const auto all = new QAction (tr ("Select all"), this);
49 connect (all,
50 &QAction::triggered,
51 this,
53
54 const auto none = new QAction (tr ("Select none"), this);
55 connect (none,
56 &QAction::triggered,
57 this,
59
60 Ui_->Tree_->addAction (all);
61 Ui_->Tree_->addAction (none);
62
63 Ui_->Tree_->setContextMenuPolicy (Qt::ActionsContextMenu);
64
66 }
67
68 void CategorySelector::SetCaption (const QString& caption)
69 {
70 Ui_->Tree_->setHeaderLabel (caption);
71 Caption_ = caption;
72 }
73
74 void CategorySelector::SetPossibleSelections (QStringList mytags, bool sort)
75 {
76 auto guard = DisableNotifications ();
77
78 Ui_->Tree_->clear ();
79
80 if (sort)
81 mytags.sort ();
82
84 for (const auto& tag : mytags)
85 {
86 if (tag.isEmpty ())
87 continue;
88
89 auto item = new QTreeWidgetItem ({ tag });
90 item->setCheckState (0, Qt::Unchecked);
91 item->setData (0, RoleTag, tag);
92 items << item;
93 }
94 Ui_->Tree_->addTopLevelItems (items);
95
96 Ui_->Tree_->setHeaderLabel (Caption_);
97 }
98
100 {
101 QStringList tags;
102
103 for (int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
104 {
105 const auto item = Ui_->Tree_->topLevelItem (i);
106 if (item->checkState (0) == Qt::Checked)
107 tags += item->data (0, RoleTag).toString ();
108 }
109
110 return tags;
111 }
112
114 {
115 QList<int> result;
116
117 for (int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
118 {
119 const auto item = Ui_->Tree_->topLevelItem (i);
120 if (item->checkState (0) == Qt::Checked)
121 result << i;
122 }
123
124 return result;
125 }
126
127 void CategorySelector::SetSelections (const QStringList& tags)
128 {
129 auto guard = DisableNotifications (false);
130
131 for (int i = 0; i < Ui_->Tree_->topLevelItemCount (); ++i)
132 {
133 const auto& tagVar = Ui_->Tree_->topLevelItem (i)->data (0, RoleTag);
134 const auto state = tags.contains (tagVar.toString ()) ?
135 Qt::Checked :
136 Qt::Unchecked;
137 Ui_->Tree_->topLevelItem (i)->setCheckState (0, state);
138 }
139 }
140
142 {
143 return Separator_;
144 }
145
146 void CategorySelector::SetSeparator (const QString& sep)
147 {
148 Separator_ = sep;
149 }
150
152 {
153 switch (mode)
154 {
156 Ui_->ButtonsBox_->setVisible (false);
157 break;
159 Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Close);
160 Ui_->ButtonsBox_->setVisible (true);
161 break;
163 Ui_->ButtonsBox_->setStandardButtons (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
164 Ui_->ButtonsBox_->setVisible (true);
165 break;
166 }
167 }
168
169 void CategorySelector::moveEvent (QMoveEvent *e)
170 {
171 QWidget::moveEvent (e);
172 QPoint pos = e->pos ();
173 QRect avail = QApplication::desktop ()->availableGeometry (this);
174 int dx = 0, dy = 0;
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 ();
180
181 if (dx || dy)
182 move (pos - QPoint (dx, dy));
183 }
184
186 {
187 auto guard = DisableNotifications ();
188
189 for (int i = 0, size = Ui_->Tree_->topLevelItemCount (); i < size; ++i)
190 Ui_->Tree_->topLevelItem (i)->setCheckState (0, Qt::Checked);
191 }
192
194 {
195 auto guard = DisableNotifications ();
196
197 for (int i = 0; i < Ui_->Tree_->topLevelItemCount (); ++i)
198 Ui_->Tree_->topLevelItem (i)->setCheckState (0, Qt::Unchecked);
199 }
200
202 {
203 auto guard = DisableNotifications (false);
204 SetSelections (text.split (Separator_, Qt::SkipEmptyParts));
205 }
206
207 void CategorySelector::NotifyTagsSelection ()
208 {
209 if (NotificationsEnabled_)
211 }
212
213 DefaultScopeGuard CategorySelector::DisableNotifications (bool reemit)
214 {
215 auto prevValue = NotificationsEnabled_;
216 NotificationsEnabled_ = false;
217 return MakeScopeGuard ([this, prevValue, reemit]
218 {
219 NotificationsEnabled_ = prevValue;
220 if (reemit)
221 NotifyTagsSelection ();
222 });
223 }
224}
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.
const int RoleTag
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:136
detail::ScopeGuard< detail::DefaultScopeGuardDeleter > DefaultScopeGuard
Definition: util.h:113
QString GetDefaultTagsSeparator()
Definition: util.cpp:14