Commit 040674e4 authored by Marcus Hanwell's avatar Marcus Hanwell
Browse files

ENH: Implemented saving of screenshots.

The vtkContextView based charts need image saving for both the UI and
to add image based testing of the new charts in ParaView. Test to follow
shortly.
parent e0833bf1
......@@ -36,22 +36,20 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "pqOutputPort.h"
#include "pqPipelineSource.h"
#include "pqServer.h"
#include "pqImageUtil.h"
#include "vtkErrorCode.h"
#include "vtkImageData.h"
#include "vtkPVDataInformation.h"
#include "vtkPVXMLElement.h"
#include "vtkContextView.h"
// #include "vtkQtChartArea.h"
// #include "vtkQtChartContentsSpace.h"
// #include "vtkQtChartView.h"
// #include "vtkQtChartWidget.h"
#include "vtkSMContextViewProxy.h"
#include "vtkSMSourceProxy.h"
#include "QVTKWidget.h"
#include <QDebug>
//-----------------------------------------------------------------------------
pqContextView::pqContextView(
const QString& type, const QString& group,
......@@ -85,35 +83,86 @@ QWidget* pqContextView::getWidget()
}
//-----------------------------------------------------------------------------
/// Returns the internal vtkQtChartView which provide the implementation for
/// Returns the internal vtkChartView that provides the implementation for
/// the chart rendering.
vtkContextView* pqContextView::getVTKContextView() const
{
return vtkSMContextViewProxy::SafeDownCast(this->getProxy())->GetChartView();
}
//-----------------------------------------------------------------------------
vtkSMContextViewProxy* pqContextView::getContextViewProxy() const
{
return vtkSMContextViewProxy::SafeDownCast(this->getProxy());
}
//-----------------------------------------------------------------------------
/// This view does not support saving to image.
bool pqContextView::saveImage(int , int , const QString& )
bool pqContextView::saveImage(int width, int height, const QString& filename)
{
return false;
QWidget* vtkwidget = this->getWidget();
QSize cursize = vtkwidget->size();
QSize fullsize = QSize(width, height);
QSize newsize = cursize;
int magnification = 1;
if (width>0 && height>0)
{
magnification = pqView::computeMagnification(fullsize, newsize);
vtkwidget->resize(newsize);
}
this->render();
int error_code = vtkErrorCode::UnknownError;
vtkImageData* vtkimage = this->captureImage(magnification);
if (vtkimage)
{
error_code = pqImageUtil::saveImage(vtkimage, filename);
vtkimage->Delete();
}
switch (error_code)
{
case vtkErrorCode::UnrecognizedFileTypeError:
qCritical() << "Failed to determine file type for file:"
<< filename.toAscii().data();
break;
case vtkErrorCode::NoError:
// success.
break;
default:
qCritical() << "Failed to save image.";
}
if (width>0 && height>0)
{
vtkwidget->resize(newsize);
vtkwidget->resize(cursize);
this->render();
}
return (error_code == vtkErrorCode::NoError);
}
//-----------------------------------------------------------------------------
/// Capture the view image into a new vtkImageData with the given magnification
/// and returns it. The caller is responsible for freeing the returned image.
vtkImageData* pqContextView::captureImage(int )
vtkImageData* pqContextView::captureImage(int magnification)
{
if (this->getWidget()->isVisible())
{
return this->getContextViewProxy()->CaptureWindow(magnification);
}
// Don't return any image when the view is not visible.
return NULL;
}
//-----------------------------------------------------------------------------
/// Capture the view image of the given size and returns it. The caller is
/// responsible for freeing the returned image.
vtkImageData* pqContextView::captureImage(const QSize& )
{
return NULL;
}
//vtkImageData* pqContextView::captureImage(const QSize& )
//{
// return NULL;
//}
//-----------------------------------------------------------------------------
/// Called to undo interaction.
......
......@@ -53,6 +53,9 @@ public:
/// the chart rendering.
virtual vtkContextView* getVTKContextView() const;
/// Returns the context view proxy associated with this object.
virtual vtkSMContextViewProxy* getContextViewProxy() const;
/// This view does not support saving to image.
virtual bool saveImage(int width, int height, const QString& filename);
......@@ -62,7 +65,7 @@ public:
/// Capture the view image of the given size and return it. The caller is
/// responsible for freeing the returned image.
virtual vtkImageData* captureImage(const QSize& asize);
//virtual vtkImageData* captureImage(const QSize& asize);
/// This view supports undo so this method always returns true.
virtual bool supportsUndo() const {return true;}
......
......@@ -15,6 +15,9 @@
#include "vtkSMContextViewProxy.h"
#include "vtkContextView.h"
#include "vtkRenderWindow.h"
#include "vtkWindowToImageFilter.h"
#include "vtkProcessModule.h"
#include "vtkObjectFactory.h"
#include "vtkQtChartInteractorSetup.h"
......@@ -45,7 +48,7 @@ public:
};
vtkCxxRevisionMacro(vtkSMContextViewProxy, "1.2");
vtkCxxRevisionMacro(vtkSMContextViewProxy, "1.3");
//----------------------------------------------------------------------------
vtkSMContextViewProxy::vtkSMContextViewProxy()
{
......@@ -95,6 +98,40 @@ vtkContextView* vtkSMContextViewProxy::GetChartView()
return this->ChartView;
}
//-----------------------------------------------------------------------------
vtkImageData* vtkSMContextViewProxy::CaptureWindow(int magnification)
{
this->GetChartView()->GetRenderWindow()->SwapBuffersOff();
this->GetChartView()->Render();
vtkWindowToImageFilter* w2i = vtkWindowToImageFilter::New();
w2i->SetInput(this->GetChartView()->GetRenderWindow());
w2i->SetMagnification(magnification);
w2i->Update();
w2i->ReadFrontBufferOff();
w2i->ShouldRerenderOff();
vtkImageData* capture = vtkImageData::New();
capture->ShallowCopy(w2i->GetOutput());
w2i->Delete();
this->GetChartView()->GetRenderWindow()->SwapBuffersOn();
// Update image extents based on ViewPosition
int extents[6];
capture->GetExtent(extents);
for (int cc=0; cc < 4; cc++)
{
extents[cc] += this->ViewPosition[cc/2]*magnification;
}
capture->SetExtent(extents);
cout << "Capture image called: " << extents[0] << " " << extents[1] << endl;
capture->Print(cout);
return capture;
}
//----------------------------------------------------------------------------
bool vtkSMContextViewProxy::WriteImage(const char*)
{
......
......@@ -23,6 +23,7 @@
#include "vtkSMViewProxy.h"
class vtkContextView;
class vtkImageData;
//BTX
class QVTKWidget;
//ETX
......@@ -48,6 +49,10 @@ public:
vtkContextView* GetChartView();
//ETX
// Description:
// Capture the contents of the window at the specified magnification level.
vtkImageData* CaptureWindow(int magnification);
//BTX
protected:
vtkSMContextViewProxy();
......
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