Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef KLF_ITERATORSEARCHABLE_H
00025 #define KLF_ITERATORSEARCHABLE_H
00026
00027 #include <QString>
00028 #include <QTime>
00029 #include <QApplication>
00030
00031 #include <klfdefs.h>
00032 #include <klfsearchbar.h>
00033
00034
00036
00068 template<class Iter>
00069 class KLF_EXPORT KLFIteratorSearchable : public KLFSearchable
00070 {
00071 public:
00072 KLFIteratorSearchable() : KLFSearchable(), pSearchAborted(false) { }
00073 virtual ~KLFIteratorSearchable() { }
00074
00075 typedef Iter SearchIterator;
00076
00077
00078
00079
00082 virtual SearchIterator searchIterBegin() = 0;
00083
00087 virtual SearchIterator searchIterEnd() = 0;
00088
00091 virtual SearchIterator searchIterAdvance(const SearchIterator& pos, bool forward) { return forward ? (pos+1) : (pos-1); }
00092
00094 inline SearchIterator searchIterNext(const SearchIterator& pos) { return searchIterAdvance(pos, true); }
00095
00097 inline SearchIterator searchIterPrev(const SearchIterator& pos) { return searchIterAdvance(pos, false); }
00098
00107 virtual SearchIterator searchIterStartFrom(bool forward)
00108 { return forward ? searchIterBegin() : searchIterEnd(); }
00109
00115 virtual bool searchIterMatches(const SearchIterator& pos, const QString& queryString) = 0;
00116
00117
00126 virtual void searchPerformed(const SearchIterator& resultMatchPosition) { Q_UNUSED(resultMatchPosition); }
00127
00128
00129
00130
00132
00145 virtual SearchIterator searchIterFind(const SearchIterator& startPos, const QString& queryString, bool forward)
00146 {
00147 klfDbg( " s="<<queryString<<" from "<<startPos<<" forward="<<forward ) ;
00148
00149
00150
00151 if (forward)
00152 pCurPos = safe_cycl_advance_iterator(startPos, !forward);
00153 pSearchAborted = false;
00154 pQString = queryString;
00155 SearchIterator it = searchIterFindNext(forward);
00156 return it;
00157 }
00158
00160
00165 virtual SearchIterator searchIterFindNext(bool forward)
00166 {
00167 KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
00168 pSearchAborted = false;
00169 if (pQString.isEmpty())
00170 return tee_notify_search_result(searchIterEnd());
00171
00172 QTime t;
00173
00174 bool found = false;
00175 while ( ! found ) {
00176
00177
00178 pCurPos = safe_cycl_advance_iterator(pCurPos, forward);
00179
00180
00181 if (pCurPos == searchIterEnd())
00182 break;
00183
00184
00185
00186 if ( searchIterMatches(pCurPos, pQString) ) {
00187 found = true;
00188 break;
00189 }
00190
00191
00192 if (t.elapsed() > 150) {
00193 qApp->processEvents();
00194 if (pSearchAborted)
00195 break;
00196 t.restart();
00197 }
00198 }
00199 if (found) {
00200 klfDbg( "found "<<pQString<<" at "<<pCurPos ) ;
00201 return tee_notify_search_result(pCurPos);
00202 }
00203
00204
00205 return tee_notify_search_result(searchIterEnd());
00206 }
00207
00208
00209
00210
00211
00212
00213
00214 virtual bool searchFind(const QString& queryString, bool forward)
00215 {
00216 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00217 return KLF_DEBUG_TEE( ! (searchIterFind(searchIterStartFrom(forward), queryString, forward)
00218 == searchIterEnd()) );
00219 }
00220
00221
00222
00223
00224 virtual bool searchFindNext(bool forward)
00225 {
00226 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00227 return KLF_DEBUG_TEE( ! (searchIterFindNext(forward) == searchIterEnd()) );
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 virtual void searchAbort()
00239 {
00240 pSearchAborted = true;
00241 }
00242
00252 SearchIterator searchAdvanceIteratorSafe(const SearchIterator& it, int n = 1)
00253 {
00254 if (n == 0)
00255 return it;
00256 bool forward = (n>0);
00257 if (n < 0)
00258 n = -n;
00259
00260 SearchIterator a = it;
00261 while (n--)
00262 a = safe_cycl_advance_iterator(a, forward);
00263
00264 return a;
00265 }
00266
00267 protected:
00268
00269 inline QString searchQueryString() const { return pQString; }
00270 inline SearchIterator searchCurrentIterPos() const { return pCurPos; }
00271
00272 private:
00273 QString pQString;
00275 SearchIterator pCurPos;
00277 bool pSearchAborted;
00278
00279 inline SearchIterator tee_notify_search_result(const SearchIterator& iter)
00280 {
00281 searchPerformed(iter);
00282 return iter;
00283 }
00284
00285 inline SearchIterator safe_cycl_advance_iterator(const SearchIterator& it, bool forward)
00286 {
00287 if (forward) {
00288 if (it == searchIterEnd())
00289 return searchIterBegin();
00290 return searchIterNext(it);
00291 } else {
00292 if (it == searchIterBegin())
00293 return searchIterEnd();
00294 return searchIterPrev(it);
00295 }
00296 }
00297 };
00298
00299
00300
00301 #endif