xref: /glogg/src/quickfindmux.cpp (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 #include "log.h"
21 
22 #include "persistentinfo.h"
23 #include "configuration.h"
24 #include "quickfindmux.h"
25 
26 QuickFindMux::QuickFindMux( const QuickFindMuxSelectorInterface* selector ) :
27     QObject(), pattern_()
28 {
29     // The selector object we will use when forwarding search requests
30     selector_ = selector;
31 
32     // Forward the pattern's signal to our listeners
33     connect( &pattern_, SIGNAL( patternUpdated() ),
34              this, SLOT( notifyPatternChanged() ) );
35 }
36 
37 //
38 // Public member functions
39 //
40 void QuickFindMux::registerSearchable( QObject* searchable )
41 {
42     // The searchable can change our qf pattern
43     connect( searchable,
44              SIGNAL( changeQuickFind( const QString&, QuickFindMux::QFDirection ) ),
45              this, SLOT( changeQuickFind( const QString&, QuickFindMux::QFDirection ) ) );
46     // Send us notifications
47     connect( searchable, SIGNAL( notifyQuickFind( const QFNotification& ) ),
48              this, SIGNAL( notify( const QFNotification& ) ) );
49     // And clear them
50     connect( searchable, SIGNAL( clearQuickFindNotification() ),
51              this, SIGNAL( clearNotification() ) );
52     // Search can be initiated by the view itself
53     connect( searchable, SIGNAL( searchNext() ),
54              this, SLOT( searchNext() ) );
55     connect( searchable, SIGNAL( searchPrevious() ),
56              this, SLOT( searchPrevious() ) );
57 }
58 
59 void QuickFindMux::unregisterSearchable( QObject* searchable )
60 {
61     disconnect( searchable );
62 }
63 
64 void QuickFindMux::setDirection( QFDirection direction )
65 {
66     LOG(logDEBUG) << "QuickFindMux::setDirection: new direction: " << direction;
67     currentDirection_ = direction;
68 }
69 
70 //
71 // Public slots
72 //
73 void QuickFindMux::searchNext()
74 {
75     LOG(logDEBUG) << "QuickFindMux::searchNext";
76     if ( currentDirection_ == Forward )
77         searchForward();
78     else
79         searchBackward();
80 }
81 
82 void QuickFindMux::searchPrevious()
83 {
84     LOG(logDEBUG) << "QuickFindMux::searchPrevious";
85     if ( currentDirection_ == Forward )
86         searchBackward();
87     else
88         searchForward();
89 }
90 
91 void QuickFindMux::searchForward()
92 {
93     LOG(logDEBUG) << "QuickFindMux::searchForward";
94     SearchableWidgetInterface* searchable = getSearchableWidget();
95 
96     searchable->searchForward();
97 }
98 
99 void QuickFindMux::searchBackward()
100 {
101     LOG(logDEBUG) << "QuickFindMux::searchBackward";
102     SearchableWidgetInterface* searchable = getSearchableWidget();
103 
104     searchable->searchBackward();
105 }
106 
107 void QuickFindMux::setNewPattern(
108         const QString& new_pattern, bool ignore_case )
109 {
110     static Configuration& config = Persistent<Configuration>( "settings" );
111 
112     LOG(logDEBUG) << "QuickFindMux::setNewPattern";
113     pattern_.changeSearchPattern( new_pattern, ignore_case );
114 
115     // If we must do an incremental search, we do it now
116     if ( config.isQuickfindIncremental() ) {
117         SearchableWidgetInterface* searchable = getSearchableWidget();
118         if ( currentDirection_ == Forward )
119             searchable->incrementallySearchForward();
120         else
121             searchable->incrementallySearchBackward();
122     }
123 }
124 
125 void QuickFindMux::confirmPattern(
126         const QString& new_pattern, bool ignore_case )
127 {
128     static Configuration& config = Persistent<Configuration>( "settings" );
129 
130     pattern_.changeSearchPattern( new_pattern, ignore_case );
131 
132     // if non-incremental, we perform the search now
133     if ( ! config.isQuickfindIncremental() ) {
134         searchNext();
135     }
136     else {
137         SearchableWidgetInterface* searchable = getSearchableWidget();
138         searchable->incrementalSearchStop();
139     }
140 }
141 
142 void QuickFindMux::cancelSearch()
143 {
144     static Configuration& config = Persistent<Configuration>( "settings" );
145 
146     if ( config.isQuickfindIncremental() ) {
147         SearchableWidgetInterface* searchable = getSearchableWidget();
148         searchable->incrementalSearchAbort();
149     }
150 }
151 
152 //
153 // Private slots
154 //
155 void QuickFindMux::changeQuickFind(
156         const QString& new_pattern, QFDirection new_direction )
157 {
158     pattern_.changeSearchPattern( new_pattern );
159     setDirection( new_direction );
160 }
161 
162 void QuickFindMux::notifyPatternChanged()
163 {
164     emit patternChanged( pattern_.getPattern() );
165 }
166 
167 //
168 // Private member functions
169 //
170 
171 // Use the registered 'selector' to determine where to send the search requests.
172 SearchableWidgetInterface* QuickFindMux::getSearchableWidget() const
173 {
174     SearchableWidgetInterface* searchable = NULL;
175 
176     if ( selector_ )
177         searchable = selector_->getActiveSearchable();
178     else
179         LOG(logERROR) << "QuickFindMux::getActiveSearchable() no registered selector";
180 
181     return searchable;
182 }
183