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 <KLocalizedString>
15
16#include <QComboBox>
17#include <QHBoxLayout>
18#include <QPushButton>
19#include <QStandardPaths>
20
21using namespace MessageList::Core;
22QuickSearchLine::QuickSearchLine(QWidget *parent)
23 : QWidget(parent)
24 , mSearchEdit(new SearchLineStatus(this))
25 , mTagFilterCombo(new QComboBox(this))
26{
27 auto vbox = new QVBoxLayout(this);
28 vbox->setContentsMargins({});
29 vbox->setSpacing(0);
30
31 auto w = new QWidget;
32 auto hbox = new QHBoxLayout(w);
33 hbox->setContentsMargins({});
34 hbox->setSpacing(0);
35 vbox->addWidget(w);
36
37 connect(mSearchEdit, &SearchLineStatus::filterActionChanged, this, &QuickSearchLine::slotFilterActionChanged);
38 connect(mSearchEdit, &SearchLineStatus::searchOptionChanged, this, &QuickSearchLine::searchOptionChanged);
39 connect(mSearchEdit, &SearchLineStatus::forceLostFocus, this, &QuickSearchLine::forceLostFocus);
40 mSearchEdit->setPlaceholderText(i18nc("Search for messages.", "Search"));
41 mSearchEdit->setObjectName(QLatin1StringView("quicksearch"));
42 mSearchEdit->setClearButtonEnabled(true);
43
44 connect(mSearchEdit, &QLineEdit::textChanged, this, &QuickSearchLine::slotSearchEditTextEdited);
45 connect(mSearchEdit, &SearchLineStatus::clearButtonClicked, this, &QuickSearchLine::slotClearButtonClicked);
46 connect(mSearchEdit, &SearchLineStatus::saveFilter, this, &QuickSearchLine::saveFilter);
47 connect(mSearchEdit, &SearchLineStatus::activateFilter, this, &QuickSearchLine::activateFilter);
48
49 hbox->addWidget(mSearchEdit);
50
51 // The status filter button. Will be populated later, as populateStatusFilterCombo() is virtual
52 mTagFilterCombo->setMaximumWidth(300);
53 mTagFilterCombo->setMaximumWidth(200);
54 mTagFilterCombo->hide();
55 hbox->addWidget(mTagFilterCombo);
56
57 // Be disable until we have a storageModel => logical that it's disable.
58 mSearchEdit->setEnabled(false);
59 mTagFilterCombo->setEnabled(false);
60
61 installEventFilter(this);
62 mTagFilterCombo->installEventFilter(this);
63 changeQuicksearchVisibility(MessageListSettings::self()->showQuickSearch());
64}
65
66QuickSearchLine::~QuickSearchLine() = default;
67
68void QuickSearchLine::slotSearchEditTextEdited(const QString &text)
69{
70 int minimumStringLength = 3;
71 if (text.startsWith(QLatin1Char('"')) && text.endsWith(QLatin1Char('"'))) {
73 }
74 if (!text.trimmed().isEmpty()) {
76 Q_EMIT searchEditTextEdited(text);
77 }
78 } else {
79 slotClearButtonClicked();
80 }
81}
82
83void QuickSearchLine::slotClearButtonClicked()
84{
85 if (mTagFilterCombo->isVisible()) {
86 mTagFilterCombo->setCurrentIndex(0);
87 }
88 mSearchEdit->clearFilterButtonClicked();
89 Q_EMIT clearButtonClicked();
90}
91
92void QuickSearchLine::setSearchOptions(QuickSearchLine::SearchOptions opts)
93{
94 mSearchEdit->setSearchOptions(opts);
95}
96
97QuickSearchLine::SearchOptions QuickSearchLine::searchOptions() const
98{
99 return mSearchEdit->searchOptions();
100}
101
102void QuickSearchLine::focusQuickSearch(const QString &selectedText)
103{
104 if (!selectedText.isEmpty()) {
105 mSearchEdit->setText(selectedText);
106 }
107 mSearchEdit->setFocus();
108}
109
110QComboBox *QuickSearchLine::tagFilterComboBox() const
111{
112 return mTagFilterCombo;
113}
114
115SearchLineStatus *QuickSearchLine::searchEdit() const
116{
117 return mSearchEdit;
118}
119
120void QuickSearchLine::resetFilter()
121{
122 if (mTagFilterCombo->isVisible()) {
123 mTagFilterCombo->setCurrentIndex(0);
124 }
125 mSearchEdit->clearFilterButtonClicked();
126 mSearchEdit->setLocked(false);
127}
128
129void QuickSearchLine::slotFilterActionChanged(const QList<Akonadi::MessageStatus> &lst)
130{
131 mLstStatus = lst;
132 Q_EMIT statusButtonsClicked();
133}
134
135void QuickSearchLine::setFilterMessageStatus(const QList<Akonadi::MessageStatus> &newLstStatus)
136{
137 mLstStatus = newLstStatus;
138 mSearchEdit->setFilterMessageStatus(mLstStatus);
139}
140
141QList<Akonadi::MessageStatus> QuickSearchLine::status() const
142{
143 return mLstStatus;
144}
145
146bool QuickSearchLine::containsOutboundMessages() const
147{
148 return mSearchEdit->containsOutboundMessages();
149}
150
151void QuickSearchLine::setContainsOutboundMessages(bool containsOutboundMessages)
152{
153 mSearchEdit->setContainsOutboundMessages(containsOutboundMessages);
154}
155
156void QuickSearchLine::updateComboboxVisibility()
157{
158 mTagFilterCombo->setVisible(!mSearchEdit->isHidden() && mTagFilterCombo->count());
159}
160
161bool QuickSearchLine::eventFilter(QObject *object, QEvent *e)
162{
163 const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
164 if (shortCutOverride) {
165 e->accept();
166 return true;
167 }
168 return QWidget::eventFilter(object, e);
169}
170
171void QuickSearchLine::changeQuicksearchVisibility(bool show)
172{
173 mSearchEdit->setVisible(show);
174 mTagFilterCombo->setVisible(show && mTagFilterCombo->count());
175}
176
177void QuickSearchLine::addCompletionItem(const QString &str)
178{
179 mSearchEdit->addCompletionItem(str);
180}
181
182#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()
QEvent::Type type() const const
void setText(const QString &)
void textChanged(const QString &text)
Q_EMITQ_EMIT
virtual bool eventFilter(QObject *watched, QEvent *event)
T qobject_cast(QObject *object)
bool isEmpty() const const
bool isHidden() const const
void setFocus()
void show()
bool isVisible() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.