Salome HOME
Issue #599 - hide trihedron when create Sketch,
[modules/shaper.git] / src / Events / Events_Loop.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Events_Loop.hxx
4 // Created:     Thu Mar 13 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Events_Loop_H_
8 #define Events_Loop_H_
9
10 #include <Events_Message.h>
11 #include <Events_Listener.h>
12
13 #include <map>
14 #include <set>
15 #include <list>
16
17 class Events_MessageGroup;
18
19 /**\class Events_Loop
20  * \ingroup EventsLoop
21  * \brief Base class that manages the receiving and sending of all
22  * not Qt-events in the application.
23  *
24  * One per application, initialized on start. Listeners must register in this loop
25  * to get events, called by senders. Sending of events is very fast (just adding to container).
26  * Performing of events is processed in separated thread, so, sender takes 
27  * control back immideately.
28  */
29 class Events_Loop
30 {
31   /// map from event ID to sender pointer to listeners that must be called for this
32   std::map<char*, std::map<void*, std::list<Events_Listener*> > > myListeners;
33
34   /// map from event ID to listeners which must process message without waiting for flush
35   std::map<char*, Events_Listener*> myImmediateListeners;
36
37   /// map from event ID to groupped messages (accumulated on flush)
38   std::map<char*, std::shared_ptr<Events_Message> > myGroups;
39
40   ///< set of messages that are flushed right now, so they are not grouped
41   std::set<char*> myFlushed;
42
43   /// to process flushes or not
44   bool myFlushActive;
45
46   //! The empty constructor, will be called at startup of the application, only once
47   Events_Loop() : myFlushActive(true) {}
48
49  public:
50   ///! Returns the main object of the loop, one per application.
51   EVENTS_EXPORT static Events_Loop* loop();
52   //! Returns the unique event by the given name. Call this method only on initialization of object
53   //! to speedup the events processing without parsing of the string.
54   EVENTS_EXPORT static Events_ID eventByName(const char* theName);
55
56   //! Allows to send an event
57   //! \param theMessage the enevt message to send
58   //! \param isGroup is true for grouping messages if possible
59   EVENTS_EXPORT void send(const std::shared_ptr<Events_Message>& theMessage, bool isGroup = true);
60
61   //! Registers (or adds if such listener is already registered) a listener 
62   //! that will be called on the event and from the defined sender
63   EVENTS_EXPORT void registerListener(Events_Listener* theListener, const Events_ID theID,
64                                       void* theSender = 0, bool theImmediate = false);
65
66   //! Initializes sending of a group-message by the given ID
67   EVENTS_EXPORT void flush(const Events_ID& theID);
68
69   //! Allows to disable flushes: needed in synchronization of document mechanism 
70   //! (to synchronize all and only then flush create, update, etc in correct order)
71   //! \param theActivate a state about flushe is active. If false, the flush is disabled
72   //! \return the previous active flush state
73   EVENTS_EXPORT bool activateFlushes(const bool theActivate);
74   
75   //! Clears all collected messages
76   EVENTS_EXPORT void clear(const Events_ID& theID);
77
78   //! Enables flush without grouping for the given message
79   EVENTS_EXPORT void autoFlush(const Events_ID& theID, const bool theAuto = true);
80
81   //! Returns true if the evement is flushed right now
82   EVENTS_EXPORT bool isFlushed(const Events_ID& theID);
83   //! Sets the flag that the event is flished right now
84   EVENTS_EXPORT void setFlushed(const Events_ID& theID, const bool theValue);
85 };
86
87 #endif