Messagelib

findbarbase.cpp
1 /* SPDX-FileCopyrightText: 2010 Torgny Nyblom <[email protected]>
2  * SPDX-FileCopyrightText: 2010-2021 Laurent Montel <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "findbarbase.h"
8 #include <PimCommon/LineEditWithCompleterNg>
9 
10 #include <KColorScheme>
11 #include <KLocalizedString>
12 #include <QIcon>
13 #include <QLineEdit>
14 
15 #include <QEvent>
16 #include <QHBoxLayout>
17 #include <QKeyEvent>
18 #include <QLabel>
19 #include <QMenu>
20 #include <QPushButton>
21 #include <QTimer>
22 #include <QToolButton>
23 
24 using namespace WebEngineViewer;
25 
26 FindBarBase::FindBarBase(QWidget *parent)
27  : QWidget(parent)
28 {
29  auto lay = new QHBoxLayout(this);
30  lay->setContentsMargins({});
31 
32  auto closeBtn = new QToolButton(this);
33  closeBtn->setIcon(QIcon::fromTheme(QStringLiteral("dialog-close")));
34  closeBtn->setObjectName(QStringLiteral("close"));
35  closeBtn->setIconSize(QSize(16, 16));
36  closeBtn->setToolTip(i18n("Close"));
37 
38 #ifndef QT_NO_ACCESSIBILITY
39  closeBtn->setAccessibleName(i18n("Close"));
40 #endif
41 
42  closeBtn->setAutoRaise(true);
43  lay->addWidget(closeBtn);
44 
45  auto label = new QLabel(i18nc("Find text", "F&ind:"), this);
46  lay->addWidget(label);
47 
48  mSearch = new PimCommon::LineEditWithCompleterNg(this);
49  mSearch->setObjectName(QStringLiteral("searchline"));
50  mSearch->setToolTip(i18n("Text to search for"));
51  mSearch->setClearButtonEnabled(true);
52  label->setBuddy(mSearch);
53  lay->addWidget(mSearch);
54 
55  mFindNextBtn = new QPushButton(QIcon::fromTheme(QStringLiteral("go-down-search")), i18nc("Find and go to the next search match", "Next"), this);
56  mFindNextBtn->setToolTip(i18n("Jump to next match"));
57  mFindNextBtn->setObjectName(QStringLiteral("findnext"));
58  lay->addWidget(mFindNextBtn);
59  mFindNextBtn->setEnabled(false);
60 
61  mFindPrevBtn = new QPushButton(QIcon::fromTheme(QStringLiteral("go-up-search")), i18nc("Find and go to the previous search match", "Previous"), this);
62  mFindPrevBtn->setToolTip(i18n("Jump to previous match"));
63  mFindPrevBtn->setObjectName(QStringLiteral("findprevious"));
64  lay->addWidget(mFindPrevBtn);
65  mFindPrevBtn->setEnabled(false);
66 
67  auto optionsBtn = new QPushButton(this);
68  optionsBtn->setText(i18n("Options"));
69  optionsBtn->setToolTip(i18n("Modify search behavior"));
70  mOptionsMenu = new QMenu(optionsBtn);
71  mCaseSensitiveAct = mOptionsMenu->addAction(i18n("Case sensitive"));
72  mCaseSensitiveAct->setCheckable(true);
73 
74  optionsBtn->setMenu(mOptionsMenu);
75  lay->addWidget(optionsBtn);
76 
77  connect(closeBtn, &QToolButton::clicked, this, &FindBarBase::closeBar);
78  connect(mFindNextBtn, &QPushButton::clicked, this, &FindBarBase::findNext);
79  connect(mFindPrevBtn, &QPushButton::clicked, this, &FindBarBase::findPrev);
80  connect(mCaseSensitiveAct, &QAction::toggled, this, &FindBarBase::caseSensitivityChanged);
81  connect(mSearch, &QLineEdit::textChanged, this, &FindBarBase::autoSearch);
82  connect(mSearch, &QLineEdit::returnPressed, this, &FindBarBase::findNext);
83 
84  mStatus = new QLabel;
85  mStatus->setObjectName(QStringLiteral("status"));
86  mStatus->setTextFormat(Qt::PlainText);
87  QFontMetrics fm(mStatus->font());
88  mNotFoundString = i18n("Phrase not found");
89  mStatus->setFixedWidth(fm.boundingRect(mNotFoundString).width());
90  lay->addWidget(mStatus);
91 
93  hide();
94 }
95 
96 FindBarBase::~FindBarBase() = default;
97 
98 QMenu *FindBarBase::optionsMenu() const
99 {
100  return mOptionsMenu;
101 }
102 
103 QString FindBarBase::text() const
104 {
105  return mSearch->text();
106 }
107 
108 void FindBarBase::setText(const QString &text)
109 {
110  mSearch->setText(text);
111  addToCompletion(text);
112 }
113 
114 void FindBarBase::focusAndSetCursor()
115 {
116  setFocus();
117  mStatus->clear();
118  mSearch->selectAll();
119  mSearch->setFocus();
120 }
121 
122 void FindBarBase::slotClearSearch()
123 {
124  clearSelections();
125 }
126 
127 void FindBarBase::autoSearch(const QString &str)
128 {
129  const bool isNotEmpty = (!str.isEmpty());
130  mFindPrevBtn->setEnabled(isNotEmpty);
131  mFindNextBtn->setEnabled(isNotEmpty);
132  if (isNotEmpty) {
133  QTimer::singleShot(0, this, [this]() {
134  slotSearchText();
135  });
136  } else {
137  mStatus->clear();
138  clearSelections();
139  }
140 }
141 
142 void FindBarBase::slotSearchText(bool backward, bool isAutoSearch)
143 {
144  searchText(backward, isAutoSearch);
145 }
146 
147 void FindBarBase::setFoundMatch(bool match)
148 {
149 #ifndef QT_NO_STYLE_STYLESHEET
150  QString styleSheet;
151 
152  if (!mSearch->text().isEmpty()) {
153  if (mNegativeBackground.isEmpty()) {
155  mPositiveBackground = QStringLiteral("QLineEdit{ background-color:%1 }").arg(bgBrush.brush(mSearch->palette()).color().name());
157  mNegativeBackground = QStringLiteral("QLineEdit{ background-color:%1 }").arg(bgBrush.brush(mSearch->palette()).color().name());
158  }
159  if (match) {
160  styleSheet = mPositiveBackground;
161  mStatus->clear();
162  } else {
163  styleSheet = mNegativeBackground;
164  mStatus->setText(mNotFoundString);
165  }
166  }
167  mSearch->setStyleSheet(styleSheet);
168 #endif
169 }
170 
171 void FindBarBase::searchText(bool backward, bool isAutoSearch)
172 {
173  Q_UNUSED(backward)
174  Q_UNUSED(isAutoSearch)
175 }
176 
177 void FindBarBase::addToCompletion(const QString &text)
178 {
179  mSearch->addCompletionItem(text);
180 }
181 
182 void FindBarBase::findNext()
183 {
184  searchText(false, false);
185  addToCompletion(mLastSearchStr);
186 }
187 
188 void FindBarBase::findPrev()
189 {
190  searchText(true, false);
191  addToCompletion(mLastSearchStr);
192 }
193 
194 void FindBarBase::caseSensitivityChanged(bool b)
195 {
196  updateSensitivity(b);
197 }
198 
199 void FindBarBase::updateSensitivity(bool)
200 {
201 }
202 
203 void FindBarBase::slotHighlightAllChanged(bool b)
204 {
205  updateHighLight(b);
206 }
207 
208 void FindBarBase::updateHighLight(bool)
209 {
210 }
211 
212 void FindBarBase::clearSelections()
213 {
214  setFoundMatch(false);
215 }
216 
217 void FindBarBase::closeBar()
218 {
219  // Make sure that all old searches are cleared
220  mSearch->clear();
221  clearSelections();
222  mSearch->clearFocus();
223  Q_EMIT hideFindBar();
224 }
225 
226 bool FindBarBase::event(QEvent *e)
227 {
228  // Close the bar when pressing Escape.
229  // Not using a QShortcut for this because it could conflict with
230  // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
231  // With a shortcut override we can catch this before it gets to kactions.
232  const bool shortCutOverride = (e->type() == QEvent::ShortcutOverride);
233  if (shortCutOverride || e->type() == QEvent::KeyPress) {
234  auto kev = static_cast<QKeyEvent *>(e);
235  if (kev->key() == Qt::Key_Escape) {
236  if (shortCutOverride) {
237  e->accept();
238  return true;
239  }
240  e->accept();
241  closeBar();
242  return true;
243  } else if (kev->key() == Qt::Key_Enter || kev->key() == Qt::Key_Return) {
244  e->accept();
245  if (shortCutOverride) {
246  return true;
247  }
248  if (mSearch->text().isEmpty()) {
249  return true;
250  }
251  if (kev->modifiers() & Qt::ShiftModifier) {
252  findPrev();
253  } else if (kev->modifiers() == Qt::NoModifier) {
254  findNext();
255  }
256  return true;
257  }
258  }
259  return QWidget::event(e);
260 }
ShiftModifier
ShortcutOverride
QEvent::Type type() const const
PlainText
const QList< QKeySequence > & findPrev()
void textChanged(const QString &text)
void toggled(bool checked)
void clear()
const QList< QKeySequence > & findNext()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
bool isEmpty() const const
void clicked(bool checked)
void accept()
QString i18n(const char *text, const TYPE &arg...)
void returnPressed()
bool match(const MessageItem *item) const
Returns true if the specified parameters match this filter and false otherwise.
Definition: filter.cpp:67
The FindBarBase class.
Definition: findbarbase.h:24
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual bool event(QEvent *event) override
Key_Escape
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 23:04:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.