keepassxc

Форк
0
/
SignalMultiplexer.cpp 
146 строк · 3.7 Кб
1
/*
2
 *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3
 *
4
 *  This program is free software: you can redistribute it and/or modify
5
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation, either version 2 or (at your option)
7
 *  version 3 of the License.
8
 *
9
 *  This program is distributed in the hope that it will be useful,
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  GNU General Public License for more details.
13
 *
14
 *  You should have received a copy of the GNU General Public License
15
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#include "SignalMultiplexer.h"
19

20
#include "core/Global.h"
21

22
SignalMultiplexer::SignalMultiplexer() = default;
23

24
SignalMultiplexer::~SignalMultiplexer()
25
{
26
    // disconnect all connections
27
    setCurrentObject(nullptr);
28
}
29

30
QObject* SignalMultiplexer::currentObject() const
31
{
32
    return m_currentObject;
33
}
34

35
void SignalMultiplexer::setCurrentObject(QObject* object)
36
{
37
    // remove all Connections from the list whose senders/receivers have been deleted
38
    QMutableListIterator<Connection> i = m_connections;
39
    while (i.hasNext()) {
40
        const Connection& con = i.next();
41

42
        if (!con.sender && !con.receiver) {
43
            i.remove();
44
        }
45
    }
46

47
    if (m_currentObject) {
48
        for (const Connection& con : asConst(m_connections)) {
49
            disconnect(con);
50
        }
51
    }
52

53
    m_currentObject = object;
54

55
    if (object) {
56
        for (const Connection& con : asConst(m_connections)) {
57
            connect(con);
58
        }
59
    }
60
}
61

62
void SignalMultiplexer::connect(QObject* sender, const char* signal, const char* slot)
63
{
64
    Q_ASSERT(sender);
65

66
    Connection con;
67
    con.slot = slot;
68
    con.sender = sender;
69
    con.signal = signal;
70
    m_connections << con;
71

72
    if (m_currentObject) {
73
        connect(con);
74
    }
75
}
76

77
void SignalMultiplexer::connect(const char* signal, QObject* receiver, const char* slot)
78
{
79
    Q_ASSERT(receiver);
80

81
    Connection con;
82
    con.receiver = receiver;
83
    con.signal = signal;
84
    con.slot = slot;
85
    m_connections << con;
86

87
    if (m_currentObject) {
88
        connect(con);
89
    }
90
}
91

92
void SignalMultiplexer::disconnect(QObject* sender, const char* signal, const char* slot)
93
{
94
    Q_ASSERT(sender);
95

96
    QMutableListIterator<Connection> i = m_connections;
97
    while (i.hasNext()) {
98
        const Connection& con = i.next();
99

100
        if (con.sender == sender && qstrcmp(con.signal, signal) == 0 && qstrcmp(con.slot, slot) == 0) {
101
            if (m_currentObject) {
102
                disconnect(con);
103
            }
104
            i.remove();
105
        }
106
    }
107
}
108

109
void SignalMultiplexer::disconnect(const char* signal, QObject* receiver, const char* slot)
110
{
111
    Q_ASSERT(receiver);
112

113
    QMutableListIterator<Connection> i = m_connections;
114
    while (i.hasNext()) {
115
        const Connection& con = i.next();
116

117
        if (con.receiver == receiver && qstrcmp(con.signal, signal) == 0 && qstrcmp(con.slot, slot) == 0) {
118
            if (m_currentObject) {
119
                disconnect(con);
120
            }
121
            i.remove();
122
        }
123
    }
124
}
125

126
void SignalMultiplexer::connect(const Connection& con)
127
{
128
    Q_ASSERT(con.sender || con.receiver);
129

130
    if (con.sender) {
131
        QObject::connect(con.sender, con.signal, m_currentObject, con.slot);
132
    } else {
133
        QObject::connect(m_currentObject, con.signal, con.receiver, con.slot);
134
    }
135
}
136

137
void SignalMultiplexer::disconnect(const Connection& con)
138
{
139
    Q_ASSERT(con.sender || con.receiver);
140

141
    if (con.sender) {
142
        QObject::disconnect(con.sender, con.signal, m_currentObject, con.slot);
143
    } else {
144
        QObject::disconnect(m_currentObject, con.signal, con.receiver, con.slot);
145
    }
146
}
147

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.