/*
    QGLView
    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 "view_editor.h"
#include "ui_view_editor.h"
#include <QFileDialog>
#include <colorbutton.h>
#include <spinslider.h>


ViewEditor::ViewEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::ViewEditor();
	ui->setupUi(this);
	view = 0;
	active = true;
	ui->checkCameraLight->setCheckState(Qt::PartiallyChecked);
#if QT_VERSION >= QT_VERSION_CHECK(5,12,0)
	ui->spinDepthStart->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType);
#endif
}


void ViewEditor::assignQGLView(QGLView * v) {
	view = v;
	if (!view) return;
	active = false;
	ui->spinFOV->setValue(view->FOV());
	ui->spinDepthStart->setValue(view->depthStart());
	ui->groupHoverHalo->setChecked(view->isHoverHaloEnabled());
	ui->groupSelectionHalo->setChecked(view->isSelectionHaloEnabled());
	ui->spinHoverHaloFill->setValue(view->hoverHaloFillAlpha());
	ui->spinSelectionHaloFill->setValue(view->selectionHaloFillAlpha());
	ui->colorHoverHalo->setColor(view->hoverHaloColor());
	ui->colorSelectionHalo->setColor(view->selectionHaloColor());
	ui->checkFXAA->setChecked(view->isFeatureEnabled(QGLView::qglFXAA));
	ui->checkCameraOrbit->setChecked(view->isCameraOrbit());
	ui->checkCameraLight->setCheckState((Qt::CheckState)view->cameraLightMode());
	ui->checkService->setChecked(view->isServiceMode());
	ui->lineHDR->setProperty("GLpath", view->environmentMapFile());
	ui->lineHDR->setText(QFileInfo(view->environmentMapFile()).fileName());
	active = true;
}


void ViewEditor::changeEvent(QEvent * e) {
	QWidget::changeEvent(e);
	switch (e->type()) {
	case QEvent::LanguageChange:
		ui->retranslateUi(this);
		break;
	default:
		break;
	}
}


void ViewEditor::on_spinFOV_valueChanged(double val) {
	if (!view || !active) return;
	view->setFOV(val);
}


void ViewEditor::on_spinDepthStart_valueChanged(double val) {
	if (!view || !active) return;
	view->setDepthStart(val);
}


void ViewEditor::on_spinViewGamma_valueChanged(double val) {
	if (!view || !active) return;
	view->setGamma(val);
}


void ViewEditor::on_comboViewRenderMode_currentIndexChanged(int val) {
	if (!view || !active) return;
	static int modes[] = {GL_POINT, GL_LINE, GL_FILL};
	view->setRenderMode((ObjectBase::RenderMode)modes[val]);
}


void ViewEditor::on_groupHoverHalo_clicked(bool val) {
	if (!view || !active) return;
	view->setHoverHaloEnabled(val);
}


void ViewEditor::on_groupSelectionHalo_clicked(bool val) {
	if (!view || !active) return;
	view->setSelectionHaloEnabled(val);
}


void ViewEditor::on_spinHoverHaloFill_valueChanged(double val) {
	if (!view || !active) return;
	view->setHoverHaloFillAlpha(val);
}


void ViewEditor::on_spinSelectionHaloFill_valueChanged(double val) {
	if (!view || !active) return;
	view->setSelectionHaloFillAlpha(val);
}


void ViewEditor::on_colorHoverHalo_colorChanged(QColor color) {
	if (!view || !active) return;
	view->setHoverHaloColor(color);
}


void ViewEditor::on_colorSelectionHalo_colorChanged(QColor color) {
	if (!view || !active) return;
	view->setSelectionHaloColor(color);
}


void ViewEditor::on_checkAutoExposure_toggled(bool val) {
	if (!view || !active) return;
	view->setAutoExposure(val);
}


void ViewEditor::on_checkFXAA_clicked(bool val) {
	if (!view || !active) return;
	view->setFeature(QGLView::qglFXAA, val);
}


void ViewEditor::on_checkCameraOrbit_clicked(bool val) {
	if (!view || !active) return;
	view->setCameraOrbit(val);
}


void ViewEditor::on_checkService_clicked(bool val) {
	if (!view || !active) return;
	view->setServiceMode(val);
}


void ViewEditor::on_checkCameraLight_stateChanged(int s) {
	if (!view || !active) return;
	view->setCameraLightMode((QGLView::CameraLightMode)s);

}


void ViewEditor::on_buttonHDRClear_clicked() {
	if (!view || !active) return;
	ui->lineHDR->setText("");
	ui->lineHDR->setProperty("GLpath", "");
	view->setEnvironmentMapFile("");
}


void ViewEditor::on_buttonHDRSelect_clicked() {
	if (!view || !active) return;
	QString str = QFileDialog::getOpenFileName(this, "Select image", ui->lineHDR->property("GLpath").toString(), "Radiance HDR(*.hdr)");
	if (str.isEmpty()) return;
	ui->lineHDR->setText(QFileInfo(str).fileName());
	ui->lineHDR->setProperty("GLpath", str);
	view->setEnvironmentMapFile(str);
}
