qsignalmapper.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/kernel/qsignalmapper.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtCore module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34#include "qsignalmapper.h"-
35#include "qhash.h"-
36#include "qobject_p.h"-
37-
38QT_BEGIN_NAMESPACE-
39-
40class QSignalMapperPrivate : public QObjectPrivate-
41{-
42 Q_DECLARE_PUBLIC(QSignalMapper)-
43public:-
44 void _q_senderDestroyed() {-
45 Q_Q(QSignalMapper);-
46 q->removeMappings(q->sender());-
47 }
executed 5 times by 1 test: end of block
Executed by:
  • tst_QSignalMapper
5
48 QHash<QObject *, int> intHash;-
49 QHash<QObject *, QString> stringHash;-
50 QHash<QObject *, QWidget*> widgetHash;-
51 QHash<QObject *, QObject*> objectHash;-
52-
53};-
54-
55-
56/*!-
57 \class QSignalMapper-
58 \inmodule QtCore-
59 \brief The QSignalMapper class bundles signals from identifiable senders.-
60-
61 \ingroup objectmodel-
62-
63-
64 This class collects a set of parameterless signals, and re-emits-
65 them with integer, string or widget parameters corresponding to-
66 the object that sent the signal.-
67-
68 The class supports the mapping of particular strings or integers-
69 with particular objects using setMapping(). The objects' signals-
70 can then be connected to the map() slot which will emit the-
71 mapped() signal with the string or integer associated with the-
72 original signalling object. Mappings can be removed later using-
73 removeMappings().-
74-
75 Example: Suppose we want to create a custom widget that contains-
76 a group of buttons (like a tool palette). One approach is to-
77 connect each button's \c clicked() signal to its own custom slot;-
78 but in this example we want to connect all the buttons to a-
79 single slot and parameterize the slot by the button that was-
80 clicked.-
81-
82 Here's the definition of a simple custom widget that has a single-
83 signal, \c clicked(), which is emitted with the text of the button-
84 that was clicked:-
85-
86 \snippet qsignalmapper/buttonwidget.h 0-
87 \snippet qsignalmapper/buttonwidget.h 1-
88-
89 The only function that we need to implement is the constructor:-
90-
91 \snippet qsignalmapper/buttonwidget.cpp 0-
92 \snippet qsignalmapper/buttonwidget.cpp 1-
93 \snippet qsignalmapper/buttonwidget.cpp 2-
94-
95 A list of texts is passed to the constructor. A signal mapper is-
96 constructed and for each text in the list a QPushButton is-
97 created. We connect each button's \c clicked() signal to the-
98 signal mapper's map() slot, and create a mapping in the signal-
99 mapper from each button to the button's text. Finally we connect-
100 the signal mapper's mapped() signal to the custom widget's \c-
101 clicked() signal. When the user clicks a button, the custom-
102 widget will emit a single \c clicked() signal whose argument is-
103 the text of the button the user clicked.-
104-
105 \sa QObject, QButtonGroup, QActionGroup-
106*/-
107-
108/*!-
109 Constructs a QSignalMapper with parent \a parent.-
110*/-
111QSignalMapper::QSignalMapper(QObject* parent)-
112 : QObject(*new QSignalMapperPrivate, parent)-
113{-
114}
executed 1 time by 1 test: end of block
Executed by:
  • tst_QSignalMapper
1
115-
116/*!-
117 Destroys the QSignalMapper.-
118*/-
119QSignalMapper::~QSignalMapper()-
120{-
121}-
122-
123/*!-
124 Adds a mapping so that when map() is signalled from the given \a-
125 sender, the signal mapped(\a id) is emitted.-
126-
127 There may be at most one integer ID for each sender.-
128-
129 \sa mapping()-
130*/-
131void QSignalMapper::setMapping(QObject *sender, int id)-
132{-
133 Q_D(QSignalMapper);-
134 d->intHash.insert(sender, id);-
135 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));-
136}
executed 3 times by 1 test: end of block
Executed by:
  • tst_QSignalMapper
3
137-
138/*!-
139 Adds a mapping so that when map() is signalled from the \a sender,-
140 the signal mapped(\a text ) is emitted.-
141-
142 There may be at most one text for each sender.-
143*/-
144void QSignalMapper::setMapping(QObject *sender, const QString &text)-
145{-
146 Q_D(QSignalMapper);-
147 d->stringHash.insert(sender, text);-
148 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));-
149}
executed 2 times by 1 test: end of block
Executed by:
  • tst_QSignalMapper
2
150-
151/*!-
152 Adds a mapping so that when map() is signalled from the \a sender,-
153 the signal mapped(\a widget ) is emitted.-
154-
155 There may be at most one widget for each sender.-
156*/-
157void QSignalMapper::setMapping(QObject *sender, QWidget *widget)-
158{-
159 Q_D(QSignalMapper);-
160 d->widgetHash.insert(sender, widget);-
161 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));-
162}
never executed: end of block
0
163-
164/*!-
165 Adds a mapping so that when map() is signalled from the \a sender,-
166 the signal mapped(\a object ) is emitted.-
167-
168 There may be at most one object for each sender.-
169*/-
170void QSignalMapper::setMapping(QObject *sender, QObject *object)-
171{-
172 Q_D(QSignalMapper);-
173 d->objectHash.insert(sender, object);-
174 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));-
175}
never executed: end of block
0
176-
177/*!-
178 Returns the sender QObject that is associated with the \a id.-
179-
180 \sa setMapping()-
181*/-
182QObject *QSignalMapper::mapping(int id) const-
183{-
184 Q_D(const QSignalMapper);-
185 return d->intHash.key(id);
never executed: return d->intHash.key(id);
0
186}-
187-
188/*!-
189 \overload mapping()-
190*/-
191QObject *QSignalMapper::mapping(const QString &id) const-
192{-
193 Q_D(const QSignalMapper);-
194 return d->stringHash.key(id);
never executed: return d->stringHash.key(id);
0
195}-
196-
197/*!-
198 \overload mapping()-
199-
200 Returns the sender QObject that is associated with the \a widget.-
201*/-
202QObject *QSignalMapper::mapping(QWidget *widget) const-
203{-
204 Q_D(const QSignalMapper);-
205 return d->widgetHash.key(widget);
never executed: return d->widgetHash.key(widget);
0
206}-
207-
208/*!-
209 \overload mapping()-
210-
211 Returns the sender QObject that is associated with the \a object.-
212*/-
213QObject *QSignalMapper::mapping(QObject *object) const-
214{-
215 Q_D(const QSignalMapper);-
216 return d->objectHash.key(object);
never executed: return d->objectHash.key(object);
0
217}-
218-
219/*!-
220 Removes all mappings for \a sender.-
221-
222 This is done automatically when mapped objects are destroyed.-
223-
224 \note This does not disconnect any signals. If \a sender is not destroyed-
225 then this will need to be done explicitly if required.-
226*/-
227void QSignalMapper::removeMappings(QObject *sender)-
228{-
229 Q_D(QSignalMapper);-
230-
231 d->intHash.remove(sender);-
232 d->stringHash.remove(sender);-
233 d->widgetHash.remove(sender);-
234 d->objectHash.remove(sender);-
235}
executed 5 times by 1 test: end of block
Executed by:
  • tst_QSignalMapper
5
236-
237/*!-
238 This slot emits signals based on which object sends signals to it.-
239*/-
240void QSignalMapper::map() { map(sender()); }
executed 3 times by 1 test: end of block
Executed by:
  • tst_QSignalMapper
3
241-
242/*!-
243 This slot emits signals based on the \a sender object.-
244*/-
245void QSignalMapper::map(QObject *sender)-
246{-
247 Q_D(QSignalMapper);-
248 if (d->intHash.contains(sender))
d->intHash.contains(sender)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSignalMapper
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QSignalMapper
1-2
249 emit mapped(d->intHash.value(sender));
executed 2 times by 1 test: mapped(d->intHash.value(sender));
Executed by:
  • tst_QSignalMapper
2
250 if (d->stringHash.contains(sender))
d->stringHash.contains(sender)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • tst_QSignalMapper
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QSignalMapper
1-2
251 emit mapped(d->stringHash.value(sender));
executed 2 times by 1 test: mapped(d->stringHash.value(sender));
Executed by:
  • tst_QSignalMapper
2
252 if (d->widgetHash.contains(sender))
d->widgetHash.contains(sender)Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tst_QSignalMapper
0-3
253 emit mapped(d->widgetHash.value(sender));
never executed: mapped(d->widgetHash.value(sender));
0
254 if (d->objectHash.contains(sender))
d->objectHash.contains(sender)Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tst_QSignalMapper
0-3
255 emit mapped(d->objectHash.value(sender));
never executed: mapped(d->objectHash.value(sender));
0
256}
executed 3 times by 1 test: end of block
Executed by:
  • tst_QSignalMapper
3
257-
258-
259/*!-
260 \fn void QSignalMapper::mapped(int i)-
261-
262 This signal is emitted when map() is signalled from an object that-
263 has an integer mapping set. The object's mapped integer is passed-
264 in \a i.-
265-
266 \sa setMapping()-
267*/-
268-
269/*!-
270 \fn void QSignalMapper::mapped(const QString &text)-
271-
272 This signal is emitted when map() is signalled from an object that-
273 has a string mapping set. The object's mapped string is passed in-
274 \a text.-
275-
276 \sa setMapping()-
277*/-
278-
279/*!-
280 \fn void QSignalMapper::mapped(QWidget *widget)-
281-
282 This signal is emitted when map() is signalled from an object that-
283 has a widget mapping set. The object's mapped widget is passed in-
284 \a widget.-
285-
286 \sa setMapping()-
287*/-
288-
289/*!-
290 \fn void QSignalMapper::mapped(QObject *object)-
291-
292 This signal is emitted when map() is signalled from an object that-
293 has an object mapping set. The object provided by the map is passed in-
294 \a object.-
295-
296 \sa setMapping()-
297*/-
298-
299QT_END_NAMESPACE-
300-
301#include "moc_qsignalmapper.cpp"-
302-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9