xref: /glogg/src/quickfindmux.h (revision d2b38ed83f53a067d87d84d0ac5f64ab0d88fa3a)
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     void registerSelector( const QuickFindMuxSelectorInterface* selector );
87 
88     // Set the direction that will be used by the search when searching
89     // forward.
90     void setDirection( QFDirection direction );
91 
92   signals:
93     void patternChanged( const QString& );
94     void notify( const QFNotification& );
95     void clearNotification();
96 
97   public slots:
98     // Signal the current pattern must be altered (will start an incremental
99     // search if the options are configured in such a way).
100     void setNewPattern( const QString& new_pattern, bool ignore_case );
101 
102     // Signal the current pattern must be altered and is confirmed
103     // (will stop an incremental search if needed)
104     void confirmPattern( const QString& new_pattern, bool ignore_case );
105 
106     // Signal the user cancelled the search
107     // (used for incremental only)
108     void cancelSearch();
109 
110     // Starts a search in the specified direction
111     void searchNext();
112     void searchPrevious();
113 
114     // Idem but ignore the direction and always search in the
115     // specified direction
116     void searchForward();
117     void searchBackward();
118 
119   private slots:
120     void changeQuickFind( const QString& new_pattern,
121             QuickFindMux::QFDirection new_direction );
122     void notifyPatternChanged();
123 
124   private:
125     const QuickFindMuxSelectorInterface* selector_;
126 
127     // The (application wide) quick find pattern
128     std::shared_ptr<QuickFindPattern> pattern_;
129 
130     QFDirection currentDirection_;
131 
132     std::vector<QObject*> registeredSearchables_;
133 
134     SearchableWidgetInterface* getSearchableWidget() const;
135     void registerSearchable( QObject* searchable );
136     void unregisterAllSearchables();
137 };
138 
139 #endif
140