Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / core / sig_mux.cpp
blob72f17ec23e22acff13e11cdfdbca3b79aa297bba
1 /*
2 SuperCollider Qt IDE
3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program 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 2 of the License, or
9 (at your option) any later version.
11 This program 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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "sig_mux.hpp"
23 namespace ScIDE {
25 SignalMultiplexer::SignalMultiplexer(QObject *parent) :
26 QObject(parent)
29 void SignalMultiplexer::connect(QObject *sender, const char *signal, const char *slot)
31 Connection conn;
32 conn.sender = sender;
33 conn.signal = signal;
34 conn.slot = slot;
36 mConnections << conn;
37 connect(conn);
40 bool SignalMultiplexer::disconnect(QObject *sender, const char *signal, const char *slot)
42 QList<Connection>::Iterator it;
43 for (it = mConnections.begin(); it != mConnections.end(); ++it) {
44 Connection conn = *it;
45 if ((QObject*)conn.sender == sender &&
46 qstrcmp(conn.signal, signal) == 0 && qstrcmp(conn.slot, slot) == 0) {
47 disconnect(conn);
48 mConnections.erase(it);
49 return true;
52 return false;
55 void SignalMultiplexer::connect(const char *signal, QObject *receiver, const char *slot)
57 Connection conn;
58 conn.receiver = receiver;
59 conn.signal = signal;
60 conn.slot = slot;
62 mConnections << conn;
63 connect(conn);
66 bool SignalMultiplexer::disconnect(const char *signal, QObject *receiver, const char *slot)
68 QList<Connection>::Iterator it;
69 for (it = mConnections.begin(); it != mConnections.end(); ++it) {
70 Connection conn = *it;
71 if ((QObject*)conn.receiver == receiver &&
72 qstrcmp(conn.signal, signal) == 0 && qstrcmp(conn.slot, slot) == 0) {
73 disconnect(conn);
74 mConnections.erase(it);
75 return true;
78 return false;
81 void SignalMultiplexer::connect(const Connection &conn)
83 if (!mObject)
84 return;
85 if (!conn.sender && !conn.receiver)
86 return;
88 if (conn.sender)
89 QObject::connect((QObject*)conn.sender, conn.signal, (QObject*)mObject, conn.slot);
90 else
91 QObject::connect((QObject*)mObject, conn.signal, (QObject*)conn.receiver, conn.slot);
94 void SignalMultiplexer::disconnect(const Connection &conn)
96 if (!mObject)
97 return;
98 if (!conn.sender && !conn.receiver)
99 return;
101 if (conn.sender)
102 QObject::disconnect((QObject*)conn.sender, conn.signal, (QObject*)mObject, conn.slot);
103 else
104 QObject::disconnect((QObject*)mObject, conn.signal, (QObject*)conn.receiver, conn.slot);
108 void SignalMultiplexer::setCurrentObject(QObject *newObject)
110 if (newObject == mObject)
111 return;
113 QList<Connection>::ConstIterator it;
114 for (it = mConnections.begin(); it != mConnections.end(); ++it)
115 disconnect(*it);
116 mObject = newObject;
117 for (it = mConnections.begin(); it != mConnections.end(); ++it)
118 connect(*it);
121 } // namespace ScIDE