Debian Codegraph @

src/librssguard/gui/toolbars/feedstoolbar.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "gui/toolbars/feedstoolbar.h"
4
5 #include "3rd-party/boolinq/boolinq.h"
6 #include "core/feedsproxymodel.h"
7 #include "gui/reusable/nonclosablemenu.h"
8 #include "miscellaneous/application.h"
9 #include "miscellaneous/iconfactory.h"
10 #include "miscellaneous/settings.h"
11
12 #include <QWidgetAction>
13
14 FeedsToolBar::FeedsToolBar(const QString& title, QWidget* parent) : BaseToolBar(title, parent) {
15 // Update right margin of filter textbox.
16 QMargins margins = contentsMargins();
17
18 margins.setRight(margins.right() + FILTER_RIGHT_MARGIN);
19 setContentsMargins(margins);
20
21 initializeFilter();
22 initializeSearchBox();
23 }
24
25 QList<QAction*> FeedsToolBar::availableActions() const {
26 QList<QAction*> available_actions = qApp->userActions();
27
28 available_actions.append(m_actionSearchMessages);
29 available_actions.append(m_actionMessageFilter);
30
31 return available_actions;
32 }
33
34 QList<QAction*> FeedsToolBar::activatedActions() const {
35 return actions();
36 }
37
38 void FeedsToolBar::saveAndSetActions(const QStringList& actions) {
39 qApp->settings()->setValue(GROUP(GUI), GUI::FeedsToolbarActions, actions.join(QSL(",")));
40 loadSpecificActions(convertActions(actions));
41
42 // If user hidden search messages box, then remove the filter.
43 if (!activatedActions().contains(m_actionSearchMessages)) {
44 m_txtSearchMessages->clear();
45 }
46
47 handleMessageFilterChange(nullptr);
48 }
49
50 QList<QAction*> FeedsToolBar::convertActions(const QStringList& actions) {
51 QList<QAction*> available_actions = availableActions();
52 QList<QAction*> spec_actions;
53
54 spec_actions.reserve(actions.size());
55
56 // Iterate action names and add respectable actions into the toolbar.
57 for (const QString& action_name : actions) {
58 QAction* matching_action = findMatchingAction(action_name, available_actions);
59
60 if (matching_action != nullptr) {
61 // Add existing standard action.
62 spec_actions.append(matching_action);
63 }
64 else if (action_name == QSL(SEPARATOR_ACTION_NAME)) {
65 // Add new separator.
66 auto* act = new QAction(this);
67
68 act->setSeparator(true);
69 spec_actions.append(act);
70 }
71 else if (action_name == QSL(SEARCH_BOX_ACTION_NAME)) {
72 // Add search box.
73 spec_actions.append(m_actionSearchMessages);
74 }
75 else if (action_name.startsWith(QSL(FILTER_ACTION_NAME))) {
76 // Add filter button.
77 spec_actions.append(m_actionMessageFilter);
78 activateAction(action_name, m_actionMessageFilter);
79 }
80 else if (action_name == QSL(SPACER_ACTION_NAME)) {
81 // Add new spacer.
82 auto* spacer = new QWidget(this);
83
84 spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
85 auto* action = new QWidgetAction(this);
86
87 action->setDefaultWidget(spacer);
88 action->setIcon(qApp->icons()->fromTheme(QSL("system-search")));
89 action->setProperty("type", SPACER_ACTION_NAME);
90 action->setProperty("name", tr("Toolbar spacer"));
91 spec_actions.append(action);
92 }
93 }
94
95 return spec_actions;
96 }
97
98 void FeedsToolBar::loadSpecificActions(const QList<QAction*>& actions, bool initial_load) {
99 Q_UNUSED(initial_load)
100
101 clear();
102
103 for (QAction* act : actions) {
104 addAction(act);
105 }
106 }
107
108 inline FeedsProxyModel::FeedListFilter operator|(FeedsProxyModel::FeedListFilter a, FeedsProxyModel::FeedListFilter b) {
109 return static_cast<FeedsProxyModel::FeedListFilter>(static_cast<int>(a) | static_cast<int>(b));
110 }
111
112 void FeedsToolBar::handleMessageFilterChange(QAction* action) {
113 FeedsProxyModel::FeedListFilter task =
114 action == nullptr ? FeedsProxyModel::FeedListFilter(0) : action->data().value<FeedsProxyModel::FeedListFilter>();
115 std::list<QAction*> checked_tasks_std = boolinq::from(m_menuMessageFilter->actions())
116 .where([](QAction* act) {
117 return act->isChecked();
118 })
119 .toStdList();
120
121 if (task == FeedsProxyModel::FeedListFilter::NoFiltering || checked_tasks_std.empty()) {
122 task = FeedsProxyModel::FeedListFilter::NoFiltering;
123
124 checked_tasks_std.clear();
125
126 // Uncheck everything.
127 m_menuMessageFilter->blockSignals(true);
128
129 for (QAction* tsk : m_menuMessageFilter->actions()) {
130 tsk->setChecked(false);
131 }
132
133 m_menuMessageFilter->blockSignals(false);
134 }
135 else {
136 task = FeedsProxyModel::FeedListFilter(0);
137
138 for (QAction* tsk : checked_tasks_std) {
139 task = task | tsk->data().value<FeedsProxyModel::FeedListFilter>();
140 }
141 }
142
143 m_btnMessageFilter->setDefaultAction(checked_tasks_std.empty() ? m_menuMessageFilter->actions().constFirst()
144 : checked_tasks_std.front());
145
146 if (checked_tasks_std.size() > 1) {
147 drawNumberOfCriterias(m_btnMessageFilter, int(checked_tasks_std.size()));
148 }
149
150 saveToolButtonSelection(QSL(FILTER_ACTION_NAME),
151 GUI::FeedsToolbarActions,
152 FROM_STD_LIST(QList<QAction*>, checked_tasks_std));
153 emit feedFilterChanged(task);
154 }
155
156 QStringList FeedsToolBar::defaultActions() const {
157 return QString(GUI::FeedsToolbarActionsDef)
158 .split(',',
159 #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0
160 Qt::SplitBehaviorFlags::SkipEmptyParts);
161 #else
162 QString::SplitBehavior::SkipEmptyParts);
163 #endif
164 }
165
166 QStringList FeedsToolBar::savedActions() const {
167 return qApp->settings()
168 ->value(GROUP(GUI), SETTING(GUI::FeedsToolbarActions))
169 .toString()
170 .split(',',
171 #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0
172 Qt::SplitBehaviorFlags::SkipEmptyParts);
173 #else
174 QString::SplitBehavior::SkipEmptyParts);
175 #endif
176 }
177
178 QList<QAction*> FeedsToolBar::extraActions() const {
179 return m_menuMessageFilter->actions();
180 }
181
182 void FeedsToolBar::initializeSearchBox() {
183 m_txtSearchMessages =
184 new SearchLineEdit(QSL("feed_list_searcher"),
185 {SearchLineEdit::CustomSearchChoice(tr("Everywhere"), int(SearchFields::SearchAll)),
186 SearchLineEdit::CustomSearchChoice(tr("Titles only"), int(SearchFields::SearchTitleOnly))},
187 this);
188 m_txtSearchMessages->setSizePolicy(QSizePolicy::Policy::Expanding,
189 m_txtSearchMessages->sizePolicy().verticalPolicy());
190 m_txtSearchMessages->setPlaceholderText(tr("Search feeds"));
191
192 // Setup wrapping action for search box.
193 m_actionSearchMessages = new QWidgetAction(this);
194
195 m_actionSearchMessages->setDefaultWidget(m_txtSearchMessages);
196 m_actionSearchMessages->setIcon(qApp->icons()->fromTheme(QSL("system-search")));
197 m_actionSearchMessages->setProperty("type", SEARCH_BOX_ACTION_NAME);
198 m_actionSearchMessages->setProperty("name", tr("Feeds search box"));
199
200 connect(m_txtSearchMessages, &SearchLineEdit::searchCriteriaChanged, this, &FeedsToolBar::searchCriteriaChanged);
201 }
202
203 SearchLineEdit* FeedsToolBar::searchBox() const {
204 return m_txtSearchMessages;
205 }
206
207 void FeedsToolBar::initializeFilter() {
208 m_menuMessageFilter = new NonClosableMenu(tr("Menu for filtering feeds"), this);
209
210 QString fl = QSL(" ") + tr("(feed list)");
211
212 addActionToMenu(m_menuMessageFilter,
213 qApp->icons()->fromTheme(QSL("mail-mark-read")),
214 tr("No extra filtering"),
215 fl,
216 QVariant::fromValue(FeedsProxyModel::FeedListFilter::NoFiltering),
217 QSL("feedlist_no_filtering"));
218 addActionToMenu(m_menuMessageFilter,
219 qApp->icons()->fromTheme(QSL("mail-mark-read")),
220 tr("Show unread items"),
221 fl,
222 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowUnread),
223 QSL("feedlist_show_unread"));
224 addActionToMenu(m_menuMessageFilter,
225 qApp->icons()->fromTheme(QSL("mail-mark-read")),
226 tr("Show non-empty items"),
227 fl,
228 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowNonEmpty),
229 QSL("feedlist_non_empty"));
230 addActionToMenu(m_menuMessageFilter,
231 qApp->icons()->fromTheme(QSL("mail-mark-read")),
232 tr("Show feeds with new articles"),
233 fl,
234 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowWithNewArticles),
235 QSL("feedlist_new_articles"));
236 addActionToMenu(m_menuMessageFilter,
237 qApp->icons()->fromTheme(QSL("mail-mark-read")),
238 tr("Show feeds with error"),
239 fl,
240 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowWithError),
241 QSL("feedlist_with_error"));
242 addActionToMenu(m_menuMessageFilter,
243 qApp->icons()->fromTheme(QSL("mail-mark-read")),
244 tr("Show switched off feeds"),
245 fl,
246 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowSwitchedOff),
247 QSL("feedlist_switched_off"));
248 addActionToMenu(m_menuMessageFilter,
249 qApp->icons()->fromTheme(QSL("mail-mark-read")),
250 tr("Show quiet feeds"),
251 fl,
252 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowQuiet),
253 QSL("feedlist_quiet"));
254 addActionToMenu(m_menuMessageFilter,
255 qApp->icons()->fromTheme(QSL("mail-mark-read")),
256 tr("Show feeds with article filters"),
257 fl,
258 QVariant::fromValue(FeedsProxyModel::FeedListFilter::ShowWithArticleFilters),
259 QSL("feedlist_with_filters"));
260
261 m_btnMessageFilter = new QToolButton(this);
262 m_btnMessageFilter->setToolTip(tr("Display all feeds"));
263 m_btnMessageFilter->setMenu(m_menuMessageFilter);
264 m_btnMessageFilter->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup);
265 m_btnMessageFilter->setIcon(qApp->icons()->fromTheme(QSL("mail-mark-read")));
266 m_btnMessageFilter->setDefaultAction(m_menuMessageFilter->actions().constFirst());
267
268 m_actionMessageFilter = new QWidgetAction(this);
269 m_actionMessageFilter->setDefaultWidget(m_btnMessageFilter);
270 m_actionMessageFilter->setIcon(m_btnMessageFilter->icon());
271 m_actionMessageFilter->setProperty("type", FILTER_ACTION_NAME);
272 m_actionMessageFilter->setProperty("name", tr("Feed list filter"));
273
274 connect(m_menuMessageFilter, &QMenu::triggered, this, &FeedsToolBar::handleMessageFilterChange);
275 connect(this, &FeedsToolBar::toolButtonStyleChanged, this, [=](Qt::ToolButtonStyle style) {
276 m_btnMessageFilter->setToolButtonStyle(style);
277 });
278 }