22 #include <vtkXMLDataElement.h> 23 #include <vtkXMLParser.h> 33 class EventConfigXMLParser :
public vtkXMLParser
36 EventConfigXMLParser(EventConfigPrivate *d);
42 void StartElement(
const char *elementName,
const char **atts)
override;
47 void EndElement(
const char *elementName)
override;
49 std::string ReadXMLStringAttribute(
const std::string &name,
const char **atts);
50 bool ReadXMLBooleanAttribute(
const std::string &name,
const char **atts);
53 EventConfigPrivate *
const d;
59 EventConfigPrivate(
const EventConfigPrivate &other);
63 std::string variantName;
67 typedef std::list<EventMapping> EventListType;
73 void InsertMapping(
const EventMapping &mapping);
75 void CopyMapping(
const EventListType);
89 EventMapping m_CurrEventMapping;
94 EventListType m_EventList;
100 EventConfigXMLParser m_XmlParser;
104 mitk::EventConfigPrivate::EventConfigPrivate()
105 : m_PropertyList(PropertyList::New()), m_EventPropertyList(PropertyList::New()), m_Errors(false), m_XmlParser(this)
108 m_XmlParser.SetReferenceCount(0);
111 mitk::EventConfigPrivate::EventConfigPrivate(
const EventConfigPrivate &other)
112 :
us::SharedData(other),
113 m_PropertyList(other.m_PropertyList->
Clone()),
114 m_EventPropertyList(other.m_EventPropertyList->
Clone()),
115 m_CurrEventMapping(other.m_CurrEventMapping),
116 m_EventList(other.m_EventList),
117 m_Errors(other.m_Errors),
121 m_XmlParser.SetReferenceCount(0);
124 void mitk::EventConfigPrivate::InsertMapping(
const EventMapping &mapping)
126 for (
auto it = m_EventList.begin(); it != m_EventList.end(); ++it)
128 if (*(it->interactionEvent) == *mapping.interactionEvent)
131 m_EventList.erase(it);
135 m_EventList.push_back(mapping);
138 void mitk::EventConfigPrivate::CopyMapping(
const EventListType eventList)
140 EventListType::const_iterator iter;
141 for (iter = eventList.begin(); iter != eventList.end(); ++iter)
143 InsertMapping(*(iter));
147 mitk::EventConfigXMLParser::EventConfigXMLParser(EventConfigPrivate *d) : d(d)
151 void mitk::EventConfigXMLParser::StartElement(
const char *elementName,
const char **atts)
153 std::string name(elementName);
163 d->m_PropertyList->SetStringProperty(name.c_str(), value.c_str());
174 eventVariant.c_str());
175 d->m_CurrEventMapping.variantName = eventVariant;
183 d->m_EventPropertyList->SetStringProperty(name.c_str(), value.c_str());
187 void mitk::EventConfigXMLParser::EndElement(
const char *elementName)
189 const std::string name(elementName);
194 if (event.IsNotNull())
196 d->m_CurrEventMapping.interactionEvent = event;
197 d->InsertMapping(d->m_CurrEventMapping);
201 MITK_WARN <<
"EventConfig: Unknown Event-Type in config. Entry skipped: " << name;
206 std::string mitk::EventConfigXMLParser::ReadXMLStringAttribute(
const std::string &name,
const char **atts)
210 const char **attsIter = atts;
214 if (name == *attsIter)
222 return std::string();
225 bool mitk::EventConfigXMLParser::ReadXMLBooleanAttribute(
const std::string &name,
const char **atts)
227 std::string s = ReadXMLStringAttribute(name, atts);
228 std::transform(s.begin(), s.end(), s.begin(), ::toupper);
243 if (module ==
nullptr)
250 MITK_ERROR <<
"Resource not valid. State machine pattern in module " << module->
GetName()
251 <<
" not found: /Interactions/" << filename;
257 newConfig.d->m_XmlParser.SetStream(&stream);
258 bool success = newConfig.d->m_XmlParser.Parse() && !newConfig.d->m_Errors;
268 newConfig.d->m_XmlParser.SetStream(&inputStream);
269 bool success = newConfig.d->m_XmlParser.Parse() && !newConfig.d->m_Errors;
278 auto it_end = configDescription.end();
279 for (
auto it = configDescription.begin(); it != it_end; ++it)
281 std::string typeVariant;
283 if (typeVariant !=
"")
286 if (event.IsNotNull())
288 d->m_CurrEventMapping.interactionEvent = event;
289 std::string eventVariant;
291 d->m_CurrEventMapping.variantName = eventVariant;
292 d->InsertMapping(d->m_CurrEventMapping);
296 MITK_WARN <<
"EventConfig: Unknown Event-Type in config. When constructing from PropertyList.";
302 if (typeVariant !=
"")
304 std::string name, value;
307 d->m_PropertyList->SetStringProperty(name.c_str(), value.c_str());
325 return !(d->m_EventList.empty() && d->m_PropertyList->IsEmpty());
330 if (module ==
nullptr)
337 MITK_ERROR <<
"Resource not valid. State machine pattern in module " << module->
GetName()
338 <<
" not found: /Interactions/" << fileName;
344 newConfig.d->m_XmlParser.SetStream(&stream);
345 bool success = newConfig.d->m_XmlParser.Parse() && !newConfig.d->m_Errors;
358 d->m_PropertyList->ConcatenatePropertyList(config.d->m_PropertyList->
Clone(),
true);
359 d->m_EventPropertyList = config.d->m_EventPropertyList->
Clone();
360 d->m_CurrEventMapping = config.d->m_CurrEventMapping;
361 d->CopyMapping(config.d->m_EventList);
368 return d->m_PropertyList;
374 if (std::strcmp(interactionEvent->GetNameOfClass(),
"InternalEvent") == 0)
376 auto *internalEvent =
dynamic_cast<InternalEvent *
>(interactionEvent.GetPointer());
380 for (
auto it = d->m_EventList.begin(); it != d->m_EventList.end(); ++it)
382 if (*(it->interactionEvent) == *interactionEvent)
384 return (*it).variantName;
390 if (std::strcmp(interactionEvent->GetNameOfClass(),
"InteractionKeyEvent") == 0)
393 return (
"Std" + keyEvent->GetKey());
400 d->m_PropertyList->Clear();
401 d->m_EventPropertyList->Clear();
402 d->m_CurrEventMapping.variantName.clear();
403 d->m_CurrEventMapping.interactionEvent =
nullptr;
404 d->m_EventList.clear();
static const std::string xmlParameterName()
itk::SmartPointer< Self > Pointer
void ClearConfig()
Reset this EventConfig object, rendering it invalid.
itk::SmartPointer< const Self > ConstPointer
static const std::string xmlParameterValue()
EventConfig()
Constructs an invalid EventConfig object.
static const std::string xmlTagConfigRoot()
std::string GetMappedEvent(const EventType &interactionEvent) const
static const std::string xmlTagParam()
DataCollection - Class to facilitate loading/accessing structured data.
Class to create events from within the application to signal about internal events.
EventConfig & operator=(const EventConfig &other)
static const std::string xmlParameterEventClass()
static InteractionEvent::Pointer CreateEvent(PropertyList::Pointer eventDescription)
bool AddConfig(const std::string &filename, const us::Module *module=nullptr)
This method extends this configuration.
static const std::string xmlParameterEventVariant()
std::string GetSignalName() const
bool IsValid() const
Checks wether this EventConfig object is valid.
Module * GetModule() const
ModuleResource GetResource(const std::string &path) const
static const std::string xmlTagEventVariant()
Handles key events. Takes a std::string for pressed key or special key description, mitk::ModifierKeys for modifiers
Configuration Object for Statemachines.
itk::SmartPointer< Self > Pointer
std::string GetName() const
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
PropertyList::Pointer GetAttributes() const
static const std::string xmlTagAttribute()