Subversion Repositories svn1

Rev

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

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