Subversion Repositories svn1

Rev

Rev 377 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 - 1
#include <QFileDialog>
320 david 2
#include <QFileInfo>
211 - 3
#include "qmdownloadlegtimes.h"
4
#include "ui_qmdownloadlegtimes.h"
5
#include "mainwindow.h"
6
 
7
#include    "consts.h"
8
#include    "structs.h"
9
#include    "proto.h"
10
 
11
QmDownloadLegTimes::QmDownloadLegTimes(QWidget *parent) :
12
    QDialog(parent),
13
    ui(new Ui::QmDownloadLegTimes)
14
{
15
    ui->setupUi(this);
16
    connect(ui->cancel, SIGNAL(clicked()), this, SLOT(close()));
17
    connect(ui->save, SIGNAL(clicked()), this, SLOT(save()));
18
    connect(ui->legNumber, SIGNAL(valueChanged(int)), this, SLOT(legNumberChanged(int)));
212 - 19
    connect(ui->legStart, SIGNAL(clicked()), this, SLOT(updateFileName()));
20
    connect(ui->legEnd, SIGNAL(clicked()), this, SLOT(updateFileName()));
211 - 21
 
22
    ui->legNumber->setMaximum(config.num_legs);
23
    legNumberChanged(0);
24
}
25
 
26
QmDownloadLegTimes::~QmDownloadLegTimes()
27
{
28
    delete ui;
29
}
30
 
31
void QmDownloadLegTimes::legNumberChanged(int leg)
32
{
33
    ui->save->setEnabled(leg != 0);
212 - 34
    updateFileName();
211 - 35
}
36
 
320 david 37
/*----------------------------------------------------------------------------
38
** FUNCTION           : save
39
**
40
** DESCRIPTION        : Save leg time information to an external file
41
**
42
**
43
** INPUTS             :
44
**
45
** RETURNS            :
46
**
47
----------------------------------------------------------------------------*/
48
 
211 - 49
void QmDownloadLegTimes::save(void)
50
{
51
    bool manstart = !ui->legEnd->isChecked();
52
    int leg = ui->legNumber->value();
53
 
320 david 54
    /*
55
     * Locate the required data file and prepare for processing
56
     */
212 - 57
    QString filename = ui->fileName->text();
211 - 58
    MainWindow::showMessage(QString("Using: ") + filename);
320 david 59
 
60
    /*
61
    **  Join a directory and the suggested filename 
62
    **  The directory will be either
63
    **      The remembers directory
64
    **      Appications 'filepath'
65
    */
66
    filename = QDir(appSettings->value("Recent/SaveLegTimesDir",filepath).toString()).filePath(filename);
211 - 67
    qDebug("Using:%s", qPrintable(filename));
68
 
320 david 69
    /*
70
    ** Let the user navigate to where we will store the file
71
    */
72
    filename = QFileDialog::getSaveFileName(
73
        this, tr("Store Leg Data to File"),
74
        filename,
75
        tr("Data (*.txt);;All (*.*)"),
76
 
77
        );
212 - 78
    if ( filename.isEmpty() )
79
    {
80
        return;
81
    }
82
 
320 david 83
    /*
84
    ** Remember the directory part of the user specified file name 
85
    ** Will put future files in the directory too 
86
    */
87
    QFileInfo fileinfo(filename);
88
    appSettings->setValue("Recent/SaveLegTimesDir", fileinfo.absoluteDir().absolutePath());
212 - 89
 
320 david 90
    /*
91
    ** Open the file for output.
92
    */
211 - 93
    QFile file;
94
    file.setFileName(filename);
95
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
96
    {
97
        MainWindow::showMessage("Cannot open external leg file");
98
        return;
99
    }
100
    QTextStream out(&file);
101
 
102
    /*
103
     * Write the data to the data file
104
     */
105
    team_type   team_buf;
106
    for(int i = config.min_team; i <= config.max_team; i++ )
107
    {
108
        if( valid_field( i ) && g_record( i, &team_buf ) )
109
        {
110
            time_t ptime;
111
            if (manstart)
112
                ptime = team_buf.leg[leg].start ;
113
            else
114
                ptime = team_buf.leg[leg].end;
115
            if (ptime >= 0 )
116
            {
117
                out << i;
118
                out << " ";
119
                out <<  time_a( ptime );
120
                out << endl;
121
            }
122
        }
212 - 123
    }
124
}
211 - 125
 
212 - 126
void QmDownloadLegTimes::updateFileName(void)
127
{
128
    bool manstart = !ui->legEnd->isChecked();
129
    int leg = ui->legNumber->value();
130
    if (leg)
131
    {
132
        ui->fileName->setText(QString("%1leg%2.txt").arg(manstart ? "S" : "").arg(QString::number(leg)) );
211 - 133
    }
212 - 134
    else
135
    {
136
        ui->fileName->clear();
137
    }
211 - 138
}
139