/*
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                                                 ┃
┃            Class Name: configdialog.cpp                                                         ┃
┃                Author: Michael Krämer                                                           ┃
┃                 Email: Michael@dk8pp.de                                                         ┃
┃                  Date: 10.11.2025                                                               ┃
┃                  Tool: Qt                                                                       ┃
┃                Device: Windows/Linux                                                            ┃
┃               Version: 0.1                                                                      ┃
┃        Program status: still testing                                                            ┃
┃                                                                                                 ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃                                                                                                 ┃
┃  Purpose: Program configuration                                                                 ┃
┃                                                                                                 ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
┃                                                                                                 ┃
┃  © Copyright 2025-2026 Michael Krämer, DK8PP                                                    ┃
┃                                                                                                 ┃
┃  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/>.                                         ┃
┃                                                                                                 ┃
┃  ============================================================================================   ┃
┃                                                                                                 ┃
┃  Dieses Programm ist Freie Software: Sie können es unter den Bedingungen der GNU Lesser         ┃
┃  General Public License, wie von der Free Software Foundation, Version 3 der Lizenz oder        ┃
┃  (nach Ihrer Wahl) jeder neueren veröffentlichten Version, weiter verteilen und/oder            ┃
┃  modifizieren.                                                                                  ┃
┃                                                                                                 ┃
┃  Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch OHNE   ┃
┃  JEDE GEWÄHR,; sogar ohne die implizite Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN        ┃
┃  BESTIMMTEN ZWECK. Siehe die GNU Lesser General Public License für weitere Einzelheiten.        ┃
┃                                                                                                 ┃
┃  Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit diesem Programm      ┃
┃  erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.                             ┃
┃                                                                                                 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/

#include "CompSynt.h"
#include "configdialog.h"
#include "ui_configdialog.h"
#include "showfile.h"
#include <QMessageBox>
#include <QFile>

/*
╭─────────────────────────────────────────────────────────────────────────────────────────────────╮
│       global variables                                                                          │
╰─────────────────────────────────────────────────────────────────────────────────────────────────╯
*/
extern int32_t resdev ;      /* maximum deviation of resistors */

/*
╭─────────────────────────────────────────────────────────────────────────────────────────────────╮
│       ConfigDialog                                                                              │
├─────────────────────────────────────────────────────────────────────────────────────────────────┤
│                                                                                                 │
│ Constructor of InfoDialog class                                                                 │
│                                                                                                 │
╰─────────────────────────────────────────────────────────────────────────────────────────────────╯
*/
ConfigDialog::ConfigDialog(CompSynt *parent) :
    QDialog(parent),
    ui(new Ui::ConfigDialog)
{
int index ;
    ui->setupUi(this) ;
    compsynt = parent ;

//         ╭──────────────────────────────────────────────────────────────────────────────── const QObject *sender
//         │                           ╭──────────────────────────────────────────────────── const char *signal
//         │                           │                                 ╭────────────────── const QObject *receiver
//         │                           │                                 │             ╭──── const char *method/slot
//         │                           │                                 │             │
   connect(ui->PB_ConfigDone,          &QPushButton::clicked,            this,         &ConfigDialog::PUB_Done_clicked) ;

    ui->SB_ConfigPrintLines->setValue(compsynt->printline_count) ;
    ui->CB_OutputAlignment->clear() ;
    ui->CB_OutputAlignment->insertItem(0,tr("left")) ;
    ui->CB_OutputAlignment->insertItem(1,tr("right")) ;
    ui->CB_OutputAlignment->insertItem(2,tr("center")) ;
    if (compsynt->output_alignment == Qt::AlignLeft) ui->CB_OutputAlignment->setCurrentIndex(0) ;
    if (compsynt->output_alignment == Qt::AlignRight) ui->CB_OutputAlignment->setCurrentIndex(1) ;
    if (compsynt->output_alignment == Qt::AlignHCenter) ui->CB_OutputAlignment->setCurrentIndex(2) ;

    ui->CB_DiscardOOR->setChecked(!compsynt->discardOutOfRange) ;
    ui->CB_Locale->setChecked(compsynt->useLocale) ;

    ui->SB_RDecades->setValue(compsynt->rdecades) ;
    ui->SB_DecimalPlaces->setValue(compsynt->decimal_places) ;
    ui->SB_RMin->setValue(compsynt->log10rmin) ;
    ui->SB_RDeviation->setValue(resdev) ;
//    ui->SB_RDeviation->setValue(compsynt->resdev) ;
    ui->SB_CDecades->setValue(compsynt->cdecades) ;
    ui->SB_CMin->setValue(compsynt->log10cmin) ;

    ui->CB_ExportFormat->clear() ;
    ui->CB_ExportFormat->insertItem(0,tr("include units, e.g. 150mΩ")) ;
    ui->CB_ExportFormat->insertItem(1,tr("plain float, e.g. 0,1500")) ;
    ui->CB_ExportFormat->insertItem(2,tr("exponential format, e.g 1.500e-01")) ;
    if (compsynt->private_fileformat == FMT_UNITS) ui->CB_ExportFormat->setCurrentIndex(0) ;
    if (compsynt->private_fileformat == FMT_FLOAT) ui->CB_ExportFormat->setCurrentIndex(1) ;
    if (compsynt->private_fileformat == FMT_EXP) ui->CB_ExportFormat->setCurrentIndex(2) ;

    ui->CB_NormalColor->clear() ;
    ui->CB_NormalColor->addItems(colors) ;
    index = colors.indexOf(compsynt->colorNormalName) ;
    ui->CB_NormalColor->setCurrentIndex(index) ;

    ui->CB_OverPowerColor->clear() ;
    ui->CB_OverPowerColor->addItems(colors) ;
    index = colors.indexOf(compsynt->colorExPowerName) ;
    ui->CB_OverPowerColor->setCurrentIndex(index) ;

    ui->CB_OverVoltageColor->clear() ;
    ui->CB_OverVoltageColor->addItems(colors) ;
    index = colors.indexOf(compsynt->colorExVoltageName) ;
    ui->CB_OverVoltageColor->setCurrentIndex(index) ;
}

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

/*
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                                                 ┃
┃       Slots                                                                                     ┃
┃                                                                                                 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
*/

/*
╭─────────────────────────────────────────────────────────────────────────────────────────────────╮
│       PUB_Done_clicked                                                                          │
╰─────────────────────────────────────────────────────────────────────────────────────────────────╯
*/
void ConfigDialog::PUB_Done_clicked()
{
int index ;
QString color ;
    compsynt->printline_count = ui->SB_ConfigPrintLines->value() ;

    index = ui->CB_OutputAlignment->currentIndex() ;
    if (index == 0) compsynt->output_alignment = Qt::AlignLeft ;
    if (index == 1) compsynt->output_alignment = Qt::AlignRight ;
    if (index == 2) compsynt->output_alignment = Qt::AlignHCenter ;

    compsynt->discardOutOfRange = !ui->CB_DiscardOOR->isChecked() ;
    compsynt->useLocale = ui->CB_Locale->isChecked() ;

    compsynt->rdecades = ui->SB_RDecades->value() ;
    compsynt->decimal_places = ui->SB_DecimalPlaces->value() ;
    compsynt->log10rmin = ui->SB_RMin->value() ;
    resdev = ui->SB_RDeviation->value() ;
    compsynt->cdecades = ui->SB_CDecades->value() ;
    compsynt->log10cmin = ui->SB_CMin->value() ;

    index = ui->CB_ExportFormat->currentIndex() ;
    if (index == 0) compsynt->private_fileformat = FMT_UNITS ;
    if (index == 1) compsynt->private_fileformat = FMT_FLOAT ;
    if (index == 2) compsynt->private_fileformat = FMT_EXP ;

    index = ui->CB_NormalColor->currentIndex() ;
    if (index != -1)
    {
       compsynt->colorNormalName = colors[index] ;
       compsynt->colorNormal     = QColor(compsynt->colorNormalName) ;
    }

    index = ui->CB_OverPowerColor->currentIndex() ;
    if (index != -1)
    {
       compsynt->colorExPowerName = colors[index] ;
       compsynt->colorExPower     = QColor(compsynt->colorExPowerName) ;
    }

    index = ui->CB_OverVoltageColor->currentIndex() ;
    if (index != -1)
    {
       compsynt->colorExVoltageName = colors[index] ;
       compsynt->colorExVoltage     = QColor(compsynt->colorExVoltageName) ;
    }

    this->close() ; /* will send a closeEvent */
}


