/*
    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 "material_map_editor.h"

#include "ui_material_map_editor.h"


MaterialMapEditor::MaterialMapEditor(QWidget * parent): QWidget(parent) {
	ui = new Ui::MaterialMapEditor();
	ui->setupUi(this);
	active = true;
}


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


void MaterialMapEditor::resizeEvent(QResizeEvent * e) {
	ui->iconedLabel->setFixedWidth(ui->iconedLabel->height());
	ui->iconedLabel->setIconSize(ui->iconedLabel->size());
}


void MaterialMapEditor::updateIcon() {
	ui->iconedLabel->setIcon(QIcon(ui->linePath->property("GLpath").toString()));
}


void MaterialMapEditor::setMap(const Map & m) {
	active = false;
	ui->sliderAmount->setValue(m.color_amount);
	ui->sliderOffset->setValue(m.color_offset);
	ui->spinScaleX->setValue(m.bitmap_scale.x());
	ui->spinScaleY->setValue(m.bitmap_scale.y());
	ui->linePath->setProperty("GLpath", m.bitmap_path);
	ui->linePath->setText(QFileInfo(m.bitmap_path).fileName());
	updateIcon();
	active = true;
}


Map MaterialMapEditor::map() {
	Map m;
	m.color_amount = ui->sliderAmount->value();
	m.color_offset = ui->sliderOffset->value();
	m.bitmap_scale.setX(ui->spinScaleX->value());
	m.bitmap_scale.setY(ui->spinScaleY->value());
	m.bitmap_path = ui->linePath->property("GLpath").toString();
	return m;
}


void MaterialMapEditor::on_buttonSelect_clicked() {
	QString str = QFileDialog::getOpenFileName(this,
	                                           "Select image",
	                                           ui->linePath->property("GLpath").toString(),
	                                           "Images(*.bmp *.jpg *.jpeg *.png *.tif *.tiff *.tga);;All files(*)");
	if (str.isEmpty()) return;
	ui->linePath->setProperty("GLpath", str);
	ui->linePath->setText(QFileInfo(str).fileName());
	updateIcon();
	mapChanged();
}


void MaterialMapEditor::on_buttonClear_clicked() {
	ui->linePath->setText("");
	ui->linePath->setProperty("GLpath", "");
	updateIcon();
	mapChanged();
}
