Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSignalMapper Class Reference

The KisSignalMapper class bundles signals from identifiable senders. More...

#include <KisSignalMapper.h>

+ Inheritance diagram for KisSignalMapper:

Classes

class  Private
 

Public Slots

void map ()
 
void map (QObject *sender)
 

Signals

void mapped (const QString &)
 
void mapped (int)
 
void mapped (QObject *)
 
void mapped (QWidget *)
 

Public Member Functions

 KisSignalMapper (QObject *parent=nullptr)
 
QObject * mapping (const QString &text) const
 
QObject * mapping (int id) const
 
QObject * mapping (QObject *object) const
 
QObject * mapping (QWidget *widget) const
 
void removeMappings (QObject *sender)
 
void setMapping (QObject *sender, const QString &text)
 
void setMapping (QObject *sender, int id)
 
void setMapping (QObject *sender, QObject *object)
 
void setMapping (QObject *sender, QWidget *widget)
 
 ~KisSignalMapper ()
 

Private Attributes

QScopedPointer< Privated
 

Related Symbols

(Note that these are not member symbols.)

 mapping
 
 mapping
 
 mapping
 

Detailed Description

The KisSignalMapper class bundles signals from identifiable senders.

\inmodule QtCore \obsolete The recommended solution is connecting the signal to a lambda.

This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.

The class supports the mapping of particular strings or integers with particular objects using setMapping(). The objects' signals can then be connected to the map() slot which will Q_EMIT the mapped() signal with the string or integer associated with the original signaling object. Mappings can be removed later using removeMappings().

Example: Suppose we want to create a custom widget that contains a group of buttons (like a tool palette). One approach is to connect each button's clicked() signal to its own custom slot; but in this example we want to connect all the buttons to a single slot and parameterize the slot by the button that was clicked.

Here's the definition of a simple custom widget that has a single signal, clicked(), which is emitted with the text of the button that was clicked:

The only function that we need to implement is the constructor:

A list of texts is passed to the constructor. A signal mapper is constructed and for each text in the list a QPushButton is created. We connect each button's clicked() signal to the signal mapper's map() slot, and create a mapping in the signal mapper from each button to the button's text. Finally we connect the signal mapper's mapped() signal to the custom widget's clicked() signal. When the user clicks a button, the custom widget will Q_EMIT a single clicked() signal whose argument is the text of the button the user clicked.

This class was mostly useful before lambda functions could be used as slots. The example above can be rewritten simpler without KisSignalMapper by connecting to a lambda function.

See also
QObject, QButtonGroup, QActionGroup

\inmodule QtCore \obsolete The recommended solution is connecting the signal to a lambda.

This class collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal.

The class supports the mapping of particular strings or integers with particular objects using setMapping(). The objects' signals can then be connected to the map() slot which will Q_EMIT the mapped() signal with the string or integer associated with the original signaling object. Mappings can be removed later using removeMappings().

Example: Suppose we want to create a custom widget that contains a group of buttons (like a tool palette). One approach is to connect each button's clicked() signal to its own custom slot; but in this example we want to connect all the buttons to a single slot and parameterize the slot by the button that was clicked.

Here's the definition of a simple custom widget that has a single signal, clicked(), which is emitted with the text of the button that was clicked:

The only function that we need to implement is the constructor:

A list of texts is passed to the constructor. A signal mapper is constructed and for each text in the list a QPushButton is created. We connect each button's clicked() signal to the signal mapper's map() slot, and create a mapping in the signal mapper from each button to the button's text. Finally we connect the signal mapper's mapped() signal to the custom widget's clicked() signal. When the user clicks a button, the custom widget will Q_EMIT a single clicked() signal whose argument is the text of the button the user clicked.

This class was mostly useful before lambda functions could be used as slots. The example above can be rewritten simpler without KisSignalMapper by connecting to a lambda function.

See also
QObject, QButtonGroup, QActionGroup

Definition at line 78 of file KisSignalMapper.h.

Constructor & Destructor Documentation

◆ KisSignalMapper()

KisSignalMapper::KisSignalMapper ( QObject * parent = nullptr)
explicit

Constructs a KisSignalMapper with parent parent.

Definition at line 97 of file KisSignalMapper.cpp.

98 : QObject(parent)
99 , d(new Private(this))
100{
101}
QScopedPointer< Private > d

◆ ~KisSignalMapper()

KisSignalMapper::~KisSignalMapper ( )

Destroys the KisSignalMapper.

Definition at line 106 of file KisSignalMapper.cpp.

107{
108}

Member Function Documentation

◆ map [1/2]

void KisSignalMapper::map ( )
slot

This slot emits signals based on which object sends signals to it.

Definition at line 217 of file KisSignalMapper.cpp.

217{ map(sender()); }

References map().

◆ map [2/2]

void KisSignalMapper::map ( QObject * sender)
slot

This slot emits signals based on the sender object.

Definition at line 222 of file KisSignalMapper.cpp.

223{
224 if (d->intHash.contains(sender))
225 Q_EMIT mapped(d->intHash.value(sender));
226 if (d->stringHash.contains(sender))
227 Q_EMIT mapped(d->stringHash.value(sender));
228 if (d->widgetHash.contains(sender))
229 Q_EMIT mapped(d->widgetHash.value(sender));
230 if (d->objectHash.contains(sender))
231 Q_EMIT mapped(d->objectHash.value(sender));
232}
void mapped(int)

References d, and mapped().

◆ mapped [1/4]

void KisSignalMapper::mapped ( const QString & text)
signal

This signal is emitted when map() is signalled from an object that has a string mapping set. The object's mapped string is passed in text.

See also
setMapping()

◆ mapped [2/4]

void KisSignalMapper::mapped ( int i)
signal

This signal is emitted when map() is signalled from an object that has an integer mapping set. The object's mapped integer is passed in i.

See also
setMapping()

◆ mapped [3/4]

void KisSignalMapper::mapped ( QObject * object)
signal

This signal is emitted when map() is signalled from an object that has an object mapping set. The object provided by the map is passed in object.

See also
setMapping()

◆ mapped [4/4]

void KisSignalMapper::mapped ( QWidget * widget)
signal

This signal is emitted when map() is signalled from an object that has a widget mapping set. The object's mapped widget is passed in widget.

See also
setMapping()

◆ mapping() [1/4]

QObject * KisSignalMapper::mapping ( const QString & text) const

Definition at line 173 of file KisSignalMapper.cpp.

174{
175 return d->stringHash.key(id);
176}

References d.

◆ mapping() [2/4]

QObject * KisSignalMapper::mapping ( int id) const

Returns the sender QObject that is associated with the id.

See also
setMapping()

Definition at line 165 of file KisSignalMapper.cpp.

166{
167 return d->intHash.key(id);
168}

References d.

◆ mapping() [3/4]

QObject * KisSignalMapper::mapping ( QObject * object) const

Returns the sender QObject that is associated with the id.

See also
setMapping()

Definition at line 193 of file KisSignalMapper.cpp.

194{
195 return d->objectHash.key(object);
196}

References d.

◆ mapping() [4/4]

QObject * KisSignalMapper::mapping ( QWidget * widget) const

Definition at line 183 of file KisSignalMapper.cpp.

184{
185 return d->widgetHash.key(widget);
186}

References d.

◆ removeMappings()

void KisSignalMapper::removeMappings ( QObject * sender)

Removes all mappings for sender.

This is done automatically when mapped objects are destroyed.

Note
This does not disconnect any signals. If sender is not destroyed then this will need to be done explicitly if required.

Definition at line 206 of file KisSignalMapper.cpp.

207{
208 d->intHash.remove(sender);
209 d->stringHash.remove(sender);
210 d->widgetHash.remove(sender);
211 d->objectHash.remove(sender);
212}

References d.

◆ setMapping() [1/4]

void KisSignalMapper::setMapping ( QObject * sender,
const QString & text )

Adds a mapping so that when map() is signalled from the sender, the signal mapped(text ) is emitted.

There may be at most one text for each sender.

Definition at line 130 of file KisSignalMapper.cpp.

131{
132 d->stringHash.insert(sender, text);
133 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
134}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))

References connect(), and d.

◆ setMapping() [2/4]

void KisSignalMapper::setMapping ( QObject * sender,
int id )

Adds a mapping so that when map() is signalled from the given sender, the signal mapped(id) is emitted.

There may be at most one integer ID for each sender.

See also
mapping()

Definition at line 118 of file KisSignalMapper.cpp.

119{
120 d->intHash.insert(sender, id);
121 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
122}

References connect(), and d.

◆ setMapping() [3/4]

void KisSignalMapper::setMapping ( QObject * sender,
QObject * object )

Adds a mapping so that when map() is signalled from the sender, the signal mapped(object ) is emitted.

There may be at most one object for each sender.

Definition at line 154 of file KisSignalMapper.cpp.

155{
156 d->objectHash.insert(sender, object);
157 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
158}

References connect(), and d.

◆ setMapping() [4/4]

void KisSignalMapper::setMapping ( QObject * sender,
QWidget * widget )

Adds a mapping so that when map() is signalled from the sender, the signal mapped(widget ) is emitted.

There may be at most one widget for each sender.

Definition at line 142 of file KisSignalMapper.cpp.

143{
144 d->widgetHash.insert(sender, widget);
145 connect(sender, SIGNAL(destroyed()), this, SLOT(_q_senderDestroyed()));
146}

References connect(), and d.

Friends And Related Symbol Documentation

◆ mapping() [1/3]

KisSignalMapper::mapping ( )
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns the sender QObject that is associated with the widget.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns the sender QObject that is associated with the object.

◆ mapping() [2/3]

mapping ( )
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns the sender QObject that is associated with the widget.

◆ mapping() [3/3]

mapping ( )
related

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns the sender QObject that is associated with the object.

Member Data Documentation

◆ d

QScopedPointer<Private> KisSignalMapper::d
private

Definition at line 220 of file KisSignalMapper.h.


The documentation for this class was generated from the following files: