Skip to content

Commit

Permalink
BUG: Fix issues with sending vtkMRMLTextNode
Browse files Browse the repository at this point in the history
1. Check to see that the text node is not empty before attempting to store it in a string.
2. Invoke TextModifiedEvent when SetText is called so that messages are sent when text is modified.

See https://discourse.slicer.org/t/problem-using-registeroutgoingmrmlnode-in-slicer-4-10/5942
  • Loading branch information
Sunderlandkyl committed Feb 28, 2019
1 parent 308aaf4 commit fc7da7c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
31 changes: 17 additions & 14 deletions OpenIGTLinkIF/MRML/vtkMRMLIGTLConnectorNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ unsigned int vtkMRMLIGTLConnectorNode::vtkInternal::AssignOutGoingNodeToDevice(v
{
igtlioStringDevice* stringDevice = static_cast<igtlioStringDevice*>(device.GetPointer());
vtkMRMLTextNode* textNode = vtkMRMLTextNode::SafeDownCast(node);
igtlioStringConverter::ContentData content = { static_cast<unsigned int>(textNode->GetEncoding()), textNode->GetText() };
std::string text;
if (textNode->GetText())
{
text = textNode->GetText();
}
igtlioStringConverter::ContentData content = { static_cast<unsigned int>(textNode->GetEncoding()), text };
stringDevice->SetContent(content);
modifiedEvent = vtkMRMLTextNode::TextModifiedEvent;
}
Expand Down Expand Up @@ -846,11 +851,11 @@ void vtkMRMLIGTLConnectorNode::ProcessMRMLEvents( vtkObject *caller, unsigned lo
{
return;
}
int n = this->GetNumberOfNodeReferences(this->GetOutgoingNodeReferenceRole());

int n = this->GetNumberOfNodeReferences(this->GetOutgoingNodeReferenceRole());
for (int i = 0; i < n; i ++)
{
const char* id = GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
const char* id = this->GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
if (strcmp(node->GetID(), id) == 0)
{
this->PushNode(node);
Expand Down Expand Up @@ -1236,7 +1241,7 @@ void vtkMRMLIGTLConnectorNode::OnNodeReferenceAdded(vtkMRMLNodeReference *refere
{
igtlioDeviceKeyType key;
key.name = node->GetName();
std::vector<std::string> deviceTypes = GetDeviceTypeFromMRMLNodeType(node->GetNodeTagName());
std::vector<std::string> deviceTypes = this->GetDeviceTypeFromMRMLNodeType(node->GetNodeTagName());
for (size_t typeIndex = 0; typeIndex < deviceTypes.size(); typeIndex++)
{
key.type = deviceTypes[typeIndex];
Expand Down Expand Up @@ -1268,8 +1273,8 @@ void vtkMRMLIGTLConnectorNode::OnNodeReferenceAdded(vtkMRMLNodeReference *refere
return;
}
device->SetMessageDirection(igtlioDevice::MESSAGE_DIRECTION_OUT);
unsigned int NodeModifiedEvent = this->Internal->AssignOutGoingNodeToDevice(node, device);
node->AddObserver(NodeModifiedEvent, this, &vtkMRMLIGTLConnectorNode::ProcessIOConnectorEvents);
unsigned int nodeModifiedEvent = this->Internal->AssignOutGoingNodeToDevice(node, device);
node->AddObserver(nodeModifiedEvent, this, &vtkMRMLIGTLConnectorNode::ProcessIOConnectorEvents);

// Need to update the events here because observed events are not saved in the scene
// for each reference and therefore only the role-default event observers are added.
Expand All @@ -1280,15 +1285,13 @@ void vtkMRMLIGTLConnectorNode::OnNodeReferenceAdded(vtkMRMLNodeReference *refere
int n = this->GetNumberOfNodeReferences(this->GetOutgoingNodeReferenceRole());
for (int i = 0; i < n; i ++)
{
const char* id = GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
const char* id = this->GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
if (strcmp(node->GetID(), id) == 0)
{
vtkIntArray* nodeEvents;
nodeEvents = vtkIntArray::New();
nodeEvents->InsertNextValue(NodeModifiedEvent);
vtkSmartPointer<vtkIntArray> nodeEvents = vtkSmartPointer<vtkIntArray>::New();
nodeEvents->InsertNextValue(nodeModifiedEvent);
this->SetAndObserveNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i,
node->GetID(),nodeEvents );
nodeEvents->Delete();
break;
}
}
Expand Down Expand Up @@ -1483,7 +1486,7 @@ int vtkMRMLIGTLConnectorNode::RegisterOutgoingMRMLNode(vtkMRMLNode* node, const
int n = this->GetNumberOfNodeReferences(this->GetOutgoingNodeReferenceRole());
for (int i = 0; i < n; i ++)
{
const char* id = GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
const char* id = this->GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
if (strcmp(node->GetID(), id) == 0)
{
// Alredy on the list. Remove it.
Expand All @@ -1497,7 +1500,7 @@ int vtkMRMLIGTLConnectorNode::RegisterOutgoingMRMLNode(vtkMRMLNode* node, const
igtlioDevicePointer device = NULL;
igtlioDeviceKeyType key;
key.name = node->GetName();
std::vector<std::string> deviceTypes = GetDeviceTypeFromMRMLNodeType(node->GetNodeTagName());
std::vector<std::string> deviceTypes = this->GetDeviceTypeFromMRMLNodeType(node->GetNodeTagName());
for (size_t typeIndex = 0; typeIndex < deviceTypes.size(); typeIndex++)
{
key.type = deviceTypes[typeIndex];
Expand Down Expand Up @@ -1528,7 +1531,7 @@ int vtkMRMLIGTLConnectorNode::RegisterOutgoingMRMLNode(vtkMRMLNode* node, const
int n = this->GetNumberOfNodeReferences(this->GetOutgoingNodeReferenceRole());
for (int i = 0; i < n; i ++)
{
const char* id = GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
const char* id = this->GetNthNodeReferenceID(this->GetOutgoingNodeReferenceRole(), i);
if (strcmp(node->GetID(), id) == 0)
{
// Alredy on the list. Remove it.
Expand Down
35 changes: 35 additions & 0 deletions OpenIGTLinkIF/MRML/vtkMRMLTextNode.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ vtkMRMLTextNode::~vtkMRMLTextNode()
this->SetText(NULL);
}

//----------------------------------------------------------------------------
void vtkMRMLTextNode::SetText(const char* text)
{
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting Text to " << (text ? text : "(null)")); \
if (this->Text == nullptr && text == nullptr)
{
return;
}
if (this->Text && text && (!strcmp(this->Text, text)))
{
return;
}

delete[] this->Text;
if (text)
{
size_t n = strlen(text) + 1;
char *cp1 = new char[n];
const char *cp2 = (text);
this->Text = cp1;
do
{
*cp1++ = *cp2++;
}
while (--n);
}
else
{
this->Text = nullptr;
}

this->InvokeCustomModifiedEvent(vtkMRMLTextNode::TextModifiedEvent);
this->Modified();
}

//----------------------------------------------------------------------------
void vtkMRMLTextNode::ReadXMLAttributes(const char** atts)
{
Expand Down
2 changes: 1 addition & 1 deletion OpenIGTLinkIF/MRML/vtkMRMLTextNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class VTK_SLICER_OPENIGTLINKIF_MODULE_MRML_EXPORT vtkMRMLTextNode : public vtkM

///
/// Set text encoding
vtkSetStringMacro(Text);
virtual void SetText(const char* text);
vtkGetStringMacro(Text);

///
Expand Down

0 comments on commit fc7da7c

Please sign in to comment.