Messagelib

quicksearchline.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7
8#include "quicksearchline.h"
9#include "messagelistsettings.h"
10#include <KStringHandler>
11
12#include "core/filter.h"
13#include "searchlinestatus.h"
14#include "searchstatusbuttons.h"
15#include <KLocalizedString>
16
17#include <QComboBox>
18#include <QEvent>
19#include <QHBoxLayout>
20#include <QPushButton>
21#include <QStandardPaths>
22#if USE_SEARCH_COMMAND_LINE
23#include "core/widgets/searchlinecommandwidget.h"
24#endif
25using namespace MessageList::Core;
26QuickSearchLine::QuickSearchLine(QWidget *parent)
27 : QWidget(parent)
28 , mSearchEdit(new SearchLineStatus(this))
29 , mSearchStatusButtons(new SearchStatusButtons(this))
30 , mSearchMessageByButtons(new SearchMessageByButtons(this))
31#if USE_SEARCH_COMMAND_LINE
32 , mSearchLineCommandWidget(new SearchLineCommandWidget(this))
33#endif
34 , mTagFilterCombo(new QComboBox(this))
35{
36 auto vbox = new QVBoxLayout(this);
37 vbox->setContentsMargins({});
38 vbox->setSpacing(0);
39
40 auto w = new QWidget;
41 auto hbox = new QHBoxLayout(w);
42 hbox->setContentsMargins({});
43 hbox->setSpacing(0);
44 vbox->addWidget(w);
45
46 vbox->addWidget(mSearchMessageByButtons);
47 mSearchMessageByButtons->setVisible(false);
48#if USE_SEARCH_COMMAND_LINE
49 vbox->addWidget(mSearchLineCommandWidget);
50 mSearchLineCommandWidget->setVisible(false);
51 connect(mSearchEdit, &SearchLineStatus::searchCommandActionRequested, this, [this]() {
52 mSearchLineCommandWidget->setVisible(!mSearchLineCommandWidget->isVisible());
53 });
54 connect(mSearchLineCommandWidget, &SearchLineCommandWidget::insertCommand, mSearchEdit, &SearchLineStatus::slotInsertCommand);
55
56 connect(mSearchEdit, &QLineEdit::textChanged, this, [this](const QString &str) {
57 MessageList::Core::SearchLineCommand c;
58 c.parseSearchLineCommand(str);
59 mSearchLineCommandWidget->setLabel(c.generateCommadLineStr());
60 });
61#endif
62
63 connect(mSearchEdit, &SearchLineStatus::forceLostFocus, this, &QuickSearchLine::forceLostFocus);
64 mSearchEdit->setPlaceholderText(i18nc("Search for messages.", "Search"));
65 mSearchEdit->setObjectName(QLatin1StringView("quicksearch"));
66 mSearchEdit->setClearButtonEnabled(true);
67 connect(mSearchMessageByButtons, &SearchMessageByButtons::searchOptionChanged, this, [this]() {
68 mSearchEdit->filterAdded();
69 Q_EMIT searchOptionChanged();
70 });
71
72 connect(mSearchEdit, &QLineEdit::textChanged, this, &QuickSearchLine::slotSearchEditTextEdited);
73 connect(mSearchEdit, &SearchLineStatus::clearButtonClicked, this, &QuickSearchLine::slotClearButtonClicked);
74 connect(mSearchEdit, &SearchLineStatus::saveFilter, this, &QuickSearchLine::saveFilter);
75 connect(mSearchEdit, &SearchLineStatus::activateFilter, this, &QuickSearchLine::activateFilter);
76
77 hbox->addWidget(mSearchEdit);
78 mSearchStatusButtons->setObjectName(QLatin1StringView("mSearchStatusButtons"));
79 hbox->addWidget(mSearchStatusButtons);
80 connect(mSearchStatusButtons, &SearchStatusButtons::filterStatusChanged, this, &QuickSearchLine::slotFilterActionChanged);
81
82 // The status filter button. Will be populated later, as populateStatusFilterCombo() is virtual
83 mTagFilterCombo->setObjectName(QLatin1StringView("mTagFilterCombo"));
84 mTagFilterCombo->setMaximumWidth(300);
85 mTagFilterCombo->setMaximumWidth(200);
86 mTagFilterCombo->hide();
87 hbox->addWidget(mTagFilterCombo);
88
89 // Be disable until we have a storageModel => logical that it's disable.
90 mSearchEdit->setEnabled(false);
91 mTagFilterCombo->setEnabled(false);
92
93 installEventFilter(this);
94 mTagFilterCombo->installEventFilter(this);
95 changeQuicksearchVisibility(MessageListSettings::self()->showQuickSearch());
96}
97
98QuickSearchLine::~QuickSearchLine() = default;
99
100void QuickSearchLine::slotSearchEditTextEdited(const QString &text)
101{
102 int minimumStringLength = 3;
103 if (text.startsWith(QLatin1Char('"')) && text.endsWith(QLatin1Char('"'))) {
104 minimumStringLength = 5;
105 }
106 if (!text.trimmed().isEmpty()) {
107 if (KStringHandler::logicalLength(text) >= minimumStringLength) {
108 mSearchMessageByButtons->setVisible(true);
109 Q_EMIT searchEditTextEdited(text);
110 }
111 } else {
112 slotClearButtonClicked();
113 }
114}
115
116void QuickSearchLine::slotClearButtonClicked()
117{
118 if (mTagFilterCombo->isVisible()) {
119 mTagFilterCombo->setCurrentIndex(0);
120 }
121 mSearchEdit->clearFilterButtonClicked();
122 mSearchStatusButtons->clearFilter();
123 mSearchMessageByButtons->clearFilter();
124 mSearchMessageByButtons->setVisible(false);
125 Q_EMIT clearButtonClicked();
126}
127
128void QuickSearchLine::setSearchOptions(SearchMessageByButtons::SearchOptions opts)
129{
130 mSearchMessageByButtons->setSearchOptions(opts);
131 mSearchMessageByButtons->setVisible(true);
132 mSearchEdit->filterAdded();
133}
134
135SearchMessageByButtons::SearchOptions QuickSearchLine::searchOptions() const
136{
137 return mSearchMessageByButtons->searchOptions();
138}
139
140void QuickSearchLine::focusQuickSearch(const QString &selectedText)
141{
142 if (!selectedText.isEmpty()) {
143 mSearchEdit->setText(selectedText);
144 }
145 mSearchEdit->setFocus();
146}
147
148QComboBox *QuickSearchLine::tagFilterComboBox() const
149{
150 return mTagFilterCombo;
151}
152
153SearchLineStatus *QuickSearchLine::searchEdit() const
154{
155 return mSearchEdit;
156}
157
158void QuickSearchLine::resetFilter()
159{
160 if (mTagFilterCombo->isVisible()) {
161 mTagFilterCombo->setCurrentIndex(0);
162 }
163 mSearchEdit->clearFilterButtonClicked();
164 mSearchEdit->setLocked(false);
165 mSearchStatusButtons->clearFilter();
166}
167
168void QuickSearchLine::slotFilterActionChanged(const QList<Akonadi::MessageStatus> &lst)
169{
170 mLstStatus = lst;
171 mSearchEdit->filterAdded();
172 Q_EMIT statusButtonsClicked();
173}
174
175void QuickSearchLine::setFilterMessageStatus(const QList<Akonadi::MessageStatus> &newLstStatus)
176{
177 mLstStatus = newLstStatus;
178 mSearchEdit->filterAdded();
179 mSearchStatusButtons->setFilterMessageStatus(mLstStatus);
180}
181
182QList<Akonadi::MessageStatus> QuickSearchLine::status() const
183{
184 return mLstStatus;
185}
186
187bool QuickSearchLine::containsOutboundMessages() const
188{
189 return mSearchMessageByButtons->containsOutboundMessages();
190}
191
192void QuickSearchLine::setContainsOutboundMessages(bool containsOutboundMessages)
193{
194 mSearchMessageByButtons->setContainsOutboundMessages(containsOutboundMessages);
195}
196
197void QuickSearchLine::updateComboboxVisibility()
198{
199 mTagFilterCombo->setVisible(!mSearchEdit->isHidden() && mTagFilterCombo->count());
200}
201
202bool QuickSearchLine::eventFilter(QObject *object, QEvent *e)
203{
204 const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
205 if (shortCutOverride) {
206 e->accept();
207 return true;
208 }
209 return QWidget::eventFilter(object, e);
210}
211
212void QuickSearchLine::changeQuicksearchVisibility(bool show)
213{
214 mSearchEdit->setVisible(show);
215 mTagFilterCombo->setVisible(show && mTagFilterCombo->count());
216 mSearchStatusButtons->setVisible(show);
217}
218
219void QuickSearchLine::addCompletionItem(const QString &str)
220{
221 mSearchEdit->addCompletionItem(str);
222}
223
224#include "moc_quicksearchline.cpp"
constexpr bool isEmpty() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCOREADDONS_EXPORT int logicalLength(const QString &text)
The implementation independent part of the MessageList library.
Definition aggregation.h:22
void setCurrentIndex(int index)
ShortcutOverride
void accept()
Type type() const const
void setText(const QString &)
void textChanged(const QString &text)
Q_EMITQ_EMIT
virtual bool eventFilter(QObject *watched, QEvent *event)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isHidden() const const
void setFocus()
void show()
virtual void setVisible(bool visible)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.