xref: /glogg/src/quickfindmux.h (revision bb02e0acf44ddb4e4f83d6127a1e488789162922)
1 /*
2  * Copyright (C) 2013 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 <QObject>
24 #include <QString>
25 
26 #include "quickfindpattern.h"
27 
28 // Interface representing a widget searchable in both direction.
29 class SearchableWidgetInterface {
30   public:
31     virtual void searchForward() = 0;
32     virtual void searchBackward() = 0;
33 
34     virtual void incrementallySearchForward() = 0;
35     virtual void incrementallySearchBackward() = 0;
36     virtual void incrementalSearchStop() = 0;
37     virtual void incrementalSearchAbort() = 0;
38 };
39 
40 // Interface representing the selector. It will be called and asked
41 // who the search have to be forwarded to.
42 class QuickFindMuxSelectorInterface {
43   public:
44     virtual SearchableWidgetInterface* getActiveSearchable() const = 0;
45 };
46 
47 class QFNotification;
48 
49 // Represents a multiplexer (unique application wise) dispatching the
50 // Quick Find search from the UI to the relevant view.
51 // It is also its responsability to determine if an incremental search
52 // must be performed and to react accordingly.
53 // It owns the QuickFindPattern.
54 class QuickFindMux : public QObject
55 {
56   Q_OBJECT
57 
58   public:
59 
60     enum QFDirection {
61         Forward,
62         Backward,
63     };
64 
65     // Construct the multiplexer, the selector class will be called
66     // and ask who the search have to be forwarded to.
67     QuickFindMux( const QuickFindMuxSelectorInterface* selector );
68 
69     // Register/unregister this searchable widget with the mux for messages
70     // to be forwarded back to the client.
71     // The client should call this for each possible searchable widget.
72     void registerSearchable( QObject* searchable );
73     void unregisterSearchable( QObject* searchable );
74 
75     // Set the direction that will be used by the search when searching
76     // forward.
77     void setDirection( QFDirection direction );
78 
79     // Get a pointer to the pattern
80     QuickFindPattern* getPattern() { return &pattern_; }
81 
82   signals:
83     void patternChanged( const QString& );
84     void notify( const QFNotification& );
85     void clearNotification();
86 
87   public slots:
88     // Signal the current pattern must be altered (will start an incremental
89     // search if the options are configured in such a way).
90     void setNewPattern( const QString& new_pattern, bool ignore_case );
91 
92     // Signal the current pattern must be altered and is confirmed
93     // (will stop an incremental search if needed)
94     void confirmPattern( const QString& new_pattern, bool ignore_case );
95 
96     // Signal the user cancelled the search
97     // (used for incremental only)
98     void cancelSearch();
99 
100     // Starts a search in the specified direction
101     void searchNext();
102     void searchPrevious();
103 
104     // Idem but ignore the direction and always search in the
105     // specified direction
106     void searchForward();
107     void searchBackward();
108 
109   private slots:
110     void changeQuickFind( const QString& new_pattern,
111             QuickFindMux::QFDirection new_direction );
112     void notifyPatternChanged();
113 
114   private:
115     const QuickFindMuxSelectorInterface* selector_;
116 
117     // The (application wide) quick find pattern
118     QuickFindPattern pattern_;
119 
120     QFDirection currentDirection_;
121 
122     SearchableWidgetInterface* getSearchableWidget() const;
123 };
124 
125 #endif
126