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