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

ENH: Simplified vtkImageItem, added a test for it.

The vtkImageItem class had soem test code in it that shouldn't really
have made it into a release. Simplifying vtkImageItem seems like the
right way to go here, we can add back any missing features later.
parent 76a9cbfd
......@@ -5,6 +5,7 @@ IF (VTK_USE_RENDERING AND VTK_USE_VIEWS)
SET(MyTests
TestContextScene.cxx
TestVector.cxx
TestFreeTypeRender.cxx
)
IF (VTK_DATA_ROOT)
......@@ -13,6 +14,7 @@ IF (VTK_USE_RENDERING AND VTK_USE_VIEWS)
# TestContextUnicode.cxx # Disabled until Freetype unicode works
TestBarGraph.cxx
TestContext.cxx
TestContextImage.cxx
TestDiagram.cxx
TestLinePlot.cxx
TestMarks.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestContext.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 "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkObjectFactory.h"
#include "vtkImageItem.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkPNGReader.h"
#include "vtkImageData.h"
#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
//----------------------------------------------------------------------------
int TestContextImage(int argc, char * argv [])
{
char* logo = vtkTestUtilities::ExpandDataFileName(argc, argv,
"Data/vtk.png");
// Set up a 2D context view, context test object and add it to the scene
vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
view->GetRenderWindow()->SetSize(275, 275);
vtkSmartPointer<vtkImageItem> item = vtkSmartPointer<vtkImageItem>::New();
view->GetScene()->AddItem(item);
vtkSmartPointer<vtkPNGReader> reader = vtkSmartPointer<vtkPNGReader>::New();
reader->SetFileName(logo);
reader->Update();
item->SetImage(vtkImageData::SafeDownCast(reader->GetOutput()));
item->SetPosition(25, 30);
view->GetRenderWindow()->SetMultiSamples(0);
view->GetRenderWindow()->Render();
int retVal = vtkRegressionTestImage(view->GetRenderWindow());
if(retVal == vtkRegressionTester::DO_INTERACTOR)
{
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
}
return !retVal;
}
......@@ -15,19 +15,8 @@
#include "vtkImageItem.h"
// Get my new commands
#include "vtkCommand.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkContextMouseEvent.h"
#include "vtkTransform2D.h"
#include "vtkPen.h"
#include "vtkBrush.h"
#include "vtkTextProperty.h"
#include "vtkImageData.h"
#include "vtkStdString.h"
#include "vtkObjectFactory.h"
//-----------------------------------------------------------------------------
......@@ -39,149 +28,25 @@ vtkCxxSetObjectMacro(vtkImageItem, Image, vtkImageData);
//-----------------------------------------------------------------------------
vtkImageItem::vtkImageItem()
{
this->Label = NULL;
this->Image = NULL;
this->MouseOver = false;
this->MouseButtonPressed = -1;
this->ScalarFunction = NULL;
}
//-----------------------------------------------------------------------------
vtkImageItem::~vtkImageItem()
{
this->SetLabel(NULL);
this->SetImage(NULL);
}
//-----------------------------------------------------------------------------
bool vtkImageItem::Paint(vtkContext2D *painter)
{
// Drawing a hard wired diagram 800x600 as a demonstration of the 2D API
painter->GetTextProp()->SetVerticalJustificationToCentered();
painter->GetTextProp()->SetJustificationToCentered();
painter->GetTextProp()->SetColor(0.0, 0.0, 0.0);
painter->GetTextProp()->SetFontSize(24);
painter->GetPen()->SetColor(0, 0, 0);
if (this->MouseOver)
{
painter->GetBrush()->SetColor(255, 0, 0);
}
else
{
painter->GetBrush()->SetColor(0, 255, 0);
}
painter->DrawRect(this->Dimensions[0], this->Dimensions[1],
this->Dimensions[2], this->Dimensions[3]);
if (this->Image)
{
// Draw our image in the bottom left corner of the item
painter->DrawImage(this->Dimensions[0]+10, this->Dimensions[1]+10, this->Image);
}
if (this->MouseOver && this->Label)
{
painter->GetBrush()->SetColor(255, 200, 0);
painter->DrawRect(this->Dimensions[0]+10, this->Dimensions[1]+50,
100, 20);
painter->GetTextProp()->SetColor(0.0, 0.0, 0.0);
painter->GetTextProp()->SetFontSize(12);
painter->DrawString(this->Dimensions[0]+60, this->Dimensions[1]+60,
this->Label);
}
if (this->ScalarFunction)
{
// We have a function pointer - do something...
;
}
return true;
}
//-----------------------------------------------------------------------------
bool vtkImageItem::Hit(const vtkContextMouseEvent &mouse)
{
if (mouse.Pos[0] > this->Dimensions[0] &&
mouse.Pos[0] < this->Dimensions[0]+this->Dimensions[2] &&
mouse.Pos[1] > this->Dimensions[1] &&
mouse.Pos[1] < this->Dimensions[1]+this->Dimensions[3])
{
return true;
painter->DrawImage(this->Position[0], this->Position[1], this->Image);
}
else
{
return false;
}
}
//-----------------------------------------------------------------------------
bool vtkImageItem::MouseEnterEvent(const vtkContextMouseEvent &)
{
this->MouseOver = true;
return true;
}
//-----------------------------------------------------------------------------
bool vtkImageItem::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
// Work out our deltas...
int deltaX = static_cast<int>(mouse.ScenePos[0] - mouse.LastScenePos[0]);
int deltaY = static_cast<int>(mouse.ScenePos[1] - mouse.LastScenePos[1]);
if (mouse.Button == 0) // Left mouse button - translate
{
// Move the block by this amount
this->Dimensions[0] += deltaX;
this->Dimensions[1] += deltaY;
return true;
}
else if (mouse.Button == 1)
{
// Resize the block by this amount
this->Dimensions[0] += deltaX;
this->Dimensions[1] += deltaY;
this->Dimensions[2] -= deltaX;
this->Dimensions[3] -= deltaY;
return true;
}
else if (mouse.Button == 2)
{
// Resize the block by this amount
this->Dimensions[2] += deltaX;
this->Dimensions[3] += deltaY;
return true;
}
return false;
}
//-----------------------------------------------------------------------------
bool vtkImageItem::MouseLeaveEvent(const vtkContextMouseEvent &)
{
this->MouseOver = false;
return true;
}
//-----------------------------------------------------------------------------
bool vtkImageItem::MouseButtonPressEvent(const vtkContextMouseEvent &)
{
return true;
}
//-----------------------------------------------------------------------------
bool vtkImageItem::MouseButtonReleaseEvent(const vtkContextMouseEvent &)
{
return true;
}
void vtkImageItem::SetScalarFunctor(double (*scalarFunction)(double, double))
{
this->ScalarFunction = scalarFunction;
}
//-----------------------------------------------------------------------------
void vtkImageItem::PrintSelf(ostream &os, vtkIndent indent)
{
......
......@@ -17,15 +17,14 @@
// scene.
//
// .SECTION Description
// This vtkContextItem draws the supplied image in the scene. Optionally showing
// the label as a tooltip on mouse over.
// This vtkContextItem draws the supplied image in the scene.
#ifndef __vtkImageItem_h
#define __vtkImageItem_h
#include "vtkContextItem.h"
#include "vtkSmartPointer.h" // For SP ivars.
class vtkContext2D;
class vtkImageData;
class VTK_CHARTS_EXPORT vtkImageItem : public vtkContextItem
......@@ -40,40 +39,6 @@ public:
// Paint event for the item.
virtual bool Paint(vtkContext2D *painter);
//BTX
// Description:
// Return true if the supplied x, y coordinate is inside the item.
virtual bool Hit(const vtkContextMouseEvent &mouse);
// Description:
// Mouse enter event.
virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse move event.
virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse leave event.
virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse button down event.
virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse button release event.
virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse);
//ETX
// Description:
// Set the mouse over label for the item.
vtkSetStringMacro(Label);
// Description:
// Get the mouse over label for the item.
vtkGetStringMacro(Label);
// Description:
// Set the image of the item.
void SetImage(vtkImageData *image);
......@@ -83,35 +48,22 @@ public:
vtkGetObjectMacro(Image, vtkImageData);
// Description:
// Set the dimensions of the item, bottom corner, width, height.
vtkSetVector4Macro(Dimensions, int);
// Set the position of the bottom corner of the image.
vtkSetVector2Macro(Position, float);
// Description:
// Get the dimensions of the item, bottom corner, width, height.
vtkGetVector4Macro(Dimensions, int);
//BTX
void SetScalarFunctor(double (*scalarFunction)(double, double));
//ETX
// Get the position of the bottom corner of the image.
vtkGetVector2Macro(Position, float);
//BTX
protected:
vtkImageItem();
~vtkImageItem();
int Dimensions[4];
float Position[2];
int LastPosition[2];
char *Label;
vtkImageData *Image;
bool MouseOver;
int MouseButtonPressed;
// Some function pointers to optionally do funky things...
double (*ScalarFunction)(double, double);
private:
vtkImageItem(const vtkImageItem &); // Not implemented.
void operator=(const vtkImageItem &); // Not implemented.
......
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