Commit 14dfc51f authored by Davide Punzo's avatar Davide Punzo
Browse files

ENH: Adding Click and Drag Action in ChartXY

parent d75f6d07
......@@ -32,6 +32,7 @@ vtkChart::MouseActions::MouseActions()
this->Select() = vtkContextMouseEvent::RIGHT_BUTTON;
this->ZoomAxis() = -1;
this->SelectPolygon() = -1;
this->ClickAndDrag() = -1;
}
//-----------------------------------------------------------------------------
......
......@@ -68,6 +68,7 @@ public:
* SELECT_RECTANGLE - selects points within a rectangle
* SELECT_POLYGON - selects points within a polygon
* SELECT - alias for SELECT_RECTANGLE
* CLICKANDDRAG - move one point selected by a click
* NOTIFY - Post vtkCommand::InteractionEvent on selection of a point
*/
enum {
......@@ -77,6 +78,7 @@ public:
SELECT,
SELECT_RECTANGLE = SELECT,
SELECT_POLYGON,
CLICK_AND_DRAG,
NOTIFY
};
......@@ -449,12 +451,13 @@ protected:
{
public:
MouseActions();
enum { MaxAction = 5 };
enum { MaxAction = 6 };
short& Pan() { return Data[0]; }
short& Zoom() { return Data[1]; }
short& ZoomAxis() { return Data[2]; }
short& Select() { return Data[3]; }
short& SelectPolygon() { return Data[4]; }
short& ClickAndDrag() { return Data[5]; }
short& operator[](int index) { return Data[index]; }
short Data[MaxAction];
};
......
......@@ -180,6 +180,10 @@ vtkChartXY::vtkChartXY()
this->ForceAxesToBounds = false;
this->ZoomWithMouseWheel = true;
this->AdjustLowerBoundForLogPlot = false;
this->DragPoint = false;
this->DragPointAlongX = true;
this->DragPointAlongY = true;
}
//-----------------------------------------------------------------------------
......@@ -818,6 +822,23 @@ void vtkChartXY::RecalculatePlotBounds()
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkChartXY::ReleasePlotSelections()
{
std::vector<vtkPlot*>::iterator it = this->ChartPrivate->plots.begin();
for (; it != this->ChartPrivate->plots.end(); ++it)
{
vtkPlot* plot = *it;
if (!plot)
{
continue;
}
vtkNew<vtkIdTypeArray> emptySelectionArray;
emptySelectionArray->Initialize();
plot->SetSelection(emptySelectionArray.GetPointer());
}
}
//-----------------------------------------------------------------------------
bool vtkChartXY::UpdateLayout(vtkContext2D* painter)
{
......@@ -1608,6 +1629,62 @@ bool vtkChartXY::MouseMoveEvent(const vtkContextMouseEvent& mouse)
this->Scene->SetDirty(true);
}
}
else if (mouse.GetButton() == this->Actions.ClickAndDrag() &&
this->DragPoint && (this->DragPointAlongX || this->DragPointAlongY))
{
// Iterate through each corner, and check for a nearby point
std::vector<vtkContextTransform*>::iterator it = this->ChartPrivate->PlotCorners.begin();
for (; it != this->ChartPrivate->PlotCorners.end(); ++it)
{
vtkContextTransform* plotCorner = *it;
if (!plotCorner)
{
continue;
}
int items = static_cast<int>(plotCorner->GetNumberOfItems());
if (items == 0)
{
continue;
}
vtkVector2f position;
vtkTransform2D* transform = plotCorner->GetTransform();
transform->InverseTransformPoints(mouse.GetPos().GetData(),
position.GetData(), 1);
for (int j = 0; j < items; ++j)
{
vtkPlot* plot = vtkPlot::SafeDownCast(plotCorner->GetItem(j));
if (!plot || plot->IsA("vtkPlotBar"))
{
continue;
}
vtkIdTypeArray* selectionArray = plot->GetSelection();
if (!selectionArray || selectionArray->GetNumberOfValues() < 1)
{
continue;
}
if (selectionArray->GetNumberOfValues() > 1)
{
vtkDebugMacro("Move event (Click and Drag) found more than one point to update.");
}
vtkIdType index = selectionArray->GetValue(0);
if (this->DragPointAlongX)
{
vtkDataArray* xArray = plot->GetData()->GetInputArrayToProcess(0, plot->GetInput());
xArray->SetVariantValue(index, position.GetX());
}
if (this->DragPointAlongY)
{
vtkDataArray* yArray = plot->GetData()->GetInputArrayToProcess(1, plot->GetInput());
yArray->SetVariantValue(index, position.GetY());
}
plot->GetSelection()->Modified();
plot->GetInput()->Modified();
this->Scene->SetDirty(true);
}
}
}
else if (mouse.GetButton() == vtkContextMouseEvent::NO_BUTTON)
{
this->Scene->SetDirty(true);
......@@ -1780,6 +1857,12 @@ bool vtkChartXY::MouseButtonPressEvent(const vtkContextMouseEvent& mouse)
this->DrawSelectionPolygon = true;
return true;
}
else if (mouse.GetButton() == this->Actions.ClickAndDrag())
{
this->ReleasePlotSelections();
this->DragPoint = this->LocatePointInPlots(mouse, vtkCommand::SelectionChangedEvent);
return true;
}
else if (mouse.GetButton() == this->ActionsClick.Select() ||
mouse.GetButton() == this->ActionsClick.Notify())
{
......@@ -2067,6 +2150,12 @@ bool vtkChartXY::MouseButtonReleaseEvent(const vtkContextMouseEvent& mouse)
{
return true;
}
else if (mouse.GetButton() == this->Actions.ClickAndDrag())
{
this->ReleasePlotSelections();
this->DragPoint = false;
return true;
}
return false;
}
......
......@@ -260,6 +260,28 @@ public:
vtkBooleanMacro(AdjustLowerBoundForLogPlot, bool);
//@}
//@{
/**
* Set if the point can be dragged along X
* by the ClickAndDrag Action
* True by default.
*/
vtkSetMacro(DragPointAlongX, bool);
vtkGetMacro(DragPointAlongX, bool);
vtkBooleanMacro(DragPointAlongX, bool);
//@}
//@{
/**
* Set if the point can be dragged along Y
* by the ClickAndDrag Action
* True by default.
*/
vtkSetMacro(DragPointAlongY, bool);
vtkGetMacro(DragPointAlongY, bool);
vtkBooleanMacro(DragPointAlongY, bool);
//@}
/**
* Set the information passed to the tooltip.
*/
......@@ -321,6 +343,11 @@ protected:
*/
void RecalculatePlotBounds();
/**
* Remove all the selection from Plots
*/
void ReleasePlotSelections();
/**
* Update the layout of the chart, this may require the vtkContext2D in order
* to get font metrics etc. Initially this was added to resize the charts
......@@ -424,12 +451,24 @@ protected:
*/
bool AdjustLowerBoundForLogPlot;
/**
* Properties to enable the drag of a point for the ClickAndDrag Action
*/
bool DragPointAlongX;
bool DragPointAlongY;
private:
vtkChartXY(const vtkChartXY&) VTK_DELETE_FUNCTION;
void operator=(const vtkChartXY&) VTK_DELETE_FUNCTION;
vtkChartXYPrivate* ChartPrivate; // Private class where I hide my STL containers
/**
* Internal variable to handle update of drag:
* true if a point has been selected by the user click.
*/
bool DragPoint;
/**
* Figure out the spacing between the bar chart plots, and their offsets.
*/
......
......@@ -486,6 +486,7 @@ bool vtkPlotPoints::SelectPointsInPolygon(const vtkContextPolygon &polygon)
this->Selection->InsertNextValue(pointId);
}
}
this->Selection->Modified();
// return true if we selected any points
return this->Selection->GetNumberOfTuples() > 0;
......
Supports Markdown
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