1 | /****************************************************************************
|
---|
2 | ** $Id: qsignalmapper.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Implementation of QSignalMapper class
|
---|
5 | **
|
---|
6 | ** Created : 980503
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include "qsignalmapper.h"
|
---|
39 | #ifndef QT_NO_SIGNALMAPPER
|
---|
40 | #include "qptrdict.h"
|
---|
41 |
|
---|
42 | struct QSignalMapperRec {
|
---|
43 | QSignalMapperRec()
|
---|
44 | {
|
---|
45 | has_int = 0;
|
---|
46 | str_id = QString::null;
|
---|
47 | }
|
---|
48 |
|
---|
49 | uint has_int:1;
|
---|
50 |
|
---|
51 | int int_id;
|
---|
52 | QString str_id;
|
---|
53 | // extendable to other types of identification
|
---|
54 | };
|
---|
55 |
|
---|
56 | class QSignalMapperData {
|
---|
57 | public:
|
---|
58 | QSignalMapperData()
|
---|
59 | {
|
---|
60 | dict.setAutoDelete( TRUE );
|
---|
61 | }
|
---|
62 |
|
---|
63 | QPtrDict<QSignalMapperRec> dict;
|
---|
64 | };
|
---|
65 |
|
---|
66 | /*!
|
---|
67 | \class QSignalMapper qsignalmapper.h
|
---|
68 | \brief The QSignalMapper class bundles signals from identifiable senders.
|
---|
69 |
|
---|
70 | \ingroup io
|
---|
71 |
|
---|
72 | This class collects a set of parameterless signals, and re-emits
|
---|
73 | them with integer or string parameters corresponding to the object
|
---|
74 | that sent the signal.
|
---|
75 | */
|
---|
76 |
|
---|
77 | /*!
|
---|
78 | Constructs a QSignalMapper called \a name, with parent \a parent.
|
---|
79 | Like all QObjects, it will be deleted when the parent is deleted.
|
---|
80 | */
|
---|
81 | QSignalMapper::QSignalMapper( QObject* parent, const char* name ) :
|
---|
82 | QObject( parent, name )
|
---|
83 | {
|
---|
84 | d = new QSignalMapperData;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /*!
|
---|
88 | Destroys the QSignalMapper.
|
---|
89 | */
|
---|
90 | QSignalMapper::~QSignalMapper()
|
---|
91 | {
|
---|
92 | delete d;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /*!
|
---|
96 | Adds a mapping so that when map() is signaled from the given \a
|
---|
97 | sender, the signal mapped(\a identifier) is emitted.
|
---|
98 |
|
---|
99 | There may be at most one integer identifier for each object.
|
---|
100 | */
|
---|
101 | void QSignalMapper::setMapping( const QObject* sender, int identifier )
|
---|
102 | {
|
---|
103 | QSignalMapperRec* rec = getRec(sender);
|
---|
104 | rec->int_id = identifier;
|
---|
105 | rec->has_int = 1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /*!
|
---|
109 | \overload
|
---|
110 |
|
---|
111 | Adds a mapping so that when map() is signaled from the given \a
|
---|
112 | sender, the signal mapper(\a identifier) is emitted.
|
---|
113 |
|
---|
114 | There may be at most one string identifier for each object, and it
|
---|
115 | may not be empty.
|
---|
116 | */
|
---|
117 | void QSignalMapper::setMapping( const QObject* sender, const QString &identifier )
|
---|
118 | {
|
---|
119 | QSignalMapperRec* rec = getRec(sender);
|
---|
120 | rec->str_id = identifier;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*!
|
---|
124 | Removes all mappings for \a sender. This is done automatically
|
---|
125 | when mapped objects are destroyed.
|
---|
126 | */
|
---|
127 | void QSignalMapper::removeMappings( const QObject* sender )
|
---|
128 | {
|
---|
129 | d->dict.remove((void*)sender);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void QSignalMapper::removeMapping()
|
---|
133 | {
|
---|
134 | removeMappings(sender());
|
---|
135 | }
|
---|
136 |
|
---|
137 | /*!
|
---|
138 | This slot emits signals based on which object sends signals to it.
|
---|
139 | */
|
---|
140 | void QSignalMapper::map()
|
---|
141 | {
|
---|
142 | const QObject* s = sender();
|
---|
143 | QSignalMapperRec* rec = d->dict.find( (void*)s );
|
---|
144 | if ( rec ) {
|
---|
145 | if ( rec->has_int )
|
---|
146 | emit mapped( rec->int_id );
|
---|
147 | if ( !rec->str_id.isEmpty() )
|
---|
148 | emit mapped( rec->str_id );
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | QSignalMapperRec* QSignalMapper::getRec( const QObject* sender )
|
---|
153 | {
|
---|
154 | QSignalMapperRec* rec = d->dict.find( (void*)sender );
|
---|
155 | if (!rec) {
|
---|
156 | rec = new QSignalMapperRec;
|
---|
157 | d->dict.insert( (void*)sender, rec );
|
---|
158 | connect( sender, SIGNAL(destroyed()), this, SLOT(removeMapping()) );
|
---|
159 | }
|
---|
160 | return rec;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /*!
|
---|
164 | \fn void QSignalMapper::mapped(int)
|
---|
165 |
|
---|
166 | This signal is emitted when map() is signaled from an object that
|
---|
167 | has an integer mapping set.
|
---|
168 |
|
---|
169 | \sa setMapping()
|
---|
170 | */
|
---|
171 |
|
---|
172 | /*!
|
---|
173 | \overload void QSignalMapper::mapped(const QString&)
|
---|
174 |
|
---|
175 | This signal is emitted when map() is signaled from an object that
|
---|
176 | has a string mapping set.
|
---|
177 |
|
---|
178 | \sa setMapping()
|
---|
179 | */
|
---|
180 | #endif //QT_NO_SIGNALMAPPER
|
---|