ctkEventDispatcher.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * ctkEventDispatcher.h
  3. * ctkEventBus
  4. *
  5. * Created by Paolo Quadrani on 27/03/09.
  6. * Copyright 2009 B3C. All rights reserved.
  7. *
  8. * See Licence at: http://tiny.cc/QXJ4D
  9. *
  10. */
  11. #ifndef CTKEVENTDISPATCHER_H
  12. #define CTKEVENTDISPATCHER_H
  13. #include "ctkEventDefinitions.h"
  14. namespace ctkEventBus {
  15. /**
  16. Class name: ctkEventDispatcher
  17. This allows dispatching events coming from local application to attached observers.
  18. */
  19. class org_commontk_eventbus_EXPORT ctkEventDispatcher : public QObject {
  20. Q_OBJECT
  21. public:
  22. /// object constructor.
  23. ctkEventDispatcher();
  24. /// object destructor.
  25. virtual ~ctkEventDispatcher();
  26. /// Add the observer to the events.
  27. /** Return true if observer has beed added correctly, false otherwise.
  28. This method check before adding a new observer that it has not already been inserted into the events' Hash with the same id and callback signature.*/
  29. bool addObserver(ctkBusEvent &props);
  30. /// remove the callback from the observer's hash.
  31. bool removeObserver(ctkBusEvent &props);
  32. /// remove the callback from the observer's hash.
  33. bool removeObserver(const QObject *obj, const QString topic, bool qt_disconnect = true);
  34. /// Remove the signal from the signal's hash.
  35. bool removeSignal(const QObject *obj, const QString topic = "", bool qt_disconnect = true);
  36. /// register custom signals use by objects to raise them events.
  37. /** Return true if signal has beed added correctly, false otherwise.
  38. This method check before adding a new signal that it has not already been inserted into the events' Hash with the same id and signal signature.
  39. WARNING: due to Qt limitation you cannot use the same signal in different Topics.*/
  40. bool registerSignal(ctkBusEvent &props);
  41. /// Remove the signal from the signal's hash.
  42. bool removeSignal(ctkBusEvent &props);
  43. /// method used to check if the given signal has been already registered for the given id.
  44. bool isLocalSignalPresent(const QString topic) const;
  45. /// Emit event corresponding to the given id (present into the event_dictionary) locally to the application.
  46. virtual void notifyEvent(ctkBusEvent &event_dictionary, ctkEventArgumentsList *argList = NULL, ctkGenericReturnArgument *returnArg = NULL) const;
  47. /// clean the signal and callback hashes.
  48. /** This method is used when the destructor is called. The destructor of the dispatcher is called by the ctkEventBusManager destructor.*/
  49. void resetHashes();
  50. Q_SIGNALS:
  51. /// Default notification signals for default events.
  52. void notifyDefaultEvent();
  53. /// Signal used to notify to observers that the remote communication has been terminated with success.
  54. void remoteCommunicationDone();
  55. /// Signal used to notify to observers that the remote communication failed.
  56. void remoteCommunicationFailed();
  57. protected:
  58. /// Register MAF global events
  59. virtual void initializeGlobalEvents();
  60. /// Interanl method used to remove the given event property.
  61. bool removeEventItem(ctkBusEvent &props);
  62. /// Return the signal item property associated to the given ID.
  63. ctkEventItemListType signalItemProperty(const QString topic) const;
  64. private:
  65. /// method used to check if the given object has been already registered for the given id and signature.
  66. bool isSignaturePresent(ctkBusEvent &props) const;
  67. /// disconnection signal/observer.
  68. /** This function disconnects the signal and all the observers. */
  69. bool disconnectSignal(ctkBusEvent &props);
  70. /// This function disconnects observer from signal.
  71. bool disconnectCallback(ctkBusEvent &props);
  72. /// Remove the given object from the has passed as argument
  73. bool removeFromHash(ctkEventsHashType *hash, const QObject *obj, const QString topic, bool qt_disconnect = true);
  74. ctkEventsHashType m_CallbacksHash; ///< Callbacks' hash for receiving events like updates or refreshes.
  75. ctkEventsHashType m_SignalsHash; ///< Signals' hash for sending events.
  76. };
  77. /////////////////////////////////////////////////////////////
  78. // Inline methods
  79. /////////////////////////////////////////////////////////////
  80. inline ctkEventItemListType ctkEventDispatcher::signalItemProperty(const QString topic) const {
  81. return m_SignalsHash.values(topic);
  82. }
  83. } // namespace ctkEventBus
  84. #endif // CTKEVENTDISPATCHER_H