xref: /glogg/src/signalmux.h (revision 9cacd6a97a0b10112ec2ce02191415958748faec)
1313a820fSNicolas Bonnefon /*
2313a820fSNicolas Bonnefon  * Copyright (C) 2013 Nicolas Bonnefon and other contributors
3313a820fSNicolas Bonnefon  *
4313a820fSNicolas Bonnefon  * This file is part of glogg.
5313a820fSNicolas Bonnefon  *
6313a820fSNicolas Bonnefon  * glogg is free software: you can redistribute it and/or modify
7313a820fSNicolas Bonnefon  * it under the terms of the GNU General Public License as published by
8313a820fSNicolas Bonnefon  * the Free Software Foundation, either version 3 of the License, or
9313a820fSNicolas Bonnefon  * (at your option) any later version.
10313a820fSNicolas Bonnefon  *
11313a820fSNicolas Bonnefon  * glogg is distributed in the hope that it will be useful,
12313a820fSNicolas Bonnefon  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13313a820fSNicolas Bonnefon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14313a820fSNicolas Bonnefon  * GNU General Public License for more details.
15313a820fSNicolas Bonnefon  *
16313a820fSNicolas Bonnefon  * You should have received a copy of the GNU General Public License
17313a820fSNicolas Bonnefon  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18313a820fSNicolas Bonnefon  */
19313a820fSNicolas Bonnefon 
20313a820fSNicolas Bonnefon #ifndef SIGNALMUX_H
21313a820fSNicolas Bonnefon #define SIGNALMUX_H
22313a820fSNicolas Bonnefon 
23313a820fSNicolas Bonnefon // This class multiplexes Qt signals exchanged between one main
24313a820fSNicolas Bonnefon // window and several 'document' widgets.
25313a820fSNicolas Bonnefon // The main window register signals with the mux (instead of directly
26313a820fSNicolas Bonnefon // with the document) and then just notify the mux when the 'current
27313a820fSNicolas Bonnefon // document' changes
28313a820fSNicolas Bonnefon /*
29313a820fSNicolas Bonnefon    +---------------------------------------+
30313a820fSNicolas Bonnefon    |                                       |
31313a820fSNicolas Bonnefon    |              Main Window              |--+
32313a820fSNicolas Bonnefon    |                                       |  |
33313a820fSNicolas Bonnefon    +---------------------------------------+  |
34313a820fSNicolas Bonnefon                      +  ^                     |
35313a820fSNicolas Bonnefon                      v  +                     |
36313a820fSNicolas Bonnefon    +---------------------------------------+  |
37313a820fSNicolas Bonnefon    |                  MUX                  |<-+
38313a820fSNicolas Bonnefon    +---------------------------------------+
39313a820fSNicolas Bonnefon       +----------^ +
40313a820fSNicolas Bonnefon       | +----------+
41313a820fSNicolas Bonnefon       | v
42313a820fSNicolas Bonnefon    +--+-----+ +--------+ +-------+ +-------+
43313a820fSNicolas Bonnefon    |        | |        | |       | |       |
44313a820fSNicolas Bonnefon    | Doc 1  | | Doc 2  | | Doc 3 | | Doc 4 |
45313a820fSNicolas Bonnefon    |        | |        | |       | |       |
46313a820fSNicolas Bonnefon    +--------+ +--------+ +-------+ +-------+
47313a820fSNicolas Bonnefon */
48*9cacd6a9SNicolas Bonnefon // Largely inspired by http://doc.qt.digia.com/qq/qq08-action-multiplexer.html
49313a820fSNicolas Bonnefon 
50313a820fSNicolas Bonnefon #include <list>
51313a820fSNicolas Bonnefon #include <QPointer>
52313a820fSNicolas Bonnefon 
53313a820fSNicolas Bonnefon class QObject;
54313a820fSNicolas Bonnefon 
55*9cacd6a9SNicolas Bonnefon class MuxableDocumentInterface {
56*9cacd6a9SNicolas Bonnefon   public:
57*9cacd6a9SNicolas Bonnefon     // Send all signals refering to a state of the document to update
58*9cacd6a9SNicolas Bonnefon     // the parent widget.
sendAllStateSignals()59*9cacd6a9SNicolas Bonnefon     void sendAllStateSignals()
60*9cacd6a9SNicolas Bonnefon     { doSendAllStateSignals(); }
61*9cacd6a9SNicolas Bonnefon 
62*9cacd6a9SNicolas Bonnefon   protected:
63*9cacd6a9SNicolas Bonnefon     virtual void doSendAllStateSignals() = 0;
64*9cacd6a9SNicolas Bonnefon };
65*9cacd6a9SNicolas Bonnefon 
66313a820fSNicolas Bonnefon class SignalMux {
67313a820fSNicolas Bonnefon   public:
68313a820fSNicolas Bonnefon     SignalMux();
69313a820fSNicolas Bonnefon 
70313a820fSNicolas Bonnefon     // Connect an 'downstream' signal
71313a820fSNicolas Bonnefon     void connect(QObject *sender, const char *signal, const char *slot);
72313a820fSNicolas Bonnefon     void disconnect(QObject *sender, const char *signal, const char *slot);
73313a820fSNicolas Bonnefon     // Connect an 'upstream' signal
74313a820fSNicolas Bonnefon     void connect(const char *signal, QObject *receiver, const char *slot);
75313a820fSNicolas Bonnefon     void disconnect(const char *signal, QObject *receiver, const char *slot);
76313a820fSNicolas Bonnefon 
77313a820fSNicolas Bonnefon     // Change the current document
78313a820fSNicolas Bonnefon     void setCurrentDocument( QObject* current_document );
79313a820fSNicolas Bonnefon 
80313a820fSNicolas Bonnefon   private:
81313a820fSNicolas Bonnefon     struct Connection {
82313a820fSNicolas Bonnefon         QPointer<QObject>   source;
83313a820fSNicolas Bonnefon         QPointer<QObject>   sink;
84313a820fSNicolas Bonnefon         const char*         signal;
85313a820fSNicolas Bonnefon         const char*         slot;
86313a820fSNicolas Bonnefon     };
87313a820fSNicolas Bonnefon 
88313a820fSNicolas Bonnefon     void connect( const Connection& connection);
89313a820fSNicolas Bonnefon     void disconnect( const Connection& connection);
90313a820fSNicolas Bonnefon 
91313a820fSNicolas Bonnefon     std::list<Connection> connectionList_;
92313a820fSNicolas Bonnefon 
93313a820fSNicolas Bonnefon     QPointer<QObject> currentDocument_;
94313a820fSNicolas Bonnefon };
95313a820fSNicolas Bonnefon 
96313a820fSNicolas Bonnefon #endif
97