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

KDE's Doxygen guidelines are available online.