• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdeui
 

kdeui

  • kdeui
kiconviewsearchline.cpp
1 /* This file is part of the KDE libraries
2  Copyright (c) 2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>
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 
27 #include "kiconviewsearchline.h"
28 
29 #include <tqiconview.h>
30 #include <klocale.h>
31 #include <tqtimer.h>
32 #include <kdebug.h>
33 
34 #define DEFAULT_CASESENSITIVE false
35 
36 typedef TQValueList <TQIconViewItem *> QIconViewItemList;
37 
38 class KIconViewSearchLine::KIconViewSearchLinePrivate
39 {
40 public:
41  KIconViewSearchLinePrivate() :
42  iconView( 0 ),
43  caseSensitive( DEFAULT_CASESENSITIVE ),
44  activeSearch( false ),
45  queuedSearches( 0 ) {}
46 
47  TQIconView *iconView;
48  bool caseSensitive;
49  bool activeSearch;
50  TQString search;
51  int queuedSearches;
52 };
53 
54 /******************************************************************************
55  * Public Methods *
56  *****************************************************************************/
57 KIconViewSearchLine::KIconViewSearchLine( TQWidget *parent,
58  TQIconView *iconView,
59  const char *name ) :
60  KLineEdit( parent, name )
61 {
62  d = NULL;
63  init( iconView );
64 }
65 
66 KIconViewSearchLine::KIconViewSearchLine( TQWidget *parent, const char *name ) :
67  KLineEdit( parent, name )
68 {
69  d = NULL;
70  init( NULL );
71 }
72 
73 KIconViewSearchLine::~KIconViewSearchLine()
74 {
75  clear(); // empty hiddenItems, returning items back to iconView
76  delete d;
77 }
78 
79 bool KIconViewSearchLine::caseSensitive() const
80 {
81  return d->caseSensitive;
82 }
83 
84 TQIconView *KIconViewSearchLine::iconView() const
85 {
86  return d->iconView;
87 }
88 
89 /******************************************************************************
90  * Public Slots *
91  *****************************************************************************/
92 void KIconViewSearchLine::updateSearch( const TQString &s )
93 {
94  if( ! d->iconView )
95  return;
96 
97  d->search = s.isNull() ? text() : s;
98  TQIconViewItem *currentItem = d->iconView->currentItem();
99  TQIconViewItem *item = NULL;
100 
101  // Remove Non-Matching items, add them to the hidden list
102  TQIconViewItem *i = d->iconView->firstItem();
103  while ( i != NULL ) {
104  item = i;
105  i = i->nextItem(); // Point to next, otherwise will loose it.
106  if ( ! itemMatches( item, d->search ) ) {
107  hideItem( item );
108 
109  if ( item == currentItem )
110  currentItem = NULL; // It's not in iconView anymore.
111  }
112  else {
113  showItem( item );
114  }
115  }
116 
117  d->iconView->sort(); // This also arranges items in grid
118 
119  if ( currentItem != NULL )
120  d->iconView->ensureItemVisible( currentItem );
121 }
122 
123 void KIconViewSearchLine::clear()
124 {
125  if( ! d->iconView )
126  return; // disabled
127 
128  // Clear hidden list, give items back to TQIconView, if it still exists
129  TQIconViewItem *item = NULL;
130 
131  TQIconViewItem *i = d->iconView->firstItem();
132  while ( i != NULL ) {
133  item = i;
134  i = i->nextItem(); // Point to next, otherwise will loose it.
135  showItem( item );
136  }
137 
138  d->search = "";
139  d->queuedSearches = 0;
140  KLineEdit::clear();
141 }
142 
143 void KIconViewSearchLine::iconDeleted(const TQString &filename) {
144  // Do nothing...
145 }
146 
147 void KIconViewSearchLine::setCaseSensitive( bool cs )
148 {
149  d->caseSensitive = cs;
150 }
151 
152 void KIconViewSearchLine::setIconView( TQIconView *iv )
153 {
154  if ( d->iconView != NULL )
155  disconnect( d->iconView, TQT_SIGNAL( destroyed() ),
156  this, TQT_SLOT( iconViewDeleted() ) );
157 
158  d->iconView = iv;
159 
160  if ( iv != NULL )
161  {
162  connect( d->iconView, TQT_SIGNAL( destroyed() ),
163  this, TQT_SLOT( iconViewDeleted() ) );
164  setEnabled( true );
165  }
166  else
167  setEnabled( false );
168 }
169 
170 /******************************************************************************
171  * Protected Methods *
172  *****************************************************************************/
173 bool KIconViewSearchLine::itemMatches( const TQIconViewItem *item,
174  const TQString &s ) const
175 {
176  if ( s.isEmpty() )
177  return true;
178 
179  if ( item == NULL )
180  return false;
181 
182  TQString itemtext = item->text();
183  return ( itemtext.find( s, 0, caseSensitive() ) >= 0 );
184 }
185 
186 void KIconViewSearchLine::init( TQIconView *iconView )
187 {
188  delete d;
189  d = new KIconViewSearchLinePrivate;
190 
191  d->iconView = iconView;
192 
193  connect( this, TQT_SIGNAL( textChanged( const TQString & ) ),
194  this, TQT_SLOT( queueSearch( const TQString & ) ) );
195 
196  if ( iconView != NULL )
197  {
198  connect( iconView, TQT_SIGNAL( destroyed() ),
199  this, TQT_SLOT( iconViewDeleted() ) );
200  setEnabled( true );
201  }
202  else
203  setEnabled( false );
204 }
205 
206 void KIconViewSearchLine::hideItem( TQIconViewItem *item )
207 {
208  if ( ( item == NULL ) || ( d->iconView == NULL ) )
209  return;
210 
211  item->setVisible(false);
212 }
213 
214 void KIconViewSearchLine::showItem( TQIconViewItem *item )
215 {
216  if ( d->iconView == NULL )
217  {
218  kdDebug() << __FILE__ << ":" << __LINE__ <<
219  "showItem() could not be called while there's no iconView set." <<
220  endl;
221  return;
222  }
223 
224  item->setVisible(true);
225 }
226 
227 /******************************************************************************
228  * Protected Slots *
229  *****************************************************************************/
230 void KIconViewSearchLine::queueSearch( const TQString &s )
231 {
232  d->queuedSearches++;
233  d->search = s;
234  TQTimer::singleShot( 200, this, TQT_SLOT( activateSearch() ) );
235 }
236 
237 void KIconViewSearchLine::activateSearch()
238 {
239  d->queuedSearches--;
240 
241  if ( d->queuedSearches <= 0 )
242  {
243  updateSearch( d->search );
244  d->queuedSearches = 0;
245  }
246  else {
247  TQTimer::singleShot( 200, this, TQT_SLOT( activateSearch() ) );
248  }
249 }
250 
251 /******************************************************************************
252  * Private Slots *
253  *****************************************************************************/
254 void KIconViewSearchLine::iconViewDeleted()
255 {
256  d->iconView = NULL;
257  setEnabled( false );
258 }
259 
260 #include "kiconviewsearchline.moc"
KIconViewSearchLine::~KIconViewSearchLine
virtual ~KIconViewSearchLine()
Destroys the KIconViewSearchLine.
Definition: kiconviewsearchline.cpp:73
KIconViewSearchLine::init
void init(TQIconView *iconView=0)
Do initialization common to both constructors.
Definition: kiconviewsearchline.cpp:186
KIconViewSearchLine::iconDeleted
void iconDeleted(const TQString &filename)
Should be called before updateSearch() whenever an icon is deleted.
Definition: kiconviewsearchline.cpp:143
kdDebug
kdbgstream kdDebug(int area=0)
klocale.h
KLineEdit::clear
virtual void clear()
Reimplemented to workaround a buggy TQLineEdit::clear() (changing the clipboard to the text we just h...
Definition: klineedit.cpp:1322
KIconViewSearchLine::clear
void clear()
Clear line edit and empty hiddenItems, returning elements to iconView.
Definition: kiconviewsearchline.cpp:123
KIconViewSearchLine::iconView
TQIconView * iconView() const
Returns the iconview that is currently filtered by the search.
Definition: kiconviewsearchline.cpp:84
KIconViewSearchLine::updateSearch
virtual void updateSearch(const TQString &s=TQString::null)
Updates search to only make visible the items that match s.
Definition: kiconviewsearchline.cpp:92
KIconViewSearchLine::showItem
void showItem(TQIconViewItem *item)
Show item.
Definition: kiconviewsearchline.cpp:214
KIconViewSearchLine::caseSensitive
bool caseSensitive() const
Returns true if the search is case sensitive.
Definition: kiconviewsearchline.cpp:79
KIconViewSearchLine::itemMatches
virtual bool itemMatches(const TQIconViewItem *item, const TQString &s) const
Returns true if item matches the search s.
Definition: kiconviewsearchline.cpp:173
KIconViewSearchLine::setIconView
void setIconView(TQIconView *iv)
Sets the TQIconView that is filtered by this search line.
Definition: kiconviewsearchline.cpp:152
KIconViewSearchLine::activateSearch
void activateSearch()
When the timer started with queueSearch() expires this slot is called.
Definition: kiconviewsearchline.cpp:237
KIconViewSearchLine::queueSearch
void queueSearch(const TQString &s)
When keys are pressed a new search string is created and a timer is activated.
Definition: kiconviewsearchline.cpp:230
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition: klineedit.h:145
KIconViewSearchLine::KIconViewSearchLine
KIconViewSearchLine(TQWidget *parent=0, TQIconView *iconView=0, const char *name=0)
Constructs a KIconViewSearchLine with iconView being the TQIconView to be filtered.
Definition: kiconviewsearchline.cpp:57
KIconViewSearchLine::setCaseSensitive
void setCaseSensitive(bool cs)
Make the search case sensitive or case insensitive.
Definition: kiconviewsearchline.cpp:147
endl
kndbgstream & endl(kndbgstream &s)
KIconViewSearchLine::hideItem
void hideItem(TQIconViewItem *item)
Hide item.
Definition: kiconviewsearchline.cpp:206

kdeui

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

kdeui

Skip menu "kdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeui by doxygen 1.8.13
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |