From 2ffcb731843841803754674df61e7f8bdc1910ce Mon Sep 17 00:00:00 2001
From: Utkarsh Ayachit <utkarsh.ayachit@kitware.com>
Date: Wed, 24 Aug 2016 12:22:03 -0400
Subject: [PATCH] Fix order of representations in a view.

vtkView keeps representations provided to `AddRepresentation` call in an
internal datastructure. If a representation was comprised of other
internal representations and it called vtkView::AddRepresentation() then
the order in which the representations would get stored in the internal
datastructure did not match the order in which `AddRepresentation` was
called. Not a big deal, except in cases where the internal
representation expects the outer representation to have been updated (as
was was the case with #16832).

Fixed vtkView::AddRepresentation() to maintain the order in internal
datastructure to match the order in which AddRepresentation is called.

Fixes #16832.
---
 Views/Core/vtkView.cxx | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/Views/Core/vtkView.cxx b/Views/Core/vtkView.cxx
index 7ac032c8ed..499eddca8e 100644
--- a/Views/Core/vtkView.cxx
+++ b/Views/Core/vtkView.cxx
@@ -186,6 +186,14 @@ void vtkView::AddRepresentation(vtkDataRepresentation* rep)
 {
   if (rep != NULL && !this->IsRepresentationPresent(rep))
     {
+    // We add the representation to the internal data-structure before calling
+    // AddToView(). This ensures that if the `rep` itself calls
+    // AddRepresentation() for an internal representation, the internal
+    // representation gets added after the `rep` which makes more sense as it
+    // preserves the order for representations in which AddRepresentation() was
+    // called.
+    size_t index = this->Implementation->Representations.size();
+    this->Implementation->Representations.push_back(rep);
     if (rep->AddToView(this))
       {
       rep->AddObserver(vtkCommand::SelectionChangedEvent, this->GetObserver());
@@ -196,7 +204,11 @@ void vtkView::AddRepresentation(vtkDataRepresentation* rep)
       rep->AddObserver(vtkCommand::UpdateEvent, this->GetObserver());
 
       this->AddRepresentationInternal(rep);
-      this->Implementation->Representations.push_back(rep);
+      }
+    else
+      {
+      this->Implementation->Representations.erase(
+        this->Implementation->Representations.begin() + index);
       }
     }
 }
-- 
GitLab