Salome HOME
14214b0ade849b33e24d31e0145d20e99546a4ce
[modules/gui.git] / src / Qtx / QtxSearchTool.h
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // File   : QtxSearchTool.h
21 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
22 //
23 #ifndef QTXSEARCHTOOL_H
24 #define QTXSEARCHTOOL_H
25
26 #include "Qtx.h"
27 #include <QFrame>
28 #include <QList>
29 #include <QMap>
30 #include <QPersistentModelIndex>
31 #include <QPointer>
32
33 class QCheckBox;
34 class QLineEdit;
35 class QShortcut;
36 class QTimer;
37 class QToolButton;
38 class QTreeView;
39
40 class QTX_EXPORT QtxSearchTool : public QFrame
41 {
42   Q_OBJECT
43
44   //! Find operation type
45   enum {
46     fAny,             //!< find any appropriate
47     fNext,            //!< find next appropriate
48     fPrevious,        //!< find previous appropriate
49     fFirst,           //!< find first appropriate
50     fLast             //!< find last appropriate
51   };
52
53 public:
54   class Searcher;
55
56   //! Search tool controls
57   typedef enum { 
58     Search    = 0x00000001,                          //!< line edit field for searched text
59     Next      = 0x00000002,                          //!< 'find next' button
60     Prev      = 0x00000004,                          //!< 'find previous' button
61     First     = 0x00000008,                          //!< 'find first' button
62     Last      = 0x00000010,                          //!< 'find last' button
63     Close     = 0x00000020,                          //!< 'close' button
64     Case      = 0x00000040,                          //!< 'case sensitive search' check box
65     RegExp    = 0x00000080,                          //!< 'regular expression' check box
66     Wrap      = 0x00000100,                          //!< 'wrap search' check box
67     Basic     = Search   | Next   | Prev | Close,    //!< basic controls: text field, "next", "prev" and "close" buttons
68     Standard  = Basic    | First  | Last,            //!< standard controls: same as Basic plus "first" and "last" buttons
69     Modifiers = Case     | RegExp | Wrap,            //!< search modifiers check boxes 
70     All       = Standard | Modifiers                 //!< all controls
71   } Controls;
72
73   typedef enum {
74     None        = 0x00000000,                                 //!< search tool can be activated programmatically only
75     HotKey      = 0x00000001,                                 //!< search tool is activated by hot key
76     SlashKey    = 0x00000002,                                 //!< search tool is activated by splash key ( / )
77     StandardKey = 0x00000004,                                 //!< search tool is activated by standard key combination (e.g. F3)
78     PrintKey    = 0x00000008,                                 //!< search tool is activated when user types any print key
79     Any         = HotKey | SlashKey | StandardKey | PrintKey  //!< search tool is activated by any of above mentioned ways
80   } Activator;
81
82   QtxSearchTool( QWidget*, QWidget* = 0, int = All, Qt::Orientation = Qt::Vertical );
83   QtxSearchTool( QWidget*, int = All, Qt::Orientation = Qt::Vertical );
84   virtual ~QtxSearchTool();
85
86   QWidget*            watchedWidget() const;
87   void                setWatchedWidget( QWidget* );
88
89   Searcher*           searcher() const;
90   void                setSearcher( Searcher* );
91
92   int                 activators() const;
93   void                setActivators( const int );
94
95   int                 controls() const;
96   void                setControls( const int );
97
98   QList<QKeySequence> shortcuts() const;
99   void                setShortcuts( const QKeySequence& );
100   void                setShortcuts( const QList<QKeySequence>& );
101
102   int                 addCustomWidget( QWidget*, int = -1 );
103   QWidget*            customWidget( int ) const;
104   int                 customWidgetId( QWidget* ) const;
105
106   bool                isAutoHideEnabled() const;
107   void                enableAutoHide( bool );
108
109   bool                isCaseSensitive() const;
110   bool                isRegExpSearch() const;
111   bool                isSearchWrapped() const;
112
113   void                setCaseSensitive( bool );
114   void                setRegExpSearch( bool );
115   void                setSearchWrapped( bool );
116
117   virtual bool        event( QEvent* );
118   virtual bool        eventFilter( QObject*, QEvent* );
119
120 public slots:
121   virtual void        find();
122   virtual void        findNext();
123   virtual void        findPrevious();
124   virtual void        findFirst();
125   virtual void        findLast();
126
127 private slots:
128   void                find( const QString&, int = fAny );
129   void                modifierSwitched();
130
131 private:
132   void                init( Qt::Orientation );
133   bool                focused() const;
134   void                clearShortcuts();
135   void                initShortcuts( const QList<QKeySequence>& );
136   void                updateShortcuts();
137   void                updateControls();
138
139 private:
140   typedef QPointer<QShortcut> ShortcutPtr;
141   typedef QList<ShortcutPtr>  ShortcutList;
142
143 private:
144   QWidget*            myBtnWidget;
145   QWidget*            myModWidget;
146   QLineEdit*          myData;
147   QToolButton*        myToFirst;
148   QToolButton*        myToLast;
149   QToolButton*        myNext;
150   QToolButton*        myPrev;
151   QToolButton*        myClose;
152   QCheckBox*          myIsCaseSens;
153   QCheckBox*          myIsRegExp;
154   QCheckBox*          myWrap;
155   QWidget*            myWatched;
156   Searcher*           mySearcher;
157   int                 myControls;
158   int                 myActivators;
159   ShortcutList        myShortcuts;
160   QTimer*             myAutoHideTimer;
161   bool                myAutoHideEnabled;
162   QMap<int, QWidget*> myWidgets;
163 };
164
165 class QTX_EXPORT QtxSearchTool::Searcher
166 {
167 public:
168   Searcher();
169   virtual ~Searcher();
170
171   virtual bool find( const QString&, QtxSearchTool* ) = 0;
172   virtual bool findNext( const QString&, QtxSearchTool* ) = 0;
173   virtual bool findPrevious( const QString&, QtxSearchTool* ) = 0;
174   virtual bool findFirst( const QString&, QtxSearchTool* ) = 0;
175   virtual bool findLast( const QString&, QtxSearchTool* ) = 0;
176 };
177
178 class QTX_EXPORT QtxTreeViewSearcher : public QtxSearchTool::Searcher
179 {
180 public:
181   QtxTreeViewSearcher( QTreeView*, int = 0 );
182   virtual ~QtxTreeViewSearcher();
183
184   int                    searchColumn() const;
185   void                   setSearchColumn( int );
186   
187   virtual bool           find( const QString&, QtxSearchTool* );
188   virtual bool           findNext( const QString&, QtxSearchTool* );
189   virtual bool           findPrevious( const QString&, QtxSearchTool* );
190   virtual bool           findFirst( const QString&, QtxSearchTool* );
191   virtual bool           findLast( const QString&, QtxSearchTool* );
192
193 protected:
194   virtual Qt::MatchFlags matchFlags( QtxSearchTool* ) const;
195   
196 private:
197   QModelIndexList        findItems( const QString&, QtxSearchTool* );
198   QModelIndex            findNearest( const QModelIndex&, const QModelIndexList&, bool );
199   void                   showItem( const QModelIndex& );
200   QString                getId( const QModelIndex& );
201   int                    compareIndices( const QModelIndex&, const QModelIndex& );
202
203 private:
204   QTreeView*             myView;
205   int                    myColumn;
206   QPersistentModelIndex  myIndex;
207 };
208
209 #endif // QTXSEARCHTOOL_H