1bb02e0acSNicolas Bonnefon /* 2*b423cd88SNicolas 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 #ifndef QUICKFINDMUX_H 21bb02e0acSNicolas Bonnefon #define QUICKFINDMUX_H 22bb02e0acSNicolas Bonnefon 23*b423cd88SNicolas Bonnefon #include <memory> 24*b423cd88SNicolas Bonnefon #include <vector> 25*b423cd88SNicolas Bonnefon 26bb02e0acSNicolas Bonnefon #include <QObject> 27bb02e0acSNicolas Bonnefon #include <QString> 28bb02e0acSNicolas Bonnefon 29bb02e0acSNicolas Bonnefon #include "quickfindpattern.h" 30bb02e0acSNicolas Bonnefon 31bb02e0acSNicolas Bonnefon // Interface representing a widget searchable in both direction. 32bb02e0acSNicolas Bonnefon class SearchableWidgetInterface { 33bb02e0acSNicolas Bonnefon public: 34bb02e0acSNicolas Bonnefon virtual void searchForward() = 0; 35bb02e0acSNicolas Bonnefon virtual void searchBackward() = 0; 36bb02e0acSNicolas Bonnefon 37bb02e0acSNicolas Bonnefon virtual void incrementallySearchForward() = 0; 38bb02e0acSNicolas Bonnefon virtual void incrementallySearchBackward() = 0; 39bb02e0acSNicolas Bonnefon virtual void incrementalSearchStop() = 0; 40bb02e0acSNicolas Bonnefon virtual void incrementalSearchAbort() = 0; 41bb02e0acSNicolas Bonnefon }; 42bb02e0acSNicolas Bonnefon 43bb02e0acSNicolas Bonnefon // Interface representing the selector. It will be called and asked 44bb02e0acSNicolas Bonnefon // who the search have to be forwarded to. 45bb02e0acSNicolas Bonnefon class QuickFindMuxSelectorInterface { 46bb02e0acSNicolas Bonnefon public: 47*b423cd88SNicolas Bonnefon // Return the searchable widget to use. 48*b423cd88SNicolas Bonnefon SearchableWidgetInterface* getActiveSearchable() const 49*b423cd88SNicolas Bonnefon { return doGetActiveSearchable(); } 50*b423cd88SNicolas Bonnefon // Return the list of all possible searchables, this 51*b423cd88SNicolas Bonnefon // is done on registration in order to establish 52*b423cd88SNicolas Bonnefon // listeners on all searchables. 53*b423cd88SNicolas Bonnefon std::vector<QObject*> getAllSearchables() const 54*b423cd88SNicolas Bonnefon { return doGetAllSearchables(); } 55*b423cd88SNicolas Bonnefon protected: 56*b423cd88SNicolas Bonnefon virtual SearchableWidgetInterface* doGetActiveSearchable() const = 0; 57*b423cd88SNicolas Bonnefon virtual std::vector<QObject*> doGetAllSearchables() const = 0; 58bb02e0acSNicolas Bonnefon }; 59bb02e0acSNicolas Bonnefon 60bb02e0acSNicolas Bonnefon class QFNotification; 61bb02e0acSNicolas Bonnefon 62bb02e0acSNicolas Bonnefon // Represents a multiplexer (unique application wise) dispatching the 63bb02e0acSNicolas Bonnefon // Quick Find search from the UI to the relevant view. 64bb02e0acSNicolas Bonnefon // It is also its responsability to determine if an incremental search 65bb02e0acSNicolas Bonnefon // must be performed and to react accordingly. 66bb02e0acSNicolas Bonnefon class QuickFindMux : public QObject 67bb02e0acSNicolas Bonnefon { 68bb02e0acSNicolas Bonnefon Q_OBJECT 69bb02e0acSNicolas Bonnefon 70bb02e0acSNicolas Bonnefon public: 71bb02e0acSNicolas Bonnefon 72bb02e0acSNicolas Bonnefon enum QFDirection { 73bb02e0acSNicolas Bonnefon Forward, 74bb02e0acSNicolas Bonnefon Backward, 75bb02e0acSNicolas Bonnefon }; 76bb02e0acSNicolas Bonnefon 77*b423cd88SNicolas Bonnefon // Construct the multiplexer, taking a reference to the pattern 78*b423cd88SNicolas Bonnefon QuickFindMux( std::shared_ptr<QuickFindPattern> pattern ); 79bb02e0acSNicolas Bonnefon 80*b423cd88SNicolas Bonnefon // Register a new selector, which will be called and asked 81*b423cd88SNicolas Bonnefon // who the search have to be forwarded to. 82*b423cd88SNicolas Bonnefon // The selector is called immediately when registering to get the list of 83*b423cd88SNicolas Bonnefon // searchables. 84*b423cd88SNicolas Bonnefon // The previous selector and its associated views are automatically 85*b423cd88SNicolas Bonnefon // deregistered. 86*b423cd88SNicolas Bonnefon void registerSelector( const QuickFindMuxSelectorInterface* selector ); 87bb02e0acSNicolas Bonnefon 88bb02e0acSNicolas Bonnefon // Set the direction that will be used by the search when searching 89bb02e0acSNicolas Bonnefon // forward. 90bb02e0acSNicolas Bonnefon void setDirection( QFDirection direction ); 91bb02e0acSNicolas Bonnefon 92bb02e0acSNicolas Bonnefon signals: 93bb02e0acSNicolas Bonnefon void patternChanged( const QString& ); 94bb02e0acSNicolas Bonnefon void notify( const QFNotification& ); 95bb02e0acSNicolas Bonnefon void clearNotification(); 96bb02e0acSNicolas Bonnefon 97bb02e0acSNicolas Bonnefon public slots: 98bb02e0acSNicolas Bonnefon // Signal the current pattern must be altered (will start an incremental 99bb02e0acSNicolas Bonnefon // search if the options are configured in such a way). 100bb02e0acSNicolas Bonnefon void setNewPattern( const QString& new_pattern, bool ignore_case ); 101bb02e0acSNicolas Bonnefon 102bb02e0acSNicolas Bonnefon // Signal the current pattern must be altered and is confirmed 103bb02e0acSNicolas Bonnefon // (will stop an incremental search if needed) 104bb02e0acSNicolas Bonnefon void confirmPattern( const QString& new_pattern, bool ignore_case ); 105bb02e0acSNicolas Bonnefon 106bb02e0acSNicolas Bonnefon // Signal the user cancelled the search 107bb02e0acSNicolas Bonnefon // (used for incremental only) 108bb02e0acSNicolas Bonnefon void cancelSearch(); 109bb02e0acSNicolas Bonnefon 110bb02e0acSNicolas Bonnefon // Starts a search in the specified direction 111bb02e0acSNicolas Bonnefon void searchNext(); 112bb02e0acSNicolas Bonnefon void searchPrevious(); 113bb02e0acSNicolas Bonnefon 114bb02e0acSNicolas Bonnefon // Idem but ignore the direction and always search in the 115bb02e0acSNicolas Bonnefon // specified direction 116bb02e0acSNicolas Bonnefon void searchForward(); 117bb02e0acSNicolas Bonnefon void searchBackward(); 118bb02e0acSNicolas Bonnefon 119bb02e0acSNicolas Bonnefon private slots: 120bb02e0acSNicolas Bonnefon void changeQuickFind( const QString& new_pattern, 121bb02e0acSNicolas Bonnefon QuickFindMux::QFDirection new_direction ); 122bb02e0acSNicolas Bonnefon void notifyPatternChanged(); 123bb02e0acSNicolas Bonnefon 124bb02e0acSNicolas Bonnefon private: 125bb02e0acSNicolas Bonnefon const QuickFindMuxSelectorInterface* selector_; 126bb02e0acSNicolas Bonnefon 127bb02e0acSNicolas Bonnefon // The (application wide) quick find pattern 128*b423cd88SNicolas Bonnefon std::shared_ptr<QuickFindPattern> pattern_; 129bb02e0acSNicolas Bonnefon 130bb02e0acSNicolas Bonnefon QFDirection currentDirection_; 131bb02e0acSNicolas Bonnefon 132bb02e0acSNicolas Bonnefon SearchableWidgetInterface* getSearchableWidget() const; 133*b423cd88SNicolas Bonnefon void registerSearchable( QObject* searchable ); 134*b423cd88SNicolas Bonnefon void unregisterAllSearchables(); 135bb02e0acSNicolas Bonnefon }; 136bb02e0acSNicolas Bonnefon 137bb02e0acSNicolas Bonnefon #endif 138