ctkCallback.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /// The following example prints debug statement everytime the current value
  22. /// of the slider is changed:
  23. /// void print() { qDebug() << "signal called"; }
  24. /// ...
  25. /// QSlider slider(Qt::Horizontal);
  26. /// ctkCallback callback(print);
  27. /// QObject::connect(&slider, SIGNAL(valueChanged(int)),
  28. /// &callback, SLOT(invoke()));
  29. /// ...
  30. /// The following example prints the new value of the slider
  31. /// void print(void* data){
  32. /// qDebug() << reinterpret_cast<QSlider*>(data)->value();
  33. /// }
  34. /// QSlider slider(Qt::Horizontal);
  35. /// ctkCallback callback(print);
  36. /// callback.setData(&slider);
  37. /// QObject::connect(&slider, SIGNAL(valueChanged(int)),
  38. /// &callback, SLOT(invoke()));
  39. class CTK_CORE_EXPORT ctkCallback : public QObject
  40. {
  41. Q_OBJECT
  42. public:
  43. ctkCallback(QObject * parentObject = 0);
  44. ctkCallback(void (*callback)(void * data), QObject * parentObject = 0);
  45. virtual ~ctkCallback();
  46. /// Returns the current pointer function
  47. void (*callback()const)(void*);
  48. /// Sets a pointer function to call when invoke() is called.
  49. void setCallback(void (*callback)(void * data));
  50. /// Returns the current callback data.
  51. /// \note By default ctkCallback itself will be passed as callback data
  52. /// \sa setCallbackData
  53. void * callbackData()const;
  54. /// Set callback data
  55. void setCallbackData(void * data);
  56. public slots:
  57. /// Internally calls the pointer function \a callback.
  58. virtual void invoke();
  59. private:
  60. void (*Callback)(void * data);
  61. void * CallbackData;
  62. };
  63. #endif