Subversion Repositories svn1

Rev

Rev 249 | Rev 272 | Go to most recent revision | Details | Compare with Previous | 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;
271 david 102
    bool has_RefError;
103
    int  bad_refs = 0;
145 - 104
    QRegExp csv_split("\"?,\"?");
105
    while (!in.atEnd())
106
    {
224 - 107
        QString line = in.readLine();
108
        line = line.trimmed();             // Remove leading and training White Space
109
        line.remove(0xA0);                 // M$ special uglyness
145 - 110
 
271 david 111
        has_RefError = hasRefError(line);
224 - 112
        QStringList parts = splitCsvData(line);
113
        QString first = parts.value(0);
114
        bool ok;
115
        if ( first.isEmpty() )
116
            continue;
117
        int team = first.toInt(&ok);
118
        if ( ! ok || team <= 0 )
119
            continue;
120
        tableWidget->setRowCount(1+ii);
145 - 121
 
224 - 122
        // Insert Team number
271 david 123
        QTableWidgetItem *item = new QTableWidgetItem(first);
124
        tableWidget->setItem(ii, 0, item );
224 - 125
        parts.removeFirst();
271 david 126
        if ( has_RefError )
127
        {
128
            item->setBackgroundColor(QColor(0,0,200,50));
129
            bad_refs++;
130
        }
145 - 131
 
224 - 132
        // Extract Team Name
271 david 133
        item = new QTableWidgetItem( parts.value(0) );
134
        tableWidget->setItem(ii, 1, item );
135
        if ( hasRefError(parts.value(0)) )
136
        {
137
            item->setBackgroundColor(QColor(0,0,200,50));
138
        }
224 - 139
        parts.removeFirst();
271 david 140
 
224 - 141
        // Extract Team Cat
271 david 142
        item = new QTableWidgetItem( parts.value(0)) ;
224 - 143
        tableWidget->setItem(ii, 2, item );
145 - 144
 
176 - 145
        if (config.lookup_class(qPrintable(parts.value(0)) ) <= 0 )
148 - 146
        {
147
            item->setBackgroundColor(QColor(200,0,0,50));
148
            bad_cat++;
149
        }
271 david 150
        if ( hasRefError(parts.value(0)) )
151
        {
152
            item->setBackgroundColor(QColor(0,0,200,50));
153
        }
148 - 154
        parts.removeFirst();
271 david 155
 
224 - 156
        int yy = 0;
157
        while ( parts.count() > 0)
158
        {
159
            // Name of competitor
271 david 160
            item = new QTableWidgetItem( parts.value(0));
161
            tableWidget->setItem(ii, 3+yy, item);
162
            if ( hasRefError(parts.value(0)) )
163
            {
164
                item->setBackgroundColor(QColor(0,0,200,50));
165
            }
224 - 166
            parts.removeFirst();
271 david 167
 
145 - 168
 
224 - 169
// Not loading age at the moment
170
// Reason: The CSV file is being create with a '0' for the NE teams
171
#if DO_AGE_LOAD
172
            // Posible age - if its a number
173
            int age = parts.value(0).toInt(&ok);
174
            if ( ok )
175
            {
176
                if ( age > 0 )
177
                {
178
                    tableWidget->setItem(ii, 4+yy, new QTableWidgetItem( parts.value(0)));
179
                }
180
                parts.removeFirst();
181
            }
182
#endif
183
            yy += 2;
184
        }
185
        ii++;
186
    }
221 - 187
    tableWidget->resizeColumnsToContents();
145 - 188
 
148 - 189
    // Report errors
271 david 190
    reportErrors( bad_cat, bad_refs);
191
 
146 david 192
    // Connect up buttons
145 - 193
 
221 - 194
    connect (load, SIGNAL(clicked()), this, SLOT(loadData()));
195
    connect(cancel, SIGNAL(clicked()), this, SLOT(close()));
144 - 196
}
197
 
198
QmDialogLoadExternalTeams::~QmDialogLoadExternalTeams()
199
{
221 - 200
 
144 - 201
}
146 david 202
 
203
void QmDialogLoadExternalTeams::loadData(void)
204
{
205
    qDebug ("LoadData");
206
    team_type team_buf;
148 - 207
    int bad_cat = 0;
271 david 208
    int bad_refs = 0;
221 - 209
    for ( int ii = 0; ii < tableWidget->rowCount(); ii++)
146 david 210
    {
221 - 211
        if ( tableWidget->isRowHidden(ii))
148 - 212
        {
213
            continue;
214
        }
215
        bool bad = false;
146 david 216
        QTableWidgetItem *item;
221 - 217
        item = tableWidget->item(ii,0);
146 david 218
        if (item)
219
        {
220
            int team = item->data(Qt::EditRole).toInt();
221
            if ( team > 0 && team <= config.max_team )
222
            {
223
                g_record( team, &team_buf );
224
 
225
                // Name
221 - 226
                item = tableWidget->item(ii,1);
146 david 227
                if (item)
228
                {
229
                    strncpy(team_buf.name , qPrintable(item->data(Qt::EditRole).toString()), sizeof(team_buf.name));
230
                }
231
 
232
                // Category
221 - 233
                item = tableWidget->item(ii,2);
146 david 234
                if (item)
235
                {
176 - 236
                    int category = config.lookup_class(qPrintable(item->data(Qt::EditRole).toString()) );
146 david 237
                    if (category)
238
                    {
239
                        team_buf.teamclass = category;
240
                    }
148 - 241
                    else
242
                    {
243
                        bad_cat++;
244
                        bad = true;
245
                    }
146 david 246
                }
247
 
248
                // Team member names and ages
249
                int member = 0;
221 - 250
                for (int yy = 3; yy < tableWidget->columnCount(); yy+= 2, member++)
146 david 251
                {
147 david 252
                    if (member > config.num_legs)
253
                    {
254
                        break;
255
                    }
221 - 256
                    item = tableWidget->item(ii,yy);
146 david 257
                    if (item)
258
                    {
259
                        strncpy(team_buf.members[member].name , qPrintable(item->data(Qt::EditRole).toString()), sizeof(team_buf.members[member].name));
271 david 260
                        if ( hasRefError(item->data(Qt::EditRole).toString()) )
261
                        {
262
                            bad = true;
263
                            bad_refs++;
264
                        }
146 david 265
                    }
221 - 266
                    item = tableWidget->item(ii,1+yy);
146 david 267
                    if (item)
268
                    {
269
                        int age = item->data(Qt::EditRole).toInt();
270
                        if (age)
271
                        {
272
                            team_buf.members[member].age = age;
273
                        }
274
                    }
275
                }
276
 
249 - 277
                team_buf.flags.valid = TRUE;
146 david 278
                put_team_record( team, &team_buf );
279
            }
148 - 280
            else
281
            {
282
                bad = true;
283
            }
146 david 284
        }
148 - 285
        else
286
        {
287
            bad = true;
288
        }
289
        if (!bad)
290
        {
221 - 291
            tableWidget->hideRow(ii);
148 - 292
        }
146 david 293
    }
148 - 294
 
295
    // Report errors
271 david 296
    reportErrors( bad_cat, bad_refs);
297
}
298
 
299
/*----------------------------------------------------------------------------
300
** FUNCTION           : reportErrors
301
**
302
** DESCRIPTION        : Report errors on the main window
303
**
304
**
305
** INPUTS             : bad_cat - count of category erors
306
**                      bad_refs - count of Reference errors
307
**
308
** RETURNS            :
309
**
310
----------------------------------------------------------------------------*/
311
 
312
void QmDialogLoadExternalTeams::reportErrors( int bad_cat, int bad_refs )
313
{
314
    if ( bad_cat && bad_refs )
148 - 315
    {
271 david 316
        MainWindow::showMessage("Invalid Categories in data and bad REFs");
317
    }
318
    else if (bad_cat)
319
    {
148 - 320
        MainWindow::showMessage("Invalid Categories in data");
321
    }
271 david 322
    else if (bad_refs)
323
    {
324
        MainWindow::showMessage("Imported data has bad REFs");
325
    }
326
 
146 david 327
}
149 - 328
 
329
/*========================================================================
330
 *
331
 *  Generate team name file
332
 *
333
 *  Purpose:
334
 *      This function is called to Generate team name file in the format
335
 *      that can be read by the load command
336
 *
337
 *      The file contains team number,Team name,Team class
338
 *      The operator is prompted to enter the name of the file
339
 *
340
 *  Parameters:
341
 *      None
342
 *
343
 *  Returns:
344
 *      Nothing
345
 *
346
 *========================================================================*/
347
void QmDialogLoadExternalTeams::storeData(const QString &efile)
348
{
349
    QFile file(efile);
350
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
351
    {
352
        MainWindow::showMessage("Cannot open external data file");
353
        return;
354
    }
355
    QTextStream out(&file);
356
 
357
    // Print headings
151 david 358
    out << toCsv("Team Number");
359
    out << "," << toCsv("Team Name");
360
    out << "," <<  toCsv("Class Abr");
149 - 361
 
362
    for( int j = 1; j <= config.num_legs; j++ )
363
    {
151 david 364
        out << "," << toCsv("Competitor Name");
365
        out << "," << toCsv("Age");
149 - 366
    }
367
    out << endl;
368
 
369
    /*
370
     * Put the data into the file
371
     */
170 - 372
    team_type   team_buf;
149 - 373
    for(int i = config.min_team; i <= config.max_team; i++ )
374
    {
375
        if( valid_field( i ) && g_record( i, &team_buf ) )
376
        {
377
            /*
378
            **  Basic information
379
            **      - Team number
380
            **      - Full team name
381
            */
151 david 382
            out << toCsv(team_buf.numb);
383
            out << "," << toCsv(team_buf.name);
384
            out << "," << toCsv(team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr);
149 - 385
 
386
            for(int j = 1; j <= config.num_legs; j++ )
387
            {
151 david 388
                out << "," << toCsv(team_buf.members[j-1].name);
389
                out << "," << toCsv(team_buf.members[j-1].age);
149 - 390
            }
391
            out <<endl;
392
        }
393
    }
394
}
151 david 395
 
396
QStringList QmDialogLoadExternalTeams::splitCsvData( const QString str)
397
{
398
    QStringList results;
399
 
400
    const QChar *data = str.constData();
401
    while (!data->isNull())
402
    {
403
        QString result;
404
        bool quoted = false;
405
        /*
406
        **  Extract the next record
407
        */
408
        while ( TRUE )
409
        {
410
            QChar uch = *data;
411
 
412
            /*
413
            **  End of the field
414
            */
415
            if ( uch == '\n' || uch == '\r' || uch == '\0' )
416
                break;
417
 
418
            data++;
419
 
420
            /*
421
            ** Ugly character from MS CSV files
422
            */
423
            if ( uch == (char) 0xA0 )
424
            {
425
                continue;
426
            }
427
 
428
            if ( !quoted && uch == ',' )
429
            {
430
                break;
431
            }
432
 
433
            /*
434
            **  An unquoted " will start scanning for a matching quote
435
            */
436
            if ( !quoted && uch == '"' )
437
            {
438
                quoted = true;
439
                continue;
440
            }
441
 
442
            /*
443
            **  A quoted " may be an embedded quote or the end of a quote
444
            */
445
            if ( quoted && uch == '"' )
446
            {
447
                if ( *data != '"' )
448
                {
449
                    quoted = FALSE;
450
                    continue;
451
                }
452
 
453
                /*
454
                **  Skip one " and pick up the next
455
                */
456
                ++data;
457
            }
458
 
459
            /*
460
            **  Save this character
461
            */
462
            result += uch;
463
        }
464
 
465
        /*
466
        **  Clean up the extracted string
467
        */
468
        results += result.trimmed();
469
     }
470
    return results;
471
}
271 david 472
/*----------------------------------------------------------------------------
473
** FUNCTION           : hasRefError
474
**
475
** DESCRIPTION        : Determine if a string contains an Excel Reference
476
**                      error: #REF!
477
**
478
**
479
** INPUTS             : data - String to test
480
**
481
** RETURNS            : True: Is an error
482
**
483
----------------------------------------------------------------------------*/
151 david 484
 
271 david 485
bool QmDialogLoadExternalTeams::hasRefError( const QString data)
486
{
487
    return data.contains("#REF!");
488
}
489
 
152 david 490
/*========================================================================
491
 *
492
 *  Generate team name file
493
 *
494
 *  Purpose:
495
 *      This function is called to Generate team name file
496
 *
497
 *      The file contains team number,Team name,Team class
498
 *      The operator is prompted to enter the name of the file
499
 *
500
 *  Parameters:
501
 *      None
502
 *
503
 *  Returns:
504
 *      Nothing
505
 *
506
 *========================================================================*/
507
 
508
void QmDialogLoadExternalTeams::storeTeamInfo(const QString &efile)
509
{
510
    QFile file(efile);
511
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
512
    {
513
        MainWindow::showMessage("Cannot open external team info file");
514
        return;
515
    }
516
    QTextStream out(&file);
517
 
518
     /*
519
     * Put the data into the file
520
     */
170 - 521
    team_type   team_buf;
152 david 522
    for(int i = config.min_team; i <= config.max_team; i++ )
523
    {
524
        if( valid_field( i ) && g_record( i, &team_buf ) )
525
        {
526
            /*
527
            **  Basic information
528
            **      - Team number
529
            **      - Full team name
530
            **      - Category
531
            */
532
            out.setFieldAlignment(QTextStream::AlignLeft);
533
            out.setFieldWidth(5);
534
            out << team_buf.numb;
535
            out.setFieldWidth(0);
536
            out << ",";
537
            out.setFieldWidth(MAX_TM_NAME+1);
538
            out << team_buf.name;
539
            out.setFieldWidth(0);
540
            out << ",";
541
            out << (team_buf.teamclass <= 0 ? "" : config.team_class[team_buf.teamclass - 1].abr);
542
            out <<endl;
543
        }
544
    }
545
}
546
 
151 david 547
QString QmDialogLoadExternalTeams::toCsv(const QString &str)
548
{
549
    QString result = QString(str);
550
    if ( result.contains("\"") || result.contains(",") )
551
    {
552
        result.replace("\"", "\"\"");
553
        result.prepend("\"");
554
        result.append("\"");
555
    }
556
    return result;
557
}
558
 
559
QString QmDialogLoadExternalTeams::toCsv(const int data)
560
{
561
    return QString::number(data);
562
}