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
00025 #ifndef KLFSEARCHBAR_H
00026 #define KLFSEARCHBAR_H
00027
00028 #include <QObject>
00029 #include <QWidget>
00030 #include <QFrame>
00031 #include <QMovie>
00032 #include <QLabel>
00033 #include <QTime>
00034
00035 #include <klfdefs.h>
00036
00037 #include <klfutil.h>
00038
00039 class QLineEdit;
00040 class KLFWaitAnimationOverlay;
00041
00042 class KLFSearchBar;
00043 class KLFSearchableProxy;
00044 namespace Ui { class KLFSearchBar; }
00045
00046
00047
00049
00073 class KLF_EXPORT KLFPosSearchable : public KLFTarget
00074 {
00075 public:
00076
00097 struct Pos {
00124 struct PosData {
00125 PosData() : r(0) { }
00126 virtual ~PosData() { }
00127
00129 virtual bool equals(PosData *other) const = 0;
00130
00133 virtual QString toDebug() { return QLatin1String("<PosData>"); }
00134
00135 int ref() { return ++r; }
00136 int deref() { return --r; }
00137
00140 virtual bool wantAutoDelete() { return true; }
00141 private:
00142 int r;
00143 };
00144
00146 Pos() : posdata()
00147 {
00148 }
00149
00150 Pos(const Pos& other) : posdata(other.posdata)
00151 {
00152 }
00153
00154 ~Pos()
00155 {
00156 if (posdata != NULL)
00157 posdata.setAutoDelete(posdata->wantAutoDelete());
00158 }
00159
00160 Pos& operator=(const Pos& other)
00161 {
00162 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00163 posdata = other.posdata;
00164 return *this;
00165 }
00166
00168 bool valid() const {
00169 KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ;
00170 klfDbg("posdata="<<posdata) ;
00171 return (posdata != NULL) ;
00172 };
00180 bool equals(const Pos& other) const {
00181 if (valid() && other.valid())
00182 return posdata->equals(other.data<PosData>());
00183
00184 return (valid() == other.valid());
00185 }
00186
00188
00196 KLFRefPtr<PosData> posdata;
00197
00211 template<class TT>
00212 inline TT * data() const
00213 {
00214 TT *ptr = posdata.dyn_cast<TT*>();
00215 KLF_ASSERT_NOT_NULL(ptr, "accessing a posdata that is NULL or of incompatible type!", return NULL;) ;
00216 return ptr;
00217 }
00218 };
00219
00232 virtual Pos searchStartFrom(bool forward);
00233
00252 virtual Pos searchFind(const QString& queryString, const Pos& fromPos, bool forward) = 0;
00253
00256 virtual void searchMoveToPos(const Pos& pos) { Q_UNUSED(pos); }
00257
00262 virtual void searchPerformed(const QString& queryString, bool found, const Pos& pos)
00263 { Q_UNUSED(queryString); Q_UNUSED(found); Q_UNUSED(pos); }
00264
00267 virtual void searchAborted() = 0;
00268
00272 virtual void searchReinitialized() { }
00273
00274
00279 virtual QString searchQueryString() const { klfDbg("pQString="<<pQString) ; return pQString; }
00280
00284 virtual void setSearchQueryString(const QString& s) { klfDbg("pQString="<<pQString<<"; setting to "<<s) ; pQString = s; }
00285
00286 virtual bool searchHasInterruptRequested() { return pInterruptRequested; }
00287
00288 virtual void setSearchInterruptRequested(bool on);
00289
00290 private:
00291 QString pQString;
00292 bool pInterruptRequested;
00293 };
00294
00295 KLF_EXPORT QDebug& operator<<(QDebug& str, const KLFPosSearchable::Pos& pos);
00296
00297
00298 class KLF_EXPORT KLFPosSearchableProxy : public KLFPosSearchable, public KLFTargeter
00299 {
00300 public:
00301 KLFPosSearchableProxy() { }
00302 virtual ~KLFPosSearchableProxy();
00303
00304 virtual Pos searchStartFrom(bool forward);
00305 virtual Pos searchFind(const QString& queryString, const Pos& fromPos, bool forward);
00306 virtual void searchMoveToPos(const Pos& pos);
00307 virtual void searchPerformed(const QString& queryString, bool found, const Pos& pos);
00308 virtual void searchAborted();
00309 virtual void searchReinitialized();
00310 virtual QString searchQueryString() const;
00311 virtual void setSearchQueryString(const QString& s);
00312 virtual bool searchHasInterruptRequested();
00313 virtual void setSearchInterruptRequested(bool on);
00314
00315 virtual void setSearchTarget(KLFPosSearchable *t) { setTarget(t); }
00316
00317 protected:
00318 virtual KLFPosSearchable *target() { return dynamic_cast<KLFPosSearchable*>(pTarget); }
00319 virtual const KLFPosSearchable *target() const { return dynamic_cast<KLFPosSearchable*>(pTarget); }
00320 };
00321
00322
00323
00324
00326
00346 class KLF_EXPORT KLFSearchable : public KLFPosSearchable
00347 {
00348 public:
00349 KLFSearchable();
00350 virtual ~KLFSearchable();
00351
00353
00365 virtual bool searchFind(const QString& queryString, bool forward) = 0;
00366
00368
00374 inline bool searchFind(const QString& queryString) { return searchFind(queryString, true); }
00375
00377
00386 virtual bool searchFindNext(bool forward) = 0;
00387
00389
00393 virtual void searchAbort() = 0;
00394
00395
00396 virtual Pos searchFind(const QString& queryString, const Pos& fromPos, bool forward);
00397 virtual void searchMoveToPos(const Pos& pos)
00398 { Q_UNUSED(pos); }
00399 virtual void searchPerformed(const QString& queryString, bool found, const Pos& pos)
00400 { Q_UNUSED(queryString); Q_UNUSED(found); Q_UNUSED(pos); }
00401 virtual void searchAborted() { searchAbort(); }
00402 };
00403
00404
00406
00412 class KLF_EXPORT KLFSearchableProxy : public KLFSearchable, public KLFTargeter
00413 {
00414 public:
00415 KLFSearchableProxy() : KLFTargeter() { }
00416 virtual ~KLFSearchableProxy();
00417
00418 void setSearchTarget(KLFPosSearchable *target) { setTarget(target); }
00419 virtual void setTarget(KLFTarget *target);
00420
00421 virtual bool searchFind(const QString& queryString, bool forward);
00422 virtual bool searchFindNext(bool forward);
00423 virtual void searchAbort();
00424
00425 protected:
00426 virtual KLFSearchable *target() { return dynamic_cast<KLFSearchable*>(pTarget); }
00427 };
00428
00429
00430
00431
00432 struct KLFSearchBarPrivate;
00433
00435
00464 class KLF_EXPORT KLFSearchBar : public QFrame, public KLFTargeter
00465 {
00466 Q_OBJECT
00467
00468 Q_PROPERTY(QString currentSearchText READ currentSearchText) ;
00469 Q_PROPERTY(bool autoHide READ autoHide WRITE setAutoHide) ;
00470 Q_PROPERTY(bool showOverlayMode READ showOverlayMode WRITE setShowOverlayMode) ;
00471 Q_PROPERTY(QRect showOverlayRelativeGeometry READ showOverlayRelativeGeometry
00472 WRITE setShowOverlayRelativeGeometry ) ;
00473 Q_PROPERTY(QString focusOutText READ focusOutText WRITE setFocusOutText) ;
00474 Q_PROPERTY(QColor colorFound READ colorFound WRITE setColorFound) ;
00475 Q_PROPERTY(QColor colorNotFound READ colorNotFound WRITE setColorNotFound) ;
00476 Q_PROPERTY(bool showHideButton READ hideButtonShown WRITE setShowHideButton) ;
00477 Q_PROPERTY(bool showSearchLabel READ showSearchLabel WRITE setShowSearchLabel) ;
00478 Q_PROPERTY(bool emacsStyleBackspace READ emacsStyleBackspace WRITE setEmacsStyleBackspace) ;
00479 Q_PROPERTY(int resetTimeout READ resetTimeout WRITE setResetTimeout) ;
00480 public:
00481
00482 enum SearchState { Default, FocusOut, Found, NotFound, Aborted };
00483
00484 KLFSearchBar(QWidget *parent = NULL);
00485 virtual ~KLFSearchBar();
00488 virtual void registerShortcuts(QWidget *parent);
00489
00492 virtual void setSearchTarget(KLFPosSearchable *target) { setTarget(target); }
00493 virtual void setTarget(KLFTarget *target);
00494
00495 QString currentSearchText() const;
00496 bool autoHide() const;
00497 bool showOverlayMode() const;
00498 QRect showOverlayRelativeGeometry() const;
00499 QString focusOutText() const;
00501 QColor colorFound() const;
00503 QColor colorNotFound() const;
00504 bool hideButtonShown() const;
00505 bool showSearchLabel() const;
00506 bool emacsStyleBackspace() const;
00507 int resetTimeout() const;
00508
00511 KLFPosSearchable::Pos currentSearchPos() const;
00512
00513 SearchState currentState() const;
00514
00517 void setAutoHide(bool autohide);
00518
00524 void setShowOverlayMode(bool showOverlayMode);
00525 void setShowOverlayRelativeGeometry(const QRect& relativeGeometryPercent);
00526 void setShowOverlayRelativeGeometry(int widthPercent, int heightPercent,
00527 int positionXPercent, int positionYPercent);
00528 void setColorFound(const QColor& color);
00529 void setColorNotFound(const QColor& color);
00530 void setShowHideButton(bool showHideButton);
00531 void setShowSearchLabel(bool show);
00532 void setEmacsStyleBackspace(bool on);
00537 void setResetTimeout(int ms);
00538
00539 virtual bool eventFilter(QObject *obj, QEvent *ev);
00540
00541 QLineEdit * editor();
00542
00543 signals:
00544 void stateChanged(SearchState state);
00545 void searchPerformed(bool found);
00546 void searchPerformed(const QString& queryString, bool found);
00547 void found();
00548 void found(const QString& queryString, bool forward);
00549 void found(const QString& queryString, bool forward, const KLFPosSearchable::Pos& pos);
00550 void didNotFind();
00551 void didNotFind(const QString& queryString, bool forward);
00552 void searchAborted();
00553 void escapePressed();
00554 void searchReinitialized();
00555
00557
00560 void hasMatch(bool hasmatch);
00561
00562 void visibilityChanged(bool isShown);
00563
00564 public slots:
00566 void clear();
00570 void focusOrNext(bool forward = true);
00573 void focusOrPrev() { focusOrNext(false); }
00574 void find(const QString& string);
00575 void find(const QString& string, bool forward);
00576 void findNext(bool forward = true);
00577 void findPrev() { findNext(false); }
00578 void abortSearch();
00579
00580 void focus();
00581
00582 virtual void setSearchText(const QString& text);
00583 void setFocusOutText(const QString& focusOutText);
00584
00585 protected:
00586 Ui::KLFSearchBar *u;
00587
00589 bool searchBarHasFocus();
00590
00591 virtual bool event(QEvent *event);
00592
00593
00594 bool _isInQtDesigner;
00595 friend class KLFSearchBarDesPlugin;
00596
00597 protected slots:
00598
00599 virtual void slotSearchFocusIn();
00600 virtual void slotSearchFocusOut();
00601 virtual void slotSearchReset();
00602 virtual void updateSearchFound(bool found);
00603
00604 void promptEmptySearch();
00605
00607 virtual void displayState(SearchState state);
00608
00610 void setCurrentState(SearchState state);
00611
00612 void emitFoundSignals(const KLFPosSearchable::Pos& pos, const QString& searchstring, bool forward);
00613
00616 void showSearchBarText(const QString& text);
00617
00618 private:
00619
00620 inline KLFPosSearchable *target() { return dynamic_cast<KLFPosSearchable*>(pTarget); }
00621
00622 KLFSearchBarPrivate *d;
00623
00624 void adjustOverlayGeometry();
00625
00626 QString palettePropName(SearchState state) const;
00627 QString statePropValue(SearchState state) const;
00628
00629
00630
00631 friend class KLFSearchable;
00632
00633 void performFind(bool forward, bool isFindNext = false);
00634
00635 KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()
00636 };
00637
00638
00639
00640
00641 #endif