#include "graphic_conf.h"

#include "qad_types.h"
#include "ui_graphic_conf.h"

// GraphicType

GraphicType::GraphicType(QString name_, QColor color, Qt::PenStyle style, double width, bool visible_): name(name_), visible(visible_) {
	pen.setColor(color);
	pen.setStyle(style);
	if (qRound(width) == width)
		pen.setWidth(qRound(width));
	else
		pen.setWidthF(width);
	pen.setWidth(1);
	pen.setCosmetic(true);
}


void GraphicType::removeData() {
	polyline.clear();
	polyline_pause.clear();
	_lod.clear();
	_lod_pause.clear();
}


bool GraphicType::init() {
	if (pb) return false;
	pb = new QCheckBox(name);
	return true;
}


void GraphicType::destroy() {
	if (!pb) return;
	delete pb;
	pb = nullptr;
}


// GraphicConf

GraphicConf::GraphicConf(QVector<GraphicType> & graphics_, QWidget * parent): QDialog(parent), graphics(graphics_) {
	ui = new Ui::GraphicConf();
	ui->setupUi(this);
	QStringList styles;
	int fh    = qMax<int>(fontMetrics().height(), 22);
	int thick = lineThickness(this);
	QSize sz(fh * 2.5, fh);
	styles << tr("NoPen") << tr("Solid") << tr("Dash") << tr("Dot") << tr("Dash-Dot") << tr("Dash-Dot-Dot");
	ui->comboStyleGrid->setIconSize(sz);
	ui->comboStyleGraphic->setIconSize(sz);
	ui->cbGraphicNames->setIconSize(sz);
	for (int i = 0; i < styles.size(); i++) {
		QPixmap pix(sz);
		pix.fill();
		QPainter p(&pix);
		p.setPen(QPen(Qt::black, thick, (Qt::PenStyle)i));
		p.drawLine(0, pix.height() / 2, pix.width(), pix.height() / 2);
		p.end();
		ui->comboStyleGraphic->addItem(QIcon(pix), styles[i]);
		ui->comboStyleGrid->addItem(QIcon(pix), styles[i]);
	}
}


void GraphicConf::changeEvent(QEvent * e) {
	QDialog::changeEvent(e);
	if (e->type() == QEvent::LanguageChange) {
		ui->retranslateUi(this);
		return;
	}
}


void GraphicConf::readParams() {
	ui->cbGraphicNames->clear();
	for (int i = 0; i < graphicItems.size(); i++)
		ui->cbGraphicNames->addItem(graphicItems[i].icon, graphicItems[i].name);
}


void GraphicConf::on_cbGraphicNames_currentIndexChanged(int i) {
	if (i < 0) return;
	if (graphicItems.isEmpty()) return;
	ui->comboStyleGraphic->setCurrentIndex((int)graphics[i].pen.style());
	ui->colorGraphic->setColor(graphics[i].pen.color());
	ui->colorFill->setColor(graphics[i].fill_color);
	ui->spinLineWidthGraphic->setValue(graphics[i].pen.widthF());
	ui->spinPointWidthGraphic->setValue(graphics[i].pointWidth);
	ui->checkLines->setChecked(graphics[i].lines);
	ui->checkPoints->setChecked(graphics[i].points);
	ui->checkFill->setChecked(graphics[i].fill);
}


void GraphicConf::on_colorGraphic_colorChanged(const QColor & c) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].pen.setColor(c);
}


void GraphicConf::on_comboStyleGraphic_currentIndexChanged(int index) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].pen.setStyle((Qt::PenStyle)index);
}


void GraphicConf::on_spinLineWidthGraphic_valueChanged(double value) {
	if (graphicItems.isEmpty()) return;
	if (qRound(value) == value)
		graphics[ui->cbGraphicNames->currentIndex()].pen.setWidth(qRound(value));
	else
		graphics[ui->cbGraphicNames->currentIndex()].pen.setWidthF(value);
}


void GraphicConf::on_spinPointWidthGraphic_valueChanged(double value) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].pointWidth = value;
}


void GraphicConf::on_checkLines_toggled(bool on) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].lines = on;
}


void GraphicConf::on_checkPoints_toggled(bool on) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].points = on;
}


void GraphicConf::on_checkFill_toggled(bool on) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].fill = on;
}


void GraphicConf::on_spinLineWidthGraphicAll_valueChanged(double value) {
	for (auto & g: graphics) {
		if (qRound(value) == value)
			g.pen.setWidth(qRound(value));
		else
			g.pen.setWidthF(value);
	}
	QSignalBlocker bl(ui->spinLineWidthGraphic);
	ui->spinLineWidthGraphic->setValue(value);
}


void GraphicConf::on_spinPointWidthGraphicAll_valueChanged(double value) {
	for (auto & g: graphics)
		g.pointWidth = value;
	QSignalBlocker bl(ui->spinPointWidthGraphic);
	ui->spinPointWidthGraphic->setValue(value);
}


void GraphicConf::on_checkLinesAll_toggled(bool on) {
	for (auto & g: graphics)
		g.lines = on;
	QSignalBlocker bl(ui->checkLines);
	ui->checkLines->setChecked(on);
}


void GraphicConf::on_checkPointsAll_toggled(bool on) {
	for (auto & g: graphics)
		g.points = on;
	QSignalBlocker bl(ui->checkPoints);
	ui->checkPoints->setChecked(on);
}


void GraphicConf::on_buttonExport_clicked() {
	emit exportClicked();
}


void GraphicConf::on_colorFill_colorChanged(const QColor & color) {
	if (graphicItems.isEmpty()) return;
	graphics[ui->cbGraphicNames->currentIndex()].fill_color = color;
}
