Messagelib

quicksearchline.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2021 Laurent Montel <[email protected]>
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 
21 using namespace MessageList::Core;
22 QuickSearchLine::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(QStringLiteral("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 
66 QuickSearchLine::~QuickSearchLine() = default;
67 
68 void QuickSearchLine::slotSearchEditTextEdited(const QString &text)
69 {
70  int minimumStringLength = 3;
71  if (text.startsWith(QLatin1Char('"')) && text.endsWith(QLatin1Char('"'))) {
72  minimumStringLength = 5;
73  }
74  if (!text.trimmed().isEmpty()) {
75  if (KStringHandler::logicalLength(text) >= minimumStringLength) {
76  Q_EMIT searchEditTextEdited(text);
77  }
78  } else {
79  slotClearButtonClicked();
80  }
81 }
82 
83 void QuickSearchLine::slotClearButtonClicked()
84 {
85  if (mTagFilterCombo->isVisible()) {
86  mTagFilterCombo->setCurrentIndex(0);
87  }
88  mSearchEdit->clearFilterButtonClicked();
89  Q_EMIT clearButtonClicked();
90 }
91 
92 void QuickSearchLine::setSearchOptions(QuickSearchLine::SearchOptions opts)
93 {
94  mSearchEdit->setSearchOptions(opts);
95 }
96 
97 QuickSearchLine::SearchOptions QuickSearchLine::searchOptions() const
98 {
99  return mSearchEdit->searchOptions();
100 }
101 
102 void QuickSearchLine::focusQuickSearch(const QString &selectedText)
103 {
104  if (!selectedText.isEmpty()) {
105  mSearchEdit->setText(selectedText);
106  }
107  mSearchEdit->setFocus();
108 }
109 
110 QComboBox *QuickSearchLine::tagFilterComboBox() const
111 {
112  return mTagFilterCombo;
113 }
114 
115 SearchLineStatus *QuickSearchLine::searchEdit() const
116 {
117  return mSearchEdit;
118 }
119 
120 void QuickSearchLine::resetFilter()
121 {
122  if (mTagFilterCombo->isVisible()) {
123  mTagFilterCombo->setCurrentIndex(0);
124  }
125  mSearchEdit->clearFilterButtonClicked();
126  mSearchEdit->setLocked(false);
127 }
128 
129 void QuickSearchLine::slotFilterActionChanged(const QVector<Akonadi::MessageStatus> &lst)
130 {
131  mLstStatus = lst;
132  Q_EMIT statusButtonsClicked();
133 }
134 
135 void QuickSearchLine::setFilterMessageStatus(const QVector<Akonadi::MessageStatus> &newLstStatus)
136 {
137  mLstStatus = newLstStatus;
138  mSearchEdit->setFilterMessageStatus(mLstStatus);
139 }
140 
141 QVector<Akonadi::MessageStatus> QuickSearchLine::status() const
142 {
143  return mLstStatus;
144 }
145 
146 bool QuickSearchLine::containsOutboundMessages() const
147 {
148  return mSearchEdit->containsOutboundMessages();
149 }
150 
151 void QuickSearchLine::setContainsOutboundMessages(bool containsOutboundMessages)
152 {
153  mSearchEdit->setContainsOutboundMessages(containsOutboundMessages);
154 }
155 
156 void QuickSearchLine::updateComboboxVisibility()
157 {
158  mTagFilterCombo->setVisible(!mSearchEdit->isHidden() && mTagFilterCombo->count());
159 }
160 
161 bool 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 
171 void QuickSearchLine::changeQuicksearchVisibility(bool show)
172 {
173  mSearchEdit->setVisible(show);
174  mTagFilterCombo->setVisible(show && mTagFilterCombo->count());
175 }
176 
177 void QuickSearchLine::addCompletionItem(const QString &str)
178 {
179  mSearchEdit->addCompletionItem(str);
180 }
ShortcutOverride
QEvent::Type type() const const
KCOREADDONS_EXPORT int logicalLength(const QString &text)
void textChanged(const QString &text)
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
void installEventFilter(QObject *filterObj)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
QString trimmed() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
virtual bool eventFilter(QObject *watched, QEvent *event)
void accept()
void setCurrentIndex(int index)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.