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