1 /* 2 * Copyright (C) 2013 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef SIGNALMUX_H 21 #define SIGNALMUX_H 22 23 // This class multiplexes Qt signals exchanged between one main 24 // window and several 'document' widgets. 25 // The main window register signals with the mux (instead of directly 26 // with the document) and then just notify the mux when the 'current 27 // document' changes 28 /* 29 +---------------------------------------+ 30 | | 31 | Main Window |--+ 32 | | | 33 +---------------------------------------+ | 34 + ^ | 35 v + | 36 +---------------------------------------+ | 37 | MUX |<-+ 38 +---------------------------------------+ 39 +----------^ + 40 | +----------+ 41 | v 42 +--+-----+ +--------+ +-------+ +-------+ 43 | | | | | | | | 44 | Doc 1 | | Doc 2 | | Doc 3 | | Doc 4 | 45 | | | | | | | | 46 +--------+ +--------+ +-------+ +-------+ 47 */ 48 // Largely inspired by http://doc.qt.digia.com/qq/qq08-action-multiplexer.html 49 50 #include <list> 51 #include <QPointer> 52 53 class QObject; 54 55 class MuxableDocumentInterface { 56 public: 57 // Send all signals refering to a state of the document to update 58 // the parent widget. 59 void sendAllStateSignals() 60 { doSendAllStateSignals(); } 61 62 protected: 63 virtual void doSendAllStateSignals() = 0; 64 }; 65 66 class SignalMux { 67 public: 68 SignalMux(); 69 70 // Connect an 'downstream' signal 71 void connect(QObject *sender, const char *signal, const char *slot); 72 void disconnect(QObject *sender, const char *signal, const char *slot); 73 // Connect an 'upstream' signal 74 void connect(const char *signal, QObject *receiver, const char *slot); 75 void disconnect(const char *signal, QObject *receiver, const char *slot); 76 77 // Change the current document 78 void setCurrentDocument( QObject* current_document ); 79 80 private: 81 struct Connection { 82 QPointer<QObject> source; 83 QPointer<QObject> sink; 84 const char* signal; 85 const char* slot; 86 }; 87 88 void connect( const Connection& connection); 89 void disconnect( const Connection& connection); 90 91 std::list<Connection> connectionList_; 92 93 QPointer<QObject> currentDocument_; 94 }; 95 96 #endif 97