MNE-CPP  0.1.9
A Framework for Electrophysiology
eventmanager.cpp
1 //=============================================================================================================
36 //=============================================================================================================
37 // INCLUDES
38 //=============================================================================================================
39 
40 #include "eventmanager.h"
41 
42 //=============================================================================================================
43 // QT INCLUDES
44 //=============================================================================================================
45 
46 #include <QDebug>
47 
48 //=============================================================================================================
49 // USED NAMESPACES
50 //=============================================================================================================
51 
52 using namespace EVENTSLIB;
53 
54 //=============================================================================================================
55 // local and static declarations
56 //=============================================================================================================
57 
58 constexpr static int invalidID(0);
59 static std::string defaultGroupName("Default");
61 //=============================================================================================================
62 
64 : m_pSharedMemManager(std::make_unique<EVENTSINTERNAL::EventSharedMemManager>(this))
65 , m_iEventIdCounter(invalidID)
66 , m_iGroupIdCounter(invalidID)
67 , m_bDefaultGroupNotCreated(true)
68 , m_DefaultGroupId(invalidID)
69 {
70 
71 }
72 
73 //=============================================================================================================
74 
75 Event EventManager::getEvent(idNum eventId) const
76 {
77  auto eventInt = findEventINT(eventId);
78  if(eventInt != m_EventsListBySample.end())
79  {
80  return Event(eventInt->second);
81  }
82  return {};
83 }
84 
85 //=============================================================================================================
86 
87 std::multimap<int, EVENTSINTERNAL::EventINT>::const_iterator EventManager::findEventINT(idNum eventId) const
88 {
89  int sample = m_MapIdToSample.at(eventId);
90  auto eventsRange = m_EventsListBySample.equal_range(sample);
91  for(auto e = eventsRange.first; e != eventsRange.second; ++e)
92  {
93  if( e->second.getId() == eventId)
94  {
95  return e;
96  }
97  }
98  return m_EventsListBySample.end();
99 }
100 
101 //=============================================================================================================
102 
103 std::unique_ptr<std::vector<Event> >
104 EventManager::getEvents(const std::vector<idNum> eventIds) const
105 {
106  auto pEventsList(allocateOutputContainer<Event>(eventIds.size()));
107  for (const auto& id: eventIds)
108  {
109  auto event = getEvent(id);
110  if(event.id != invalidID)
111  {
112  pEventsList->push_back(event);
113  }
114  }
115  return pEventsList;
116 }
117 
118 //=============================================================================================================
119 
120 std::unique_ptr<std::vector<Event> > EventManager::getAllEvents() const
121 {
122  auto pEventsList(allocateOutputContainer<Event>(getNumEvents()));
123  for(auto& e: m_EventsListBySample)
124  {
125  pEventsList->emplace_back(Event(e.second));
126  }
127  return pEventsList;
128 }
129 
130 //=============================================================================================================
131 
132 std::unique_ptr<std::vector<Event> > EventManager::getEventsInSample(int sample) const
133 {
134  size_t numEventsInSample = m_EventsListBySample.count(sample);
135  auto pEventsList(allocateOutputContainer<Event>(numEventsInSample));
136 
137  auto eventsRange = m_EventsListBySample.equal_range(sample);
138  for(auto e = eventsRange.first; e != eventsRange.second; e++)
139  {
140  pEventsList->emplace_back(Event(e->second));
141  }
142  return pEventsList;
143 }
144 
145 //=============================================================================================================
146 
147 std::unique_ptr<std::vector<Event> >
148 EventManager::getEventsBetween(int sampleStart, int sampleEnd) const
149 {
150  int memoryHint = ((sampleEnd-sampleStart)/1000)+2;
151  auto pEventsList(allocateOutputContainer<Event>(memoryHint));
152 
153  auto eventStart = m_EventsListBySample.lower_bound(sampleStart);
154  auto eventEnd = m_EventsListBySample.upper_bound(sampleEnd);
155 
156  for(auto e = eventStart; e != eventEnd; e++)
157  {
158  pEventsList->emplace_back(Event(e->second));
159  }
160  return pEventsList;
161 }
162 
163 //=============================================================================================================
164 
165 std::unique_ptr<std::vector<Event> >
166 EventManager::getEventsBetween(int sampleStart, int sampleEnd, idNum groupId) const
167 {
168  int memoryHint = ((sampleEnd-sampleStart)/1000)+2;
169  auto pEventsList(allocateOutputContainer<Event>(memoryHint));
170 
171  auto eventStart = m_EventsListBySample.lower_bound(sampleStart);
172  auto eventEnd = m_EventsListBySample.upper_bound(sampleEnd);
173 
174  for(auto e = eventStart; e != eventEnd; e++)
175  {
176  if(e->second.getGroupId() == groupId)
177  {
178  pEventsList->emplace_back(Event(e->second));
179  }
180  }
181  return pEventsList;
182 }
183 
184 //=============================================================================================================
185 
186 std::unique_ptr<std::vector<Event> >
187 EventManager::getEventsBetween(int sampleStart, int sampleEnd, const std::vector<idNum>& groupIdsList) const
188 {
189  int memoryHint = (sampleEnd-sampleStart)/200;
190  auto pEventsList(allocateOutputContainer<Event>(memoryHint));
191 
192  auto eventStart = m_EventsListBySample.lower_bound(sampleStart);
193  auto eventEnd = m_EventsListBySample.upper_bound(sampleEnd);
194 
195  for(auto e = eventStart; e != eventEnd; e++)
196  {
197  for(auto& groupId: groupIdsList)
198  {
199  if(e->second.getGroupId() == groupId)
200  {
201  pEventsList->emplace_back(Event(e->second));
202  }
203  }
204  }
205  return pEventsList;
206 }
207 
208 //=============================================================================================================
209 
210 std::unique_ptr<std::vector<Event> >
211 EventManager::getEventsInGroup(const idNum groupId) const
212 {
213  auto pEventsList(allocateOutputContainer<Event>());
214 
215  for(const auto& e: m_EventsListBySample)
216  {
217  if(e.second.getGroupId() == groupId)
218  {
219  pEventsList->emplace_back(Event(e.second));
220  }
221  }
222  return pEventsList;
223 }
224 
225 //=============================================================================================================
226 
227 std::unique_ptr<std::vector<Event> > EventManager::getEventsInGroups(const std::vector<idNum>& groupIdsList) const
228 {
229  auto pEventsList(allocateOutputContainer<Event>());
230 
231  for(const auto& e: m_EventsListBySample)
232  {
233  for (const auto groupId : groupIdsList)
234  {
235  if(e.second.getGroupId() == groupId)
236  {
237  pEventsList->emplace_back(Event(e.second));
238  break;
239  }
240  }
241  }
242 
243  return pEventsList;
244 }
245 
246 //=============================================================================================================
247 
248 idNum EventManager::generateNewEventId()
249 {
250  return ++m_iEventIdCounter;
251 }
252 
253 //=============================================================================================================
254 
255 idNum EventManager::generateNewGroupId()
256 {
257  return ++m_iGroupIdCounter;
258 }
259 
260 //=============================================================================================================
261 
263 {
264  return m_EventsListBySample.size();
265 }
266 
267 //=============================================================================================================
268 
269 Event EventManager::addEvent(int sample, idNum groupId)
270 {
271  EVENTSINTERNAL::EventINT newEvent(generateNewEventId(), sample, groupId);
272  insertEvent(newEvent);
273 
274  if(m_pSharedMemManager->isInit())
275  {
276  qDebug() << "Sending event to SM: Sample: " << sample;
277  m_pSharedMemManager->addEvent(newEvent.getSample());
278  }
279 
280  return Event(newEvent);
281 }
282 
283 //=============================================================================================================
284 
286 {
287  createDefaultGroupIfNeeded();
288  return addEvent(sample, m_DefaultGroupId);
289 }
290 
291 //=============================================================================================================
292 
293 bool EventManager::moveEvent(idNum eventId, int newSample)
294 {
295  bool status(false);
296  auto event = findEventINT(eventId);
297  if(event != m_EventsListBySample.end())
298  {
299  EVENTSINTERNAL::EventINT newEvent(event->second);
300  newEvent.setSample(newSample);
301  deleteEvent(eventId);
302  insertEvent(newEvent);
303  status = true;
304  }
305  return status;
306 }
307 
308 //=============================================================================================================
309 
310 bool EventManager::deleteEvent(idNum eventId) noexcept
311 {
312  bool eventFound(false);
313  eventFound = eraseEvent(eventId);
314  if(eventFound && m_pSharedMemManager->isInit())
315  {
316  m_pSharedMemManager->deleteEvent(m_MapIdToSample.at(eventId));
317  }
318  return eventFound;
319 }
320 
321 //=============================================================================================================
322 
323 bool EventManager::eraseEvent(idNum eventId)
324 {
325  auto event = findEventINT(eventId);
326  if(event != m_EventsListBySample.end())
327  {
328  m_EventsListBySample.erase(event);
329  m_MapIdToSample.erase(eventId);
330  return true;
331  }
332  return false;
333 }
334 
335 //=============================================================================================================
336 
337 bool EventManager::deleteEvents(const std::vector<idNum>& eventIds)
338 {
339  bool status(eventIds.size());
340  for(const auto& id: eventIds)
341  {
342  status = status && deleteEvent(id);
343  }
344  return status;
345 }
346 
347 //=============================================================================================================
348 
349 bool EventManager::deleteEvents(std::unique_ptr<std::vector<Event> > eventIds)
350 {
351  bool status(eventIds->size());
352  for(auto e: *eventIds){
353  status = status && deleteEvent(e.id);
354  }
355  return status;
356 }
357 
358 //=============================================================================================================
359 
361 {
362  std::vector<idNum> idList;
363  for(auto& e: m_EventsListBySample)
364  {
365  if(e.second.getGroupId() == groupId)
366  {
367  idList.emplace_back(e.second.getId());
368  }
369  }
370  return deleteEvents(idList);
371 }
372 
373 //=============================================================================================================
374 
375 void EventManager::insertEvent(const EVENTSINTERNAL::EventINT& e)
376 {
377  m_EventsListBySample.emplace(std::make_pair(e.getSample(),e));
378  m_MapIdToSample[e.getId()] = e.getSample();
379 }
380 
381 //=============================================================================================================
382 
384 {
385  return (int)(m_GroupsList.size());
386 }
387 
388 //=============================================================================================================
389 
390 EventGroup EventManager::getGroup(const idNum groupId) const
391 {
392  auto groupFound = m_GroupsList.find(groupId);
393  if(groupFound != m_GroupsList.end())
394  {
395  return EventGroup(groupFound->second);
396  } else
397  {
398  return {};
399  }
400 }
401 
402 //=============================================================================================================
403 
404 std::unique_ptr<std::vector<EventGroup> > EventManager::getAllGroups() const
405 {
406  size_t numGroups(m_GroupsList.size());
407  auto pGroupsList(allocateOutputContainer<EventGroup>(numGroups));
408  for(const auto& g: m_GroupsList)
409  {
410  pGroupsList->emplace_back(EventGroup(g.second));
411  }
412  return pGroupsList;
413 }
414 
415 //=============================================================================================================
416 
417 std::unique_ptr<std::vector<EventGroup> >
418 EventManager::getGroups(const std::vector<idNum>& groupIds) const
419 {
420  auto pGroupList(allocateOutputContainer<EventGroup>(groupIds.size()));
421  for(const auto& id: groupIds)
422  {
423  auto group = getGroup(id);
424  if (group.id != invalidID)
425  {
426  pGroupList->push_back(group);
427  }
428  }
429  return pGroupList;
430 }
431 
432 //=============================================================================================================
433 
434 EventGroup EventManager::addGroup(const std::string& sGroupName)
435 {
436  EVENTSINTERNAL::EventGroupINT newGroup(generateNewGroupId(), sGroupName);
437  m_GroupsList.emplace(newGroup.getId(), newGroup);
438  return EventGroup(newGroup);
439 }
440 
441 //=============================================================================================================
442 
443 EventGroup EventManager::addGroup(const std::string& sGroupName, const RgbColor& color)
444 {
445  EVENTSINTERNAL::EventGroupINT newGroup(generateNewGroupId(), sGroupName, color);
446  m_GroupsList.emplace(newGroup.getId(), newGroup);
447  return EventGroup(newGroup);
448 }
449 
450 //=============================================================================================================
451 
452 bool EventManager::deleteGroup(const idNum groupId)
453 {
454  bool out(false);
455  auto events = getEventsInGroup(groupId);
456  if(events->empty())
457  {
458  auto groupToDeleteIter = m_GroupsList.find(groupId);
459  if(groupToDeleteIter != m_GroupsList.end())
460  {
461  m_GroupsList.erase(groupToDeleteIter);
462  out = true;
463  if(!m_bDefaultGroupNotCreated && (groupId == m_DefaultGroupId))
464  {
465  m_bDefaultGroupNotCreated = true;
466  m_DefaultGroupId = invalidID;
467  }
468  }
469  }
470  return out;
471 }
472 
473 //=============================================================================================================
474 
475 bool EventManager::deleteGroups(const std::vector<idNum>& groupIds)
476 {
477  bool out(groupIds.size());
478  for(auto g: groupIds)
479  {
480  out = out && deleteGroup(g);
481  }
482  return out;
483 }
484 
485 //=============================================================================================================
486 
487 void EventManager::renameGroup(const idNum groupId, const std::string& newName)
488 {
489  auto group = m_GroupsList.find(groupId);
490  if(group != m_GroupsList.end())
491  {
492  group->second.setName(newName);
493  }
494 }
495 
496 //=============================================================================================================
497 
498 void EventManager::setGroupColor(const idNum groupId, const RgbColor& color)
499 {
500  auto group = m_GroupsList.find(groupId);
501  if( group != m_GroupsList.end())
502  {
503  group->second.setColor(color);
504  }
505 }
506 
507 //=============================================================================================================
508 
509 EventGroup EventManager::mergeGroups(const std::vector<idNum>& groupIds, const std::string& newName)
510 {
511  EVENTSLIB::EventGroup newGroup = addGroup(newName);
512  auto eventsAll = getAllEvents();
513  bool state(true);
514  for(const auto& ev: *eventsAll)
515  {
516  for(auto g: groupIds)
517  {
518  if(ev.groupId == g)
519  {
520  state = state && addEventToGroup(ev.id, newGroup.id);
521  }
522  }
523  }
524  deleteGroups(groupIds);
525  return newGroup;
526 }
527 
528 //=============================================================================================================
529 
530 EventGroup EventManager::duplicateGroup(const idNum groupId, const std::string& newName)
531 {
532  EVENTSLIB::EventGroup newGroup = addGroup(newName);
533  auto eventsToDuplicate = getEventsInGroup(groupId);
534  for( const auto& e: *eventsToDuplicate)
535  {
536  addEvent(e.sample, newGroup.id);
537  }
538  return newGroup;
539 }
540 
541 //=============================================================================================================
542 
543 bool EventManager::addEventToGroup(const idNum eventId, const idNum groupId)
544 {
545  bool state(false);
546  int sample(0);
547  if(m_MapIdToSample.count(eventId))
548  {
549  sample = m_MapIdToSample.at(eventId);
550  auto eventsRange = m_EventsListBySample.equal_range(sample);
551  std::multimap<int, EVENTSINTERNAL::EventINT>::iterator e = eventsRange.first;
552  for(; e != eventsRange.second; ++e)
553  {
554  if( e->second.getId() == eventId)
555  {
556  e->second.setGroupId(groupId);
557  state = true;
558  break;
559  }
560  }
561  }
562  return state;
563 }
564 
565 //=============================================================================================================
566 
567 bool EventManager::addEventsToGroup(const std::vector<idNum>& eventIds, const idNum groupId)
568 {
569  bool state(true);
570  for( idNum id: eventIds)
571  {
572  state = state && addEventToGroup(id, groupId);
573  }
574  return state;
575 }
576 
577 //=============================================================================================================
578 
580 {
581  initSharedMemory(SharedMemoryMode::READ);
582 }
583 
584 //=============================================================================================================
585 
586 void EventManager::initSharedMemory(SharedMemoryMode mode)
587 {
588  m_pSharedMemManager->init(mode);
589 }
590 
591 //=============================================================================================================
592 
594 {
595  m_pSharedMemManager->stop();
596 }
597 
598 //=============================================================================================================
599 
601 {
602  return m_pSharedMemManager->isInit();
603 }
604 
605 void EventManager::createDefaultGroupIfNeeded()
606 {
607  if(m_bDefaultGroupNotCreated)
608  {
609  EventGroup defaultGroup(addGroup(defaultGroupName));
610  m_bDefaultGroupNotCreated = false;
611  m_DefaultGroupId = defaultGroup.id;
612  }
613 }
EVENTSLIB::EventManager::getNumEvents
size_t getNumEvents() const
Definition: eventmanager.cpp:262
EVENTSLIB::EventManager::getEventsInSample
std::unique_ptr< std::vector< Event > > getEventsInSample(int sample) const
Definition: eventmanager.cpp:132
EVENTSLIB::Event
Definition: event.h:71
eventmanager.h
EventManager declaration.
EVENTSLIB::EventManager::getEvent
Event getEvent(idNum eventId) const
Definition: eventmanager.cpp:75
EVENTSLIB::EventManager::isSharedMemoryInit
bool isSharedMemoryInit()
Definition: eventmanager.cpp:600
EVENTSLIB::EventManager::moveEvent
bool moveEvent(idNum eventId, int newSample)
Definition: eventmanager.cpp:293
EVENTSLIB::EventManager::deleteGroup
bool deleteGroup(const idNum groupId)
Definition: eventmanager.cpp:452
EVENTSLIB::EventGroup::id
idNum id
Definition: eventgroup.h:141
EVENTSLIB::EventManager::deleteEventsInGroup
bool deleteEventsInGroup(idNum groupId)
Definition: eventmanager.cpp:360
EVENTSLIB::EventManager::getAllEvents
std::unique_ptr< std::vector< Event > > getAllEvents() const
Definition: eventmanager.cpp:120
EVENTSLIB::EVENTSINTERNAL::EventINT::setSample
void setSample(int iSample)
Definition: event.cpp:125
EVENTSLIB::EventManager::getGroups
std::unique_ptr< std::vector< EventGroup > > getGroups(const std::vector< idNum > &groupIds) const
Definition: eventmanager.cpp:418
EVENTSLIB::EventManager::getAllGroups
std::unique_ptr< std::vector< EventGroup > > getAllGroups() const
Definition: eventmanager.cpp:404
EVENTSLIB::EVENTSINTERNAL::EventINT::getId
idNum getId() const
Definition: event.cpp:146
EVENTSLIB::EventManager::getNumGroups
int getNumGroups() const
Definition: eventmanager.cpp:383
EVENTSLIB::EventManager::getEventsBetween
std::unique_ptr< std::vector< Event > > getEventsBetween(int sampleStart, int sampleEnd) const
Definition: eventmanager.cpp:148
EVENTSLIB::EventGroup
EventGroup class is designed as a data holder for a group. It is designed towards ease of use for a c...
Definition: eventgroup.h:116
EVENTSLIB::EVENTSINTERNAL::EventGroupINT::getId
idNum getId() const
Definition: eventgroup.cpp:197
EVENTSLIB::EventManager::deleteGroups
bool deleteGroups(const std::vector< idNum > &groupIds)
Definition: eventmanager.cpp:475
EVENTSLIB::EventManager::getEventsInGroup
std::unique_ptr< std::vector< Event > > getEventsInGroup(const idNum groupId) const
Definition: eventmanager.cpp:211
EVENTSLIB::EventManager::duplicateGroup
EventGroup duplicateGroup(const idNum groupId, const std::string &newName)
Definition: eventmanager.cpp:530
EVENTSLIB::EventManager::EventManager
EventManager()
Definition: eventmanager.cpp:63
EVENTSLIB::EventManager::getEventsInGroups
std::unique_ptr< std::vector< Event > > getEventsInGroups(const std::vector< idNum > &groupIdsList) const
Definition: eventmanager.cpp:227
std
Definition: event.h:282
EVENTSLIB::EventManager::addEventsToGroup
bool addEventsToGroup(const std::vector< idNum > &eventIds, const idNum groupId)
Definition: eventmanager.cpp:567
EVENTSLIB::EventManager::deleteEvents
bool deleteEvents(const std::vector< idNum > &eventIds)
Definition: eventmanager.cpp:337
EVENTSLIB::EventManager::getEvents
std::unique_ptr< std::vector< Event > > getEvents(const std::vector< idNum > eventIds) const
Definition: eventmanager.cpp:104
EVENTSLIB::EventManager::addGroup
EventGroup addGroup(const std::string &sGroupName)
Definition: eventmanager.cpp:434
EVENTSLIB::EventManager::addEvent
Event addEvent(int sample)
Definition: eventmanager.cpp:285
EVENTSLIB::EventManager::setGroupColor
void setGroupColor(const idNum groupId, const RgbColor &color)
Definition: eventmanager.cpp:498
EVENTSLIB::EVENTSINTERNAL::EventINT::setGroupId
void setGroupId(idNum iGroup)
Definition: event.cpp:139
EVENTSLIB::EventManager::stopSharedMemory
void stopSharedMemory()
Definition: eventmanager.cpp:593
EVENTSLIB::EventManager::getGroup
EventGroup getGroup(idNum groupId) const
Definition: eventmanager.cpp:390
EVENTSLIB::RgbColor
Definition: eventgroup.h:74
EVENTSLIB::EventManager::renameGroup
void renameGroup(const idNum groupId, const std::string &newName)
Definition: eventmanager.cpp:487
EVENTSLIB::EventManager::mergeGroups
EventGroup mergeGroups(const std::vector< idNum > &groupIds, const std::string &newName)
Definition: eventmanager.cpp:509
EVENTSLIB::EVENTSINTERNAL::EventINT
Definition: event.h:115
EVENTSLIB::EventManager::initSharedMemory
void initSharedMemory()
Definition: eventmanager.cpp:579
EVENTSLIB::EventManager::deleteEvent
bool deleteEvent(idNum eventId) noexcept
Definition: eventmanager.cpp:310
EVENTSLIB::EventManager::addEventToGroup
bool addEventToGroup(const idNum eventId, const idNum groupId)
Definition: eventmanager.cpp:543
EVENTSLIB::EVENTSINTERNAL::EventINT::getSample
int getSample() const
Definition: event.cpp:118
EVENTSLIB::EVENTSINTERNAL::EventGroupINT
the class stores the concept of an event group internally in the Event library.
Definition: eventgroup.h:154