xref: /glogg/src/signalmux.h (revision 313a820ff17bfd02bca88e04703027110d330191)
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 // Strongly 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 SignalMux {
56   public:
57     SignalMux();
58 
59     // Connect an 'downstream' signal
60     void connect(QObject *sender, const char *signal, const char *slot);
61     void disconnect(QObject *sender, const char *signal, const char *slot);
62     // Connect an 'upstream' signal
63     void connect(const char *signal, QObject *receiver, const char *slot);
64     void disconnect(const char *signal, QObject *receiver, const char *slot);
65 
66     // Change the current document
67     void setCurrentDocument( QObject* current_document );
68 
69   private:
70     struct Connection {
71         QPointer<QObject>   source;
72         QPointer<QObject>   sink;
73         const char*         signal;
74         const char*         slot;
75     };
76 
77     void connect( const Connection& connection);
78     void disconnect( const Connection& connection);
79 
80     std::list<Connection> connectionList_;
81 
82     QPointer<QObject> currentDocument_;
83 };
84 
85 #endif
86