1bb02e0acSNicolas Bonnefon /* 2b423cd88SNicolas Bonnefon * Copyright (C) 2013, 2014 Nicolas Bonnefon and other contributors 3bb02e0acSNicolas Bonnefon * 4bb02e0acSNicolas Bonnefon * This file is part of glogg. 5bb02e0acSNicolas Bonnefon * 6bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9bb02e0acSNicolas Bonnefon * (at your option) any later version. 10bb02e0acSNicolas Bonnefon * 11bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15bb02e0acSNicolas Bonnefon * 16bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18bb02e0acSNicolas Bonnefon */ 19bb02e0acSNicolas Bonnefon 20bb02e0acSNicolas Bonnefon #include "log.h" 21bb02e0acSNicolas Bonnefon 22bb02e0acSNicolas Bonnefon #include "persistentinfo.h" 23bb02e0acSNicolas Bonnefon #include "configuration.h" 24bb02e0acSNicolas Bonnefon #include "quickfindmux.h" 25bb02e0acSNicolas Bonnefon 26b423cd88SNicolas Bonnefon #include "qfnotifications.h" 27b423cd88SNicolas Bonnefon 28b423cd88SNicolas Bonnefon QuickFindMux::QuickFindMux( std::shared_ptr<QuickFindPattern> pattern ) : 29*d2b38ed8SNicolas Bonnefon QObject(), pattern_( pattern ), registeredSearchables_() 30bb02e0acSNicolas Bonnefon { 31b423cd88SNicolas Bonnefon selector_ = nullptr; 32bb02e0acSNicolas Bonnefon 33bb02e0acSNicolas Bonnefon // Forward the pattern's signal to our listeners 34b423cd88SNicolas Bonnefon connect( pattern_.get(), SIGNAL( patternUpdated() ), 35bb02e0acSNicolas Bonnefon this, SLOT( notifyPatternChanged() ) ); 36bb02e0acSNicolas Bonnefon } 37bb02e0acSNicolas Bonnefon 38bb02e0acSNicolas Bonnefon // 39bb02e0acSNicolas Bonnefon // Public member functions 40bb02e0acSNicolas Bonnefon // 41b423cd88SNicolas Bonnefon void QuickFindMux::registerSelector( 42b423cd88SNicolas Bonnefon const QuickFindMuxSelectorInterface* selector ) 43bb02e0acSNicolas Bonnefon { 44b423cd88SNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::registerSelector"; 45bb02e0acSNicolas Bonnefon 46b423cd88SNicolas Bonnefon // The selector object we will use when forwarding search requests 47b423cd88SNicolas Bonnefon selector_ = selector; 48b423cd88SNicolas Bonnefon 49b423cd88SNicolas Bonnefon unregisterAllSearchables(); 50b423cd88SNicolas Bonnefon 51b423cd88SNicolas Bonnefon for ( auto i: selector_->getAllSearchables() ) 52b423cd88SNicolas Bonnefon registerSearchable( i ); 53bb02e0acSNicolas Bonnefon } 54bb02e0acSNicolas Bonnefon 55bb02e0acSNicolas Bonnefon void QuickFindMux::setDirection( QFDirection direction ) 56bb02e0acSNicolas Bonnefon { 57bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::setDirection: new direction: " << direction; 58bb02e0acSNicolas Bonnefon currentDirection_ = direction; 59bb02e0acSNicolas Bonnefon } 60bb02e0acSNicolas Bonnefon 61bb02e0acSNicolas Bonnefon // 62bb02e0acSNicolas Bonnefon // Public slots 63bb02e0acSNicolas Bonnefon // 64bb02e0acSNicolas Bonnefon void QuickFindMux::searchNext() 65bb02e0acSNicolas Bonnefon { 66bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::searchNext"; 67bb02e0acSNicolas Bonnefon if ( currentDirection_ == Forward ) 68bb02e0acSNicolas Bonnefon searchForward(); 69bb02e0acSNicolas Bonnefon else 70bb02e0acSNicolas Bonnefon searchBackward(); 71bb02e0acSNicolas Bonnefon } 72bb02e0acSNicolas Bonnefon 73bb02e0acSNicolas Bonnefon void QuickFindMux::searchPrevious() 74bb02e0acSNicolas Bonnefon { 75bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::searchPrevious"; 76bb02e0acSNicolas Bonnefon if ( currentDirection_ == Forward ) 77bb02e0acSNicolas Bonnefon searchBackward(); 78bb02e0acSNicolas Bonnefon else 79bb02e0acSNicolas Bonnefon searchForward(); 80bb02e0acSNicolas Bonnefon } 81bb02e0acSNicolas Bonnefon 82bb02e0acSNicolas Bonnefon void QuickFindMux::searchForward() 83bb02e0acSNicolas Bonnefon { 84bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::searchForward"; 85bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 86bb02e0acSNicolas Bonnefon 87bb02e0acSNicolas Bonnefon searchable->searchForward(); 88bb02e0acSNicolas Bonnefon } 89bb02e0acSNicolas Bonnefon 90bb02e0acSNicolas Bonnefon void QuickFindMux::searchBackward() 91bb02e0acSNicolas Bonnefon { 92bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::searchBackward"; 93bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 94bb02e0acSNicolas Bonnefon 95bb02e0acSNicolas Bonnefon searchable->searchBackward(); 96bb02e0acSNicolas Bonnefon } 97bb02e0acSNicolas Bonnefon 98bb02e0acSNicolas Bonnefon void QuickFindMux::setNewPattern( 99bb02e0acSNicolas Bonnefon const QString& new_pattern, bool ignore_case ) 100bb02e0acSNicolas Bonnefon { 101bb02e0acSNicolas Bonnefon static Configuration& config = Persistent<Configuration>( "settings" ); 102bb02e0acSNicolas Bonnefon 103bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::setNewPattern"; 104b423cd88SNicolas Bonnefon pattern_->changeSearchPattern( new_pattern, ignore_case ); 105bb02e0acSNicolas Bonnefon 106bb02e0acSNicolas Bonnefon // If we must do an incremental search, we do it now 107bb02e0acSNicolas Bonnefon if ( config.isQuickfindIncremental() ) { 108bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 109bb02e0acSNicolas Bonnefon if ( currentDirection_ == Forward ) 110bb02e0acSNicolas Bonnefon searchable->incrementallySearchForward(); 111bb02e0acSNicolas Bonnefon else 112bb02e0acSNicolas Bonnefon searchable->incrementallySearchBackward(); 113bb02e0acSNicolas Bonnefon } 114bb02e0acSNicolas Bonnefon } 115bb02e0acSNicolas Bonnefon 116bb02e0acSNicolas Bonnefon void QuickFindMux::confirmPattern( 117bb02e0acSNicolas Bonnefon const QString& new_pattern, bool ignore_case ) 118bb02e0acSNicolas Bonnefon { 119bb02e0acSNicolas Bonnefon static Configuration& config = Persistent<Configuration>( "settings" ); 120bb02e0acSNicolas Bonnefon 121b423cd88SNicolas Bonnefon pattern_->changeSearchPattern( new_pattern, ignore_case ); 122bb02e0acSNicolas Bonnefon 123bb02e0acSNicolas Bonnefon // if non-incremental, we perform the search now 124bb02e0acSNicolas Bonnefon if ( ! config.isQuickfindIncremental() ) { 125bb02e0acSNicolas Bonnefon searchNext(); 126bb02e0acSNicolas Bonnefon } 127bb02e0acSNicolas Bonnefon else { 128bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 129bb02e0acSNicolas Bonnefon searchable->incrementalSearchStop(); 130bb02e0acSNicolas Bonnefon } 131bb02e0acSNicolas Bonnefon } 132bb02e0acSNicolas Bonnefon 133bb02e0acSNicolas Bonnefon void QuickFindMux::cancelSearch() 134bb02e0acSNicolas Bonnefon { 135bb02e0acSNicolas Bonnefon static Configuration& config = Persistent<Configuration>( "settings" ); 136bb02e0acSNicolas Bonnefon 137bb02e0acSNicolas Bonnefon if ( config.isQuickfindIncremental() ) { 138bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 139bb02e0acSNicolas Bonnefon searchable->incrementalSearchAbort(); 140bb02e0acSNicolas Bonnefon } 141bb02e0acSNicolas Bonnefon } 142bb02e0acSNicolas Bonnefon 143bb02e0acSNicolas Bonnefon // 144bb02e0acSNicolas Bonnefon // Private slots 145bb02e0acSNicolas Bonnefon // 146bb02e0acSNicolas Bonnefon void QuickFindMux::changeQuickFind( 147bb02e0acSNicolas Bonnefon const QString& new_pattern, QFDirection new_direction ) 148bb02e0acSNicolas Bonnefon { 149b423cd88SNicolas Bonnefon pattern_->changeSearchPattern( new_pattern ); 150bb02e0acSNicolas Bonnefon setDirection( new_direction ); 151bb02e0acSNicolas Bonnefon } 152bb02e0acSNicolas Bonnefon 153bb02e0acSNicolas Bonnefon void QuickFindMux::notifyPatternChanged() 154bb02e0acSNicolas Bonnefon { 155b423cd88SNicolas Bonnefon emit patternChanged( pattern_->getPattern() ); 156bb02e0acSNicolas Bonnefon } 157bb02e0acSNicolas Bonnefon 158bb02e0acSNicolas Bonnefon // 159bb02e0acSNicolas Bonnefon // Private member functions 160bb02e0acSNicolas Bonnefon // 161bb02e0acSNicolas Bonnefon 162bb02e0acSNicolas Bonnefon // Use the registered 'selector' to determine where to send the search requests. 163bb02e0acSNicolas Bonnefon SearchableWidgetInterface* QuickFindMux::getSearchableWidget() const 164bb02e0acSNicolas Bonnefon { 165b423cd88SNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::getSearchableWidget"; 166b423cd88SNicolas Bonnefon 167bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = NULL; 168bb02e0acSNicolas Bonnefon 169bb02e0acSNicolas Bonnefon if ( selector_ ) 170bb02e0acSNicolas Bonnefon searchable = selector_->getActiveSearchable(); 171bb02e0acSNicolas Bonnefon else 172bb02e0acSNicolas Bonnefon LOG(logERROR) << "QuickFindMux::getActiveSearchable() no registered selector"; 173bb02e0acSNicolas Bonnefon 174bb02e0acSNicolas Bonnefon return searchable; 175bb02e0acSNicolas Bonnefon } 176b423cd88SNicolas Bonnefon 177b423cd88SNicolas Bonnefon void QuickFindMux::registerSearchable( QObject* searchable ) 178b423cd88SNicolas Bonnefon { 179b423cd88SNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::registerSearchable"; 180b423cd88SNicolas Bonnefon 181b423cd88SNicolas Bonnefon // The searchable can change our qf pattern 182b423cd88SNicolas Bonnefon connect( searchable, 183b423cd88SNicolas Bonnefon SIGNAL( changeQuickFind( const QString&, QuickFindMux::QFDirection ) ), 184b423cd88SNicolas Bonnefon this, SLOT( changeQuickFind( const QString&, QuickFindMux::QFDirection ) ) ); 185b423cd88SNicolas Bonnefon // Send us notifications 186b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( notifyQuickFind( const QFNotification& ) ), 187b423cd88SNicolas Bonnefon this, SIGNAL( notify( const QFNotification& ) ) ); 188b423cd88SNicolas Bonnefon 189b423cd88SNicolas Bonnefon // And clear them 190b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( clearQuickFindNotification() ), 191b423cd88SNicolas Bonnefon this, SIGNAL( clearNotification() ) ); 192b423cd88SNicolas Bonnefon // Search can be initiated by the view itself 193b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( searchNext() ), 194b423cd88SNicolas Bonnefon this, SLOT( searchNext() ) ); 195b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( searchPrevious() ), 196b423cd88SNicolas Bonnefon this, SLOT( searchPrevious() ) ); 197*d2b38ed8SNicolas Bonnefon 198*d2b38ed8SNicolas Bonnefon registeredSearchables_.push_back( searchable ); 199b423cd88SNicolas Bonnefon } 200b423cd88SNicolas Bonnefon 201b423cd88SNicolas Bonnefon void QuickFindMux::unregisterAllSearchables() 202b423cd88SNicolas Bonnefon { 203*d2b38ed8SNicolas Bonnefon for ( auto searchable: registeredSearchables_ ) 204*d2b38ed8SNicolas Bonnefon disconnect( searchable, 0, this, 0 ); 205*d2b38ed8SNicolas Bonnefon 206*d2b38ed8SNicolas Bonnefon registeredSearchables_.clear(); 207b423cd88SNicolas Bonnefon } 208