Commit cfff9102 authored by Mathieu Westphal's avatar Mathieu Westphal
Browse files

Adding a vtkProgressBarRepresentation and Widget

In order to visualize simply progress bar in vtkRenderWindow
this commits add a vtkProgressBarRepresentation and associated
widget to display a progress bar with a progress rate and progress color,
as well as a Background and associated background color
parent 24d00e51
Pipeline #17236 passed with stage
......@@ -95,6 +95,8 @@ set(Module_SRCS
vtkPolygonalHandleRepresentation3D.cxx
vtkPolygonalSurfaceContourLineInterpolator.cxx
vtkPolygonalSurfacePointPlacer.cxx
vtkProgressBarRepresentation.cxx
vtkProgressBarWidget.cxx
vtkProp3DButtonRepresentation.cxx
vtkRectilinearWipeRepresentation.cxx
vtkRectilinearWipeWidget.cxx
......
......@@ -46,6 +46,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests
TestPlaybackWidget.cxx
TestPointHandleRepresentation3D.cxx
TestProgrammaticPlacement.cxx
TestProgressBarWidget.cxx
TestProp3DButtonRepresentation.cxx,NO_VALID
TestRectilinearWipeWidget.cxx
TestResliceCursorWidget2.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestProgressBarWidget.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
//
// This example tests the vtkProgressBarWidget.
// First include the required header files for the VTK classes we are using.
#include "vtkActor.h"
#include "vtkCommand.h"
#include "vtkConeSource.h"
#include "vtkCylinderSource.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkNew.h"
#include "vtkPolyDataMapper.h"
#include "vtkProgressBarRepresentation.h"
#include "vtkProgressBarWidget.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkTestUtilities.h"
int TestProgressBarWidget(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
// Create the RenderWindow, Renderer and both Actors
//
vtkNew<vtkRenderer> ren1;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren1.Get());
vtkNew<vtkInteractorStyleTrackballCamera> style;
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.Get());
iren->SetInteractorStyle(style.Get());
// Create a test pipeline
//
vtkNew<vtkSphereSource> ss;
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(ss->GetOutputPort());
vtkNew<vtkActor> sph;
sph->SetMapper(mapper.Get());
vtkNew<vtkCylinderSource> cs;
vtkNew<vtkPolyDataMapper> csMapper;
csMapper->SetInputConnection(cs->GetOutputPort());
vtkNew<vtkActor> cyl;
cyl->SetMapper(csMapper.Get());
cyl->AddPosition(5, 0, 0);
vtkNew<vtkConeSource> coneSource;
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(coneSource->GetOutputPort());
vtkNew<vtkActor> cone;
cone->SetMapper(coneMapper.Get());
cone->AddPosition(0, 5, 0);
// Create the widget
vtkNew<vtkProgressBarRepresentation> rep;
vtkNew<vtkProgressBarWidget> widget;
widget->SetInteractor(iren.Get());
widget->SetRepresentation(rep.Get());
// Create the widget
vtkNew<vtkProgressBarWidget> widget2;
widget2->SetInteractor(iren.Get());
widget2->CreateDefaultRepresentation();
vtkProgressBarRepresentation* rep2 = vtkProgressBarRepresentation::SafeDownCast(widget2->GetRepresentation());
// Add the actors to the renderer, set the background and size
//
ren1->AddActor(sph.Get());
ren1->AddActor(cyl.Get());
ren1->AddActor(cone.Get());
ren1->SetBackground(0.1, 0.2, 0.4);
renWin->SetSize(300, 300);
// render the image
//
iren->Initialize();
rep->SetProgressRate(0.4);
rep->SetPosition(0.4, 0.4);
rep->SetProgressBarColor(0.2, 0.4, 0);
rep->SetBackgroundColor(1, 1, 0.5);
rep->DrawBackgroundOff();
rep2->SetProgressRate(0.8);
rep2->SetProgressBarColor(0.1, 0.8, 0);
rep2->SetBackgroundColor(1, 1, 0.5);
rep2->DrawBackgroundOn();
renWin->Render();
widget->On();
widget2->On();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkProgressBarRepresentation.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkProgressBarRepresentation.h"
#include "vtkActor2D.h"
#include "vtkCellArray.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkPropCollection.h"
#include "vtkProperty2D.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkUnsignedCharArray.h"
const double PROGRESS_BAR_WIDTH = 12;
const double PROGRESS_BAR_HEIGHT = 2;
vtkStandardNewMacro(vtkProgressBarRepresentation);
vtkProgressBarRepresentation::vtkProgressBarRepresentation()
{
this->ProgressRate = 0;
this->ProgressBarColor[0] = 0;
this->ProgressBarColor[1] = 1;
this->ProgressBarColor[2] = 0;
this->BackgroundColor[0] = 1;
this->BackgroundColor[1] = 1;
this->BackgroundColor[2] = 1;
this->DrawBackground = true;
// Set up the geometry
double size[2];
this->GetSize(size);
this->Position2Coordinate->SetValue(0.04*size[0], 0.04*size[1]);
this->ProportionalResizeOff();
this->Moving = 1;
this->SetShowBorder(vtkBorderRepresentation::BORDER_ACTIVE);
// Create the geometry in canonical coordinates
this->Points = vtkPoints::New();
this->Points->SetDataTypeToDouble();
this->Points->SetNumberOfPoints(10);
this->Points->SetPoint(0, 0.2, 0.2, 0.0);
this->Points->SetPoint(1, 0.2, PROGRESS_BAR_HEIGHT, 0.0);
this->Points->SetPoint(2, PROGRESS_BAR_WIDTH,
PROGRESS_BAR_HEIGHT, 0.0);
this->Points->SetPoint(3, PROGRESS_BAR_WIDTH, 0.2, 0.0);
double progressPoint = 0.2 +
(this->ProgressRate * (PROGRESS_BAR_WIDTH - 0.2));
this->Points->SetPoint(4, 0.2, 0.2, 0.0);
this->Points->SetPoint(5, 0.2, PROGRESS_BAR_HEIGHT, 0.0);
this->Points->SetPoint(6, progressPoint, PROGRESS_BAR_HEIGHT, 0.0);
this->Points->SetPoint(7, progressPoint, 0.2, 0.0);
// Frame
vtkNew<vtkCellArray> lines;
vtkIdType linesIds[5] = {0, 1, 2, 3, 0};
lines->InsertNextCell(5, linesIds);
// Progress bar
vtkNew<vtkCellArray> polys;
vtkIdType polysIds[4] = {4, 5, 6, 7};
polys->InsertNextCell(4, polysIds);
vtkNew<vtkPolyData> polydata;
polydata->SetPoints(this->Points);
polydata->SetLines(lines.Get());
polydata->SetPolys(polys.Get());
// Create cell data to color cells
this->ProgressBarData = vtkUnsignedCharArray::New();
this->ProgressBarData->SetName("Color");
this->ProgressBarData->SetNumberOfComponents(3);
this->ProgressBarData->SetNumberOfTuples(8);
polydata->GetPointData()->SetScalars(this->ProgressBarData);
// Add a transform to position progress bar
// and a mapper and actor
vtkNew<vtkTransformPolyDataFilter> transformFilter;
transformFilter->SetTransform(this->BWTransform);
transformFilter->SetInputData(polydata.Get());
vtkNew<vtkPolyDataMapper2D> mapper;
mapper->SetInputConnection(
transformFilter->GetOutputPort());
this->Property = vtkProperty2D::New();
this->Actor = vtkActor2D::New();
this->Actor->SetMapper(mapper.Get());
this->Actor->SetProperty(this->Property);
// Background cell
vtkNew<vtkCellArray> background;
background->InsertNextCell(4, linesIds);
// Background polydata
vtkNew<vtkPolyData> backgroundPolydata;
backgroundPolydata->SetPoints(this->Points);
backgroundPolydata->SetPolys(background.Get());
// Create cell data to color background
this->BackgroundData = vtkUnsignedCharArray::New();
this->BackgroundData->SetNumberOfComponents(3);
this->BackgroundData->SetNumberOfTuples(4);
backgroundPolydata->GetPointData()->SetScalars(this->BackgroundData);
// Add transform, mapper and actor
vtkNew<vtkTransformPolyDataFilter> backgroundTransformFilter;
backgroundTransformFilter->SetTransform(this->BWTransform);
backgroundTransformFilter->SetInputData(backgroundPolydata.Get());
vtkNew<vtkPolyDataMapper2D> backgroundMapper;
backgroundMapper->SetInputConnection(
backgroundTransformFilter->GetOutputPort());
this->BackgroundActor = vtkActor2D::New();
this->BackgroundActor->SetMapper(backgroundMapper.Get());
}
//-------------------------------------------------------------------------
vtkProgressBarRepresentation::~vtkProgressBarRepresentation()
{
this->Points->Delete();
this->ProgressBarData->Delete();
this->BackgroundData->Delete();
this->Property->Delete();
this->Actor->Delete();
this->BackgroundActor->Delete();
}
//-------------------------------------------------------------------------
void vtkProgressBarRepresentation::BuildRepresentation()
{
// Reposition progress bar points
double progressPoint = 0.2 +
(this->ProgressRate * (PROGRESS_BAR_WIDTH - 0.2));
this->Points->SetPoint(6, progressPoint, PROGRESS_BAR_HEIGHT, 0.0);
this->Points->SetPoint(7, progressPoint, 0.2, 0.0);
this->Points->Modified();
// Set color
double backgroundColor[3] = { this->BackgroundColor[0] * 255,
this->BackgroundColor[1] * 255, this->BackgroundColor[2] * 255};
double progressBarColor[3] = {this->ProgressBarColor[0] * 255,
this->ProgressBarColor[1] * 255, this->ProgressBarColor[2] * 255};
for (int i = 0; i < 4; i++)
{
this->ProgressBarData->SetTuple(i, backgroundColor);
this->BackgroundData->SetTuple(i, backgroundColor);
this->ProgressBarData->SetTuple(i+4, progressBarColor);
}
// Note that the transform is updated by the superclass
this->Superclass::BuildRepresentation();
}
//-------------------------------------------------------------------------
void vtkProgressBarRepresentation::GetSize(double size[2])
{
size[0] = PROGRESS_BAR_WIDTH + 0.2;
size[1] = PROGRESS_BAR_HEIGHT + 0.2;
}
//-------------------------------------------------------------------------
void vtkProgressBarRepresentation::GetActors2D(vtkPropCollection *pc)
{
if (this->DrawBackground)
{
pc->AddItem(this->BackgroundActor);
}
pc->AddItem(this->Actor);
this->Superclass::GetActors2D(pc);
}
//-------------------------------------------------------------------------
void vtkProgressBarRepresentation::ReleaseGraphicsResources(vtkWindow *w)
{
if (this->DrawBackground)
{
this->BackgroundActor->ReleaseGraphicsResources(w);
}
this->Actor->ReleaseGraphicsResources(w);
this->Superclass::ReleaseGraphicsResources(w);
}
//-------------------------------------------------------------------------
int vtkProgressBarRepresentation::RenderOverlay(vtkViewport *w)
{
int count = this->Superclass::RenderOverlay(w);
if (this->DrawBackground)
{
count += this->BackgroundActor->RenderOverlay(w);
}
count += this->Actor->RenderOverlay(w);
return count;
}
//-------------------------------------------------------------------------
int vtkProgressBarRepresentation::RenderOpaqueGeometry(vtkViewport *w)
{
int count = this->Superclass::RenderOpaqueGeometry(w);
if (this->DrawBackground)
{
count += this->BackgroundActor->RenderOpaqueGeometry(w);
}
count += this->Actor->RenderOpaqueGeometry(w);
return count;
}
//-----------------------------------------------------------------------------
int vtkProgressBarRepresentation::RenderTranslucentPolygonalGeometry(
vtkViewport *w)
{
int count = this->Superclass::RenderTranslucentPolygonalGeometry(w);
if (this->DrawBackground)
{
count += this->BackgroundActor->RenderTranslucentPolygonalGeometry(w);
}
count += this->Actor->RenderTranslucentPolygonalGeometry(w);
return count;
}
//-----------------------------------------------------------------------------
int vtkProgressBarRepresentation::HasTranslucentPolygonalGeometry()
{
int result = this->Superclass::HasTranslucentPolygonalGeometry();
if (this->DrawBackground)
{
result |= this->BackgroundActor->HasTranslucentPolygonalGeometry();
}
result |= this->Actor->HasTranslucentPolygonalGeometry();
return result;
}
//-------------------------------------------------------------------------
void vtkProgressBarRepresentation::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
if (this->Property)
{
os << indent << "Property:\n";
this->Property->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << indent << "Property: (none)\n";
}
os << indent << "ProgressRate: " << this->ProgressRate << "\n";
os << indent << "ProgressBarColor: " << this->ProgressBarColor[0] << " "
<< this->ProgressBarColor[1] << " " <<this->ProgressBarColor[2] << "\n";
os << indent << "DrawBackground: " << this->DrawBackground << "\n";
os << indent << "BackgroundColor: " << this->BackgroundColor[0] << " "
<< this->BackgroundColor[1] << " " <<this->BackgroundColor[2] << "\n";
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkProgressBarRepresentation.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkProgressBarRepresentation - represent a vtkProgressBarWidget
// .SECTION Description
// This class is used to represent a vtkProgressBarWidget.
// .SECTION See Also
// vtkProgressBarWidget
#ifndef vtkProgressBarRepresentation_h
#define vtkProgressBarRepresentation_h
#include "vtkInteractionWidgetsModule.h" // For export macro
#include "vtkBorderRepresentation.h"
class vtkActor2D;
class vtkPoints;
class vtkPolyData;
class vtkProperty2D;
class vtkUnsignedCharArray;
class VTKINTERACTIONWIDGETS_EXPORT vtkProgressBarRepresentation : public vtkBorderRepresentation
{
public:
// Description:
// Instantiate this class.
static vtkProgressBarRepresentation *New();
// Description:
// Standard VTK class methods.
vtkTypeMacro(vtkProgressBarRepresentation, vtkBorderRepresentation);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// By obtaining this property you can specify the properties of the
// representation.
vtkGetObjectMacro(Property, vtkProperty2D);
// Description:
// Set/Get the progress rate of the progress bar, between 0 and 1
// default is 0
vtkSetClampMacro(ProgressRate, double, 0, 1);
vtkGetMacro(ProgressRate, double);
// Description:
// Set/Get the progress bar color
// Default is pure green
vtkSetVector3Macro(ProgressBarColor, double);
vtkGetVector3Macro(ProgressBarColor, double);
// Description:
// Set/Get the background color
// Default is white
vtkSetVector3Macro(BackgroundColor, double);
vtkGetVector3Macro(BackgroundColor, double);
// Description:
// Set/Get background visibility
// Default is off
vtkSetMacro(DrawBackground, bool);
vtkGetMacro(DrawBackground, bool);
vtkBooleanMacro(DrawBackground, bool);
// Description:
// Satisfy the superclasses' API.
virtual void BuildRepresentation();
virtual void GetSize(double size[2]);
// Description:
// These methods are necessary to make this representation behave as
// a vtkProp.
virtual void GetActors2D(vtkPropCollection*);
virtual void ReleaseGraphicsResources(vtkWindow*);
virtual int RenderOverlay(vtkViewport*);
virtual int RenderOpaqueGeometry(vtkViewport*);
virtual int RenderTranslucentPolygonalGeometry(vtkViewport*);
virtual int HasTranslucentPolygonalGeometry();
protected:
vtkProgressBarRepresentation();
~vtkProgressBarRepresentation();
double ProgressRate;
double ProgressBarColor[3];
double BackgroundColor[3];
bool DrawBackground;
vtkPoints *Points;
vtkUnsignedCharArray *BackgroundData;
vtkUnsignedCharArray *ProgressBarData;
vtkProperty2D *Property;
vtkActor2D *Actor;
vtkActor2D *BackgroundActor;
private:
vtkProgressBarRepresentation(const vtkProgressBarRepresentation&); //Not implemented
void operator=(const vtkProgressBarRepresentation&); //Not implemented
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkProgressBarWidget.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkProgressBarWidget.h"
#include "vtkProgressBarRepresentation.h"
#include "vtkCallbackCommand.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkProgressBarWidget);
//-------------------------------------------------------------------------
vtkProgressBarWidget::vtkProgressBarWidget()
{
this->Selectable = 0;
}
//-------------------------------------------------------------------------
vtkProgressBarWidget::~vtkProgressBarWidget()
{
}
//----------------------------------------------------------------------
void vtkProgressBarWidget::CreateDefaultRepresentation()
{
if (!this->WidgetRep)
{
this->WidgetRep = vtkProgressBarRepresentation::New();
}
}
//-------------------------------------------------------------------------
void vtkProgressBarWidget::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkProgressBarWidget.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkProgressBarWidget - 2D widget for placing and manipulating a progress bar
// .SECTION Description
// This class provides support for interactively displaying and manipulating
// a progress bar.A Progress bar is defined by a progress rate and the color of the bar and
// its background.
// This widget allows you to interactively place and resize the progress bar.
// To use this widget, simply create a vtkProgressBarRepresentation (or subclass)