Messagelib

searchmessagebybuttons.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "searchmessagebybuttons.h"
8#include <KLocalizedString>
9#include <QButtonGroup>
10#include <QHBoxLayout>
11#include <QLabel>
12#include <QToolButton>
13
14using namespace MessageList::Core;
15SearchMessageByButtons::SearchMessageByButtons(QWidget *parent)
16 : QWidget{parent}
17 , mMainLayout(new QHBoxLayout(this))
18 , mButtonGroup(new QButtonGroup(this))
19{
20 mMainLayout->setObjectName(QStringLiteral("mainLayout"));
21 mMainLayout->setContentsMargins({});
22 mMainLayout->setSpacing(0);
23
24 mButtonGroup->setObjectName(QStringLiteral("mButtonGroup"));
25 connect(mButtonGroup, &QButtonGroup::idClicked, this, &SearchMessageByButtons::searchOptionChanged);
26 createButtons();
27 mMainLayout->addStretch(0);
28}
29
30SearchMessageByButtons::~SearchMessageByButtons() = default;
31
32void SearchMessageByButtons::createButtons()
33{
34 auto button = createFilterButton(i18nc("@action", "Full Message"), SearchEveryWhere);
35 button->setChecked(true); // Enable by default
36 createFilterButton(i18nc("@action", "Body"), SearchAgainstBody);
37 createFilterButton(i18nc("@action", "Subject"), SearchAgainstSubject);
38 mSearchAgainstFromOrToButton = createFilterButton(QString(), SearchAgainstTo);
39 createFilterButton(i18nc("@action", "BCC"), SearchAgainstBcc);
40 createFilterButton(i18nc("@action", "CC"), SearchAgainstCc);
41 changeSearchAgainstFromOrToText();
42}
43
44QToolButton *SearchMessageByButtons::createFilterButton(const QString &text, SearchOption option)
45{
46 auto toolButton = new QToolButton(this);
47 toolButton->setText(text);
48 toolButton->setCheckable(true);
49 toolButton->setAutoRaise(true);
50 mButtonGroup->addButton(toolButton, option);
51 mMainLayout->addWidget(toolButton, 0, Qt::AlignTop);
52 return toolButton;
53}
54
55bool SearchMessageByButtons::containsOutboundMessages() const
56{
57 return mContainsOutboundMessages;
58}
59
60void SearchMessageByButtons::setContainsOutboundMessages(bool containsOutboundMessages)
61{
62 if (mContainsOutboundMessages != containsOutboundMessages) {
63 mContainsOutboundMessages = containsOutboundMessages;
64 changeSearchAgainstFromOrToText();
65 }
66}
67
68void SearchMessageByButtons::changeSearchAgainstFromOrToText()
69{
70 if (mContainsOutboundMessages) {
71 mSearchAgainstFromOrToButton->setText(i18n("To"));
72 } else {
73 mSearchAgainstFromOrToButton->setText(i18n("From"));
74 }
75}
76
77SearchMessageByButtons::SearchOptions SearchMessageByButtons::searchOptions() const
78{
80 const int checked = mButtonGroup->checkedId();
81 if (checked == SearchAgainstTo) {
82 if (mContainsOutboundMessages) {
83 searchOptions |= SearchMessageByButtons::SearchAgainstTo;
84 } else {
85 searchOptions |= SearchMessageByButtons::SearchAgainstFrom;
86 }
87 } else {
88 searchOptions |= static_cast<SearchMessageByButtons::SearchOption>(checked);
89 }
90 return searchOptions;
91}
92
93void SearchMessageByButtons::setSearchOptions(SearchMessageByButtons::SearchOptions opts)
94{
95 mButtonGroup->button(SearchMessageByButtons::SearchEveryWhere)->setChecked(opts & SearchMessageByButtons::SearchEveryWhere);
96 mButtonGroup->button(SearchMessageByButtons::SearchAgainstBody)->setChecked(opts & SearchMessageByButtons::SearchAgainstBody);
97 mButtonGroup->button(SearchMessageByButtons::SearchAgainstSubject)->setChecked(opts & SearchMessageByButtons::SearchAgainstSubject);
98 mButtonGroup->button(SearchMessageByButtons::SearchAgainstBcc)->setChecked(opts & SearchMessageByButtons::SearchAgainstBcc);
99 mButtonGroup->button(SearchMessageByButtons::SearchAgainstCc)->setChecked(opts & SearchMessageByButtons::SearchAgainstCc);
100
101 if (mContainsOutboundMessages) {
102 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstTo);
103 } else {
104 mButtonGroup->button(SearchMessageByButtons::SearchAgainstTo)->setChecked(opts & SearchMessageByButtons::SearchAgainstFrom);
105 }
106}
107
108void SearchMessageByButtons::clearFilter()
109{
110 mButtonGroup->button(SearchMessageByButtons::SearchEveryWhere)->setChecked(true);
111}
112
113#include "moc_searchmessagebybuttons.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
The implementation independent part of the MessageList library.
Definition aggregation.h:22
void setChecked(bool)
void setText(const QString &text)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void addButton(QAbstractButton *button, int id)
QAbstractButton * button(int id) const const
int checkedId() const const
void idClicked(int id)
void setObjectName(QAnyStringView name)
AlignTop
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:05:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.