xref: /glogg/src/quickfindmux.h (revision 6e2e573c451ec24d720c967fab68538eaa3f3ab5) !
1 /*
2  * Copyright (C) 2013, 2014 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef QUICKFINDMUX_H
21 #define QUICKFINDMUX_H
22 
23 #include <memory>
24 #include <vector>
25 
26 #include <QObject>
27 #include <QString>
28 
29 #include "quickfindpattern.h"
30 
31 // Interface representing a widget searchable in both direction.
32 class SearchableWidgetInterface {
33   public:
34     virtual void searchForward() = 0;
35     virtual void searchBackward() = 0;
36 
37     virtual void incrementallySearchForward() = 0;
38     virtual void incrementallySearchBackward() = 0;
39     virtual void incrementalSearchStop() = 0;
40     virtual void incrementalSearchAbort() = 0;
41 };
42 
43 // Interface representing the selector. It will be called and asked
44 // who the search have to be forwarded to.
45 class QuickFindMuxSelectorInterface {
46   public:
47     // Return the searchable widget to use.
48     SearchableWidgetInterface* getActiveSearchable() const
49     { return doGetActiveSearchable(); }
50     // Return the list of all possible searchables, this
51     // is done on registration in order to establish
52     // listeners on all searchables.
53     std::vector<QObject*> getAllSearchables() const
54     { return doGetAllSearchables(); }
55   protected:
56     virtual SearchableWidgetInterface* doGetActiveSearchable() const = 0;
57     virtual std::vector<QObject*> doGetAllSearchables() const = 0;
58 };
59 
60 class QFNotification;
61 
62 // Represents a multiplexer (unique application wise) dispatching the
63 // Quick Find search from the UI to the relevant view.
64 // It is also its responsability to determine if an incremental search
65 // must be performed and to react accordingly.
66 class QuickFindMux : public QObject
67 {
68   Q_OBJECT
69 
70   public:
71 
72     enum QFDirection {
73         Forward,
74         Backward,
75     };
76 
77     // Construct the multiplexer, taking a reference to the pattern
78     QuickFindMux( std::shared_ptr<QuickFindPattern> pattern );
79 
80     // Register a new selector, which will be called and asked
81     // who the search have to be forwarded to.
82     // The selector is called immediately when registering to get the list of
83     // searchables.
84     // The previous selector and its associated views are automatically
85     // deregistered.
86     // A null selector is accepted, in this case QFM functionalities are
87     // disabled until a valid selector is registered.
88     void registerSelector( const QuickFindMuxSelectorInterface* selector );
89 
90     // Set the direction that will be used by the search when searching
91     // forward.
92     void setDirection( QFDirection direction );
93 
94   signals:
95     void patternChanged( const QString& );
96     void notify( const QFNotification& );
97     void clearNotification();
98 
99   public slots:
100     // Signal the current pattern must be altered (will start an incremental
101     // search if the options are configured in such a way).
102     void setNewPattern( const QString& new_pattern, bool ignore_case );
103 
104     // Signal the current pattern must be altered and is confirmed
105     // (will stop an incremental search if needed)
106     void confirmPattern( const QString& new_pattern, bool ignore_case );
107 
108     // Signal the user cancelled the search
109     // (used for incremental only)
110     void cancelSearch();
111 
112     // Starts a search in the specified direction
113     void searchNext();
114     void searchPrevious();
115 
116     // Idem but ignore the direction and always search in the
117     // specified direction
118     void searchForward();
119     void searchBackward();
120 
121   private slots:
122     void changeQuickFind( const QString& new_pattern,
123             QuickFindMux::QFDirection new_direction );
124     void notifyPatternChanged();
125 
126   private:
127     const QuickFindMuxSelectorInterface* selector_;
128 
129     // The (application wide) quick find pattern
130     std::shared_ptr<QuickFindPattern> pattern_;
131 
132     QFDirection currentDirection_;
133 
134     std::vector<QObject*> registeredSearchables_;
135 
136     SearchableWidgetInterface* getSearchableWidget() const;
137     void registerSearchable( QObject* searchable );
138     void unregisterAllSearchables();
139 };
140 
141 #endif
142