OpenCoverage

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

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