[KLF Backend][KLF Tools][KLF Home]
KLatexFormula Project
klfsearchbar.h
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfsearchbar.h
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfsearchbar.h 962 2016-12-27 10:25:56Z phfaist $ */
23 
24 
25 #ifndef KLFSEARCHBAR_H
26 #define KLFSEARCHBAR_H
27 
28 #include <QObject>
29 #include <QWidget>
30 #include <QFrame>
31 #include <QMovie>
32 #include <QLabel>
33 #include <QTime>
34 
35 #include <klfdefs.h>
36 
37 #include <klfutil.h>
38 
39 class QLineEdit;
41 
42 class KLFSearchBar;
43 class KLFSearchableProxy;
44 namespace Ui { class KLFSearchBar; }
45 
46 
47 
49 
74 {
75 public:
76 
97  struct Pos {
124  struct PosData {
125  PosData() : r(0) { }
126  virtual ~PosData() { }
127 
129  virtual bool equals(PosData *other) const = 0;
130 
133  virtual QString toDebug() { return QLatin1String("<PosData>"); }
134 
135  int ref() { return ++r; }
136  int deref() { return --r; }
137 
140  virtual bool wantAutoDelete() { return true; }
141  private:
142  int r;
143  };
144 
146  Pos() : posdata()
147  {
148  }
149 
150  Pos(const Pos& other) : posdata(other.posdata)
151  {
152  }
153 
155  {
156  if (posdata != NULL)
157  posdata.setAutoDelete(posdata->wantAutoDelete());
158  }
159 
160  Pos& operator=(const Pos& other)
161  {
163  posdata = other.posdata;
164  return *this;
165  }
166 
168  bool valid() const {
170  klfDbg("posdata="<<posdata) ;
171  return (posdata != NULL) ;
172  };
180  bool equals(const Pos& other) const {
181  if (valid() && other.valid())
182  return posdata->equals(other.data<PosData>());
183  // if both are invalid, they are equal. If one only is valid, they are not equal.
184  return (valid() == other.valid());
185  }
186 
188 
197 
211  template<class TT>
212  inline TT * data() const
213  {
214  TT *ptr = posdata.dyn_cast<TT*>();
215  KLF_ASSERT_NOT_NULL(ptr, "accessing a posdata that is NULL or of incompatible type!", return NULL;) ;
216  return ptr;
217  }
218  };
219 
232  virtual Pos searchStartFrom(bool forward);
233 
252  virtual Pos searchFind(const QString& queryString, const Pos& fromPos, bool forward) = 0;
253 
256  virtual void searchMoveToPos(const Pos& pos) { Q_UNUSED(pos); }
257 
262  virtual void searchPerformed(const QString& queryString, bool found, const Pos& pos)
263  { Q_UNUSED(queryString); Q_UNUSED(found); Q_UNUSED(pos); }
264 
267  virtual void searchAborted() = 0;
268 
272  virtual void searchReinitialized() { }
273 
274 
279  virtual QString searchQueryString() const { klfDbg("pQString="<<pQString) ; return pQString; }
280 
284  virtual void setSearchQueryString(const QString& s) { klfDbg("pQString="<<pQString<<"; setting to "<<s) ; pQString = s; }
285 
286  virtual bool searchHasInterruptRequested() { return pInterruptRequested; }
287 
288  virtual void setSearchInterruptRequested(bool on);
289 
290 private:
291  QString pQString;
292  bool pInterruptRequested;
293 };
294 
296 
297 
299 {
300 public:
302  virtual ~KLFPosSearchableProxy();
303 
304  virtual Pos searchStartFrom(bool forward);
305  virtual Pos searchFind(const QString& queryString, const Pos& fromPos, bool forward);
306  virtual void searchMoveToPos(const Pos& pos);
307  virtual void searchPerformed(const QString& queryString, bool found, const Pos& pos);
308  virtual void searchAborted();
309  virtual void searchReinitialized();
310  virtual QString searchQueryString() const;
311  virtual void setSearchQueryString(const QString& s);
312  virtual bool searchHasInterruptRequested();
313  virtual void setSearchInterruptRequested(bool on);
314 
315  virtual void setSearchTarget(KLFPosSearchable *t) { setTarget(t); }
316 
317 protected:
318  virtual KLFPosSearchable *target() { return dynamic_cast<KLFPosSearchable*>(pTarget); }
319  virtual const KLFPosSearchable *target() const { return dynamic_cast<KLFPosSearchable*>(pTarget); }
320 };
321 
322 
323 
324 
326 
347 {
348 public:
349  KLFSearchable();
350  virtual ~KLFSearchable();
351 
353 
365  virtual bool searchFind(const QString& queryString, bool forward) = 0;
366 
368 
374  inline bool searchFind(const QString& queryString) { return searchFind(queryString, true); }
375 
377 
386  virtual bool searchFindNext(bool forward) = 0;
387 
389 
393  virtual void searchAbort() = 0;
394 
395 
396  virtual Pos searchFind(const QString& queryString, const Pos& fromPos, bool forward);
397  virtual void searchMoveToPos(const Pos& pos)
398  { Q_UNUSED(pos); }
399  virtual void searchPerformed(const QString& queryString, bool found, const Pos& pos)
400  { Q_UNUSED(queryString); Q_UNUSED(found); Q_UNUSED(pos); }
401  virtual void searchAborted() { searchAbort(); }
402 };
403 
404 
406 
413 {
414 public:
416  virtual ~KLFSearchableProxy();
417 
418  void setSearchTarget(KLFPosSearchable *target) { setTarget(target); }
419  virtual void setTarget(KLFTarget *target);
420 
421  virtual bool searchFind(const QString& queryString, bool forward);
422  virtual bool searchFindNext(bool forward);
423  virtual void searchAbort();
424 
425 protected:
426  virtual KLFSearchable *target() { return dynamic_cast<KLFSearchable*>(pTarget); }
427 };
428 
429 
430 
431 
432 struct KLFSearchBarPrivate;
433 
435 
464 class KLF_EXPORT KLFSearchBar : public QFrame, public KLFTargeter
465 {
466  Q_OBJECT
467 
468  Q_PROPERTY(QString currentSearchText READ currentSearchText) ;
469  Q_PROPERTY(bool autoHide READ autoHide WRITE setAutoHide) ;
470  Q_PROPERTY(bool showOverlayMode READ showOverlayMode WRITE setShowOverlayMode) ;
471  Q_PROPERTY(QRect showOverlayRelativeGeometry READ showOverlayRelativeGeometry
472  WRITE setShowOverlayRelativeGeometry ) ;
473  Q_PROPERTY(QString focusOutText READ focusOutText WRITE setFocusOutText) ;
474  Q_PROPERTY(QColor colorFound READ colorFound WRITE setColorFound) ;
475  Q_PROPERTY(QColor colorNotFound READ colorNotFound WRITE setColorNotFound) ;
476  Q_PROPERTY(bool showHideButton READ hideButtonShown WRITE setShowHideButton) ;
477  Q_PROPERTY(bool showSearchLabel READ showSearchLabel WRITE setShowSearchLabel) ;
478  Q_PROPERTY(bool emacsStyleBackspace READ emacsStyleBackspace WRITE setEmacsStyleBackspace) ;
479  Q_PROPERTY(int resetTimeout READ resetTimeout WRITE setResetTimeout) ;
480 public:
481 
482  enum SearchState { Default, FocusOut, Found, NotFound, Aborted };
483 
484  KLFSearchBar(QWidget *parent = NULL);
485  virtual ~KLFSearchBar();
488  virtual void registerShortcuts(QWidget *parent);
489 
492  virtual void setSearchTarget(KLFPosSearchable *target) { setTarget(target); }
493  virtual void setTarget(KLFTarget *target);
494 
495  QString currentSearchText() const;
496  bool autoHide() const;
497  bool showOverlayMode() const;
498  QRect showOverlayRelativeGeometry() const;
499  QString focusOutText() const;
501  QColor colorFound() const;
503  QColor colorNotFound() const;
504  bool hideButtonShown() const;
505  bool showSearchLabel() const;
506  bool emacsStyleBackspace() const;
507  int resetTimeout() const;
508 
511  KLFPosSearchable::Pos currentSearchPos() const;
512 
513  SearchState currentState() const;
514 
517  void setAutoHide(bool autohide);
518 
524  void setShowOverlayMode(bool showOverlayMode);
525  void setShowOverlayRelativeGeometry(const QRect& relativeGeometryPercent);
526  void setShowOverlayRelativeGeometry(int widthPercent, int heightPercent,
527  int positionXPercent, int positionYPercent);
528  void setColorFound(const QColor& color);
529  void setColorNotFound(const QColor& color);
530  void setShowHideButton(bool showHideButton);
531  void setShowSearchLabel(bool show);
532  void setEmacsStyleBackspace(bool on);
537  void setResetTimeout(int ms);
538 
539  virtual bool eventFilter(QObject *obj, QEvent *ev);
540 
541  QLineEdit * editor();
542 
543 signals:
544  void stateChanged(SearchState state);
545  void searchPerformed(bool found);
546  void searchPerformed(const QString& queryString, bool found);
547  void found();
548  void found(const QString& queryString, bool forward);
549  void found(const QString& queryString, bool forward, const KLFPosSearchable::Pos& pos);
550  void didNotFind();
551  void didNotFind(const QString& queryString, bool forward);
552  void searchAborted();
553  void escapePressed();
554  void searchReinitialized();
555 
557 
560  void hasMatch(bool hasmatch);
561 
562  void visibilityChanged(bool isShown);
563 
564 public slots:
566  void clear();
570  void focusOrNext(bool forward = true);
573  void focusOrPrev() { focusOrNext(false); }
574  void find(const QString& string);
575  void find(const QString& string, bool forward);
576  void findNext(bool forward = true);
577  void findPrev() { findNext(false); }
578  void abortSearch();
579 
580  void focus();
581 
582  virtual void setSearchText(const QString& text);
583  void setFocusOutText(const QString& focusOutText);
584 
585 protected:
586  Ui::KLFSearchBar *u;
587 
589  bool searchBarHasFocus();
590 
591  virtual bool event(QEvent *event);
592 
593 
595  friend class KLFSearchBarDesPlugin;
596 
597 protected slots:
598 
599  virtual void slotSearchFocusIn();
600  virtual void slotSearchFocusOut();
601  virtual void slotSearchReset();
602  virtual void updateSearchFound(bool found);
603 
604  void promptEmptySearch();
605 
607  virtual void displayState(SearchState state);
608 
610  void setCurrentState(SearchState state);
611 
612  void emitFoundSignals(const KLFPosSearchable::Pos& pos, const QString& searchstring, bool forward);
613 
616  void showSearchBarText(const QString& text);
617 
618 private:
619 
620  inline KLFPosSearchable *target() { return dynamic_cast<KLFPosSearchable*>(pTarget); }
621 
622  KLFSearchBarPrivate *d;
623 
624  void adjustOverlayGeometry();
625 
626  QString palettePropName(SearchState state) const;
627  QString statePropValue(SearchState state) const;
628 
629  // Needed so that KLFSearchable's can ensure \ref pTarget is valid, and set it to NULL
630  // when appropriate
631  friend class KLFSearchable;
632 
633  void performFind(bool forward, bool isFindNext = false);
634 
636 };
637 
638 
639 
640 
641 #endif
bool _isInQtDesigner
Definition: klfsearchbar.h:594
virtual KLFSearchable * target()
Definition: klfsearchbar.h:426
void setSearchTarget(KLFPosSearchable *target)
Definition: klfsearchbar.h:418
bool equals(const Pos &other) const
Definition: klfsearchbar.h:180
An object that can be searched with a KLFSearchBar.
Definition: klfsearchbar.h:73
Base declarations for klatexformula and some utilities.
virtual QString searchQueryString() const
The current query string.
Definition: klfsearchbar.h:279
#define klfDbg(streamableItems)
print debug stream items
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
virtual void searchAborted()
Definition: klfsearchbar.h:401
#define KLF_DEBUG_DECLARE_ASSIGNABLE_REF_INSTANCE()
Declare that this class has an assignable debugging ref-instance.
#define KLF_ASSERT_NOT_NULL(ptr, msg, failaction)
Asserting Non-NULL pointers (NON-FATAL)
virtual void searchPerformed(const QString &queryString, bool found, const Pos &pos)
Definition: klfsearchbar.h:262
#define KLF_EXPORT
Definition: klfdefs.h:41
KLF_EXPORT QDebug & operator<<(QDebug &str, const KLFPosSearchable::Pos &pos)
An Search Bar for Incremental Search.
Definition: klfsearchbar.h:464
virtual void searchMoveToPos(const Pos &pos)
Definition: klfsearchbar.h:397
An animation display.
Definition: klfguiutil.h:412
void findPrev()
Definition: klfsearchbar.h:577
virtual const KLFPosSearchable * target() const
Definition: klfsearchbar.h:319
virtual bool searchHasInterruptRequested()
Definition: klfsearchbar.h:286
virtual void setSearchTarget(KLFPosSearchable *t)
Definition: klfsearchbar.h:315
Stores a pointer to an object with refcount.
Definition: klfutil.h:613
virtual void searchPerformed(const QString &queryString, bool found, const Pos &pos)
Definition: klfsearchbar.h:399
KLFRefPtr< PosData > posdata
Stores the actual position data, see PosData.
Definition: klfsearchbar.h:196
virtual void searchReinitialized()
Definition: klfsearchbar.h:272
virtual void setSearchTarget(KLFPosSearchable *target)
Definition: klfsearchbar.h:492
#define KLF_FUNC_NAME
An abstract position in a searchable object.
Definition: klfsearchbar.h:97
bool searchFind(const QString &queryString)
Find the first occurence of a query string.
Definition: klfsearchbar.h:374
A proxy class that relays search queries to another searchable object (OBSOLETE)
Definition: klfsearchbar.h:412
virtual KLFPosSearchable * target()
Definition: klfsearchbar.h:318
Ui::KLFSearchBar * u
Definition: klfsearchbar.h:586
virtual void setSearchQueryString(const QString &s)
Definition: klfsearchbar.h:284
virtual void searchMoveToPos(const Pos &pos)
Definition: klfsearchbar.h:256
TT * data() const
A shorthand for retrieving the posdata cast into the custom type.
Definition: klfsearchbar.h:212
void focusOrPrev()
Definition: klfsearchbar.h:573
A Base class for storing abstract position data.
Definition: klfsearchbar.h:124
An interface for objects that can be I-searched with a KLFSearchBar (OBSOLETE)
Definition: klfsearchbar.h:346
Pos & operator=(const Pos &other)
Definition: klfsearchbar.h:160
Pos(const Pos &other)
Definition: klfsearchbar.h:150
OtherPtr dyn_cast()
Definition: klfutil.h:717

Generated by doxygen 1.8.13