Wt
3.3.0
|
A utility class to connect multiple senders to a single slot. More...
#include <Wt/WSignalMapper>
Public Member Functions | |
WSignalMapper (WObject *parent=0) | |
Creates a new WSignalMapper. | |
void | setMapping (WObject *sender, const T &data) |
Associates data with a sender. | |
boost::signals::connection | mapConnect (SignalBase &signal, const T &data) |
Maps a signal without arguments. | |
template<typename S > | |
boost::signals::connection | mapConnect1 (S &signal, const T &data) |
Maps a signal with one argument. | |
Signal< T, A1 > & | mapped () |
Signal emitted in response to a signal sent to map() or map1(). | |
void | map () |
Slot to which to connect the source signal. | |
void | map1 (A1 a) |
Slot to which to connect the source signal, passing the argument to the receiver. | |
void | removeMapping (WObject *sender) |
Removes the mapping of an object. |
A utility class to connect multiple senders to a single slot.
This class is useful if you would like to respond to signals of many objects or widgets within a single slot, but need to identify the particular sender through some property.
Usage example:
void Test::createWidgets() { Wt::WSignalMapper<WText *> *MyMap = new Wt::WSignalMapper<Wt::WText *>(this); // connect mapped() to our target slot MyMap->mapped().connect(this, &Test::onClick); // connect and map every source signal to the mapper MyMap->mapConnect(text1->clicked(), text1); MyMap->mapConnect(text2->clicked(), text2); MyMap->mapConnect(text3->clicked(), text3); } void Test::onClick(WText* source) { // source is where it is coming from // ... }
The type T may be any type that has proper copy semantics and a default constructor. The mapper may pass one extra argument (type A1) from the original signal to the mapped() signal. In that case, you must connect the original signal to the map1() slot, or use mapConnect1().
The mapper uses signal.sender() to attribute ownership of a signal to a sender.
You may want to consider to boost::bind() (or std::bind()) instead, which is simpler and achieves the same in a more direct way:
text1->clicked().connect(boost::bind(&Test::onClick, this, text1)); text2->clicked().connect(boost::bind(&Test::onClick, this, text2)); text3->clicked().connect(boost::bind(&Test::onClick, this, text3));
void Wt::WSignalMapper< T, A1 >::map | ( | ) |
Slot to which to connect the source signal.
When a signal triggers the slot, the sender is identified and used to find corresponding data set with setMapping(), which is then use to propagate further in the mapped() signal.
void Wt::WSignalMapper< T, A1 >::map1 | ( | A1 | a | ) |
Slot to which to connect the source signal, passing the argument to the receiver.
When a signal triggers the slot, the sender is identified and used to find corresponding data set with setMapping(), which is then use to propagate further in the mapped() signal. The additional argument a
is passed as the second argument to the mapped() signal.
boost::signals::connection Wt::WSignalMapper< T, A1 >::mapConnect | ( | SignalBase & | signal, |
const T & | data | ||
) |
Maps a signal without arguments.
Connect the given signal with the slot, and associate the data when it is triggered.
Wt::WSignalMapper<T> *mapper = ... mapper->mapConnect(widget->clicked(), data);
is equivalent to:
Wt::WSignalMapper<T> *mapper = ... widget->clicked().connect(mapper, &Wt::WSignalMapper<T>::map); mapper->setMapping(widget, data);
boost::signals::connection Wt::WSignalMapper< T, A1 >::mapConnect1 | ( | S & | signal, |
const T & | data | ||
) |
Maps a signal with one argument.
Connect the given signal with the slot, and associate the data when it is triggered. The signal argument will be passed to the mapped() signal.
Wt::WSignalMapper<T, Wt::WMouseEvent> *mapper = ... mapper->mapConnect(widget->clicked(), data);
is equivalent to:
Wt::WSignalMapper<T, Wt::WMouseEvent> *mapper = ... widget->clicked().connect(mapper, &Wt::WSignalMapper<T, Wt::WMouseEvent>::map1); mapper->setMapping(widget, data);
Signal<T, A1>& Wt::WSignalMapper< T, A1 >::mapped | ( | ) |
Signal emitted in response to a signal sent to map() or map1().
The first argument propagated is the data that is associated with the specific sender, set in setMapping() or mapConnect(). The second argument is an argument passed from the originating signal.
void Wt::WSignalMapper< T, A1 >::removeMapping | ( | WObject * | sender | ) |
Removes the mapping of an object.
This method does not disconnect any signals; that is the responsability of the user of WSignalMapper. If no mapping of for an object exits, the mapper will ignore the signal, and not emit the mapped signal.
void Wt::WSignalMapper< T, A1 >::setMapping | ( | WObject * | sender, |
const T & | data | ||
) |