Subversion Repositories svn1

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 - 1
#include "qmdialogloadexternalteams.h"
238 - 2
//#include "ui_qmdialogloadexternalteams.h"
145 - 3
#include "QFile"
4
#include "QTextStream"
5
#include "mainwindow.h"
6
#include <QRegExp>
221 - 7
#include <QtGui/QHBoxLayout>
8
#include <QtGui/QVBoxLayout>
9
#include <QtGui/QWidget>
238 - 10
#include <QHeaderView>
144 - 11
 
145 - 12
#include    "consts.h"
13
#include    "structs.h"
14
#include    "proto.h"
15
 
16
QmDialogLoadExternalTeams::QmDialogLoadExternalTeams(const QString &efile,QWidget *parent) :
221 - 17
    QDialog(parent)
224 - 18
{
221 - 19
 
20
    //
21
    // Create Windows
22
    //
23
    resize(550, 500);
24
    setSizeGripEnabled(true);
145 - 25
    setWindowTitle("Load External Team Data");
26
 
221 - 27
    QVBoxLayout *verticalLayout;
28
    verticalLayout = new QVBoxLayout(this);
29
    verticalLayout->setContentsMargins(0, 0, 0, 0);
30
 
223 - 31
    QVBoxLayout *verticalLayout2;
32
    verticalLayout2 = new QVBoxLayout(this);
33
    verticalLayout2->setContentsMargins(5, 5, 5, 5);
34
 
221 - 35
    QHBoxLayout *horizontalLayout;
36
    horizontalLayout = new QHBoxLayout();
37
 
38
    groupBox = new QGroupBox(this);
39
    groupBox->setTitle("Data");
224 - 40
    verticalLayout->addWidget(groupBox);
41
    groupBox->setLayout(verticalLayout2);
221 - 42
 
224 - 43
    tableWidget = new QTableWidget(groupBox);
44
    tableWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
45
    tableWidget->setGeometry(QRect(15, 21, 501, 421));
46
    tableWidget->setCornerButtonEnabled(false);
47
    tableWidget->verticalHeader()->setVisible(false);
48
    tableWidget->verticalHeader()->setDefaultSectionSize(20);
49
    tableWidget->verticalHeader()->setHighlightSections(true);
50
    verticalLayout2->addWidget(tableWidget);
221 - 51
 
52
    fileName = new QLineEdit(this);
53
    fileName->setObjectName(QString::fromUtf8("fileName"));
54
    fileName->setGeometry(QRect(20, 470, 331, 20));
55
    fileName->setReadOnly(true);
56
    fileName->setText(efile);
57
    horizontalLayout->addWidget(fileName);
58
 
59
 
60
 
61
    load = new QPushButton(this);
62
    load->setObjectName(QString::fromUtf8("load"));
63
    load->setGeometry(QRect(370, 470, 75, 23));
64
    load->setText("Load");
65
    horizontalLayout->addWidget(load);
66
 
67
    cancel = new QPushButton(this);
68
    cancel->setObjectName(QString::fromUtf8("cancel"));
69
    cancel->setGeometry(QRect(460, 470, 75, 23));
70
    cancel->setAutoDefault(false);
71
    cancel->setText("Cancel");
72
    horizontalLayout->addWidget(cancel);
73
 
74
    verticalLayout->addLayout(horizontalLayout);
75
 
76
 
145 - 77
    // Open the users file
78
 
79
    QFile file(efile);
80
    if ( ! file.open(QIODevice::ReadOnly | QIODevice::Text) )
81
    {
82
        MainWindow::showMessage("Cannot open external data file");
83
        return;
84
    }
149 - 85
    MainWindow::showMessage("Loading External Data");
145 - 86
 
149 - 87
    // Insert column headers
221 - 88
    tableWidget->setColumnCount(3 + ( 2 * config.num_legs) );
149 - 89
    QStringList labels;
90
    labels << "Team" << "Team Name" << "Cat";
91
    for (int ii = 1; ii <= config.num_legs; ii++ )
92
    {
93
        labels += QString("Leg:%1").arg(QString::number(ii));
151 david 94
        labels += QString("Age:%1").arg(QString::number(ii));
149 - 95
    }
221 - 96
    tableWidget->setHorizontalHeaderLabels(labels);
145 - 97
 
98
    // Process Each line of the file
99
    QTextStream in(&file);
100
    int ii = 0;
148 - 101
    int bad_cat = 0;
145 - 102
    QRegExp csv_split("\"?,\"?");
103
    while (!in.atEnd())
104
    {
224 - 105
        QString line = in.readLine();
106
        line = line.trimmed();             // Remove leading and training White Space
107
        line.remove(0xA0);                 // M$ special uglyness
145 - 108
 
224 - 109
        QStringList parts = splitCsvData(line);
110
        QString first = parts.value(0);
111
        bool ok;
112
        if ( first.isEmpty() )
113
            continue;
114
        int team = first.toInt(&ok);
115
        if ( ! ok || team <= 0 )
116
            continue;
117
        tableWidget->setRowCount(1+ii);
145 - 118
 
224 - 119
        // Insert Team number
120
        tableWidget->setItem(ii, 0, new QTableWidgetItem(first));
121
        parts.removeFirst();
145 - 122
 
224 - 123
        // Extract Team Name
124
        tableWidget->setItem(ii, 1, new QTableWidgetItem( parts.value(0) ) );
125
        parts.removeFirst();
145 - 126
 
224 - 127
        // Extract Team Cat
128
        QTableWidgetItem *item = new QTableWidgetItem( parts.value(0)) ;
129
        tableWidget->setItem(ii, 2, item );
145 - 130
 
176 - 131
        if (config.lookup_class(qPrintable(parts.value(0)) ) <= 0 )
148 - 132
        {
133
            item->setBackgroundColor(QColor(200,0,0,50));
134
            bad_cat++;
135
        }
136
        parts.removeFirst();
137
 
224 - 138
        int yy = 0;
139
        while ( parts.count() > 0)
140
        {
141
            // Name of competitor
142
            tableWidget->setItem(ii, 3+yy, new QTableWidgetItem( parts.value(0)));
143
            parts.removeFirst();
145 - 144
 
224 - 145
// Not loading age at the moment
146
// Reason: The CSV file is being create with a '0' for the NE teams
147
#if DO_AGE_LOAD
148
            // Posible age - if its a number
149
            int age = parts.value(0).toInt(&ok);
150
            if ( ok )
151
            {
152
                if ( age > 0 )
153
                {
154
                    tableWidget->setItem(ii, 4+yy, new QTableWidgetItem( parts.value(0)));
155
                }
156
                parts.removeFirst();
157
            }
158
#endif
159
            yy += 2;
160
        }
161
        ii++;
162
    }
221 - 163
    tableWidget->resizeColumnsToContents();
145 - 164
 
148 - 165
    // Report errors
166
    if (bad_cat)
167
    {
168
        MainWindow::showMessage("Invalid Categories in data");
169
    }
170
 
146 david 171
    // Connect up buttons
145 - 172
 
221 - 173
    connect (load, SIGNAL(clicked()), this, SLOT(loadData()));
174
    connect(cancel, SIGNAL(clicked()), this, SLOT(close()));
144 - 175
}
176
 
177
QmDialogLoadExternalTeams::~QmDialogLoadExternalTeams()
178
{
221 - 179
 
144 - 180
}
146 david 181
 
182
void QmDialogLoadExternalTeams::loadData(void)
183
{
184
    qDebug ("LoadData");
185
    team_type team_buf;
148 - 186
    int bad_cat = 0;
221 - 187
    for ( int ii = 0; ii < tableWidget->rowCount(); ii++)
146 david 188
    {
221 - 189
        if ( tableWidget->isRowHidden(ii))
148 - 190
        {
191
            continue;
192
        }
193
        bool bad = false;
146 david 194
        QTableWidgetItem *item;
221 - 195
        item = tableWidget->item(ii,0);
146 david 196
        if (item)
197
        {
198
            int team = item->data(Qt::EditRole).toInt();
199
            if ( team > 0 && team <= config.max_team )
200
            {
201
                g_record( team, &team_buf );
202
 
203
                // Name
221 - 204
                item = tableWidget->item(ii,1);
146 david 205
                if (item)
206
                {
207
                    strncpy(team_buf.name , qPrintable(item->data(Qt::EditRole).toString()), sizeof(team_buf.name));
208
                }
209
 
210
                // Category
221 - 211
                item = tableWidget->item(ii,2);
146 david 212
                if (item)
213
                {
176 - 214
                    int category = config.lookup_class(qPrintable(item->data(Qt::EditRole).toString()) );
146 david 215
                    if (category)
216
                    {
217
                        team_buf.teamclass = category;
218
                    }
148 - 219
                    else
220
                    {
221
                        bad_cat++;
222
                        bad = true;
223
                    }
146 david 224
                }
225
 
226
                // Team member names and ages
227
                int member = 0;
221 - 228
                for (int yy = 3; yy < tableWidget->columnCount(); yy+= 2, member++)
146 david 229
                {
147 david 230
                    if (member > config.num_legs)
231
                    {
232
                        break;
233
                    }
221 - 234
                    item = tableWidget->item(ii,yy);
146 david 235
                    if (item)
236
                    {
237
                        strncpy(team_buf.members[member].name , qPrintable(item->data(Qt::EditRole).toString()), sizeof(team_buf.members[member].name));
238
                    }
221 - 239
                    item = tableWidget->item(ii,1+yy);
146 david 240
                    if (item)
241
                    {
242
                        int age = item->data(Qt::EditRole).toInt();
243
                        if (age)
244
                        {
245
                            team_buf.members[member].age = age;
246
                        }
247
                    }
248
                }
249
 
249 - 250
                team_buf.flags.valid = TRUE;
146 david 251
                put_team_record( team, &team_buf );
252
            }
148 - 253
            else
254
            {
255
                bad = true;
256
            }
146 david 257
        }
148 - 258
        else
259
        {
260
            bad = true;
261
        }
262
        if (!bad)
263
        {
221 - 264
            tableWidget->hideRow(ii);
148 - 265
        }
146 david 266
    }
148 - 267
 
268
    // Report errors
269
    if (bad_cat)
270
    {
271
        MainWindow::showMessage("Invalid Categories in data");
272
    }
146 david 273
}
149 - 274
 
275
/*========================================================================
276
 *
277
 *  Generate team name file
278
 *
279
 *  Purpose:
280
 *      This function is called to Generate team name file in the format
281
 *      that can be read by the load command
282
 *
283
 *      The file contains team number,Team name,Team class
284
 *      The operator is prompted to enter the name of the file
285
 *
286
 *  Parameters:
287
 *      None
288
 *
289
 *  Returns:
290
 *      Nothing
291
 *
292
 *========================================================================*/
293
void QmDialogLoadExternalTeams::storeData(const QString &efile)
294
{
295
    QFile file(efile);
296
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
297
    {
298
        MainWindow::showMessage("Cannot open external data file");
299
        return;
300
    }
301
    QTextStream out(&file);
302
 
303
    // Print headings
151 david 304
    out << toCsv("Team Number");
305
    out << "," << toCsv("Team Name");
306
    out << "," <<  toCsv("Class Abr");
149 - 307
 
308
    for( int j = 1; j <= config.num_legs; j++ )
309
    {
151 david 310
        out << "," << toCsv("Competitor Name");
311
        out << "," << toCsv("Age");
149 - 312
    }
313
    out << endl;
314
 
315
    /*
316
     * Put the data into the file
317
     */
170 - 318
    team_type   team_buf;
149 - 319
    for(int i = config.min_team; i <= config.max_team; i++ )
320
    {
321
        if( valid_field( i ) && g_record( i, &team_buf ) )
322
        {
323
            /*
324
            **  Basic information
325
            **      - Team number
326
            **      - Full team name
327
            */
151 david 328
            out << toCsv(team_buf.numb);
329
            out << "," << toCsv(team_buf.name);
330
            out << "," << toCsv(team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr);
149 - 331
 
332
            for(int j = 1; j <= config.num_legs; j++ )
333
            {
151 david 334
                out << "," << toCsv(team_buf.members[j-1].name);
335
                out << "," << toCsv(team_buf.members[j-1].age);
149 - 336
            }
337
            out <<endl;
338
        }
339
    }
340
}
151 david 341
 
342
QStringList QmDialogLoadExternalTeams::splitCsvData( const QString str)
343
{
344
    QStringList results;
345
 
346
    const QChar *data = str.constData();
347
    while (!data->isNull())
348
    {
349
        QString result;
350
        bool quoted = false;
351
        /*
352
        **  Extract the next record
353
        */
354
        while ( TRUE )
355
        {
356
            QChar uch = *data;
357
 
358
            /*
359
            **  End of the field
360
            */
361
            if ( uch == '\n' || uch == '\r' || uch == '\0' )
362
                break;
363
 
364
            data++;
365
 
366
            /*
367
            ** Ugly character from MS CSV files
368
            */
369
            if ( uch == (char) 0xA0 )
370
            {
371
                continue;
372
            }
373
 
374
            if ( !quoted && uch == ',' )
375
            {
376
                break;
377
            }
378
 
379
            /*
380
            **  An unquoted " will start scanning for a matching quote
381
            */
382
            if ( !quoted && uch == '"' )
383
            {
384
                quoted = true;
385
                continue;
386
            }
387
 
388
            /*
389
            **  A quoted " may be an embedded quote or the end of a quote
390
            */
391
            if ( quoted && uch == '"' )
392
            {
393
                if ( *data != '"' )
394
                {
395
                    quoted = FALSE;
396
                    continue;
397
                }
398
 
399
                /*
400
                **  Skip one " and pick up the next
401
                */
402
                ++data;
403
            }
404
 
405
            /*
406
            **  Save this character
407
            */
408
            result += uch;
409
        }
410
 
411
        /*
412
        **  Clean up the extracted string
413
        */
414
        results += result.trimmed();
415
     }
416
    return results;
417
}
418
 
152 david 419
/*========================================================================
420
 *
421
 *  Generate team name file
422
 *
423
 *  Purpose:
424
 *      This function is called to Generate team name file
425
 *
426
 *      The file contains team number,Team name,Team class
427
 *      The operator is prompted to enter the name of the file
428
 *
429
 *  Parameters:
430
 *      None
431
 *
432
 *  Returns:
433
 *      Nothing
434
 *
435
 *========================================================================*/
436
 
437
void QmDialogLoadExternalTeams::storeTeamInfo(const QString &efile)
438
{
439
    QFile file(efile);
440
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
441
    {
442
        MainWindow::showMessage("Cannot open external team info file");
443
        return;
444
    }
445
    QTextStream out(&file);
446
 
447
     /*
448
     * Put the data into the file
449
     */
170 - 450
    team_type   team_buf;
152 david 451
    for(int i = config.min_team; i <= config.max_team; i++ )
452
    {
453
        if( valid_field( i ) && g_record( i, &team_buf ) )
454
        {
455
            /*
456
            **  Basic information
457
            **      - Team number
458
            **      - Full team name
459
            **      - Category
460
            */
461
            out.setFieldAlignment(QTextStream::AlignLeft);
462
            out.setFieldWidth(5);
463
            out << team_buf.numb;
464
            out.setFieldWidth(0);
465
            out << ",";
466
            out.setFieldWidth(MAX_TM_NAME+1);
467
            out << team_buf.name;
468
            out.setFieldWidth(0);
469
            out << ",";
470
            out << (team_buf.teamclass <= 0 ? "" : config.team_class[team_buf.teamclass - 1].abr);
471
            out <<endl;
472
        }
473
    }
474
}
475
 
151 david 476
QString QmDialogLoadExternalTeams::toCsv(const QString &str)
477
{
478
    QString result = QString(str);
479
    if ( result.contains("\"") || result.contains(",") )
480
    {
481
        result.replace("\"", "\"\"");
482
        result.prepend("\"");
483
        result.append("\"");
484
    }
485
    return result;
486
}
487
 
488
QString QmDialogLoadExternalTeams::toCsv(const int data)
489
{
490
    return QString::number(data);
491
}