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 ) : 29d2b38ed8SNicolas 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 { 101*11582726SNicolas Bonnefon static std::shared_ptr<Configuration> config = 102*11582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 103bb02e0acSNicolas Bonnefon 104bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::setNewPattern"; 105b423cd88SNicolas Bonnefon pattern_->changeSearchPattern( new_pattern, ignore_case ); 106bb02e0acSNicolas Bonnefon 107bb02e0acSNicolas Bonnefon // If we must do an incremental search, we do it now 108*11582726SNicolas Bonnefon if ( config->isQuickfindIncremental() ) { 109bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 110bb02e0acSNicolas Bonnefon if ( currentDirection_ == Forward ) 111bb02e0acSNicolas Bonnefon searchable->incrementallySearchForward(); 112bb02e0acSNicolas Bonnefon else 113bb02e0acSNicolas Bonnefon searchable->incrementallySearchBackward(); 114bb02e0acSNicolas Bonnefon } 115bb02e0acSNicolas Bonnefon } 116bb02e0acSNicolas Bonnefon 117bb02e0acSNicolas Bonnefon void QuickFindMux::confirmPattern( 118bb02e0acSNicolas Bonnefon const QString& new_pattern, bool ignore_case ) 119bb02e0acSNicolas Bonnefon { 120*11582726SNicolas Bonnefon static std::shared_ptr<Configuration> config = 121*11582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 122bb02e0acSNicolas Bonnefon 123b423cd88SNicolas Bonnefon pattern_->changeSearchPattern( new_pattern, ignore_case ); 124bb02e0acSNicolas Bonnefon 125bb02e0acSNicolas Bonnefon // if non-incremental, we perform the search now 126*11582726SNicolas Bonnefon if ( ! config->isQuickfindIncremental() ) { 127bb02e0acSNicolas Bonnefon searchNext(); 128bb02e0acSNicolas Bonnefon } 129bb02e0acSNicolas Bonnefon else { 130bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 131bb02e0acSNicolas Bonnefon searchable->incrementalSearchStop(); 132bb02e0acSNicolas Bonnefon } 133bb02e0acSNicolas Bonnefon } 134bb02e0acSNicolas Bonnefon 135bb02e0acSNicolas Bonnefon void QuickFindMux::cancelSearch() 136bb02e0acSNicolas Bonnefon { 137*11582726SNicolas Bonnefon static std::shared_ptr<Configuration> config = 138*11582726SNicolas Bonnefon Persistent<Configuration>( "settings" ); 139bb02e0acSNicolas Bonnefon 140*11582726SNicolas Bonnefon if ( config->isQuickfindIncremental() ) { 141bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = getSearchableWidget(); 142bb02e0acSNicolas Bonnefon searchable->incrementalSearchAbort(); 143bb02e0acSNicolas Bonnefon } 144bb02e0acSNicolas Bonnefon } 145bb02e0acSNicolas Bonnefon 146bb02e0acSNicolas Bonnefon // 147bb02e0acSNicolas Bonnefon // Private slots 148bb02e0acSNicolas Bonnefon // 149bb02e0acSNicolas Bonnefon void QuickFindMux::changeQuickFind( 150bb02e0acSNicolas Bonnefon const QString& new_pattern, QFDirection new_direction ) 151bb02e0acSNicolas Bonnefon { 152b423cd88SNicolas Bonnefon pattern_->changeSearchPattern( new_pattern ); 153bb02e0acSNicolas Bonnefon setDirection( new_direction ); 154bb02e0acSNicolas Bonnefon } 155bb02e0acSNicolas Bonnefon 156bb02e0acSNicolas Bonnefon void QuickFindMux::notifyPatternChanged() 157bb02e0acSNicolas Bonnefon { 158b423cd88SNicolas Bonnefon emit patternChanged( pattern_->getPattern() ); 159bb02e0acSNicolas Bonnefon } 160bb02e0acSNicolas Bonnefon 161bb02e0acSNicolas Bonnefon // 162bb02e0acSNicolas Bonnefon // Private member functions 163bb02e0acSNicolas Bonnefon // 164bb02e0acSNicolas Bonnefon 165bb02e0acSNicolas Bonnefon // Use the registered 'selector' to determine where to send the search requests. 166bb02e0acSNicolas Bonnefon SearchableWidgetInterface* QuickFindMux::getSearchableWidget() const 167bb02e0acSNicolas Bonnefon { 168b423cd88SNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::getSearchableWidget"; 169b423cd88SNicolas Bonnefon 170bb02e0acSNicolas Bonnefon SearchableWidgetInterface* searchable = NULL; 171bb02e0acSNicolas Bonnefon 172bb02e0acSNicolas Bonnefon if ( selector_ ) 173bb02e0acSNicolas Bonnefon searchable = selector_->getActiveSearchable(); 174bb02e0acSNicolas Bonnefon else 175bb02e0acSNicolas Bonnefon LOG(logERROR) << "QuickFindMux::getActiveSearchable() no registered selector"; 176bb02e0acSNicolas Bonnefon 177bb02e0acSNicolas Bonnefon return searchable; 178bb02e0acSNicolas Bonnefon } 179b423cd88SNicolas Bonnefon 180b423cd88SNicolas Bonnefon void QuickFindMux::registerSearchable( QObject* searchable ) 181b423cd88SNicolas Bonnefon { 182b423cd88SNicolas Bonnefon LOG(logDEBUG) << "QuickFindMux::registerSearchable"; 183b423cd88SNicolas Bonnefon 184b423cd88SNicolas Bonnefon // The searchable can change our qf pattern 185b423cd88SNicolas Bonnefon connect( searchable, 186b423cd88SNicolas Bonnefon SIGNAL( changeQuickFind( const QString&, QuickFindMux::QFDirection ) ), 187b423cd88SNicolas Bonnefon this, SLOT( changeQuickFind( const QString&, QuickFindMux::QFDirection ) ) ); 188b423cd88SNicolas Bonnefon // Send us notifications 189b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( notifyQuickFind( const QFNotification& ) ), 190b423cd88SNicolas Bonnefon this, SIGNAL( notify( const QFNotification& ) ) ); 191b423cd88SNicolas Bonnefon 192b423cd88SNicolas Bonnefon // And clear them 193b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( clearQuickFindNotification() ), 194b423cd88SNicolas Bonnefon this, SIGNAL( clearNotification() ) ); 195b423cd88SNicolas Bonnefon // Search can be initiated by the view itself 196b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( searchNext() ), 197b423cd88SNicolas Bonnefon this, SLOT( searchNext() ) ); 198b423cd88SNicolas Bonnefon connect( searchable, SIGNAL( searchPrevious() ), 199b423cd88SNicolas Bonnefon this, SLOT( searchPrevious() ) ); 200d2b38ed8SNicolas Bonnefon 201d2b38ed8SNicolas Bonnefon registeredSearchables_.push_back( searchable ); 202b423cd88SNicolas Bonnefon } 203b423cd88SNicolas Bonnefon 204b423cd88SNicolas Bonnefon void QuickFindMux::unregisterAllSearchables() 205b423cd88SNicolas Bonnefon { 206d2b38ed8SNicolas Bonnefon for ( auto searchable: registeredSearchables_ ) 207d2b38ed8SNicolas Bonnefon disconnect( searchable, 0, this, 0 ); 208d2b38ed8SNicolas Bonnefon 209d2b38ed8SNicolas Bonnefon registeredSearchables_.clear(); 210b423cd88SNicolas Bonnefon } 211