ctkCallback.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #ifndef __ctkCallback_h
  15. #define __ctkCallback_h
  16. // Qt includes
  17. #include <QObject>
  18. // CTK includes
  19. #include "ctkCoreExport.h"
  20. //---------------------------------------------------------------------------
  21. /// \ingroup Core
  22. /// The following example prints debug statement everytime the current value
  23. /// of the slider is changed:
  24. /// void print() { qDebug() << "signal called"; }
  25. /// ...
  26. /// QSlider slider(Qt::Horizontal);
  27. /// ctkCallback callback(print);
  28. /// QObject::connect(&slider, SIGNAL(valueChanged(int)),
  29. /// &callback, SLOT(invoke()));
  30. /// ...
  31. /// The following example prints the new value of the slider
  32. /// void print(void* data){
  33. /// qDebug() << reinterpret_cast<QSlider*>(data)->value();
  34. /// }
  35. /// QSlider slider(Qt::Horizontal);
  36. /// ctkCallback callback(print);
  37. /// callback.setData(&slider);
  38. /// QObject::connect(&slider, SIGNAL(valueChanged(int)),
  39. /// &callback, SLOT(invoke()));
  40. class CTK_CORE_EXPORT ctkCallback : public QObject
  41. {
  42. Q_OBJECT
  43. public:
  44. ctkCallback(QObject * parentObject = 0);
  45. ctkCallback(void (*callback)(void * data), QObject * parentObject = 0);
  46. virtual ~ctkCallback();
  47. /// Returns the current pointer function
  48. void (*callback()const)(void*);
  49. /// Sets a pointer function to call when invoke() is called.
  50. void setCallback(void (*callback)(void * data));
  51. /// Returns the current callback data.
  52. /// \note By default ctkCallback itself will be passed as callback data
  53. /// \sa setCallbackData
  54. void * callbackData()const;
  55. /// Set callback data
  56. void setCallbackData(void * data);
  57. public Q_SLOTS:
  58. /// Internally calls the pointer function \a callback.
  59. virtual void invoke();
  60. private:
  61. void (*Callback)(void * data);
  62. void * CallbackData;
  63. };
  64. #endif