MIRA
SignalBinder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_SIGNALBINDER_H_
49 #define _MIRA_SIGNALBINDER_H_
50 
51 #ifndef Q_MOC_RUN
52 #include <boost/function.hpp>
53 #endif
54 
55 #include <QObject>
56 
57 #include <utils/Bind.h>
58 
60 
61 namespace mira {
62 
64 
87 class MIRA_GUI_WIDGETS_EXPORT SignalBinder : public QObject
88 {
89  Q_OBJECT
90 public:
91  SignalBinder(QObject* parent) :
92  QObject(parent) {}
93 
94 public:
95 
97  void bind(boost::function<void()> f) { mVoid = f; }
98 
100  void bindInt(boost::function<void(int)> f) { mInt = f; }
101 
103  void bindQString(boost::function<void(const QString&)> f) { mQString = f; }
104 
105 public slots:
106 
107  void slot() { if(mVoid) mVoid(); }
108  void slot(int val) { if(mInt) mInt(val); }
109  void slot(const QString& val) { if(mQString) mQString(val); }
110 
111 public:
112 
113 
119  static SignalBinder* connect(QObject* sender, const char* signal,
120  boost::function<void()> function)
121  {
122  SignalBinder* binder = new SignalBinder(sender);
123  binder->bind(function);
124  binder->QObject::connect(sender,signal, binder, SLOT(slot()));
125  return binder;
126  }
127 
133  template<typename Class>
134  static SignalBinder* connect(QObject* sender, const char* signal,
135  void (Class::*method)(), Class* instance) {
136  return connect(sender,signal,(boost::function<void()>)boost::bind(method, instance));
137  }
138 
144  static SignalBinder* connect(QObject* sender, const char* signal,
145  boost::function<void(int)> function)
146  {
147  SignalBinder* binder = new SignalBinder(sender);
148  binder->bindInt(function);
149  binder->QObject::connect(sender,signal, binder, SLOT(slot(int)));
150  return binder;
151  }
152 
158  template<typename Class>
159  static SignalBinder* connect(QObject* sender, const char* signal,
160  void (Class::*method)(int), Class* instance) {
161  return connect(sender,signal,(boost::function<void(int)>)boost::bind(method, instance, _1));
162  }
163 
169  static SignalBinder* connect(QObject* sender, const char* signal,
170  boost::function<void(const QString&)> function)
171  {
172  SignalBinder* binder = new SignalBinder(sender);
173  binder->bindQString(function);
174  binder->QObject::connect(sender,signal, binder, SLOT(slot(const QString&)));
175  return binder;
176  }
177 
183  template<typename Class>
184  static SignalBinder* connect(QObject* sender, const char* signal,
185  void (Class::*method)(const QString&), Class* instance) {
186  return connect(sender,signal,(boost::function<void(const QString&)>)boost::bind(method, instance, _1));
187  }
188 
189 private:
190 
191  boost::function<void()> mVoid;
192  boost::function<void(int)> mInt;
193  boost::function<void(const QString&)> mQString;
194 };
195 
197 
198 }
199 
200 #endif
#define MIRA_GUI_WIDGETS_EXPORT
Definition: GuiWidgetsExports.h:61
static SignalBinder * connect(QObject *sender, const char *signal, void(Class::*method)(const QString &), Class *instance)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified &#39;met...
Definition: SignalBinder.h:184
Class that provides different slots with different parameters.
Definition: SignalBinder.h:87
static SignalBinder * connect(QObject *sender, const char *signal, boost::function< void(int)> function)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified func...
Definition: SignalBinder.h:144
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
SignalBinder(QObject *parent)
Definition: SignalBinder.h:91
Class object which supports some kind of class reflection.
Definition: Class.h:97
void slot()
Definition: SignalBinder.h:107
void bindQString(boost::function< void(const QString &)> f)
binds the given function to the slot "slot(const QString&)"
Definition: SignalBinder.h:103
GUI export macro declaration.
void bindInt(boost::function< void(int)> f)
binds the given function to the slot "slot(int)"
Definition: SignalBinder.h:100
void slot(int val)
Definition: SignalBinder.h:108
static SignalBinder * connect(QObject *sender, const char *signal, boost::function< void()> function)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified func...
Definition: SignalBinder.h:119
void slot(const QString &val)
Definition: SignalBinder.h:109
Wrapper for boost/bind.
static SignalBinder * connect(QObject *sender, const char *signal, boost::function< void(const QString &)> function)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified func...
Definition: SignalBinder.h:169
void bind(boost::function< void()> f)
binds the given function to the slot "slot()"
Definition: SignalBinder.h:97
static SignalBinder * connect(QObject *sender, const char *signal, void(Class::*method)(int), Class *instance)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified &#39;met...
Definition: SignalBinder.h:159
static SignalBinder * connect(QObject *sender, const char *signal, void(Class::*method)(), Class *instance)
Create a SignalBinder instance and binds the specified &#39;signal&#39; of the &#39;sender&#39; to the specified &#39;met...
Definition: SignalBinder.h:134