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

tdeui

  • tdeui
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 <tdelocale.h>
31#include <tqtimer.h>
32#include <kdebug.h>
33
34#define DEFAULT_CASESENSITIVE false
35
36typedef TQValueList <TQIconViewItem *> QIconViewItemList;
37
38class TDEIconViewSearchLine::TDEIconViewSearchLinePrivate
39{
40public:
41 TDEIconViewSearchLinePrivate() :
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 *****************************************************************************/
57TDEIconViewSearchLine::TDEIconViewSearchLine( TQWidget *parent,
58 TQIconView *iconView,
59 const char *name ) :
60 KLineEdit( parent, name )
61{
62 d = NULL;
63 init( iconView );
64}
65
66TDEIconViewSearchLine::TDEIconViewSearchLine( TQWidget *parent, const char *name ) :
67 KLineEdit( parent, name )
68{
69 d = NULL;
70 init( NULL );
71}
72
73TDEIconViewSearchLine::~TDEIconViewSearchLine()
74{
75 clear(); // empty hiddenItems, returning items back to iconView
76 delete d;
77}
78
79bool TDEIconViewSearchLine::caseSensitive() const
80{
81 return d->caseSensitive;
82}
83
84TQIconView *TDEIconViewSearchLine::iconView() const
85{
86 return d->iconView;
87}
88
89/******************************************************************************
90 * Public Slots *
91 *****************************************************************************/
92void TDEIconViewSearchLine::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
123void TDEIconViewSearchLine::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
143void TDEIconViewSearchLine::iconDeleted(const TQString &filename) {
144 // Do nothing...
145}
146
147void TDEIconViewSearchLine::setCaseSensitive( bool cs )
148{
149 d->caseSensitive = cs;
150}
151
152void TDEIconViewSearchLine::setIconView( TQIconView *iv )
153{
154 if ( d->iconView != NULL )
155 disconnect( d->iconView, TQ_SIGNAL( destroyed() ),
156 this, TQ_SLOT( iconViewDeleted() ) );
157
158 d->iconView = iv;
159
160 if ( iv != NULL )
161 {
162 connect( d->iconView, TQ_SIGNAL( destroyed() ),
163 this, TQ_SLOT( iconViewDeleted() ) );
164 setEnabled( true );
165 }
166 else
167 setEnabled( false );
168}
169
170/******************************************************************************
171 * Protected Methods *
172 *****************************************************************************/
173bool TDEIconViewSearchLine::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
186void TDEIconViewSearchLine::init( TQIconView *iconView )
187{
188 delete d;
189 d = new TDEIconViewSearchLinePrivate;
190
191 d->iconView = iconView;
192
193 connect( this, TQ_SIGNAL( textChanged( const TQString & ) ),
194 this, TQ_SLOT( queueSearch( const TQString & ) ) );
195
196 if ( iconView != NULL )
197 {
198 connect( iconView, TQ_SIGNAL( destroyed() ),
199 this, TQ_SLOT( iconViewDeleted() ) );
200 setEnabled( true );
201 }
202 else
203 setEnabled( false );
204}
205
206void TDEIconViewSearchLine::hideItem( TQIconViewItem *item )
207{
208 if ( ( item == NULL ) || ( d->iconView == NULL ) )
209 return;
210
211 item->setVisible(false);
212}
213
214void TDEIconViewSearchLine::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 *****************************************************************************/
230void TDEIconViewSearchLine::queueSearch( const TQString &s )
231{
232 d->queuedSearches++;
233 d->search = s;
234 TQTimer::singleShot( 200, this, TQ_SLOT( activateSearch() ) );
235}
236
237void TDEIconViewSearchLine::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, TQ_SLOT( activateSearch() ) );
248 }
249}
250
251/******************************************************************************
252 * Private Slots *
253 *****************************************************************************/
254void TDEIconViewSearchLine::iconViewDeleted()
255{
256 d->iconView = NULL;
257 setEnabled( false );
258}
259
260#include "kiconviewsearchline.moc"
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition: klineedit.h:146
KLineEdit::clear
virtual void clear()
Reimplemented to workaround a buggy TQLineEdit::clear() (changing the clipboard to the text we just h...
Definition: klineedit.cpp:1320
TDEIconViewSearchLine::updateSearch
virtual void updateSearch(const TQString &s=TQString::null)
Updates search to only make visible the items that match s.
Definition: kiconviewsearchline.cpp:92
TDEIconViewSearchLine::iconDeleted
void iconDeleted(const TQString &filename)
Should be called before updateSearch() whenever an icon is deleted.
Definition: kiconviewsearchline.cpp:143
TDEIconViewSearchLine::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
TDEIconViewSearchLine::clear
void clear()
Clear line edit and empty hiddenItems, returning elements to iconView.
Definition: kiconviewsearchline.cpp:123
TDEIconViewSearchLine::itemMatches
virtual bool itemMatches(const TQIconViewItem *item, const TQString &s) const
Returns true if item matches the search s.
Definition: kiconviewsearchline.cpp:173
TDEIconViewSearchLine::activateSearch
void activateSearch()
When the timer started with queueSearch() expires this slot is called.
Definition: kiconviewsearchline.cpp:237
TDEIconViewSearchLine::showItem
void showItem(TQIconViewItem *item)
Show item.
Definition: kiconviewsearchline.cpp:214
TDEIconViewSearchLine::iconView
TQIconView * iconView() const
Returns the iconview that is currently filtered by the search.
Definition: kiconviewsearchline.cpp:84
TDEIconViewSearchLine::init
void init(TQIconView *iconView=0)
Do initialization common to both constructors.
Definition: kiconviewsearchline.cpp:186
TDEIconViewSearchLine::TDEIconViewSearchLine
TDEIconViewSearchLine(TQWidget *parent=0, TQIconView *iconView=0, const char *name=0)
Constructs a TDEIconViewSearchLine with iconView being the TQIconView to be filtered.
Definition: kiconviewsearchline.cpp:57
TDEIconViewSearchLine::setIconView
void setIconView(TQIconView *iv)
Sets the TQIconView that is filtered by this search line.
Definition: kiconviewsearchline.cpp:152
TDEIconViewSearchLine::caseSensitive
bool caseSensitive() const
Returns true if the search is case sensitive.
Definition: kiconviewsearchline.cpp:79
TDEIconViewSearchLine::hideItem
void hideItem(TQIconViewItem *item)
Hide item.
Definition: kiconviewsearchline.cpp:206
TDEIconViewSearchLine::setCaseSensitive
void setCaseSensitive(bool cs)
Make the search case sensitive or case insensitive.
Definition: kiconviewsearchline.cpp:147
TDEIconViewSearchLine::~TDEIconViewSearchLine
virtual ~TDEIconViewSearchLine()
Destroys the TDEIconViewSearchLine.
Definition: kiconviewsearchline.cpp:73
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
tdelocale.h

tdeui

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

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.