/*
    This file is part of Photoflare.

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

    Photoflare 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 General Public License for more details.

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

// Widget for LineTool settings.

#include <QGraphicsPixmapItem>

#include "LineSettingsWidget.h"
#include "ui_LineSettingsWidget.h"
#include "../Settings.h"

LineSettingsWidget::LineSettingsWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LineSettingsWidget)
{
    ui->setupUi(this);

    // Populate line styles
    for(int i=1; i <= 6; i++)
    {
        QPixmap pixmap = QPixmap (QString(":/lines/assets/lines/type_%1.png").arg(i));
        ui->styleComboBox->addItem(QString(), pixmap);
    }

    connect(ui->styleComboBox, SIGNAL(currentIndexChanged(int)), this, SIGNAL(settingsChanged()));
    connect(ui->widthSpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(settingsChanged()));
    connect(ui->opacitySpinBox, SIGNAL(valueChanged(int)), this, SIGNAL(settingsChanged()));
    connect(ui->antialiasCheckBox, SIGNAL(clicked()), this, SIGNAL(settingsChanged()));
    connect(ui->arrowCheckBox, SIGNAL(clicked()), this, SIGNAL(settingsChanged()));
    connect(ui->arrowCheckBox2, SIGNAL(clicked()), this, SIGNAL(settingsChanged()));
}

LineSettingsWidget::~LineSettingsWidget()
{
    delete ui;
}

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

int LineSettingsWidget::width()
{
    return ui->widthSpinBox->value();
}

int LineSettingsWidget::opacity()
{
    return ui->opacitySpinBox->value();
}

bool LineSettingsWidget::antialias()
{
    return ui->antialiasCheckBox->isChecked();
}

int LineSettingsWidget::style()
{
    return ui->styleComboBox->currentIndex();
}

int LineSettingsWidget::arrowStyle()
{
    if(ui->arrowCheckBox2->isChecked())
        return 2;
    else if(ui->arrowCheckBox->isChecked())
        return 1;
    else
        return 0;
}

void LineSettingsWidget::on_arrowCheckBox_clicked(bool checked)
{
    Q_UNUSED(checked);
    ui->arrowCheckBox2->setChecked(false);
}

void LineSettingsWidget::on_arrowCheckBox2_clicked(bool checked)
{
    Q_UNUSED(checked);
    ui->arrowCheckBox->setChecked(false);
}

void LineSettingsWidget::saveSettings()
{
    SETTINGS->setValue("toolSettings/line/width", width());
    SETTINGS->setValue("toolSettings/line/opacity", opacity());
    SETTINGS->setValue("toolSettings/line/antialias", antialias());
    SETTINGS->setValue("toolSettings/line/style", style());
    SETTINGS->setValue("toolSettings/line/arrowStyle", arrowStyle());
}

void LineSettingsWidget::loadSettings()
{
    ui->widthSpinBox->setValue(SETTINGS->value("toolSettings/line/width", 1).toInt());
    ui->opacitySpinBox->setValue(SETTINGS->value("toolSettings/line/opacity", 100).toInt());
    ui->antialiasCheckBox->setChecked(SETTINGS->value("toolSettings/line/antialias", false).toBool());
    ui->styleComboBox->setCurrentIndex(SETTINGS->value("toolSettings/line/style", 0).toInt());
    const int arrow = SETTINGS->value("toolSettings/line/arrowStyle", 0).toInt();
    ui->arrowCheckBox->setChecked(arrow == 1);
    ui->arrowCheckBox2->setChecked(arrow == 2);
}
