#include "qglviewplugin.h"

#include "../glwidget.h"
#include "glprimitives.h"
#include "qglview.h"

#include <QtCore/QtPlugin>


QGLViewPlugin::QGLViewPlugin(QObject * parent): QObject(parent) {
	m_initialized = false;
}


void QGLViewPlugin::initialize(QDesignerFormEditorInterface * /* core */) {
	if (m_initialized) return;

	// Add extension registrations, etc. here

	m_initialized = true;
}


bool QGLViewPlugin::isInitialized() const {
	return m_initialized;
}


QWidget * QGLViewPlugin::createWidget(QWidget * parent) {
	GLWidget * w = new GLWidget(parent);
	if (m_initialized) {
		auto axis = new GLObjectBase();
		GLObjectBase * obj;
		float al                      = 1.;
		obj                           = new GLPrimitiveLine(QVector3D(0, 0, -al), QVector3D(0, 0, al));
		obj->material().color_diffuse = Qt::darkBlue;
		obj->setAcceptLight(false);
		obj->setSelectable(false);
		axis->addChild(obj);
		obj                           = new GLPrimitiveLine(QVector3D(-al, 0, 0), QVector3D(al, 0, 0));
		obj->material().color_diffuse = Qt::darkRed;
		obj->setAcceptLight(false);
		obj->setSelectable(false);
		axis->addChild(obj);
		obj                           = new GLPrimitiveLine(QVector3D(0, -al, 0), QVector3D(0, al, 0));
		obj->material().color_diffuse = Qt::darkGreen;
		obj->setAcceptLight(false);
		obj->setSelectable(false);
		axis->addChild(obj);
		w->view()->addObject(axis);
		auto cam_light       = new Light();
		cam_light->intensity = 0.5;
		cam_light->setName("Camera_Light");
		w->view()->camera()->addChild(cam_light);
		w->start();
	}
	return w;
}


QString QGLViewPlugin::name() const {
	return QLatin1String("GLWidget");
}


QString QGLViewPlugin::group() const {
	return QLatin1String("Display Widgets");
}


QIcon QGLViewPlugin::icon() const {
	return QIcon("://icons/qglview.png");
}


QString QGLViewPlugin::toolTip() const {
	return QLatin1String("");
}


QString QGLViewPlugin::whatsThis() const {
	return QLatin1String("");
}


bool QGLViewPlugin::isContainer() const {
	return false;
}


QString QGLViewPlugin::domXml() const {
	return QLatin1String("<widget class=\"GLWidget\" name=\"glview\">\n</widget>\n");
}


QString QGLViewPlugin::includeFile() const {
	return QLatin1String("glwidget.h");
}
