/*
    QGL GLWidget
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    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 the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "glwidget.h"

#include "qglview.h"

#include <QVBoxLayout>


GLWidget::GLWidget(QWidget * parent): QWidget(parent) {
	view_     = new QGLView();
	// view_->setFlags(windowFlags() | Qt::FramelessWindowHint);
	container = QWidget::createWindowContainer(view_, this);
	lay       = new QVBoxLayout(this);
	lay->addWidget(container);
	lay->setContentsMargins(0, 0, 0, 0);
	lay->setSpacing(0);
	setMouseTracking(true);
	setWindowIcon(QIcon("://icons/qglview.png"));
	connect(view_, &QGLView::doubleClick, this, &GLWidget::viewDoubleClicked);
}


Renderer * GLWidget::renderer() {
	return &(view_->renderer_);
}


Scene * GLWidget::scene() {
	return view_->scene();
}


qreal GLWidget::lineWidth() const {
	return view_->lineWidth();
}


qreal GLWidget::FOV() const {
	return view_->FOV();
}


qreal GLWidget::depthStart() const {
	return view_->depthStart();
}


bool GLWidget::isLightEnabled() const {
	return view_->isLightEnabled();
}


bool GLWidget::isGrabMouseEnabled() const {
	return view_->isGrabMouseEnabled();
}


bool GLWidget::isMouseRotateEnabled() const {
	return view_->isMouseRotateEnabled();
}


bool GLWidget::isMouseSelectionEnabled() const {
	return view_->isMouseSelectionEnabled();
}


bool GLWidget::isCameraOrbit() const {
	return view_->isCameraOrbit();
}


bool GLWidget::isHoverHaloEnabled() const {
	return view_->isHoverHaloEnabled();
}


QColor GLWidget::hoverHaloColor() const {
	return view_->hoverHaloColor();
}


qreal GLWidget::hoverHaloFillAlpha() const {
	return view_->hoverHaloFillAlpha();
}


bool GLWidget::isSelectionHaloEnabled() const {
	return view_->isSelectionHaloEnabled();
}


QColor GLWidget::selectionHaloColor() const {
	return view_->selectionHaloColor();
}


qreal GLWidget::selectionHaloFillAlpha() const {
	return view_->selectionHaloFillAlpha();
}


void GLWidget::addObject(ObjectBase * o) {
	view_->scene()->addObject(o);
}


QByteArray GLWidget::saveCamera() {
	return view_->saveCamera();
}


void GLWidget::restoreCamera(const QByteArray & ba) {
	view_->restoreCamera(ba);
}


void GLWidget::stop() {
	view_->stop();
}


void GLWidget::start(float freq) {
	view_->start(freq);
}


void GLWidget::setLineWidth(const qreal & arg) {
	view_->setLineWidth(arg);
}


void GLWidget::setFOV(const qreal & arg) {
	view_->setFOV(arg);
}


void GLWidget::setDepthStart(const qreal & arg) {
	view_->setDepthStart(arg);
}


void GLWidget::setLightEnabled(const bool & arg) {
	view_->setLightEnabled(arg);
}


void GLWidget::setGrabMouseEnabled(const bool & arg) {
	view_->setGrabMouseEnabled(arg);
}


void GLWidget::setMouseRotateEnabled(const bool & arg) {
	view_->setMouseRotateEnabled(arg);
}


void GLWidget::setMouseSelectionEnabled(const bool & arg) {
	view_->setMouseSelectionEnabled(arg);
}


void GLWidget::setCameraOrbit(const bool & arg) {
	view_->setCameraOrbit(arg);
}


void GLWidget::setHoverHaloEnabled(const bool & arg) {
	view_->setHoverHaloEnabled(arg);
}


void GLWidget::setHoverHaloColor(const QColor & arg) {
	view_->setHoverHaloColor(arg);
}


void GLWidget::setHoverHaloFillAlpha(const qreal & arg) {
	view_->setHoverHaloFillAlpha(arg);
}


void GLWidget::setSelectionHaloEnabled(const bool & arg) {
	view_->setSelectionHaloEnabled(arg);
}


void GLWidget::setSelectionHaloColor(const QColor & arg) {
	view_->setSelectionHaloColor(arg);
}


void GLWidget::setSelectionHaloFillAlpha(const qreal & arg) {
	view_->setSelectionHaloFillAlpha(arg);
}


void GLWidget::viewDoubleClicked() {
	if (view_->windowState() == Qt::WindowFullScreen) {
		view_->showNormal();
		container = QWidget::createWindowContainer(view_, this);
		lay->addWidget(container);
		container->show();
	} else {
		view_->setParent(nullptr);
		view_->showFullScreen();
		lay->removeWidget(container);
	}
}
