#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui/QDialog>
#include <QtGui/QLayout>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsProxyWidget>
#include <Common/QOSGGraphics.h>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <iostream>

int main(int argc, char *argv[])
{
	if (argc<2)
    {
        std::cout << argv[0] <<": requires filename argument." << std::endl;
        return 1;
    }

	QApplication a(argc, argv);
	osg::QGLGraphicsView view;
    osg::ArgumentParser arguments(&argc, argv);

	osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
    if (!loadedModel)
    {
        std::cout << arguments[0] <<": No data loaded." << std::endl;
        return 1;
    }

	if (arguments.read("--composite"))
	{
		osg::QOSGCompositeScene *pScene = new osg::QOSGCompositeScene;
		view.setScene( pScene );

		unsigned int width = pScene->width();
        unsigned int height = pScene->height();

		{
            osgViewer::View* view1 = new osgViewer::View;
            view1->getCamera()->setGraphicsContext(pScene->getGraphicsWindow());
            view1->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width/2)/static_cast<double>(height), 1.0, 1000.0);
            view1->getCamera()->setViewport(new osg::Viewport(0,0,width/2,height));
            view1->setCameraManipulator(new osgGA::TrackballManipulator);
            view1->setSceneData(loadedModel.get());
            
            pScene->addView(view1);
        }
        
        {
            osgViewer::View* view2 = new osgViewer::View;
            view2->getCamera()->setGraphicsContext(pScene->getGraphicsWindow());
            view2->getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width/2)/static_cast<double>(height), 1.0, 1000.0);
            view2->getCamera()->setViewport(new osg::Viewport(width/2,0,width/2,height));
            view2->setCameraManipulator(new osgGA::TrackballManipulator);
            view2->setSceneData(loadedModel.get());
            
            pScene->addView(view2);
        }
	}
	else
	{
		osg::QOSGScene *pScene = new osg::QOSGScene;

		view.setScene( pScene );
		pScene->setCameraManipulator(new osgGA::TrackballManipulator);
        pScene->setSceneData(loadedModel.get());
	}

	// now add a widget on top off all of this
	QDialog* pDialog = new QDialog( NULL, Qt::CustomizeWindowHint | Qt::WindowTitleHint );
	pDialog->setWindowOpacity( 0.5 );
	pDialog->setWindowTitle( "A 'Hello world' widget" );
	pDialog->setLayout(new QVBoxLayout);
	pDialog->layout()->addWidget( new QLabel( "Hello OSG users !!!" ) );
	pDialog->resize(700,500);

	QGraphicsItem *item = view.scene()->addWidget( pDialog );
	item->setFlag( QGraphicsItem::ItemIsMovable );	// allow user to move it.
	item->setCacheMode( QGraphicsItem::DeviceCoordinateCache );	// cached in QPixmap (mapped to OpenGL textures, making the widgets very cheap to redraw).
	item->setPos( 10, 50 );

	view.show();
	view.resize( 800, 600 );

	return a.exec();
}
