#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QDirModel>
#include <QDrag>
#include <QMimeData>
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QtGui/QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>

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

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

void MainWindow::on_actionExit_triggered()
{
    this->close();
}

void MainWindow::on_actionSave_as_triggered()
{
    QString voltage,pomocna;
    bool    PrintRes = true;
    bool    PrintCur = true;
    int     vPort = 1;
    voltage.append(QString("\n[Voltage]\n\n"));
    QString fileName = QFileDialog::getSaveFileName(this,
             tr("Save Datalogger Config"), "",
             tr("Datalogger Config (*.dcfg)"));
    if (fileName.isEmpty())
             return;
    else {
             QFile file(tr("%1.dcfg").arg(fileName.replace(QString(".dcfg"), NULL)));
             if (!file.open(QIODevice::WriteOnly)) {
                 QMessageBox::information(this, tr("Unable to open file"), file.errorString());
                 return;
             }
             QTextStream out(&file);
             out << tr("[Setting]\n\n");
             out << tr("File = \"%1\"\n").arg(ui->uiFileName->text());
             out << tr("StartDate = \"%1\"\n").arg(ui->dateTimeEdit->dateTime().toString("dd.MM.yy"));
             out << tr("StartTime = \"%1\"\n").arg(ui->dateTimeEdit->dateTime().toString("hh:mm:ss"));

             if ((QString)(ui->comboBox_15->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_15->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_19->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_20->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_21->text());
                    PrintCur = false;

                }
                else if ((QString)(ui->comboBox_15->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_19->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_21->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_15->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_19->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_20->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_21->text()));
                }
             }

             if ((QString)(ui->comboBox_2->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_2->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_16->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_17->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_18->text());
                    PrintCur = false;
                }
                else if ((QString)(ui->comboBox_2->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_16->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_18->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_2->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_16->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_17->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_18->text()));
                }
             }

             if ((QString)(ui->comboBox_16->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_16->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_22->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_23->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_24->text());
                    PrintCur = false;

                }
                else if ((QString)(ui->comboBox_16->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_22->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_24->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_16->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_22->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_23->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_24->text()));
                }
             }

             if ((QString)(ui->comboBox_17->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_17->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_25->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_26->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_27->text());
                    PrintCur = false;

                }
                else if ((QString)(ui->comboBox_17->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_25->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_27->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_17->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_25->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_26->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_27->text()));
                }
             }

             if ((QString)(ui->comboBox_18->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_18->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_28->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_29->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_30->text());
                    PrintCur = false;

                }
                else if ((QString)(ui->comboBox_18->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_28->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_30->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_18->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_28->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_29->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_30->text()));
                }
             }

             if ((QString)(ui->comboBox_19->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_19->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_31->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_32->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_33->text());
                    PrintCur = false;

                }
                else if ((QString)(ui->comboBox_19->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_31->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_33->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_19->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_31->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_32->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_33->text()));
                }
             }
             if ((QString)(ui->comboBox_20->currentText()) != (QString)("unselected"))
             {
                if ((QString)(ui->comboBox_20->currentText()) == (QString)("Current") && PrintCur == true)
                {
                    out << tr("\n[Current]\n\n");
                    out << tr("cName = \"%1\"\n").arg(ui->lineEdit_34->text());
                    out << tr("cRange = %1\n").arg(ui->lineEdit_35->text());
                    out << tr("cSampling = %1\n\n").arg(ui->lineEdit_36->text());
                    PrintCur = false;

                }
                else if ((QString)(ui->comboBox_20->currentText()) == (QString)("Resistance") && PrintRes == true)
                {
                    out << tr("\n[Resistance]\n\n");
                    out << tr("rName = \"%1\"\n").arg(ui->lineEdit_34->text());
                    out << tr("rSampling = %1\n\n").arg(ui->lineEdit_36->text());
                    PrintRes = false;
                }
                else
                {
                    pomocna = (QString)(ui->comboBox_20->currentText());
                    voltage.append(QString("vPort = %1\n").arg(pomocna.right(1)));
                    voltage.append(QString("vName = \"%1\"\n").arg(ui->lineEdit_34->text()));
                    voltage.append(QString("rRange = %1\n").arg(ui->lineEdit_35->text()));
                    voltage.append(QString("vSampling = %1\n\n").arg(ui->lineEdit_36->text()));
                }
             }
             if (voltage != QString("\n[Voltage]\n\n"))
                out << tr("%1").arg(voltage);
             file.close();








    }
}

void MainWindow::on_comboBox_2_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_17->setEnabled(false);
    else
        ui->lineEdit_17->setEnabled(true);
}

void MainWindow::on_comboBox_15_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_20->setEnabled(false);
    else
        ui->lineEdit_20->setEnabled(true);
}

void MainWindow::on_comboBox_16_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_23->setEnabled(false);
    else
        ui->lineEdit_23->setEnabled(true);
}

void MainWindow::on_comboBox_17_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_26->setEnabled(false);
    else
        ui->lineEdit_26->setEnabled(true);
}

void MainWindow::on_comboBox_18_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_29->setEnabled(false);
    else
        ui->lineEdit_29->setEnabled(true);
}

void MainWindow::on_comboBox_19_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_32->setEnabled(false);
    else
        ui->lineEdit_32->setEnabled(true);
}

void MainWindow::on_comboBox_20_currentIndexChanged(const QString &arg1)
{
    if(arg1 == (QString)("Resistance"))
        ui->lineEdit_35->setEnabled(false);
    else
        ui->lineEdit_35->setEnabled(true);
}
