Commit a6b0b0df authored by Utkarsh Ayachit's avatar Utkarsh Ayachit

BUG #14283. Fix change events fired by vtkControlPointsItem.

This commit consolidates the logic between all vtkControlPointsItem subclasses
to consistently notify rest of the world that the control points were modified.
Previously, commit 1509669d tried to do the same with
vtkControlPointsItem::PointsModifiedEvent, however vtkCompositeControlPointsItem
wasn't firing those consistently resulting in the BUG #14283.

While attempting to fix that, I noticed that vtkControlPointsItem has
StartChanges()/EndChanges() methods that could be used for a similar purpose:
notify the world that control points are being changed. Hence, remved the
PointsModifiedEvent, instead fixing the code to consistently use
StartChanges()/EndChanges() calls.

Fixed StartChanges/EndChanges to fire the event on the vtkControlPointsItem
(along with tranfer function, as it was previously) and added logic to avoid
duplicable firing of events due nested calls to StartChanges/EndChanges.

Change-Id: I144e775cbdfbc9c1334cfc2b02609b37dda8cae7
parent 8cfc1bf2
......@@ -114,8 +114,8 @@ int TestControlPointsItemEvents(int, char*[])
cbk->EventSpy[vtkCommand::StartInteractionEvent] != 1 ||
cbk->EventSpy[vtkCommand::InteractionEvent] != 1 ||
cbk->EventSpy[vtkCommand::EndInteractionEvent] != 1 ||
cbk->EventSpy[vtkCommand::StartEvent] != 0 ||
cbk->EventSpy[vtkCommand::EndEvent] != 0)
cbk->EventSpy[vtkCommand::StartEvent] != 2 ||
cbk->EventSpy[vtkCommand::EndEvent] != 2)
{
std::cerr << "Wrong number of fired events : "
<< cbk->EventSpy[vtkCommand::ModifiedEvent] << " "
......
......@@ -158,8 +158,9 @@ void vtkColorTransferControlPointsItem::SetControlPoint(vtkIdType index, double*
xrgbms[0] = newPos[0];
xrgbms[4] = newPos[2];
xrgbms[5] = newPos[3];
this->StartChanges();
this->ColorTransferFunction->SetNodeValue(index, xrgbms);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
this->EndChanges();
}
}
......@@ -170,6 +171,9 @@ void vtkColorTransferControlPointsItem::EditPoint(float tX, float tY)
{
return;
}
this->StartChanges();
double xrgbms[6];
this->ColorTransferFunction->GetNodeValue(this->CurrentPoint, xrgbms);
xrgbms[4] += tX;
......@@ -181,8 +185,9 @@ void vtkColorTransferControlPointsItem::EditPoint(float tX, float tY)
xrgbms[4] += tX;
xrgbms[5] += tY;
this->ColorTransferFunction->SetNodeValue(this->CurrentPoint - 1, xrgbms);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
}
this->EndChanges();
}
//-----------------------------------------------------------------------------
......@@ -193,13 +198,16 @@ vtkIdType vtkColorTransferControlPointsItem::AddPoint(double* newPos)
return -1;
}
this->StartChanges();
double posX = newPos[0];
double rgb[3] = {0., 0., 0.};
this->ColorTransferFunction->GetColor(posX, rgb);
vtkIdType addedPoint =
this->ColorTransferFunction->AddRGBPoint(posX, rgb[0], rgb[1], rgb[2]);
this->vtkControlPointsItem::AddPointId(addedPoint);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
this->EndChanges();
return addedPoint;
}
......@@ -211,6 +219,9 @@ vtkIdType vtkColorTransferControlPointsItem::RemovePoint(double* currentPoint)
{
return -1;
}
this->StartChanges();
#ifndef NDEBUG
vtkIdType expectedPoint =
#endif
......@@ -218,7 +229,8 @@ vtkIdType vtkColorTransferControlPointsItem::RemovePoint(double* currentPoint)
vtkIdType removedPoint =
this->ColorTransferFunction->RemovePoint(currentPoint[0]);
assert(removedPoint == expectedPoint);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
this->EndChanges();
return removedPoint;
}
......
......@@ -213,7 +213,9 @@ void vtkCompositeControlPointsItem::SetControlPoint(vtkIdType index, double* new
(this->PointsFunction == OpacityPointsFunction ||
this->PointsFunction == ColorAndOpacityPointsFunction))
{
this->StartChanges();
this->OpacityFunction->SetNodeValue(index, newPos);
this->EndChanges();
}
}
......@@ -247,6 +249,7 @@ void vtkCompositeControlPointsItem::EditPoint(float tX, float tY)
(this->PointsFunction == ColorPointsFunction ||
this->PointsFunction == ColorAndOpacityPointsFunction))
{
this->StartChanges();
double xvms[4];
this->OpacityFunction->GetNodeValue(this->CurrentPoint, xvms);
xvms[2] += tX;
......@@ -260,6 +263,7 @@ void vtkCompositeControlPointsItem::EditPoint(float tX, float tY)
xvms[3] += tY;
this->OpacityFunction->SetNodeValue(this->CurrentPoint - 1, xvms);
}
this->EndChanges();
}
}
......
......@@ -57,6 +57,7 @@ vtkControlPointsItem::vtkControlPointsItem()
this->BlockUpdates = 0;
this->StartedInteractions = 0;
this->StartedChanges = 0;
this->Callback = vtkCallbackCommand::New();
this->Callback->SetClientData(this);
......@@ -231,13 +232,24 @@ bool vtkControlPointsItem::Paint(vtkContext2D* painter)
//-----------------------------------------------------------------------------
void vtkControlPointsItem::StartChanges()
{
this->emitEvent(vtkCommand::StartEvent);
++this->StartedChanges;
if (this->StartedChanges == 1)
{
this->InvokeEvent(vtkCommand::StartEvent);
this->emitEvent(vtkCommand::StartEvent);
}
}
//-----------------------------------------------------------------------------
void vtkControlPointsItem::EndChanges()
{
this->emitEvent(vtkCommand::EndEvent);
--this->StartedChanges;
assert(this->StartedChanges >=0);
if (this->StartedChanges == 0)
{
this->emitEvent(vtkCommand::EndEvent);
this->InvokeEvent(vtkCommand::EndEvent);
}
}
//-----------------------------------------------------------------------------
......@@ -937,6 +949,8 @@ vtkIdType vtkControlPointsItem::RemovePointId(vtkIdType pointId)
return pointId;
}
this->StartChanges();
assert(pointId != -1);
// Useless to remove the point here as it will be removed anyway in ComputePoints
this->DeselectPoint(pointId);
......@@ -956,6 +970,8 @@ vtkIdType vtkControlPointsItem::RemovePointId(vtkIdType pointId)
{
this->SetCurrentPoint(this->CurrentPoint - 1);
}
this->EndChanges();
return pointId;
}
......@@ -1252,6 +1268,9 @@ vtkIdType vtkControlPointsItem::SetPointPos(vtkIdType point, const vtkVector2f&
this->GetControlPoint(point, currentPoint);
currentPoint[0] = boundedPos[0];
currentPoint[1] = boundedPos[1];
// SetControlPoint will call StartChanges/EndChanges correctly, so we don't
// need to call it here.
this->SetControlPoint(point, currentPoint);
return point;
}
......
......@@ -41,11 +41,15 @@ public:
vtkTypeMacro(vtkControlPointsItem, vtkPlot);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Events fires by this class (and subclasses).
// \li CurrentPointChangedEvent is fired when the current point index is changed.
// \li CurrentPointEditEvent is fired to request the application to show UI to
// edit the current point.
// \li vtkCommand::StartEvent and vtkCommand::EndEvent is fired
// to mark groups of changes to control points.
enum {
CurrentPointChangedEvent = vtkCommand::UserEvent,
CurrentPointEditEvent,
PointsModifiedEvent // fired when the control points are changed by
// user-interaction.
};
// Description:
......@@ -380,6 +384,7 @@ protected:
vtkBrush* SelectedPointBrush;
int BlockUpdates;
int StartedInteractions;
int StartedChanges;
vtkIdType CurrentPoint;
double Bounds[4];
......
......@@ -130,8 +130,9 @@ void vtkPiecewiseControlPointsItem::SetControlPoint(vtkIdType index, double* new
if (newPos[0] != oldPos[0] || newPos[1] != oldPos[1] ||
newPos[2] != oldPos[2])
{
this->StartChanges();
this->PiecewiseFunction->SetNodeValue(index, newPos);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
this->EndChanges();
}
}
......@@ -142,6 +143,9 @@ void vtkPiecewiseControlPointsItem::EditPoint(float tX, float tY)
{
return;
}
this->StartChanges();
double xvms[4];
this->PiecewiseFunction->GetNodeValue(this->CurrentPoint, xvms);
xvms[2] += tX;
......@@ -153,8 +157,9 @@ void vtkPiecewiseControlPointsItem::EditPoint(float tX, float tY)
xvms[2] += tX;
xvms[3] += tY;
this->PiecewiseFunction->SetNodeValue(this->CurrentPoint - 1, xvms);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
}
this->EndChanges();
}
//-----------------------------------------------------------------------------
......@@ -164,9 +169,12 @@ vtkIdType vtkPiecewiseControlPointsItem::AddPoint(double* newPos)
{
return -1;
}
this->StartChanges();
vtkIdType addedPoint = this->PiecewiseFunction->AddPoint(newPos[0], newPos[1]);
this->Superclass::AddPointId(addedPoint);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
this->EndChanges();
return addedPoint;
}
......@@ -181,6 +189,9 @@ vtkIdType vtkPiecewiseControlPointsItem::RemovePoint(double* currentPoint)
{
return -1;
}
this->StartChanges();
#ifndef NDEBUG
vtkIdType expectedPoint =
#endif
......@@ -188,6 +199,8 @@ vtkIdType vtkPiecewiseControlPointsItem::RemovePoint(double* currentPoint)
vtkIdType removedPoint =
this->PiecewiseFunction->RemovePoint(currentPoint[0]);
assert(removedPoint == expectedPoint);
this->InvokeEvent(vtkControlPointsItem::PointsModifiedEvent);
this->EndChanges();
return removedPoint;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment