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

KDE3Support

  • sources
  • kde-4.14
  • kdelibs
  • kde3support
  • kdeui
k3iconviewsearchline.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (c) 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
26 #include "k3iconviewsearchline.h"
27 
28 #include <Qt3Support/Q3IconDrag>
29 #include <klocale.h>
30 #include <QtCore/QTimer>
31 #include <kdebug.h>
32 
33 #define DEFAULT_CASESENSITIVE false
34 
35 typedef QList <Q3IconViewItem *> QIconViewItemList;
36 
37 class K3IconViewSearchLine::K3IconViewSearchLinePrivate
38 {
39 public:
40  K3IconViewSearchLinePrivate() :
41  iconView( 0 ),
42  caseSensitive( DEFAULT_CASESENSITIVE ),
43  activeSearch( false ),
44  queuedSearches( 0 ) {}
45 
46  Q3IconView *iconView;
47  bool caseSensitive;
48  bool activeSearch;
49  QString search;
50  int queuedSearches;
51  QIconViewItemList hiddenItems;
52 };
53 
54 /******************************************************************************
55  * Public Methods *
56  *****************************************************************************/
57 K3IconViewSearchLine::K3IconViewSearchLine( QWidget *parent,
58  Q3IconView *iconView ) :
59  KLineEdit( parent )
60 {
61  d = NULL;
62  init( iconView );
63 }
64 
65 K3IconViewSearchLine::~K3IconViewSearchLine()
66 {
67  clear(); // empty hiddenItems, returning items back to iconView
68  delete d;
69 }
70 
71 bool K3IconViewSearchLine::caseSensitive() const
72 {
73  return d->caseSensitive;
74 }
75 
76 Q3IconView *K3IconViewSearchLine::iconView() const
77 {
78  return d->iconView;
79 }
80 
81 /******************************************************************************
82  * Public Slots *
83  *****************************************************************************/
84 void K3IconViewSearchLine::updateSearch( const QString &s )
85 {
86  Q3IconView *iv = d->iconView;
87  if( ! iv )
88  return; // disabled
89 
90  QString search = d->search = s.isNull() ? text() : s;
91 
92  QIconViewItemList *hi = &(d->hiddenItems);
93 
94  Q3IconViewItem *currentItem = iv->currentItem();
95 
96  Q3IconViewItem *item = NULL;
97 
98  // Remove Non-Matching items, add them them to hidden list
99  Q3IconViewItem *i = iv->firstItem();
100  while ( i != NULL )
101  {
102  item = i;
103  i = i->nextItem(); // Point to next, otherwise will loose it.
104  if ( ! itemMatches( item, search ) )
105  {
106  hideItem( item );
107 
108  if ( item == currentItem )
109  currentItem = NULL; // It's not in iconView anymore.
110  }
111  }
112 
113  // Add Matching items, remove from hidden list
114  QIconViewItemList::iterator it = hi->begin();
115  while ( it != hi->end() )
116  {
117  item = *it;
118  ++it;
119  if ( itemMatches( item, search ) )
120  showItem( item );
121  }
122 
123  iv->sort();
124 
125  if ( currentItem != NULL )
126  iv->ensureItemVisible( currentItem );
127 }
128 
129 void K3IconViewSearchLine::clear()
130 {
131  // Clear hidden list, give items back to QIconView, if it still exists
132  Q3IconViewItem *item = NULL;
133  QIconViewItemList::iterator it = d->hiddenItems.begin();
134  while ( it != d->hiddenItems.end() )
135  {
136  item = *it;
137  ++it;
138  if ( item != NULL )
139  {
140  if ( d->iconView != NULL )
141  showItem( item );
142  else
143  delete item;
144  }
145  }
146  if ( ! d->hiddenItems.isEmpty() )
147  kDebug() << __FILE__ << ":" << __LINE__ <<
148  "hiddenItems is not empty as it should be. " <<
149  d->hiddenItems.count() << " items are still there.\n" << endl;
150 
151  d->search = "";
152  d->queuedSearches = 0;
153  KLineEdit::clear();
154 }
155 
156 void K3IconViewSearchLine::setCaseSensitive( bool cs )
157 {
158  d->caseSensitive = cs;
159 }
160 
161 void K3IconViewSearchLine::setIconView( Q3IconView *iv )
162 {
163  if ( d->iconView != NULL )
164  disconnect( d->iconView, SIGNAL(destroyed()),
165  this, SLOT(iconViewDeleted()) );
166 
167  d->iconView = iv;
168 
169  if ( iv != NULL )
170  {
171  connect( d->iconView, SIGNAL(destroyed()),
172  this, SLOT(iconViewDeleted()) );
173  setEnabled( true );
174  }
175  else
176  setEnabled( false );
177 }
178 
179 /******************************************************************************
180  * Protected Methods *
181  *****************************************************************************/
182 bool K3IconViewSearchLine::itemMatches( const Q3IconViewItem *item,
183  const QString &s ) const
184 {
185  if ( s.isEmpty() )
186  return true;
187 
188  if ( item == NULL )
189  return false;
190 
191  return ( item->text().indexOf( s, 0,
192  caseSensitive()?Qt::CaseSensitive:Qt::CaseInsensitive ) >= 0 );
193 }
194 
195 void K3IconViewSearchLine::init( Q3IconView *iconView )
196 {
197  delete d;
198  d = new K3IconViewSearchLinePrivate;
199 
200  d->iconView = iconView;
201 
202  connect( this, SIGNAL(textChanged(QString)),
203  this, SLOT(queueSearch(QString)) );
204 
205  if ( iconView != NULL )
206  {
207  connect( iconView, SIGNAL(destroyed()),
208  this, SLOT(iconViewDeleted()) );
209  setEnabled( true );
210  } else {
211  setEnabled( false );
212  }
213 
214  setClearButtonShown(true);
215 }
216 
217 void K3IconViewSearchLine::hideItem( Q3IconViewItem *item )
218 {
219  if ( ( item == NULL ) || ( d->iconView == NULL ) )
220  return;
221 
222  d->hiddenItems.append( item );
223  d->iconView->takeItem( item );
224 }
225 
226 void K3IconViewSearchLine::showItem( Q3IconViewItem *item )
227 {
228  if ( d->iconView == NULL )
229  {
230  kDebug() << __FILE__ << ":" << __LINE__ <<
231  "showItem() could not be called while there's no iconView set." <<
232  endl;
233  return;
234  }
235  d->iconView->insertItem( item );
236  d->hiddenItems.removeAll( item );
237 }
238 
239 /******************************************************************************
240  * Protected Slots *
241  *****************************************************************************/
242 void K3IconViewSearchLine::queueSearch( const QString &s )
243 {
244  d->queuedSearches++;
245  d->search = s;
246  QTimer::singleShot( 200, this, SLOT(activateSearch()) );
247 }
248 
249 void K3IconViewSearchLine::activateSearch()
250 {
251  d->queuedSearches--;
252 
253  if ( d->queuedSearches <= 0 )
254  {
255  updateSearch( d->search );
256  d->queuedSearches = 0;
257  }
258 }
259 
260 /******************************************************************************
261  * Private Slots *
262  *****************************************************************************/
263 void K3IconViewSearchLine::iconViewDeleted()
264 {
265  d->iconView = NULL;
266  setEnabled( false );
267 }
268 
269 #include "k3iconviewsearchline.moc"
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QWidget
K3IconViewSearchLine::hideItem
void hideItem(Q3IconViewItem *item)
Hide item.
Definition: k3iconviewsearchline.cpp:217
kdebug.h
Q3IconViewItem::nextItem
Q3IconViewItem * nextItem() const
Q3IconViewItem::text
virtual QString text() const
K3IconViewSearchLine::~K3IconViewSearchLine
virtual ~K3IconViewSearchLine()
Destroys the K3IconViewSearchLine.
Definition: k3iconviewsearchline.cpp:65
QLineEdit::text
QString text() const
Q3IconView
QLineEdit::textChanged
void textChanged(const QString &text)
K3IconViewSearchLine::caseSensitive
bool caseSensitive() const
Returns true if the search is case sensitive.
Definition: k3iconviewsearchline.cpp:71
K3IconViewSearchLine::queueSearch
void queueSearch(const QString &s)
When keys are pressed a new search string is created and a timer is activated.
Definition: k3iconviewsearchline.cpp:242
K3IconViewSearchLine::K3IconViewSearchLine
K3IconViewSearchLine(QWidget *parent=0, Q3IconView *iconView=0)
Constructs a K3IconViewSearchLine with iconView being the QIconView to be filtered.
Definition: k3iconviewsearchline.cpp:57
K3IconViewSearchLine::showItem
void showItem(Q3IconViewItem *item)
Show item.
Definition: k3iconviewsearchline.cpp:226
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
KLineEdit::clear
virtual void clear()
QString::isNull
bool isNull() const
QWidget::setEnabled
void setEnabled(bool)
DEFAULT_CASESENSITIVE
#define DEFAULT_CASESENSITIVE
Definition: k3iconviewsearchline.cpp:33
Q3IconView::ensureItemVisible
void ensureItemVisible(Q3IconViewItem *item)
QString::isEmpty
bool isEmpty() const
k3iconviewsearchline.h
K3IconViewSearchLine::init
void init(Q3IconView *iconView=0)
Do initialization common to both constructors.
Definition: k3iconviewsearchline.cpp:195
QString
QList< Q3IconViewItem * >
K3IconViewSearchLine::iconView
Q3IconView * iconView() const
Returns the iconview that is currently filtered by the search.
Definition: k3iconviewsearchline.cpp:76
QList::end
iterator end()
Q3IconViewItem
K3IconViewSearchLine::activateSearch
void activateSearch()
When the timer started with queueSearch() expires this slot is called.
Definition: k3iconviewsearchline.cpp:249
K3IconViewSearchLine::updateSearch
virtual void updateSearch(const QString &s=QString())
Updates search to only make visible the items that match s.
Definition: k3iconviewsearchline.cpp:84
K3IconViewSearchLine::clear
void clear()
Clear line edit and empty hiddenItems, returning elements to iconView.
Definition: k3iconviewsearchline.cpp:129
KLineEdit
K3IconViewSearchLine::setIconView
void setIconView(Q3IconView *iv)
Sets the QIconView that is filtered by this search line.
Definition: k3iconviewsearchline.cpp:161
KLineEdit::setClearButtonShown
void setClearButtonShown(bool show)
K3IconViewSearchLine::itemMatches
virtual bool itemMatches(const Q3IconViewItem *item, const QString &s) const
Returns true if item matches the search s.
Definition: k3iconviewsearchline.cpp:182
K3IconViewSearchLine::setCaseSensitive
void setCaseSensitive(bool cs)
Make the search case sensitive or case insensitive.
Definition: k3iconviewsearchline.cpp:156
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q3IconView::currentItem
Q3IconViewItem * currentItem() const
QList::begin
iterator begin()
QObject::destroyed
void destroyed(QObject *obj)
Q3IconView::firstItem
Q3IconViewItem * firstItem() const
QIconViewItemList
QList< Q3IconViewItem * > QIconViewItemList
Definition: k3iconviewsearchline.cpp:35
QTimer::singleShot
singleShot
Q3IconView::sort
virtual void sort(bool ascending)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:47 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDE3Support

Skip menu "KDE3Support"
  • 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