• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KHTML

  • sources
  • kde-4.14
  • kdelibs
  • khtml
  • ui
  • findbar
khtmlfindbar.cpp
Go to the documentation of this file.
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 <germain@ebooksfrance.org>
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 <QtGui/QMenu>
30 #include <QtGui/QLineEdit>
31 
32 #define d this
33 
34 KHTMLFindBar::KHTMLFindBar( QWidget *parent ) :
35  KHTMLViewBarWidget( true, parent ),
36  m_enabled( KFind::WholeWordsOnly | KFind::FromCursor | KFind::SelectedText | KFind::CaseSensitive | KFind::FindBackwards | KFind::RegularExpression | KHTMLPart::FindLinksOnly )
37 {
38  setupUi( centralWidget() );
39 
40  m_next->setIcon( KIcon( "go-down-search" ) );
41  m_previous->setIcon( KIcon( "go-up-search" ) );
42  m_next->setDisabled( true );
43  m_previous->setDisabled( true );
44 
45  // Fill options menu
46  m_incMenu = new QMenu();
47  m_options->setMenu(m_incMenu);
48  m_caseSensitive = m_incMenu->addAction(i18n("C&ase sensitive"));
49  m_caseSensitive->setCheckable(true);
50  m_wholeWordsOnly = m_incMenu->addAction(i18n("&Whole words only"));
51  m_wholeWordsOnly->setCheckable(true);
52  m_fromCursor = m_incMenu->addAction(i18n("From c&ursor"));
53  m_fromCursor->setCheckable(true);
54  m_selectedText = m_incMenu->addAction(i18n("&Selected text"));
55  m_selectedText->setCheckable(true);
56  m_regExp = m_incMenu->addAction(i18n("Regular e&xpression"));
57  m_regExp->setCheckable(true);
58  m_findLinksOnly = m_incMenu->addAction(i18n("Find &links only"));
59  m_findLinksOnly->setCheckable(true);
60 
61  m_atEnd = false;
62 
63  m_find->setDuplicatesEnabled( false );
64  centralWidget()->setFocusProxy( m_find );
65 
66  connect( m_selectedText, SIGNAL(toggled(bool)), this, SLOT(slotSelectedTextToggled(bool)) );
67  connect( m_find, SIGNAL(editTextChanged(QString)), this, SIGNAL(searchChanged()) );
68  connect( m_find->lineEdit(), SIGNAL(clearButtonClicked()), this, SLOT(slotAddPatternToHistory()) );
69  connect( this, SIGNAL(hideMe()), this, SLOT(slotAddPatternToHistory()) );
70  connect( this, SIGNAL(searchChanged()), this, SLOT(slotSearchChanged()) );
71  connect( m_next, SIGNAL(clicked()), this, SIGNAL(findNextClicked()) );
72  connect( m_previous, SIGNAL(clicked()), this, SIGNAL(findPreviousClicked()) );
73  connect( m_caseSensitive, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
74  connect( m_wholeWordsOnly, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
75  connect( m_fromCursor, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
76  connect( m_regExp, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
77  connect( m_findLinksOnly, SIGNAL(changed()), this, SIGNAL(searchChanged()) );
78 
79  m_find->setFocus();
80 }
81 
82 QStringList KHTMLFindBar::findHistory() const
83 {
84  return d->m_find->historyItems();
85 }
86 
87 long KHTMLFindBar::options() const
88 {
89  long options = 0;
90 
91  if (d->m_caseSensitive->isChecked())
92  options |= KFind::CaseSensitive;
93  if (d->m_wholeWordsOnly->isChecked())
94  options |= KFind::WholeWordsOnly;
95  if (d->m_fromCursor->isChecked())
96  options |= KFind::FromCursor;
97  if (d->m_selectedText->isChecked())
98  options |= KFind::SelectedText;
99  if (d->m_regExp->isChecked())
100  options |= KFind::RegularExpression;
101  if (d->m_findLinksOnly->isChecked())
102  options |= KHTMLPart::FindLinksOnly;
103  return options | KHTMLPart::FindNoPopups /* | KFind::FindIncremental */;
104 }
105 
106 QString KHTMLFindBar::pattern() const
107 {
108  return m_find->currentText();
109 }
110 
111 void KHTMLFindBar::slotSearchChanged()
112 {
113  // reset background color of the combo box
114  if (pattern().isEmpty()) {
115  d->m_find->setPalette(QPalette());
116  m_next->setDisabled( true );
117  m_previous->setDisabled( true );
118  m_statusLabel->clear();
119  } else {
120  m_prevPattern = pattern();
121  m_next->setDisabled( false );
122  m_previous->setDisabled( false );
123  }
124 }
125 
126 bool KHTMLFindBar::restoreLastPatternFromHistory()
127 {
128  if (d->m_find->historyItems().isEmpty())
129  return false;
130  d->m_find->lineEdit()->setText( d->m_find->historyItems().first() );
131  return true;
132 }
133 
134 void KHTMLFindBar::setFindHistory(const QStringList &strings)
135 {
136  if (strings.count() > 0)
137  {
138  d->m_find->setHistoryItems(strings, true);
139  //d->m_find->lineEdit()->setText( strings.first() );
140  //d->m_find->lineEdit()->selectAll();
141  }
142  else
143  d->m_find->clearHistory();
144 }
145 
146 void KHTMLFindBar::setHasSelection(bool hasSelection)
147 {
148  if (hasSelection) d->m_enabled |= KFind::SelectedText;
149  else d->m_enabled &= ~KFind::SelectedText;
150  d->m_selectedText->setEnabled( hasSelection );
151  if ( !hasSelection )
152  {
153  d->m_selectedText->setChecked( false );
154  slotSelectedTextToggled( hasSelection );
155  }
156 }
157 
158 void KHTMLFindBar::slotAddPatternToHistory()
159 {
160  bool patternIsEmpty = pattern().isEmpty();
161  if (!patternIsEmpty || !m_prevPattern.isEmpty()) {
162  d->m_find->addToHistory(pattern().isEmpty() ? m_prevPattern : pattern());
163  if (patternIsEmpty && !pattern().isEmpty()) {
164  // ### Hack - addToHistory sometimes undo the clearing of the lineEdit
165  // with clear button. Clear it again.
166  bool sb = d->m_find->blockSignals(true);
167  d->m_find->lineEdit()->setText(QString());
168  d->m_find->blockSignals(sb);
169  }
170  m_prevPattern.clear();
171  }
172 }
173 
174 void KHTMLFindBar::slotSelectedTextToggled(bool selec)
175 {
176  // From cursor doesn't make sense if we have a selection
177  m_fromCursor->setEnabled( !selec && (m_enabled & KFind::FromCursor) );
178  if ( selec ) // uncheck if disabled
179  m_fromCursor->setChecked( false );
180 }
181 
182 void KHTMLFindBar::setHasCursor(bool hasCursor)
183 {
184  if (hasCursor) d->m_enabled |= KFind::FromCursor;
185  else d->m_enabled &= ~KFind::FromCursor;
186  d->m_fromCursor->setEnabled( hasCursor );
187  d->m_fromCursor->setChecked( hasCursor && (options() & KFind::FromCursor) );
188 }
189 
190 void KHTMLFindBar::setOptions(long options)
191 {
192  d->m_caseSensitive->setChecked((d->m_enabled & KFind::CaseSensitive) && (options & KFind::CaseSensitive));
193  d->m_wholeWordsOnly->setChecked((d->m_enabled & KFind::WholeWordsOnly) && (options & KFind::WholeWordsOnly));
194  d->m_fromCursor->setChecked((d->m_enabled & KFind::FromCursor) && (options & KFind::FromCursor));
195  d->m_selectedText->setChecked((d->m_enabled & KFind::SelectedText) && (options & KFind::SelectedText));
196  d->m_regExp->setChecked((d->m_enabled & KFind::RegularExpression) && (options & KFind::RegularExpression));
197  d->m_findLinksOnly->setChecked((d->m_enabled & KHTMLPart::FindLinksOnly) && (options & KHTMLPart::FindLinksOnly));
198 }
199 
200 void KHTMLFindBar::setFoundMatch( bool match )
201 {
202  if ( pattern().isEmpty() ) {
203  m_find->setPalette(QPalette());
204  m_next->setDisabled( true );
205  m_previous->setDisabled( true );
206  m_statusLabel->clear();
207  } else if ( !match ) {
208  QPalette newPal( m_find->palette() );
209  KColorScheme::adjustBackground(newPal, KColorScheme::NegativeBackground);
210  m_find->setPalette(newPal);
211  m_statusLabel->setText(i18n("Not found"));
212  } else {
213  QPalette newPal( m_find->palette() );
214  KColorScheme::adjustBackground(newPal, KColorScheme::PositiveBackground);
215  m_find->setPalette(newPal);
216  m_statusLabel->clear();
217  }
218 }
219 
220 void KHTMLFindBar::setAtEnd( bool atEnd )
221 {
222  if (atEnd == m_atEnd)
223  return;
224  if ( atEnd ) {
225  m_statusLabel->setText( i18n( "No more matches for this search direction." ) );
226  } else {
227  m_statusLabel->clear();
228  }
229  m_atEnd = atEnd;
230 }
231 
232 void KHTMLFindBar::setVisible( bool visible )
233 {
234  KHTMLViewBarWidget::setVisible( visible );
235 
236  if ( visible ) {
237  m_find->setFocus( Qt::ActiveWindowFocusReason );
238  m_find->lineEdit()->selectAll();
239  }
240 }
241 
242 bool KHTMLFindBar::event(QEvent* e)
243 {
244  // Close the bar when pressing Escape.
245  // Not using a QShortcut for this because it could conflict with
246  // window-global actions (e.g. Emil Sedgh binds Esc to "close tab").
247  // With a shortcut override we can catch this before it gets to kactions.
248  if (e->type() == QEvent::ShortcutOverride) {
249  QKeyEvent* kev = static_cast<QKeyEvent* >(e);
250  if (kev->key() == Qt::Key_Escape) {
251  e->accept();
252  emit hideMe();
253  return true;
254  }
255  }
256  return KHTMLViewBarWidget::event(e);
257 }
KHTMLFindBar::setFoundMatch
void setFoundMatch(bool match)
Definition: khtmlfindbar.cpp:200
i18n
QString i18n(const char *text)
QEvent
QWidget
QEvent::type
Type type() const
KHTMLFindBar::restoreLastPatternFromHistory
bool restoreLastPatternFromHistory()
Definition: khtmlfindbar.cpp:126
QWidget::setupUi
void setupUi(QWidget *widget)
KHTMLViewBarWidget::hideMe
void hideMe()
KHTMLFindBar::setFindHistory
void setFindHistory(const QStringList &history)
Provide the list of strings to be displayed as the history of find strings.
Definition: khtmlfindbar.cpp:134
khtmlfindbar.h
KHTMLFindBar::setHasCursor
void setHasCursor(bool hasCursor)
Hide/show the 'from cursor' option, depending on whether the application implements a cursor...
Definition: khtmlfindbar.cpp:182
khtml_part.h
QAction::setChecked
void setChecked(bool)
KHTMLViewBarWidget
Definition: khtmlviewbarwidget.h:25
QMenu::addAction
void addAction(QAction *action)
KHTMLPart::FindNoPopups
Definition: khtml_part.h:790
KHTMLPart
This class is khtml's main class.
Definition: khtml_part.h:206
QWidget::setVisible
virtual void setVisible(bool visible)
KFind::WholeWordsOnly
KHTMLFindBar::KHTMLFindBar
KHTMLFindBar(QWidget *parent=0)
Definition: khtmlfindbar.cpp:34
KHTMLFindBar::findHistory
QStringList findHistory() const
Returns the list of history items.
Definition: khtmlfindbar.cpp:82
kfind.h
d
#define d
Definition: khtmlfindbar.cpp:32
KHTMLFindBar::setOptions
void setOptions(long options)
Set the options which are checked.
Definition: khtmlfindbar.cpp:190
KFind::RegularExpression
KHTMLFindBar::setAtEnd
void setAtEnd(bool atEnd)
Definition: khtmlfindbar.cpp:220
QString::clear
void clear()
KFind::FromCursor
QList::count
int count(const T &value) const
KHTMLFindBar::setVisible
virtual void setVisible(bool visible)
Definition: khtmlfindbar.cpp:232
KHTMLFindBar::options
long options() const
Returns the state of the options.
Definition: khtmlfindbar.cpp:87
KColorScheme::PositiveBackground
QWidget::setFocusProxy
void setFocusProxy(QWidget *w)
KHTMLFindBar::searchChanged
void searchChanged()
QString::isEmpty
bool isEmpty() const
KIcon
KHTMLPart::FindLinksOnly
Definition: khtml_part.h:789
QString
KHTMLFindBar::setHasSelection
void setHasSelection(bool hasSelection)
Enable/disable the 'search in selection' option, depending on whether there actually is a selection...
Definition: khtmlfindbar.cpp:146
KHTMLFindBar::findNextClicked
void findNextClicked()
QStringList
QKeyEvent::key
int key() const
QMenu
QEvent::accept
void accept()
KFind
QAction::setCheckable
void setCheckable(bool)
KColorScheme::NegativeBackground
KFind::SelectedText
QKeyEvent
KHTMLFindBar::findPreviousClicked
void findPreviousClicked()
KColorScheme::adjustBackground
static void adjustBackground(QPalette &, BackgroundRole newRole=NormalBackground, QPalette::ColorRole color=QPalette::Base, ColorSet set=View, KSharedConfigPtr=KSharedConfigPtr())
KFind::CaseSensitive
KHTMLViewBarWidget::centralWidget
QWidget * centralWidget()
Definition: khtmlviewbarwidget.h:35
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KHTMLFindBar::event
virtual bool event(QEvent *e)
Definition: khtmlfindbar.cpp:242
QWidget::event
virtual bool event(QEvent *event)
kcolorscheme.h
KHTMLFindBar::pattern
QString pattern() const
Returns the pattern to find.
Definition: khtmlfindbar.cpp:106
QAction::setEnabled
void setEnabled(bool)
QPalette
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KHTML

Skip menu "KHTML"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal