KHtml

khtmlfindbar.cpp
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2008 Bernhard Beschow <bbeschow cs tu berlin de>
4  * (C) 2008 Germain Garand <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "khtmlfindbar.h"
23 
24 #include "khtml_part.h"
25 
26 #include <kfind.h>
27 #include <kcolorscheme.h>
28 
29 #include <QKeyEvent>
30 #include <QMenu>
31 #include <QLineEdit>
32 
33 #define d this
34 
35 KHTMLFindBar::KHTMLFindBar(QWidget *parent) :
36  KHTMLViewBarWidget(true, parent),
37  m_enabled(KFind::WholeWordsOnly | KFind::FromCursor | KFind::SelectedText | KFind::CaseSensitive | KFind::FindBackwards | KFind::RegularExpression | KHTMLPart::FindLinksOnly)
38 {
39  setupUi(centralWidget());
40 
41  m_next->setIcon(QIcon::fromTheme("go-down-search"));
42  m_previous->setIcon(QIcon::fromTheme("go-up-search"));
43  m_next->setDisabled(true);
44  m_previous->setDisabled(true);
45 
46  // Fill options menu
47  m_incMenu = new QMenu(this);
48  m_options->setMenu(m_incMenu);
49  m_caseSensitive = m_incMenu->addAction(i18n("C&ase sensitive"));
50  m_caseSensitive->setCheckable(true);
51  m_wholeWordsOnly = m_incMenu->addAction(i18n("&Whole words only"));
52  m_wholeWordsOnly->setCheckable(true);
53  m_fromCursor = m_incMenu->addAction(i18n("From c&ursor"));
54  m_fromCursor->setCheckable(true);
55  m_selectedText = m_incMenu->addAction(i18n("&Selected text"));
56  m_selectedText->setCheckable(true);
57  m_regExp = m_incMenu->addAction(i18n("Regular e&xpression"));
58  m_regExp->setCheckable(true);
59  m_findLinksOnly = m_incMenu->addAction(i18n("Find &links only"));
60  m_findLinksOnly->setCheckable(true);
61 
62  m_atEnd = false;
63 
64  m_find->setDuplicatesEnabled(false);
65  centralWidget()->setFocusProxy(m_find);
66 
67  connect(m_selectedText, SIGNAL(toggled(bool)), this, SLOT(slotSelectedTextToggled(bool)));
68  connect(m_find, SIGNAL(editTextChanged(QString)), this, SIGNAL(searchChanged()));
69  connect(m_find->lineEdit(), SIGNAL(clearButtonClicked()), this, SLOT(slotAddPatternToHistory()));
70  connect(this, SIGNAL(hideMe()), this, SLOT(slotAddPatternToHistory()));
71  connect(this, SIGNAL(searchChanged()), this, SLOT(slotSearchChanged()));
72  connect(m_next, SIGNAL(clicked()), this, SIGNAL(findNextClicked()));
73  connect(m_previous, SIGNAL(clicked()), this, SIGNAL(findPreviousClicked()));
74  connect(m_caseSensitive, SIGNAL(changed()), this, SIGNAL(searchChanged()));
75  connect(m_wholeWordsOnly, SIGNAL(changed()), this, SIGNAL(searchChanged()));
76  connect(m_fromCursor, SIGNAL(changed()), this, SIGNAL(searchChanged()));
77  connect(m_regExp, SIGNAL(changed()), this, SIGNAL(searchChanged()));
78  connect(m_findLinksOnly, SIGNAL(changed()), this, SIGNAL(searchChanged()));
79 
80  m_find->setFocus();
81 }
82 
83 QStringList KHTMLFindBar::findHistory() const
84 {
85  return d->m_find->historyItems();
86 }
87 
88 long KHTMLFindBar::options() const
89 {
90  long options = 0;
91 
92  if (d->m_caseSensitive->isChecked()) {
93  options |= KFind::CaseSensitive;
94  }
95  if (d->m_wholeWordsOnly->isChecked()) {
96  options |= KFind::WholeWordsOnly;
97  }
98  if (d->m_fromCursor->isChecked()) {
99  options |= KFind::FromCursor;
100  }
101  if (d->m_selectedText->isChecked()) {
102  options |= KFind::SelectedText;
103  }
104  if (d->m_regExp->isChecked()) {
105  options |= KFind::RegularExpression;
106  }
107  if (d->m_findLinksOnly->isChecked()) {
108  options |= KHTMLPart::FindLinksOnly;
109  }
110  return options | KHTMLPart::FindNoPopups /* | KFind::FindIncremental */;
111 }
112 
113 QString KHTMLFindBar::pattern() const
114 {
115  return m_find->currentText();
116 }
117 
118 void KHTMLFindBar::slotSearchChanged()
119 {
120  // reset background color of the combo box
121  if (pattern().isEmpty()) {
122  d->m_find->setPalette(QPalette());
123  m_next->setDisabled(true);
124  m_previous->setDisabled(true);
125  m_statusLabel->clear();
126  } else {
127  m_prevPattern = pattern();
128  m_next->setDisabled(false);
129  m_previous->setDisabled(false);
130  }
131 }
132 
133 bool KHTMLFindBar::restoreLastPatternFromHistory()
134 {
135  if (d->m_find->historyItems().isEmpty()) {
136  return false;
137  }
138  d->m_find->lineEdit()->setText(d->m_find->historyItems().first());
139  return true;
140 }
141 
142 void KHTMLFindBar::setFindHistory(const QStringList &strings)
143 {
144  if (strings.count() > 0) {
145  d->m_find->setHistoryItems(strings, true);
146  //d->m_find->lineEdit()->setText( strings.first() );
147  //d->m_find->lineEdit()->selectAll();
148  } else {
149  d->m_find->clearHistory();
150  }
151 }
152 
153 void KHTMLFindBar::setHasSelection(bool hasSelection)
154 {
155  if (hasSelection) {
156  d->m_enabled |= KFind::SelectedText;
157  } else {
158  d->m_enabled &= ~KFind::SelectedText;
159  }
160  d->m_selectedText->setEnabled(hasSelection);
161  if (!hasSelection) {
162  d->m_selectedText->setChecked(false);
163  slotSelectedTextToggled(hasSelection);
164  }
165 }
166 
167 void KHTMLFindBar::slotAddPatternToHistory()
168 {
169  bool patternIsEmpty = pattern().isEmpty();
170  if (!patternIsEmpty || !m_prevPattern.isEmpty()) {
171  d->m_find->addToHistory(pattern().isEmpty() ? m_prevPattern : pattern());
172  if (patternIsEmpty && !pattern().isEmpty()) {
173  // ### Hack - addToHistory sometimes undo the clearing of the lineEdit
174  // with clear button. Clear it again.
175  bool sb = d->m_find->blockSignals(true);
176  d->m_find->lineEdit()->setText(QString());
177  d->m_find->blockSignals(sb);
178  }
179  m_prevPattern.clear();
180  }
181 }
182 
183 void KHTMLFindBar::slotSelectedTextToggled(bool selec)
184 {
185  // From cursor doesn't make sense if we have a selection
186  m_fromCursor->setEnabled(!selec && (m_enabled & KFind::FromCursor));
187  if (selec) { // uncheck if disabled
188  m_fromCursor->setChecked(false);
189  }
190 }
191 
192 void KHTMLFindBar::setHasCursor(bool hasCursor)
193 {
194  if (hasCursor) {
195  d->m_enabled |= KFind::FromCursor;
196  } else {
197  d->m_enabled &= ~KFind::FromCursor;
198  }
199  d->m_fromCursor->setEnabled(hasCursor);
200  d->m_fromCursor->setChecked(hasCursor && (options() & KFind::FromCursor));
201 }
202 
203 void KHTMLFindBar::setOptions(long options)
204 {
205  d->m_caseSensitive->setChecked((d->m_enabled & KFind::CaseSensitive) && (options & KFind::CaseSensitive));
206  d->m_wholeWordsOnly->setChecked((d->m_enabled & KFind::WholeWordsOnly) && (options & KFind::WholeWordsOnly));
207  d->m_fromCursor->setChecked((d->m_enabled & KFind::FromCursor) && (options & KFind::FromCursor));
208  d->m_selectedText->setChecked((d->m_enabled & KFind::SelectedText) && (options & KFind::SelectedText));
209  d->m_regExp->setChecked((d->m_enabled & KFind::RegularExpression) && (options & KFind::RegularExpression));
210  d->m_findLinksOnly->setChecked((d->m_enabled & KHTMLPart::FindLinksOnly) && (options & KHTMLPart::FindLinksOnly));
211 }
212 
213 void KHTMLFindBar::setFoundMatch(bool match)
214 {
215  if (pattern().isEmpty()) {
216  m_find->setPalette(QPalette());
217  m_next->setDisabled(true);
218  m_previous->setDisabled(true);
219  m_statusLabel->clear();
220  } else if (!match) {
221  QPalette newPal(m_find->palette());
223  m_find->setPalette(newPal);
224  m_statusLabel->setText(i18n("Not found"));
225  } else {
226  QPalette newPal(m_find->palette());
228  m_find->setPalette(newPal);
229  m_statusLabel->clear();
230  }
231 }
232 
233 void KHTMLFindBar::setAtEnd(bool atEnd)
234 {
235  if (atEnd == m_atEnd) {
236  return;
237  }
238  if (atEnd) {
239  m_statusLabel->setText(i18n("No more matches for this search direction."));
240  } else {
241  m_statusLabel->clear();
242  }
243  m_atEnd = atEnd;
244 }
245 
246 void KHTMLFindBar::setVisible(bool visible)
247 {
249 
250  if (visible) {
251  m_find->setFocus(Qt::ActiveWindowFocusReason);
252  m_find->lineEdit()->selectAll();
253  }
254 }
255 
256 bool KHTMLFindBar::event(QEvent *e)
257 {
258  // Close the bar when pressing Escape.
259  // Not using a QShortcut for this because it could conflict with
260  // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
261  // With a shortcut override we can catch this before it gets to kactions.
262  if (e->type() == QEvent::ShortcutOverride) {
263  QKeyEvent *kev = static_cast<QKeyEvent * >(e);
264  if (kev->key() == Qt::Key_Escape) {
265  e->accept();
266  emit hideMe();
267  return true;
268  }
269  }
270  return KHTMLViewBarWidget::event(e);
271 }
ShortcutOverride
QEvent::Type type() const const
This class is khtml&#39;s main class.
Definition: khtml_part.h:208
virtual void setVisible(bool visible)
WholeWordsOnly
ActiveWindowFocusReason
RegularExpression
int count(const T &value) const const
QString pattern(Mode mode=Reading)
CaseSensitive
bool isEmpty() const const
int key() const const
void accept()
QString i18n(const char *text, const TYPE &arg...)
static void adjustBackground(QPalette &, BackgroundRole newRole=NormalBackground, QPalette::ColorRole color=QPalette::Base, ColorSet set=View, KSharedConfigPtr=KSharedConfigPtr())
QIcon fromTheme(const QString &name)
virtual bool event(QEvent *event) override
Key_Escape
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 16 2021 22:47:56 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.