Subversion Repositories svn1-original

Rev

Rev 335 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <QFileDialog>
#include <QFileInfo>
#include "qmdownloadlegtimes.h"
#include "ui_qmdownloadlegtimes.h"
#include "mainwindow.h"

#include    "consts.h"
#include    "structs.h"
#include    "proto.h"

QmDownloadLegTimes::QmDownloadLegTimes(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QmDownloadLegTimes)
{
    ui->setupUi(this);
    connect(ui->cancel, SIGNAL(clicked()), this, SLOT(close()));
    connect(ui->save, SIGNAL(clicked()), this, SLOT(save()));
    connect(ui->legNumber, SIGNAL(valueChanged(int)), this, SLOT(legNumberChanged(int)));
    connect(ui->legStart, SIGNAL(clicked()), this, SLOT(updateFileName()));
    connect(ui->legEnd, SIGNAL(clicked()), this, SLOT(updateFileName()));

    ui->legNumber->setMaximum(config.num_legs);
    legNumberChanged(0);
}

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

void QmDownloadLegTimes::legNumberChanged(int leg)
{
    ui->save->setEnabled(leg != 0);
    updateFileName();
}

/*----------------------------------------------------------------------------
** FUNCTION           : save
**
** DESCRIPTION        : Save leg time information to an external file
**
**
** INPUTS             :
**
** RETURNS            :
**
----------------------------------------------------------------------------*/

void QmDownloadLegTimes::save(void)
{
    bool manstart = !ui->legEnd->isChecked();
    int leg = ui->legNumber->value();

    /*
     * Locate the required data file and prepare for processing
     */
    QString filename = ui->fileName->text();
    MainWindow::showMessage(QString("Using: ") + filename);

    /*
    **  Join a directory and the suggested filename 
    **  The directory will be either
    **      The remembers directory
    **      Appications 'filepath'
    */
    filename = QDir(appSettings->value("Recent/SaveLegTimesDir",filepath).toString()).filePath(filename);
    qDebug("Using:%s", qPrintable(filename));

    /*
    ** Let the user navigate to where we will store the file
    */
    filename = QFileDialog::getSaveFileName(
        this, tr("Store Leg Data to File"),
        filename,
        tr("Data (*.txt);;All (*.*)"),
        0
        );
    if ( filename.isEmpty() )
    {
        return;
    }

    /*
    ** Remember the directory part of the user specified file name 
    ** Will put future files in the directory too 
    */
    QFileInfo fileinfo(filename);
    appSettings->setValue("Recent/SaveLegTimesDir", fileinfo.absoluteDir().absolutePath());

    /*
    ** Open the file for output.
    */
    QFile file;
    file.setFileName(filename);
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
    {
        MainWindow::showMessage("Cannot open external leg file");
        return;
    }
    QTextStream out(&file);

    /*
     * Write the data to the data file
     */
    team_type   team_buf;
    for(int i = config.min_team; i <= config.max_team; i++ )
    {
        if( valid_field( i ) && g_record( i, &team_buf ) )
        {
            time_t ptime;
            if (manstart)
                ptime = team_buf.leg[leg].start ;
            else
                ptime = team_buf.leg[leg].end;
            if (ptime >= 0 )
            {
                out << i;
                out << " ";
                out <<  time_a( ptime );
                out << endl;
            }
        }
    }
}

void QmDownloadLegTimes::updateFileName(void)
{
    bool manstart = !ui->legEnd->isChecked();
    int leg = ui->legNumber->value();
    if (leg)
    {
        ui->fileName->setText(QString("%1leg%2.txt").arg(manstart ? "S" : "").arg(QString::number(leg)) );
    }
    else
    {
        ui->fileName->clear();
    }
}