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

KDEUI

  • kdeui
  • itemviews
klistwidgetsearchline.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (c) 2003 Scott Wheeler <wheeler@kde.org>
3 Copyright (c) 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "klistwidgetsearchline.h"
21
22#include <QtGui/QListWidget>
23#include <QtGui/QApplication>
24#include <QtGui/QKeyEvent>
25#include <QtCore/QEvent>
26
27#include <QtCore/QTimer>
28#include <kdebug.h>
29
30#define DEFAULT_CASESENSITIVE Qt::CaseInsensitive
31
32class KListWidgetSearchLine::KListWidgetSearchLinePrivate
33{
34public:
35 KListWidgetSearchLinePrivate(KListWidgetSearchLine *parent) :
36 q( parent ),
37 listWidget( 0 ),
38 caseSensitivity( DEFAULT_CASESENSITIVE ),
39 activeSearch( false ),
40 queuedSearches( 0 )
41 {}
42
43 void _k_listWidgetDeleted();
44 void _k_queueSearch(const QString&);
45 void _k_activateSearch();
46 void _k_rowsInserted(const QModelIndex&, int, int);
47 void _k_dataChanged(const QModelIndex&, const QModelIndex&);
48
49 void init( QListWidget *listWidget = 0 );
50 void updateHiddenState( int start, int end );
51
52 KListWidgetSearchLine *q;
53 QListWidget *listWidget;
54 Qt::CaseSensitivity caseSensitivity;
55 bool activeSearch;
56 QString search;
57 int queuedSearches;
58};
59
60/******************************************************************************
61 * Public Methods *
62 *****************************************************************************/
63KListWidgetSearchLine::KListWidgetSearchLine( QWidget *parent, QListWidget *listWidget ) :
64 KLineEdit( parent ),
65 d( new KListWidgetSearchLinePrivate(this) )
66
67{
68 d->init( listWidget );
69}
70
71KListWidgetSearchLine::~KListWidgetSearchLine()
72{
73 clear(); // returning items back to listWidget
74 delete d;
75}
76
77Qt::CaseSensitivity KListWidgetSearchLine::caseSensitive() const
78{
79 return d->caseSensitivity;
80}
81
82QListWidget *KListWidgetSearchLine::listWidget() const
83{
84 return d->listWidget;
85}
86
87/******************************************************************************
88 * Public Slots *
89 *****************************************************************************/
90void KListWidgetSearchLine::updateSearch( const QString &s )
91{
92 d->search = s.isNull() ? text() : s;
93 if( d->listWidget ) {
94 d->updateHiddenState( 0, d->listWidget->count() - 1 );
95 }
96}
97
98void KListWidgetSearchLine::clear()
99{
100 // Show items back to QListWidget
101 if ( d->listWidget != 0 ) {
102 for (int i = 0 ; i < d->listWidget->count(); ++i) {
103 d->listWidget->item( i )->setHidden( false );
104 }
105 }
106
107 d->search = "";
108 d->queuedSearches = 0;
109 KLineEdit::clear();
110}
111
112void KListWidgetSearchLine::setCaseSensitivity( Qt::CaseSensitivity cs )
113{
114 d->caseSensitivity = cs;
115}
116
117void KListWidgetSearchLine::setListWidget( QListWidget *lw )
118{
119 if ( d->listWidget != 0 ) {
120 disconnect( d->listWidget, SIGNAL(destroyed()),
121 this, SLOT(_k_listWidgetDeleted()) );
122 d->listWidget->model()->disconnect( this );
123 }
124
125 d->listWidget = lw;
126
127 if ( lw != 0 ) {
128 connect( d->listWidget, SIGNAL(destroyed()),
129 this, SLOT(_k_listWidgetDeleted()) );
130 connect( d->listWidget->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
131 this, SLOT(_k_rowsInserted(QModelIndex,int,int)) );
132 connect( d->listWidget->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
133 this, SLOT(_k_dataChanged(QModelIndex,QModelIndex)) );
134 setEnabled( true );
135 } else
136 setEnabled( false );
137}
138
139/******************************************************************************
140 * Protected Methods *
141 *****************************************************************************/
142bool KListWidgetSearchLine::itemMatches( const QListWidgetItem *item,
143 const QString &s ) const
144{
145 if ( s.isEmpty() )
146 return true;
147
148 if ( item == 0 )
149 return false;
150
151 return ( item->text().indexOf( s, 0,
152 caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive ) >= 0 );
153}
154
155void KListWidgetSearchLine::KListWidgetSearchLinePrivate::init( QListWidget *_listWidget )
156{
157 listWidget = _listWidget;
158
159 connect( q, SIGNAL(textChanged(QString)),
160 q, SLOT(_k_queueSearch(QString)) );
161
162 if ( listWidget != 0 ) {
163 connect( listWidget, SIGNAL(destroyed()),
164 q, SLOT(_k_listWidgetDeleted()) );
165 connect( listWidget->model(), SIGNAL(rowsInserted(QModelIndex,int,int)),
166 q, SLOT(_k_rowsInserted(QModelIndex,int,int)) );
167 connect( listWidget->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
168 q, SLOT(_k_dataChanged(QModelIndex,QModelIndex)) );
169 q->setEnabled( true );
170 } else {
171 q->setEnabled( false );
172 }
173
174 q->setClearButtonShown(true);
175}
176
177void KListWidgetSearchLine::KListWidgetSearchLinePrivate::updateHiddenState( int start, int end ) {
178 if ( !listWidget ) {
179 return;
180 }
181
182 QListWidgetItem *currentItem = listWidget->currentItem();
183
184 // Remove Non-Matching items
185 for( int index = start; index <= end; ++index ) {
186 QListWidgetItem *item = listWidget->item(index);
187 if ( ! q->itemMatches( item, search ) ) {
188 item->setHidden( true );
189
190 if ( item == currentItem ) {
191 currentItem = 0; // It's not in listWidget anymore.
192 }
193 } else if ( item->isHidden() ) {
194 item->setHidden( false );
195 }
196 }
197
198 if ( listWidget->isSortingEnabled() ) {
199 listWidget->sortItems();
200 }
201
202 if ( currentItem != 0 ) {
203 listWidget->scrollToItem( currentItem );
204 }
205}
206
207bool KListWidgetSearchLine::event(QEvent *event) {
208
209 if (event->type() == QEvent::KeyPress) {
210 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
211 if(keyEvent->matches(QKeySequence::MoveToNextLine) || keyEvent->matches(QKeySequence::SelectNextLine) ||
212 keyEvent->matches(QKeySequence::MoveToPreviousLine) || keyEvent->matches(QKeySequence::SelectPreviousLine) ||
213 keyEvent->matches(QKeySequence::MoveToNextPage) || keyEvent->matches(QKeySequence::SelectNextPage) ||
214 keyEvent->matches(QKeySequence::MoveToPreviousPage) || keyEvent->matches(QKeySequence::SelectPreviousPage)
215 )
216 {
217 if(d->listWidget) {
218 QApplication::sendEvent(d->listWidget, event);
219 return true;
220 }
221 }
222 else if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return ) {
223 if(!trapReturnKey() ) {
224 if(d->listWidget) {
225 QApplication::sendEvent(d->listWidget, event);
226 return true;
227 }
228 }
229 }
230 }
231 return KLineEdit::event(event);
232}
233/******************************************************************************
234 * Protected Slots *
235 *****************************************************************************/
236void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_queueSearch( const QString &s )
237{
238 queuedSearches++;
239 search = s;
240 QTimer::singleShot( 200, q, SLOT(_k_activateSearch()) );
241}
242
243void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_activateSearch()
244{
245 queuedSearches--;
246
247 if ( queuedSearches <= 0 ) {
248 q->updateSearch( search );
249 queuedSearches = 0;
250 }
251}
252
253/******************************************************************************
254 * Private Slots *
255 *****************************************************************************/
256void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_listWidgetDeleted()
257{
258 listWidget = 0;
259 q->setEnabled( false );
260}
261
262
263void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_rowsInserted( const QModelIndex &parent, int start, int end )
264{
265 if( parent.isValid() ) {
266 return;
267 }
268
269 updateHiddenState( start, end );
270}
271
272void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_dataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
273{
274 if( topLeft.parent().isValid() ) {
275 return;
276 }
277
278 updateHiddenState( topLeft.row(), bottomRight.row() );
279}
280
281
282
283#include "klistwidgetsearchline.moc"
KLineEdit
An enhanced QLineEdit widget for inputting text.
Definition: klineedit.h:150
KLineEdit::trapReturnKey
bool trapReturnKey() const
Definition: klineedit.cpp:1421
KLineEdit::event
virtual bool event(QEvent *)
Re-implemented for internal reasons.
Definition: klineedit.cpp:1358
KLineEdit::clear
virtual void clear()
Reimplemented to workaround a buggy QLineEdit::clear() (changing the clipboard to the text we just ha...
Definition: klineedit.cpp:1698
KListWidgetSearchLine
This class makes it easy to add a search line for filtering the items in a listwidget based on a simp...
Definition: klistwidgetsearchline.h:38
KListWidgetSearchLine::clear
void clear()
Clear line edit and empty hiddenItems, returning elements to listWidget.
Definition: klistwidgetsearchline.cpp:98
KListWidgetSearchLine::updateSearch
virtual void updateSearch(const QString &s=QString())
Updates search to only make visible the items that match s.
Definition: klistwidgetsearchline.cpp:90
KListWidgetSearchLine::~KListWidgetSearchLine
virtual ~KListWidgetSearchLine()
Destroys the KListWidgetSearchLine.
Definition: klistwidgetsearchline.cpp:71
KListWidgetSearchLine::setCaseSensitivity
void setCaseSensitivity(Qt::CaseSensitivity cs)
Make the search case sensitive or case insensitive.
Definition: klistwidgetsearchline.cpp:112
KListWidgetSearchLine::event
virtual bool event(QEvent *event)
Re-implemented for internal reasons.
Definition: klistwidgetsearchline.cpp:207
KListWidgetSearchLine::KListWidgetSearchLine
KListWidgetSearchLine(QWidget *parent=0, QListWidget *listWidget=0)
Constructs a KListWidgetSearchLine with listWidget being the QListWidget to be filtered.
Definition: klistwidgetsearchline.cpp:63
KListWidgetSearchLine::listWidget
QListWidget * listWidget() const
Returns the listWidget that is currently filtered by the search.
Definition: klistwidgetsearchline.cpp:82
KListWidgetSearchLine::caseSensitive
Qt::CaseSensitivity caseSensitive() const
Returns if the search is case sensitive.
Definition: klistwidgetsearchline.cpp:77
KListWidgetSearchLine::itemMatches
virtual bool itemMatches(const QListWidgetItem *item, const QString &s) const
Returns true if item matches the search s.
Definition: klistwidgetsearchline.cpp:142
KListWidgetSearchLine::setListWidget
void setListWidget(QListWidget *lv)
Sets the QListWidget that is filtered by this search line.
Definition: klistwidgetsearchline.cpp:117
QListWidget
QWidget
kdebug.h
DEFAULT_CASESENSITIVE
#define DEFAULT_CASESENSITIVE
Definition: klistwidgetsearchline.cpp:30
klistwidgetsearchline.h
KStandardShortcut::end
const KShortcut & end()
Goto end of the document.
Definition: kstandardshortcut.cpp:348
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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