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