| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/toolbars/messagestoolbar.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "definitions/definitions.h" |
| 7 | #include "gui/reusable/nonclosablemenu.h" |
| 8 | #include "miscellaneous/iconfactory.h" |
| 9 | #include "miscellaneous/settings.h" |
| 10 | |
| 11 | #include <chrono> |
| 12 | |
| 13 | #include <QMenu> |
| 14 | #include <QTimer> |
| 15 | |
| 16 | using namespace std::chrono_literals; |
| 17 | |
| 18 | MessagesToolBar::MessagesToolBar(const QString& title, QWidget* parent) : BaseToolBar(title, parent) { |
| 19 | initializeSearchBox(); |
| 20 | initializeHighlighter(); |
| 21 | } |
| 22 | |
| 23 | QList<QAction*> MessagesToolBar::availableActions() const { |
| 24 | QList<QAction*> available_actions = qApp->userActions(); |
| 25 | |
| 26 | available_actions.append(m_actionSearchMessages); |
| 27 | available_actions.append(m_actionMessageHighlighter); |
| 28 | available_actions.append(m_actionMessageFilter); |
| 29 | |
| 30 | return available_actions; |
| 31 | } |
| 32 | |
| 33 | QList<QAction*> MessagesToolBar::activatedActions() const { |
| 34 | return actions(); |
| 35 | } |
| 36 | |
| 37 | void MessagesToolBar::saveAndSetActions(const QStringList& actions) { |
| 38 | qApp->settings()->setValue(GROUP(GUI), GUI::MessagesToolbarDefaultButtons, actions.join(QSL(","))); |
| 39 | loadSpecificActions(convertActions(actions)); |
| 40 | |
| 41 | // If user hidden search messages box, then remove the filter. |
| 42 | if (!activatedActions().contains(m_actionSearchMessages)) { |
| 43 | m_txtSearchMessages->clear(); |
| 44 | } |
| 45 | |
| 46 | handleMessageFilterChange(nullptr); |
| 47 | } |
| 48 | |
| 49 | QList<QAction*> MessagesToolBar::convertActions(const QStringList& actions) { |
| 50 | QList<QAction*> available_actions = availableActions(); |
| 51 | QList<QAction*> spec_actions; |
| 52 | |
| 53 | spec_actions.reserve(actions.size()); |
| 54 | |
| 55 | // Iterate action names and add respectable actions into the toolbar. |
| 56 | for (const QString& action_name : actions) { |
| 57 | auto* matching_action = findMatchingAction(action_name, available_actions); |
| 58 | |
| 59 | if (matching_action != nullptr) { |
| 60 | // Add existing standard action. |
| 61 | spec_actions.append(matching_action); |
| 62 | } |
| 63 | else if (action_name == QSL(SEPARATOR_ACTION_NAME)) { |
| 64 | // Add new separator. |
| 65 | auto* act = new QAction(this); |
| 66 | |
| 67 | act->setSeparator(true); |
| 68 | spec_actions.append(act); |
| 69 | } |
| 70 | else if (action_name == QSL(SEARCH_BOX_ACTION_NAME)) { |
| 71 | // Add search box. |
| 72 | spec_actions.append(m_actionSearchMessages); |
| 73 | } |
| 74 | else if (action_name.startsWith(QSL(HIGHLIGHTER_ACTION_NAME))) { |
| 75 | // Add highlighter button. |
| 76 | spec_actions.append(m_actionMessageHighlighter); |
| 77 | activateAction(action_name, m_actionMessageHighlighter); |
| 78 | } |
| 79 | else if (action_name.startsWith(QSL(FILTER_ACTION_NAME))) { |
| 80 | // Add filter button. |
| 81 | spec_actions.append(m_actionMessageFilter); |
| 82 | activateAction(action_name, m_actionMessageFilter); |
| 83 | } |
| 84 | else if (action_name == QSL(SPACER_ACTION_NAME)) { |
| 85 | // Add new spacer. |
| 86 | auto* spacer = new QWidget(this); |
| 87 | |
| 88 | spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 89 | auto* action = new QWidgetAction(this); |
| 90 | |
| 91 | action->setDefaultWidget(spacer); |
| 92 | action->setIcon(qApp->icons()->fromTheme(QSL("go-jump"))); |
| 93 | action->setProperty("type", SPACER_ACTION_NAME); |
| 94 | action->setProperty("name", tr("Toolbar spacer")); |
| 95 | spec_actions.append(action); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return spec_actions; |
| 100 | } |
| 101 | |
| 102 | void MessagesToolBar::loadSpecificActions(const QList<QAction*>& actions, bool initial_load) { |
| 103 | Q_UNUSED(initial_load) |
| 104 | |
| 105 | clear(); |
| 106 | |
| 107 | for (QAction* act : actions) { |
| 108 | addAction(act); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | inline MessagesProxyModel::MessageListFilter operator|(MessagesProxyModel::MessageListFilter a, |
| 113 | MessagesProxyModel::MessageListFilter b) { |
| 114 | return static_cast<MessagesProxyModel::MessageListFilter>(static_cast<int>(a) | static_cast<int>(b)); |
| 115 | } |
| 116 | |
| 117 | inline MessagesModel::MessageHighlighter operator|(MessagesModel::MessageHighlighter a, |
| 118 | MessagesModel::MessageHighlighter b) { |
| 119 | return static_cast<MessagesModel::MessageHighlighter>(static_cast<int>(a) | static_cast<int>(b)); |
| 120 | } |
| 121 | |
| 122 | void MessagesToolBar::handleMessageHighlighterChange(QAction* action) { |
| 123 | MessagesModel::MessageHighlighter task = action->data().value<MessagesModel::MessageHighlighter>(); |
| 124 | std::list<QAction*> checked_tasks_std = boolinq::from(m_menuMessageHighlighter->actions()) |
| 125 | .where([](QAction* act) { |
| 126 | return act->isChecked(); |
| 127 | }) |
| 128 | .toStdList(); |
| 129 | |
| 130 | if (task == MessagesModel::MessageHighlighter::NoHighlighting || checked_tasks_std.empty()) { |
| 131 | checked_tasks_std.clear(); |
| 132 | |
| 133 | // Uncheck everything. |
| 134 | m_menuMessageHighlighter->blockSignals(true); |
| 135 | |
| 136 | for (QAction* tsk : m_menuMessageHighlighter->actions()) { |
| 137 | tsk->setChecked(false); |
| 138 | } |
| 139 | |
| 140 | m_menuMessageHighlighter->blockSignals(false); |
| 141 | } |
| 142 | else { |
| 143 | task = MessagesModel::MessageHighlighter(0); |
| 144 | |
| 145 | for (QAction* tsk : checked_tasks_std) { |
| 146 | task = task | tsk->data().value<MessagesModel::MessageHighlighter>(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | m_btnMessageHighlighter->setDefaultAction(checked_tasks_std.empty() ? m_menuMessageHighlighter->actions().constFirst() |
| 151 | : checked_tasks_std.front()); |
| 152 | |
| 153 | if (checked_tasks_std.size() > 1) { |
| 154 | drawNumberOfCriterias(m_btnMessageHighlighter, int(checked_tasks_std.size())); |
| 155 | } |
| 156 | |
| 157 | saveToolButtonSelection(QSL(HIGHLIGHTER_ACTION_NAME), |
| 158 | GUI::MessagesToolbarDefaultButtons, |
| 159 | FROM_STD_LIST(QList<QAction*>, checked_tasks_std)); |
| 160 | emit messageHighlighterChanged(task); |
| 161 | } |
| 162 | |
| 163 | void MessagesToolBar::handleMessageFilterChange(QAction* action) { |
| 164 | MessagesProxyModel::MessageListFilter task = action == nullptr |
| 165 | ? MessagesProxyModel::MessageListFilter(0) |
| 166 | : action->data().value<MessagesProxyModel::MessageListFilter>(); |
| 167 | std::list<QAction*> checked_tasks_std = boolinq::from(m_menuMessageFilter->actions()) |
| 168 | .where([](QAction* act) { |
| 169 | return act->isChecked(); |
| 170 | }) |
| 171 | .toStdList(); |
| 172 | |
| 173 | if (task == MessagesProxyModel::MessageListFilter::NoFiltering || checked_tasks_std.empty()) { |
| 174 | task = MessagesProxyModel::MessageListFilter::NoFiltering; |
| 175 | |
| 176 | checked_tasks_std.clear(); |
| 177 | |
| 178 | // Uncheck everything. |
| 179 | m_menuMessageFilter->blockSignals(true); |
| 180 | |
| 181 | for (QAction* tsk : m_menuMessageFilter->actions()) { |
| 182 | tsk->setChecked(false); |
| 183 | } |
| 184 | |
| 185 | m_menuMessageFilter->blockSignals(false); |
| 186 | } |
| 187 | else { |
| 188 | task = MessagesProxyModel::MessageListFilter(0); |
| 189 | |
| 190 | for (QAction* tsk : checked_tasks_std) { |
| 191 | task = task | tsk->data().value<MessagesProxyModel::MessageListFilter>(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | m_btnMessageFilter->setDefaultAction(checked_tasks_std.empty() ? m_menuMessageFilter->actions().constFirst() |
| 196 | : checked_tasks_std.front()); |
| 197 | |
| 198 | if (checked_tasks_std.size() > 1) { |
| 199 | drawNumberOfCriterias(m_btnMessageFilter, int(checked_tasks_std.size())); |
| 200 | } |
| 201 | |
| 202 | saveToolButtonSelection(QSL(FILTER_ACTION_NAME), |
| 203 | GUI::MessagesToolbarDefaultButtons, |
| 204 | FROM_STD_LIST(QList<QAction*>, checked_tasks_std)); |
| 205 | emit messageFilterChanged(task); |
| 206 | } |
| 207 | |
| 208 | void MessagesToolBar::initializeSearchBox() { |
| 209 | m_txtSearchMessages = |
| 210 | new SearchLineEdit(QSL("article_list_searcher"), |
| 211 | {SearchLineEdit::CustomSearchChoice(tr("Everywhere"), int(SearchFields::SearchAll)), |
| 212 | SearchLineEdit::CustomSearchChoice(tr("Titles only"), int(SearchFields::SearchTitleOnly))}, |
| 213 | this); |
| 214 | m_txtSearchMessages->setSizePolicy(QSizePolicy::Policy::Expanding, |
| 215 | m_txtSearchMessages->sizePolicy().verticalPolicy()); |
| 216 | m_txtSearchMessages->setPlaceholderText(tr("Search articles")); |
| 217 | |
| 218 | // Setup wrapping action for search box. |
| 219 | m_actionSearchMessages = new QWidgetAction(this); |
| 220 | m_actionSearchMessages->setDefaultWidget(m_txtSearchMessages); |
| 221 | m_actionSearchMessages->setIcon(qApp->icons()->fromTheme(QSL("system-search"))); |
| 222 | m_actionSearchMessages->setProperty("type", SEARCH_BOX_ACTION_NAME); |
| 223 | m_actionSearchMessages->setProperty("name", tr("Article search box")); |
| 224 | |
| 225 | connect(m_txtSearchMessages, &SearchLineEdit::searchCriteriaChanged, this, &MessagesToolBar::searchCriteriaChanged); |
| 226 | } |
| 227 | |
| 228 | void MessagesToolBar::initializeHighlighter() { |
| 229 | m_menuMessageHighlighter = new NonClosableMenu(tr("Menu for highlighting articles"), this); |
| 230 | |
| 231 | QString al = QSL(" ") + tr("(article list)"); |
| 232 | |
| 233 | addActionToMenu(m_menuMessageHighlighter, |
| 234 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 235 | tr("No extra highlighting"), |
| 236 | al, |
| 237 | QVariant::fromValue(MessagesModel::MessageHighlighter::NoHighlighting), |
| 238 | QSL("articlelist_no_highlighting")); |
| 239 | addActionToMenu(m_menuMessageHighlighter, |
| 240 | qApp->icons()->fromTheme(QSL("mail-mark-unread")), |
| 241 | tr("Highlight unread articles"), |
| 242 | al, |
| 243 | QVariant::fromValue(MessagesModel::MessageHighlighter::HighlightUnread), |
| 244 | QSL("articlelist_highlight_unread")); |
| 245 | addActionToMenu(m_menuMessageHighlighter, |
| 246 | qApp->icons()->fromTheme(QSL("mail-mark-important")), |
| 247 | tr("Highlight important articles"), |
| 248 | al, |
| 249 | QVariant::fromValue(MessagesModel::MessageHighlighter::HighlightImportant), |
| 250 | QSL("articlelist_highlight_important")); |
| 251 | |
| 252 | m_menuMessageFilter = new NonClosableMenu(tr("Menu for filtering articles"), this); |
| 253 | |
| 254 | addActionToMenu(m_menuMessageFilter, |
| 255 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 256 | tr("No extra filtering"), |
| 257 | al, |
| 258 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::NoFiltering), |
| 259 | QSL("articlelist_no_filtering")); |
| 260 | addActionToMenu(m_menuMessageFilter, |
| 261 | qApp->icons()->fromTheme(QSL("mail-mark-unread")), |
| 262 | tr("Show unread articles"), |
| 263 | al, |
| 264 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowUnread), |
| 265 | QSL("articlelist_show_unread")); |
| 266 | addActionToMenu(m_menuMessageFilter, |
| 267 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 268 | tr("Show read articles"), |
| 269 | al, |
| 270 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowRead), |
| 271 | QSL("articlelist_show_read")); |
| 272 | addActionToMenu(m_menuMessageFilter, |
| 273 | qApp->icons()->fromTheme(QSL("mail-mark-important")), |
| 274 | tr("Show important articles"), |
| 275 | al, |
| 276 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowImportant), |
| 277 | QSL("articlelist_show_important")); |
| 278 | addActionToMenu(m_menuMessageFilter, |
| 279 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 280 | tr("Show today's articles"), |
| 281 | al, |
| 282 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowToday), |
| 283 | QSL("articlelist_show_today")); |
| 284 | addActionToMenu(m_menuMessageFilter, |
| 285 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 286 | tr("Show yesterday's articles"), |
| 287 | al, |
| 288 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowYesterday), |
| 289 | QSL("articlelist_show_yesterday")); |
| 290 | addActionToMenu(m_menuMessageFilter, |
| 291 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 292 | tr("Show articles in last 24 hours"), |
| 293 | al, |
| 294 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowLast24Hours), |
| 295 | QSL("articlelist_show_last24hours")); |
| 296 | addActionToMenu(m_menuMessageFilter, |
| 297 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 298 | tr("Show articles in last 48 hours"), |
| 299 | al, |
| 300 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowLast48Hours), |
| 301 | QSL("articlelist_show_last48hours")); |
| 302 | addActionToMenu(m_menuMessageFilter, |
| 303 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 304 | tr("Show this week's articles"), |
| 305 | al, |
| 306 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowThisWeek), |
| 307 | QSL("articlelist_show_this_week")); |
| 308 | addActionToMenu(m_menuMessageFilter, |
| 309 | qApp->icons()->fromTheme(QSL("mail-mark-read")), |
| 310 | tr("Show last week's articles"), |
| 311 | al, |
| 312 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowLastWeek), |
| 313 | QSL("articlelist_show_last_week")); |
| 314 | addActionToMenu(m_menuMessageFilter, |
| 315 | qApp->icons()->fromTheme(QSL("mail-attachment")), |
| 316 | tr("Show articles with attachments"), |
| 317 | al, |
| 318 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowOnlyWithAttachments), |
| 319 | QSL("articlelist_show_with_attachments")); |
| 320 | addActionToMenu(m_menuMessageFilter, |
| 321 | MessagesModel::generateIconForScore(MSG_SCORE_MAX / 2.0), |
| 322 | tr("Show articles with some score"), |
| 323 | al, |
| 324 | QVariant::fromValue(MessagesProxyModel::MessageListFilter::ShowOnlyWithScore), |
| 325 | QSL("articlelist_show_with_score")); |
| 326 | |
| 327 | m_btnMessageHighlighter = new QToolButton(this); |
| 328 | m_btnMessageHighlighter->setToolTip(tr("Display all articles")); |
| 329 | m_btnMessageHighlighter->setMenu(m_menuMessageHighlighter); |
| 330 | m_btnMessageHighlighter->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup); |
| 331 | m_btnMessageHighlighter->setIcon(qApp->icons()->fromTheme(QSL("mail-mark-read"))); |
| 332 | m_btnMessageHighlighter->setDefaultAction(m_menuMessageHighlighter->actions().constFirst()); |
| 333 | |
| 334 | m_btnMessageFilter = new QToolButton(this); |
| 335 | m_btnMessageFilter->setToolTip(tr("Display all articles")); |
| 336 | m_btnMessageFilter->setMenu(m_menuMessageFilter); |
| 337 | m_btnMessageFilter->setPopupMode(QToolButton::ToolButtonPopupMode::InstantPopup); |
| 338 | m_btnMessageFilter->setIcon(qApp->icons()->fromTheme(QSL("mail-mark-read"))); |
| 339 | m_btnMessageFilter->setDefaultAction(m_menuMessageFilter->actions().constFirst()); |
| 340 | |
| 341 | m_actionMessageHighlighter = new QWidgetAction(this); |
| 342 | m_actionMessageHighlighter->setDefaultWidget(m_btnMessageHighlighter); |
| 343 | m_actionMessageHighlighter->setIcon(m_btnMessageHighlighter->icon()); |
| 344 | m_actionMessageHighlighter->setProperty("type", HIGHLIGHTER_ACTION_NAME); |
| 345 | m_actionMessageHighlighter->setProperty("name", tr("Article highlighter")); |
| 346 | |
| 347 | m_actionMessageFilter = new QWidgetAction(this); |
| 348 | m_actionMessageFilter->setDefaultWidget(m_btnMessageFilter); |
| 349 | m_actionMessageFilter->setIcon(m_btnMessageFilter->icon()); |
| 350 | m_actionMessageFilter->setProperty("type", FILTER_ACTION_NAME); |
| 351 | m_actionMessageFilter->setProperty("name", tr("Article list filter")); |
| 352 | |
| 353 | connect(m_menuMessageHighlighter, &QMenu::triggered, this, &MessagesToolBar::handleMessageHighlighterChange); |
| 354 | connect(m_menuMessageFilter, &QMenu::triggered, this, &MessagesToolBar::handleMessageFilterChange); |
| 355 | connect(this, &MessagesToolBar::toolButtonStyleChanged, this, [=](Qt::ToolButtonStyle style) { |
| 356 | m_btnMessageHighlighter->setToolButtonStyle(style); |
| 357 | m_btnMessageFilter->setToolButtonStyle(style); |
| 358 | }); |
| 359 | } |
| 360 | |
| 361 | SearchLineEdit* MessagesToolBar::searchBox() const { |
| 362 | return m_txtSearchMessages; |
| 363 | } |
| 364 | |
| 365 | QStringList MessagesToolBar::defaultActions() const { |
| 366 | return QString(GUI::MessagesToolbarDefaultButtonsDef) |
| 367 | .split(QL1C(','), |
| 368 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 369 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 370 | #else |
| 371 | QString::SplitBehavior::SkipEmptyParts); |
| 372 | #endif |
| 373 | } |
| 374 | |
| 375 | QStringList MessagesToolBar::savedActions() const { |
| 376 | return qApp->settings() |
| 377 | ->value(GROUP(GUI), SETTING(GUI::MessagesToolbarDefaultButtons)) |
| 378 | .toString() |
| 379 | .split(QL1C(','), |
| 380 | #if QT_VERSION >= 0x050F00 // Qt >= 5.15.0 |
| 381 | Qt::SplitBehaviorFlags::SkipEmptyParts); |
| 382 | #else |
| 383 | QString::SplitBehavior::SkipEmptyParts); |
| 384 | #endif |
| 385 | } |
| 386 | |
| 387 | QList<QAction*> MessagesToolBar::extraActions() const { |
| 388 | QList<QAction*> act; |
| 389 | |
| 390 | act << m_menuMessageHighlighter->actions() << m_menuMessageFilter->actions(); |
| 391 | |
| 392 | return act; |
| 393 | } |