OgreQtOverlay::OgreQtOverlay(Ogre::Root *root, Ogre::RenderWindow *window, Ogre::uint w, Ogre::uint h, QObject *parent)
 : QGraphicsScene(parent), _width(w), _height(h)
{
	_root = root;
	_window = window;
	_time.start(); //start a timer for key events
	
	//Connect redraw signal to this class
	QObject::connect(this,SIGNAL(changed(QList<QRectF>)),this,SLOT(sceneChanged(QList<QRectF>)));
	
	//qDebug() << "WIDTH:" << _width << "HEIGHT:" << _height;
	
	//create a texture the size of the screen to draw into
	_texture = TextureManager::getSingleton().createManual("OgreQtTexture",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME
			,TEX_TYPE_2D,_width,_height,0,PF_A8R8G8B8,TU_DYNAMIC_WRITE_ONLY).getPointer();
	
	//save texture buffer for later
	_buffer = _texture->getBuffer().getPointer();
	
	//Create a material
	MaterialPtr material = MaterialManager::getSingleton().create("DynamicTextureMaterial",ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	material->getTechnique(0)->getPass(0)->createTextureUnitState("OgreQtTexture");
	material->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA);
	
	//Load overlay
	_overlay = OverlayManager::getSingleton().getByName("MyOverlays/OgreQt");
	OverlayElement *element = OverlayManager::getSingleton().getOverlayElement("MyOverlayElements/QtPanel");
	element->setMaterialName("DynamicTextureMaterial");
	
	//set the screen rect for the graphicsscene
	float ratio = (float)_width / (float)_height;
	setSceneRect(QRectF(0.0,0.0,_width,_height));
	
	//Create Cursor Pixmap
	_cursor = addPixmap(QPixmap(POINTER_FILE));
	_cursor->scale(POINTER_SCALE_X,POINTER_SCALE_Y);
	_cursor->setZValue(POINTER_Z);
	
	//set some default values
	_modifiers = Qt::NoModifier;
	_lastkeytime=0;
	_repeating=false;
	_lastclicktime = 0;
	_visible=false;
	_overlay->hide();
}
    //-----------------------------------------------------------------------
    OverlayElement* OverlayProfileSessionListener::createPanel(const String& name, Real width, Real height, Real top, Real left, 
                                      const String& materialName, bool show)
    {
        OverlayElement* panel = 
            OverlayManager::getSingleton().createOverlayElement("Panel", name);
        panel->setMetricsMode(GMM_PIXELS);
        panel->setWidth(width);
        panel->setHeight(height);
        panel->setTop(top);
        panel->setLeft(left);
        panel->setMaterialName(materialName);

        if (show) {
            panel->show();
        }
        else {
            panel->hide();
        }

        return panel;
    }