1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qsignalmapper.h"
30
#ifndef QT_NO_SIGNALMAPPER
32
#include "qobject_p.h"
34
class QSignalMapperPrivate : public QObjectPrivate
36
Q_DECLARE_PUBLIC(QSignalMapper)
38
void senderDestroyed() {
40
q->removeMappings(q->sender());
42
QHash<QObject *, int> intHash;
43
QHash<QObject *, QString> stringHash;
44
QHash<QObject *, QWidget*> widgetHash;
51
\brief The QSignalMapper class bundles signals from identifiable senders.
56
This class collects a set of parameterless signals, and re-emits
57
them with integer, string or widget parameters corresponding to
58
the object that sent the signal.
60
The class supports the mapping of particular strings or integers
61
with particular objects using setMapping(). The objects' signals
62
can then be connected to the map() slot which will emit the
63
mapped() signal with the string or integer associated with the
64
original signalling object. Mappings can be removed later using
67
Example: Suppose we want to create a custom widget that contains
68
a group of buttons (like a tool palette). One approach is to
69
connect each button's \c clicked() signal to its own custom slot;
70
but in this example we want to connect all the buttons to a
71
single slot and parameterize the slot by the button that was
74
Here's the definition of a simple custom widget that has a single
75
signal, \c clicked(), which is emitted with the text of the button
78
\quotefromfile snippets/qsignalmapper/buttonwidget.h
80
\printuntil QSignalMapper
83
The only function that we need to implement is the constructor:
85
\quotefromfile snippets/qsignalmapper/buttonwidget.cpp
91
A list of texts is passed to the constructor. A signal mapper is
92
constructed and for each text in the list a QPushButton is
93
created. We connect each button's \c clicked() signal to the
94
signal mapper's map() slot, and create a mapping in the signal
95
mapper from each button to the button's text. Finally we connect
96
the signal mapper's mapped() signal to the custom widget's \c
97
clicked() signal. When the user clicks a button, the custom
98
widget will emit a single \c clicked() signal whose argument is
99
the text of the button the user clicked.
101
\sa QObject, QButtonGroup, QActionGroup
105
Constructs a QSignalMapper with parent \a parent.
107
QSignalMapper::QSignalMapper(QObject* parent)
108
: QObject(*new QSignalMapperPrivate, parent)
117
QSignalMapper::QSignalMapper(QObject *parent, const char *name)
118
: QObject(*new QSignalMapperPrivate, parent)
120
setObjectName(QString::fromAscii(name));
125
Destroys the QSignalMapper.
127
QSignalMapper::~QSignalMapper()
132
Adds a mapping so that when map() is signalled from the given \a
133
sender, the signal mapped(\a id) is emitted.
135
There may be at most one integer ID for each object.
139
void QSignalMapper::setMapping(QObject *sender, int id)
142
d->intHash.insert(sender, id);
143
connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
149
Adds a mapping so that when map() is signalled from the given \a
150
sender, the signal mapped(\a text ) is emitted.
152
There may be at most one text for each object.
154
void QSignalMapper::setMapping(QObject *sender, const QString &text)
157
d->stringHash.insert(sender, text);
158
connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
164
Adds a mapping so that when map() is signalled from the given \a
165
sender, the signal mapped(\a widget ) is emitted.
167
There may be at most one widget for each object.
169
void QSignalMapper::setMapping(QObject *sender, QWidget *widget)
172
d->widgetHash.insert(sender, widget);
173
connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
178
Returns the sender QObject that is associated with the given \a
183
QObject *QSignalMapper::mapping(int id) const
185
Q_D(const QSignalMapper);
186
return d->intHash.key(id);
192
QObject *QSignalMapper::mapping(const QString &id) const
194
Q_D(const QSignalMapper);
195
return d->stringHash.key(id);
201
Returns the sender QObject that is associated with the given \a
204
QObject *QSignalMapper::mapping(QWidget *widget) const
206
Q_D(const QSignalMapper);
207
return d->widgetHash.key(widget);
211
Removes all mappings for \a sender.
213
This is done automatically when mapped objects are destroyed.
215
void QSignalMapper::removeMappings(QObject *sender)
219
d->intHash.remove(sender);
220
d->stringHash.remove(sender);
221
d->widgetHash.remove(sender);
225
This slot emits signals based on which object sends signals to it.
227
void QSignalMapper::map() { map(sender()); }
230
This slot emits signals based on the \a sender object.
232
void QSignalMapper::map(QObject *sender)
235
if (d->intHash.contains(sender))
236
emit mapped(d->intHash.value(sender));
237
if (d->stringHash.contains(sender))
238
emit mapped(d->stringHash.value(sender));
239
if (d->widgetHash.contains(sender))
240
emit mapped(d->widgetHash.value(sender));
245
\fn void QSignalMapper::mapped(int i)
247
This signal is emitted when map() is signalled from an object that
248
has an integer mapping set. The object's mapped integer is passed
255
\fn void QSignalMapper::mapped(const QString &text)
258
This signal is emitted when map() is signalled from an object that
259
has a string mapping set. The object's mapped string is passed in
266
\fn void QSignalMapper::mapped(QWidget *widget)
269
This signal is emitted when map() is signalled from an object that
270
has a widget mapping set. The object's mapped widget is passed in
276
#include "moc_qsignalmapper.cpp"
277
#endif // QT_NO_SIGNALMAPPER