/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkSynchronizedQVTKWidget.h
	Version:		$Revision: 1821 $
	Date:			$Date: 2011-10-02 15:41:17 +0200 (So, 02 Okt 2011) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
	See RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#ifndef SYNCHRONIZEDQVTWIDGET_H__
#define SYNCHRONIZEDQVTWIDGET_H__

#include "ritkVTK.h"

#include <QVTKWidget.h>
#include <QMutex>


namespace ritk
{

/**	@ingroup	ritkVTK
 *	@class		SynchronizedQVTKWidget
 *	@author		Jakob Wasza
 *	@brief		QVTKWidget that locks its paint event with a mutex
 *
 *	@details
 *	This class extends the QVTKWidget by a locking mechanism.
 *	The need for a locking mechanism arises from the fact
 *	that rendering is triggered by the paint event which is called
 *	arbitrarily. Manipulation of the data associated with the objects
 *	to be rendered from a different thread context may thus result
 *	in invalid object states and program crashes. The problem can be
 *	solved by locking the paint event and the data manipulation through
 *	one mutex. Additionally, this widgets emits a signal from the locked
 *	block whenever its paint event is called. \n
 *	Note: This class supports Qt4 only
 */
class RITKVTK_EXPORT SynchronizedQVTKWidget : public QVTKWidget
{
	Q_OBJECT

public:
	/// Constructor
	SynchronizedQVTKWidget(QWidget* parent = NULL, Qt::WFlags f = 0);
	/// Destructor
	~SynchronizedQVTKWidget();

	/// Lock the widget
	void Lock();

	/// Unlock the widget
	void Unlock();

signals:
	/// Emitted whenever the paint event method is called
	void PaintEventInvoked();

protected:
	/// Overloaded paint handler. Will lock the plugin
	virtual void paintEvent(QPaintEvent* event);

protected:
	/// The locking mutex
	QMutex m_Mutex;
};


} // end namespace ritk

#endif // SYNCHRONIZEDQVTWIDGET_H__