~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qsignalmapper.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qsignalmapper.h"
 
30
#ifndef QT_NO_SIGNALMAPPER
 
31
#include "qhash.h"
 
32
#include "qobject_p.h"
 
33
 
 
34
class QSignalMapperPrivate : public QObjectPrivate
 
35
{
 
36
    Q_DECLARE_PUBLIC(QSignalMapper)
 
37
public:
 
38
    void senderDestroyed() {
 
39
        Q_Q(QSignalMapper);
 
40
        q->removeMappings(q->sender());
 
41
    }
 
42
    QHash<QObject *, int> intHash;
 
43
    QHash<QObject *, QString> stringHash;
 
44
    QHash<QObject *, QWidget*> widgetHash;
 
45
 
 
46
};
 
47
 
 
48
 
 
49
/*!
 
50
    \class QSignalMapper
 
51
    \brief The QSignalMapper class bundles signals from identifiable senders.
 
52
 
 
53
    \ingroup io
 
54
    \mainclass
 
55
 
 
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.
 
59
 
 
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
 
65
    removeMappings().
 
66
 
 
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
 
72
    clicked.
 
73
 
 
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
 
76
    that was clicked:
 
77
 
 
78
    \quotefromfile snippets/qsignalmapper/buttonwidget.h
 
79
    \skipto QWidget
 
80
    \printuntil QSignalMapper
 
81
    \printuntil };
 
82
 
 
83
    The only function that we need to implement is the constructor:
 
84
 
 
85
    \quotefromfile snippets/qsignalmapper/buttonwidget.cpp
 
86
    \skipto ButtonWidget
 
87
    \printuntil connect
 
88
    \printuntil connect
 
89
    \printuntil }
 
90
 
 
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.
 
100
 
 
101
    \sa QObject, QButtonGroup, QActionGroup
 
102
*/
 
103
 
 
104
/*!
 
105
    Constructs a QSignalMapper with parent \a parent.
 
106
*/
 
107
QSignalMapper::QSignalMapper(QObject* parent)
 
108
    : QObject(*new QSignalMapperPrivate, parent)
 
109
{
 
110
}
 
111
 
 
112
#ifdef QT3_SUPPORT
 
113
/*!
 
114
    \overload
 
115
    \obsolete
 
116
 */
 
117
QSignalMapper::QSignalMapper(QObject *parent, const char *name)
 
118
    : QObject(*new QSignalMapperPrivate, parent)
 
119
{
 
120
    setObjectName(QString::fromAscii(name));
 
121
}
 
122
#endif
 
123
 
 
124
/*!
 
125
    Destroys the QSignalMapper.
 
126
*/
 
127
QSignalMapper::~QSignalMapper()
 
128
{
 
129
}
 
130
 
 
131
/*!
 
132
    Adds a mapping so that when map() is signalled from the given \a
 
133
    sender, the signal mapped(\a id) is emitted.
 
134
 
 
135
    There may be at most one integer ID for each object.
 
136
 
 
137
    \sa mapping()
 
138
*/
 
139
void QSignalMapper::setMapping(QObject *sender, int id)
 
140
{
 
141
    Q_D(QSignalMapper);
 
142
    d->intHash.insert(sender, id);
 
143
    connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
 
144
}
 
145
 
 
146
/*!
 
147
    \overload
 
148
 
 
149
    Adds a mapping so that when map() is signalled from the given \a
 
150
    sender, the signal mapped(\a text ) is emitted.
 
151
 
 
152
    There may be at most one text for each object.
 
153
*/
 
154
void QSignalMapper::setMapping(QObject *sender, const QString &text)
 
155
{
 
156
    Q_D(QSignalMapper);
 
157
    d->stringHash.insert(sender, text);
 
158
    connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
 
159
}
 
160
 
 
161
/*!
 
162
    \overload
 
163
 
 
164
    Adds a mapping so that when map() is signalled from the given \a
 
165
    sender, the signal mapped(\a widget ) is emitted.
 
166
 
 
167
    There may be at most one widget for each object.
 
168
*/
 
169
void QSignalMapper::setMapping(QObject *sender, QWidget *widget)
 
170
{
 
171
    Q_D(QSignalMapper);
 
172
    d->widgetHash.insert(sender, widget);
 
173
    connect(sender, SIGNAL(destroyed()), this, SLOT(senderDestroyed()));
 
174
}
 
175
 
 
176
 
 
177
/*!
 
178
    Returns the sender QObject that is associated with the given \a
 
179
    id.
 
180
 
 
181
    \sa setMapping()
 
182
*/
 
183
QObject *QSignalMapper::mapping(int id) const
 
184
{
 
185
    Q_D(const QSignalMapper);
 
186
    return d->intHash.key(id);
 
187
}
 
188
 
 
189
/*!
 
190
    \overload
 
191
*/
 
192
QObject *QSignalMapper::mapping(const QString &id) const
 
193
{
 
194
    Q_D(const QSignalMapper);
 
195
    return d->stringHash.key(id);
 
196
}
 
197
 
 
198
/*!
 
199
    \overload
 
200
 
 
201
    Returns the sender QObject that is associated with the given \a
 
202
    widget.
 
203
*/
 
204
QObject *QSignalMapper::mapping(QWidget *widget) const
 
205
{
 
206
    Q_D(const QSignalMapper);
 
207
    return d->widgetHash.key(widget);
 
208
}
 
209
 
 
210
/*!
 
211
    Removes all mappings for \a sender.
 
212
 
 
213
    This is done automatically when mapped objects are destroyed.
 
214
*/
 
215
void QSignalMapper::removeMappings(QObject *sender)
 
216
{
 
217
    Q_D(QSignalMapper);
 
218
 
 
219
    d->intHash.remove(sender);
 
220
    d->stringHash.remove(sender);
 
221
    d->widgetHash.remove(sender);
 
222
}
 
223
 
 
224
/*!
 
225
    This slot emits signals based on which object sends signals to it.
 
226
*/
 
227
void QSignalMapper::map() { map(sender()); }
 
228
 
 
229
/*!
 
230
    This slot emits signals based on the \a sender object.
 
231
*/
 
232
void QSignalMapper::map(QObject *sender)
 
233
{
 
234
    Q_D(QSignalMapper);
 
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));
 
241
}
 
242
 
 
243
 
 
244
/*!
 
245
    \fn void QSignalMapper::mapped(int i)
 
246
 
 
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
 
249
    in \a i.
 
250
 
 
251
    \sa setMapping()
 
252
*/
 
253
 
 
254
/*!
 
255
    \fn void QSignalMapper::mapped(const QString &text)
 
256
    \overload
 
257
 
 
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
 
260
    \a text.
 
261
 
 
262
    \sa setMapping()
 
263
*/
 
264
 
 
265
/*!
 
266
    \fn void QSignalMapper::mapped(QWidget *widget)
 
267
    \overload
 
268
 
 
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
 
271
    \a widget.
 
272
 
 
273
    \sa setMapping()
 
274
*/
 
275
 
 
276
#include "moc_qsignalmapper.cpp"
 
277
#endif // QT_NO_SIGNALMAPPER