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 #ifndef QUICKFINDMUX_H 21bb02e0acSNicolas Bonnefon #define QUICKFINDMUX_H 22bb02e0acSNicolas Bonnefon 23b423cd88SNicolas Bonnefon #include <memory> 24b423cd88SNicolas Bonnefon #include <vector> 25b423cd88SNicolas 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: 47b423cd88SNicolas Bonnefon // Return the searchable widget to use. getActiveSearchable()48b423cd88SNicolas Bonnefon SearchableWidgetInterface* getActiveSearchable() const 49b423cd88SNicolas Bonnefon { return doGetActiveSearchable(); } 50b423cd88SNicolas Bonnefon // Return the list of all possible searchables, this 51b423cd88SNicolas Bonnefon // is done on registration in order to establish 52b423cd88SNicolas Bonnefon // listeners on all searchables. getAllSearchables()53b423cd88SNicolas Bonnefon std::vector<QObject*> getAllSearchables() const 54b423cd88SNicolas Bonnefon { return doGetAllSearchables(); } 55b423cd88SNicolas Bonnefon protected: 56b423cd88SNicolas Bonnefon virtual SearchableWidgetInterface* doGetActiveSearchable() const = 0; 57b423cd88SNicolas 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 77b423cd88SNicolas Bonnefon // Construct the multiplexer, taking a reference to the pattern 78b423cd88SNicolas Bonnefon QuickFindMux( std::shared_ptr<QuickFindPattern> pattern ); 79bb02e0acSNicolas Bonnefon 80b423cd88SNicolas Bonnefon // Register a new selector, which will be called and asked 81b423cd88SNicolas Bonnefon // who the search have to be forwarded to. 82b423cd88SNicolas Bonnefon // The selector is called immediately when registering to get the list of 83b423cd88SNicolas Bonnefon // searchables. 84b423cd88SNicolas Bonnefon // The previous selector and its associated views are automatically 85b423cd88SNicolas Bonnefon // deregistered. 86*a1202e0cSNicolas Bonnefon // A null selector is accepted, in this case QFM functionalities are 87*a1202e0cSNicolas Bonnefon // disabled until a valid selector is registered. 88b423cd88SNicolas Bonnefon void registerSelector( const QuickFindMuxSelectorInterface* selector ); 89bb02e0acSNicolas Bonnefon 90bb02e0acSNicolas Bonnefon // Set the direction that will be used by the search when searching 91bb02e0acSNicolas Bonnefon // forward. 92bb02e0acSNicolas Bonnefon void setDirection( QFDirection direction ); 93bb02e0acSNicolas Bonnefon 94bb02e0acSNicolas Bonnefon signals: 95bb02e0acSNicolas Bonnefon void patternChanged( const QString& ); 96bb02e0acSNicolas Bonnefon void notify( const QFNotification& ); 97bb02e0acSNicolas Bonnefon void clearNotification(); 98bb02e0acSNicolas Bonnefon 99bb02e0acSNicolas Bonnefon public slots: 100bb02e0acSNicolas Bonnefon // Signal the current pattern must be altered (will start an incremental 101bb02e0acSNicolas Bonnefon // search if the options are configured in such a way). 102bb02e0acSNicolas Bonnefon void setNewPattern( const QString& new_pattern, bool ignore_case ); 103bb02e0acSNicolas Bonnefon 104bb02e0acSNicolas Bonnefon // Signal the current pattern must be altered and is confirmed 105bb02e0acSNicolas Bonnefon // (will stop an incremental search if needed) 106bb02e0acSNicolas Bonnefon void confirmPattern( const QString& new_pattern, bool ignore_case ); 107bb02e0acSNicolas Bonnefon 108bb02e0acSNicolas Bonnefon // Signal the user cancelled the search 109bb02e0acSNicolas Bonnefon // (used for incremental only) 110bb02e0acSNicolas Bonnefon void cancelSearch(); 111bb02e0acSNicolas Bonnefon 112bb02e0acSNicolas Bonnefon // Starts a search in the specified direction 113bb02e0acSNicolas Bonnefon void searchNext(); 114bb02e0acSNicolas Bonnefon void searchPrevious(); 115bb02e0acSNicolas Bonnefon 116bb02e0acSNicolas Bonnefon // Idem but ignore the direction and always search in the 117bb02e0acSNicolas Bonnefon // specified direction 118bb02e0acSNicolas Bonnefon void searchForward(); 119bb02e0acSNicolas Bonnefon void searchBackward(); 120bb02e0acSNicolas Bonnefon 121bb02e0acSNicolas Bonnefon private slots: 122bb02e0acSNicolas Bonnefon void changeQuickFind( const QString& new_pattern, 123bb02e0acSNicolas Bonnefon QuickFindMux::QFDirection new_direction ); 124bb02e0acSNicolas Bonnefon void notifyPatternChanged(); 125bb02e0acSNicolas Bonnefon 126bb02e0acSNicolas Bonnefon private: 127bb02e0acSNicolas Bonnefon const QuickFindMuxSelectorInterface* selector_; 128bb02e0acSNicolas Bonnefon 129bb02e0acSNicolas Bonnefon // The (application wide) quick find pattern 130b423cd88SNicolas Bonnefon std::shared_ptr<QuickFindPattern> pattern_; 131bb02e0acSNicolas Bonnefon 132bb02e0acSNicolas Bonnefon QFDirection currentDirection_; 133bb02e0acSNicolas Bonnefon 134d2b38ed8SNicolas Bonnefon std::vector<QObject*> registeredSearchables_; 135d2b38ed8SNicolas Bonnefon 136bb02e0acSNicolas Bonnefon SearchableWidgetInterface* getSearchableWidget() const; 137b423cd88SNicolas Bonnefon void registerSearchable( QObject* searchable ); 138b423cd88SNicolas Bonnefon void unregisterAllSearchables(); 139bb02e0acSNicolas Bonnefon }; 140bb02e0acSNicolas Bonnefon 141bb02e0acSNicolas Bonnefon #endif 142