/*
 * scbus utils to switch card settings.
 *
 * Copyright (C) 2011 Otvos Attila oattila@onebithq.com
 *
 * This 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 2 of the License, or
 * (at your option) any later version.
 *
 * This 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <stdio.h>

#include "filedialog.h"
#include "ledstatus.h"

FileTab::FileTab(const int deviceid, QList<int>* devices, QWidget *parent) : QWidget(parent) {
    char sdeviceid[16];

    snprintf(sdeviceid,sizeof(sdeviceid),"%02X",deviceid);
    QLabel *deviceLabel = new QLabel(tr("Device:"));
    if(deviceid)
        deviceValueLabel = new QLabel(QString(sdeviceid));
    else
        deviceValueLabel = new QLabel(QString("Not selected"));
    deviceValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QLabel *fileLabel = new QLabel(tr("File name:"));

    fileValueLabel = new QLabel("");
    fileValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    this->deviceid=deviceid;
    this->devices=devices;

    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addWidget(deviceLabel);
    mainLayout->addWidget(deviceValueLabel);
    mainLayout->addWidget(fileLabel);
    mainLayout->addWidget(fileValueLabel);

    QLabel *dummyLabel = new QLabel("");
    dummyLabel->setAlignment(Qt::AlignBottom);
    dummyLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

    QHBoxLayout *buttonLayout1 = new QHBoxLayout();
    QHBoxLayout *buttonLayout2 = new QHBoxLayout();
    acceptButton = new QPushButton("Accept All");
    resetButton = new QPushButton("Reset All");
    loadButton = new QPushButton("Load");
    saveButton = new QPushButton("Save");
    buttonLayout1->addWidget(loadButton);
    buttonLayout1->addWidget(saveButton);
    buttonLayout2->addWidget(resetButton);
    buttonLayout2->addWidget(acceptButton);
    QWidget * buttonWidget1 = new QWidget();
    buttonWidget1->setLayout(buttonLayout1);
    QWidget * buttonWidget2 = new QWidget();
    buttonWidget2->setLayout(buttonLayout2);

    mainLayout->addWidget(buttonWidget1);
    mainLayout->addWidget(dummyLabel);
    mainLayout->addWidget(buttonWidget2);

    connect(loadButton,SIGNAL(pressed()),this,SLOT(pressLoad()));
    connect(saveButton,SIGNAL(pressed()),this,SLOT(pressSave()));
    connect(resetButton,SIGNAL(pressed()),this,SLOT(pressReset()));
    connect(acceptButton,SIGNAL(pressed()),this,SLOT(pressAccept()));
    setLayout(mainLayout);
}

void FileTab::pressLoad(void) {

    fileName = QFileDialog::getOpenFileName(this,
         tr("Open Config"), ".", tr("Config Files (*.conf);;All files (*.*)"));
    fileValueLabel->setText(fileName);
    emit loadFile(fileName);
}

void FileTab::pressSave(void) {

    fileName = QFileDialog::getSaveFileName(this,
         tr("Open Config"), ".", tr("Config Files (*.conf);;All files (*.*)"));
    fileValueLabel->setText(fileName);
    emit saveFile(fileName);
}

void FileTab::pressReset(void) {
    emit reset();
}

void FileTab::pressAccept(void) {
    emit accept();
}

void FileTab::updateDevices(void) {
}

void FileTab::setDevice(int deviceid) {
    char sdeviceid[16];

    this->deviceid=deviceid;
    snprintf(sdeviceid,sizeof(sdeviceid),"%02X",deviceid);
    if(deviceid) {
        deviceValueLabel->setText(QString(sdeviceid));
    } else {
        deviceValueLabel->setText(QString("Not selected"));
        return;
    }
}

int FileTab::getDevice(void) {
    return deviceid;
}


