Commit 0c9f4a63 authored by Marcus D. Hanwell's avatar Marcus D. Hanwell
Browse files

ENH: Refactoring the context item - improved API.

First stage of refactoring the vtkContextItem API. The implicit
transform has been removed, to be replaced with a transform item.
parent 28668f88
......@@ -16,6 +16,7 @@ SET(KIT_LIBS vtkIO vtkftgl
SET(Kit_SRCS
vtkAbstractContextBufferId.cxx
vtkAbstractContextItem.cxx
vtkAxis.cxx
vtkBarMark.cxx
vtkBlockItem.cxx
......@@ -56,6 +57,7 @@ SET(Kit_SRCS
SET_SOURCE_FILES_PROPERTIES(
vtkAbstractContextBufferId
vtkAbstractContextItem
vtkChart
vtkContextDevice2D
vtkContextItem
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAbstractContextItem.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 "vtkAbstractContextItem.h"
#include "vtkObjectFactory.h"
//-----------------------------------------------------------------------------
vtkAbstractContextItem::vtkAbstractContextItem()
{
}
//-----------------------------------------------------------------------------
vtkAbstractContextItem::~vtkAbstractContextItem()
{
}
//-----------------------------------------------------------------------------
void vtkAbstractContextItem::Update()
{
}
//-----------------------------------------------------------------------------
void vtkAbstractContextItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkContextItem.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 vtkAbstractContextItem - base class for items that are part of a
// vtkContextScene.
//
// .SECTION Description
// This class is the common base for all context scene items. You should
// generally derive from vtkContextItem, rather than this class, as it provides
// most of the commonly used API.
#ifndef __vtkAbstractContextItem_h
#define __vtkAbstractContextItem_h
#include "vtkObject.h"
class vtkContext2D;
class VTK_CHARTS_EXPORT vtkAbstractContextItem : public vtkObject
{
public:
vtkTypeMacro(vtkAbstractContextItem, vtkObject);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Perform any updates to the item that may be necessary before rendering.
// The scene should take care of calling this on all items before their
// Paint function is invoked.
virtual void Update();
// Description:
// Paint event for the item, called whenever the item needs to be drawn.
virtual bool Paint(vtkContext2D *painter) = 0;
//BTX
protected:
vtkAbstractContextItem();
~vtkAbstractContextItem();
private:
vtkAbstractContextItem(const vtkAbstractContextItem &); // Not implemented.
void operator=(const vtkAbstractContextItem &); // Not implemented.
//ETX
};
#endif //__vtkContextItem_h
......@@ -18,20 +18,14 @@
// Get my new commands
#include "vtkCommand.h"
#include "vtkAnnotationLink.h"
#include "vtkInteractorStyle.h"
#include "vtkInteractorStyleRubberBand2D.h"
#include "vtkTransform2D.h"
#include "vtkContextScene.h"
#include "vtkObjectFactory.h"
//-----------------------------------------------------------------------------
vtkCxxSetObjectMacro(vtkContextItem, Transform, vtkTransform2D);
//-----------------------------------------------------------------------------
vtkContextItem::vtkContextItem()
{
this->Transform = NULL;//vtkTransform2D::New();
this->Scene = NULL;
this->Visible = true;
this->Opacity = 1.0;
......@@ -40,19 +34,9 @@ vtkContextItem::vtkContextItem()
//-----------------------------------------------------------------------------
vtkContextItem::~vtkContextItem()
{
if (this->Transform)
{
this->Transform->Delete();
this->Transform = NULL;
}
this->SetScene(NULL);
}
//-----------------------------------------------------------------------------
void vtkContextItem::Update()
{
}
//-----------------------------------------------------------------------------
void vtkContextItem::SetScene(vtkContextScene *scene)
{
......@@ -110,16 +94,6 @@ bool vtkContextItem::MouseWheelEvent(const vtkContextMouseEvent &, int)
return false;
}
//-----------------------------------------------------------------------------
void vtkContextItem::Translate(float dx, float dy)
{
if (!this->Transform)
{
this->Transform = vtkTransform2D::New();
}
this->Transform->Translate(dx, dy);
}
// ----------------------------------------------------------------------------
void vtkContextItem::ReleaseGraphicsResources()
{
......
......@@ -22,7 +22,7 @@
#ifndef __vtkContextItem_h
#define __vtkContextItem_h
#include "vtkObject.h"
#include "vtkAbstractContextItem.h"
#include "vtkWeakPointer.h" // Needed for weak pointer references to the scene
class vtkContext2D;
......@@ -30,22 +30,12 @@ class vtkContextScene;
class vtkTransform2D;
class vtkContextMouseEvent;
class VTK_CHARTS_EXPORT vtkContextItem : public vtkObject
class VTK_CHARTS_EXPORT vtkContextItem : public vtkAbstractContextItem
{
public:
vtkTypeMacro(vtkContextItem, vtkObject);
vtkTypeMacro(vtkContextItem, vtkAbstractContextItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Perform any updates to the item that may be necessary before rendering.
// The scene should take care of calling this on all items before their
// Paint function is invoked.
virtual void Update();
// Description:
// Paint event for the item, called whenever the item needs to be drawn,
virtual bool Paint(vtkContext2D *painter) = 0;
//BTX
// Description:
// Return true if the supplied x, y coordinate is inside the item.
......@@ -88,14 +78,6 @@ public:
virtual bool MouseWheelEvent(const vtkContextMouseEvent &mouse, int delta);
//ETX
// Description:
// Set the transform for the item.
virtual void SetTransform(vtkTransform2D *transform);
// Description:
// Set the transform for the item.
vtkGetObjectMacro(Transform, vtkTransform2D);
// Description:
// Get the visibility of the item (should it be drawn).
vtkGetMacro(Visible, bool);
......@@ -112,10 +94,6 @@ public:
// Set the opacity of the item.
vtkSetMacro(Opacity, double);
// Description:
// Translate the item by the given dx, dy.
void Translate(float dx, float dy);
// Description:
// Set the vtkContextScene for the item, always set for an item in a scene.
virtual void SetScene(vtkContextScene *scene);
......@@ -123,24 +101,23 @@ public:
// Description:
// Get the vtkContextScene for the item, always set for an item in a scene.
vtkContextScene* GetScene();
// Description:
// Release graphics resources hold by the item. The default implementation
// is empty.
virtual void ReleaseGraphicsResources();
//BTX
protected:
vtkContextItem();
~vtkContextItem();
vtkTransform2D *Transform;
vtkWeakPointer<vtkContextScene> Scene;
bool Visible;
double Opacity;
private:
vtkContextItem(const vtkContextItem &); // Not implemented.
void operator=(const vtkContextItem &); // Not implemented.
......
......@@ -202,7 +202,6 @@ bool vtkContextScene::Paint(vtkContext2D *painter)
}
for (size_t i = 0; i < size; ++i)
{
painter->SetTransform(this->Storage->items[i]->GetTransform());
this->Storage->items[i]->Paint(painter);
}
if (size && this->Transform)
......@@ -231,7 +230,6 @@ void vtkContextScene::PaintIds()
}
for (size_t i = 0; i < size; ++i)
{
this->LastPainter->SetTransform(this->Storage->items[i]->GetTransform());
this->LastPainter->ApplyId(i+1);
this->Storage->items[i]->Paint(this->LastPainter);
}
......@@ -523,7 +521,6 @@ void vtkContextScene::MouseMoveEvent(int x, int y)
// Don't send the mouse move event twice...
continue;
}
this->PerformTransform(this->Storage->items[i]->GetTransform(), event);
if (i==pickedItem)
{
......@@ -550,9 +547,6 @@ void vtkContextScene::MouseMoveEvent(int x, int y)
// Check if there is a selected item that needs to receive a move event
if (this->Storage->itemMousePressCurrent >= 0)
{
this->PerformTransform(
this->Storage->items[this->Storage->itemMousePressCurrent]->GetTransform(),
event);
this->Storage->items[this->Storage->itemMousePressCurrent]->MouseMoveEvent(event);
}
else
......@@ -587,7 +581,6 @@ void vtkContextScene::ButtonPressEvent(int button, int x, int y)
event.Button = button;
for (int i = size-1; i >= 0; --i)
{
this->PerformTransform(this->Storage->items[i]->GetTransform(), event);
if (this->Storage->items[i]->Hit(event))
{
if (this->Storage->items[i]->MouseButtonPressEvent(event))
......@@ -611,10 +604,8 @@ void vtkContextScene::ButtonReleaseEvent(int button, int x, int y)
event.ScenePos[0] = x;
event.ScenePos[1] = y;
event.Button = button;
this->PerformTransform(
this->Storage->items[this->Storage->itemMousePressCurrent]->GetTransform(),
event);
this->Storage->items[this->Storage->itemMousePressCurrent]->MouseButtonReleaseEvent(event);
this->Storage->items[this->Storage->itemMousePressCurrent]
->MouseButtonReleaseEvent(event);
this->Storage->itemMousePressCurrent = -1;
}
this->Storage->Event.Button = -1;
......@@ -631,7 +622,6 @@ void vtkContextScene::MouseWheelEvent(int delta, int x, int y)
//event.Button = 1;
for (int i = size-1; i >= 0; --i)
{
this->PerformTransform(this->Storage->items[i]->GetTransform(), event);
if (this->Storage->items[i]->Hit(event))
{
if (this->Storage->items[i]->MouseWheelEvent(event, delta))
......
......@@ -44,9 +44,9 @@ vtkWedgeMark::vtkWedgeMark()
this->MouseOver=false;
this->ActiveItem=-1;
this->PaintIdMode=false;
// this->Information created in vtkMark
// add the default keys.
this->AddWedgeDefault();
}
......@@ -65,12 +65,11 @@ void vtkWedgeMark::PaintIds()
{
assert("pre: not_yet" && !this->PaintIdMode);
vtkDebugMacro("PaintId called.");
this->Scene->GetLastPainter()->SetTransform(this->GetTransform());
this->PaintIdMode=true;
this->Paint(this->Scene->GetLastPainter());
this->PaintIdMode=false;
assert("post: done" && !this->PaintIdMode);
}
......@@ -78,10 +77,10 @@ void vtkWedgeMark::PaintIds()
void vtkWedgeMark::UpdateBufferId()
{
vtkAbstractContextBufferId *bi=this->Scene->GetBufferId();
int width=bi->GetWidth();
int height=bi->GetHeight();
if(this->BufferId==0 || width!=this->BufferId->GetWidth() ||
height!=this->BufferId->GetHeight())
{
......@@ -96,7 +95,7 @@ void vtkWedgeMark::UpdateBufferId()
this->BufferId->SetWidth(width);
this->BufferId->SetHeight(height);
this->BufferId->Allocate();
this->Scene->GetLastPainter()->BufferIdModeBegin(this->BufferId);
this->PaintIds();
this->Scene->GetLastPainter()->BufferIdModeEnd();
......@@ -107,9 +106,9 @@ void vtkWedgeMark::UpdateBufferId()
vtkIdType vtkWedgeMark::GetPickedItem(int x, int y)
{
this->UpdateBufferId();
vtkIdType result=this->BufferId->GetPickedItem(x,y);
assert("post: valid_result" && result>=-1 &&
result<this->Data.GetData(this).GetNumberOfChildren());
return result;
......@@ -127,10 +126,10 @@ bool vtkWedgeMark::MouseEnterEvent(const vtkContextMouseEvent &
bool vtkWedgeMark::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
bool result=false;
// we can have this->MouseOver false if the enter event have been caught
// previously by another context item.
if(this->MouseOver)
{
vtkIdType numChildren=this->Data.GetData(this).GetNumberOfChildren();
......@@ -138,7 +137,7 @@ bool vtkWedgeMark::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
vtkIdType pickedItem=this->GetPickedItem(mouse.ScreenPos[0],
mouse.ScreenPos[1]);
if(pickedItem!=-1)
{
// cout << "picked sector is"<< pickedItem << endl;
......@@ -157,7 +156,7 @@ bool vtkWedgeMark::MouseMoveEvent(const vtkContextMouseEvent &mouse)
}
}
}
return result;
}
......@@ -185,28 +184,28 @@ void vtkWedgeMark::MouseLeaveEventOnSector(int sector)
bool vtkWedgeMark::Hit(const vtkContextMouseEvent &mouse)
{
bool result;
// each sector can have different left/bottom outer/inner radius, so we
// can't just try to hit a big bounding box first.
double* left = this->Left.GetArray(this);
double* bottom = this->Bottom.GetArray(this);
double* lineWidth = this->LineWidth.GetArray(this);
double *outerRadius=this->OuterRadius.GetArray(this);
double *innerRadius=this->InnerRadius.GetArray(this);
double *angle=this->Angle.GetArray(this);
vtkIdType numChildren=this->Data.GetData(this).GetNumberOfChildren();
double a0=0.0;
double a1=0.0;
vtkIdType i=0;
result=false;
while(!result && i<numChildren)
{
a0=a1;
a1=angle[i]+a0;
// four tests to test if the hit is inside:
// 1. Is it outside the circle of center (left,bottom) and radius
// innerRadius-lineWidth?
......@@ -217,16 +216,16 @@ bool vtkWedgeMark::Hit(const vtkContextMouseEvent &mouse)
// 4. Is it on the negative side of the start line shifted by lineWidth on
// the negative side?
// hit=test1 && test2 && test3 && test4.
// Test1
double dx=mouse.Pos[0]-left[i];
double dy=mouse.Pos[1]-bottom[i];
double dot=dx*dx+dy*dy;
double r=innerRadius[i]-lineWidth[i];
// distance between center and point is greater than radius
result=dot>=r*r;
if(result)
{
// Test 2
......@@ -236,23 +235,23 @@ bool vtkWedgeMark::Hit(const vtkContextMouseEvent &mouse)
{
// Test 3
double a0r=vtkMath::RadiansFromDegrees(a0);
result=cos(a0r)*dy-sin(a0r)*dx+lineWidth[i]>=0;
if(result)
{
// Test 4
double a1r=vtkMath::RadiansFromDegrees(a1);
result=cos(a1r)*dy-sin(a1r)*dx-lineWidth[i]<=0;
}
}
}
++i;
}
// cout << "wedge hit=" << result << endl;
return result;
}
......@@ -262,7 +261,7 @@ void vtkWedgeMark::AddWedgeDefault()
// 1. fill style is a categorial color
this->Fields->Set(vtkWedgeMark::FILL_STYLE(),"categorial");
// 2. no stroke
this->SetLineColor(vtkMarkUtil::DefaultSeriesColorFromIndex);
this->SetFillColor(vtkMarkUtil::DefaultSeriesColorFromIndex);
}
......@@ -278,7 +277,7 @@ double vtkWedgeMark::GetMidAngle()
{
return 0.0;
}
// ----------------------------------------------------------------------------
double vtkWedgeMark::GetMidRadius()
{
......@@ -288,7 +287,7 @@ double vtkWedgeMark::GetMidRadius()
// }
return 0.0;
}
//-----------------------------------------------------------------------------
bool vtkWedgeMark::Paint(vtkContext2D *painter)
{
......@@ -297,15 +296,15 @@ bool vtkWedgeMark::Paint(vtkContext2D *painter)
vtkColor* fillColor = this->FillColor.GetArray(this);
vtkColor* lineColor = this->LineColor.GetArray(this);
double* lineWidth = this->LineWidth.GetArray(this);
double *outerRadius=this->OuterRadius.GetArray(this);
double *innerRadius=this->InnerRadius.GetArray(this);
// double *startAngle=this->StartAngle.GetArray(this);
// double *stopAngle=this->StopAngle.GetArray(this);
double *angle=this->Angle.GetArray(this);
vtkIdType numChildren = this->Data.GetData(this).GetNumberOfChildren();
if(this->PaintIdMode)
{
if(numChildren>16777214) // 24-bit limit, 0 reserved for background encoding.
......@@ -313,16 +312,16 @@ bool vtkWedgeMark::Paint(vtkContext2D *painter)
vtkWarningMacro(<<"picking will not work properly as there are too many children. Children over 16777214 will be ignored.");
numChildren=16777214;
}
}
double a0=0.0;
double a1=0.0;
for (vtkIdType i = 0; i < numChildren; ++i)
{
a0=a1;
a1=angle[i]+a0;
painter->GetBrush()->SetColorF(fillColor[i].Red,
fillColor[i].Green,
fillColor[i].Blue,
......@@ -331,10 +330,10 @@ bool vtkWedgeMark::Paint(vtkContext2D *painter)
{
painter->ApplyId(i+1);
}
painter->DrawWedge(left[i],bottom[i],outerRadius[i],innerRadius[i],
a0,a1);
if (lineWidth[i] > 0.0)
{
painter->GetPen()->SetWidth(lineWidth[i]);
......@@ -342,10 +341,10 @@ bool vtkWedgeMark::Paint(vtkContext2D *painter)
lineColor[i].Green,
lineColor[i].Blue,
lineColor[i].Alpha);
double a0r=vtkMath::RadiansFromDegrees(a0);
double a1r=vtkMath::RadiansFromDegrees(a1);
// bottom line of the wedge
painter->DrawLine(left[i]+innerRadius[i]*cos(a0r),
bottom[i]+innerRadius[i]*sin(a0r),
......@@ -356,7 +355,7 @@ bool vtkWedgeMark::Paint(vtkContext2D *painter)
bottom[i]+innerRadius[i]*sin(a1r),
left[i]+outerRadius[i]*cos(a1r),
bottom[i]+outerRadius[i]*sin(a1r));
// inside arc
painter->DrawArc(left[i],bottom[i],innerRadius[i],a0,a1);
// outside arc
......
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