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
 
250
                put_team_record( team, &team_buf );
251
            }
148 - 252
            else
253
            {
254
                bad = true;
255
            }
146 david 256
        }
148 - 257
        else
258
        {
259
            bad = true;
260
        }
261
        if (!bad)
262
        {
221 - 263
            tableWidget->hideRow(ii);
148 - 264
        }
146 david 265
    }
148 - 266
 
267
    // Report errors
268
    if (bad_cat)
269
    {
270
        MainWindow::showMessage("Invalid Categories in data");
271
    }
146 david 272
}
149 - 273
 
274
/*========================================================================
275
 *
276
 *  Generate team name file
277
 *
278
 *  Purpose:
279
 *      This function is called to Generate team name file in the format
280
 *      that can be read by the load command
281
 *
282
 *      The file contains team number,Team name,Team class
283
 *      The operator is prompted to enter the name of the file
284
 *
285
 *  Parameters:
286
 *      None
287
 *
288
 *  Returns:
289
 *      Nothing
290
 *
291
 *========================================================================*/
292
void QmDialogLoadExternalTeams::storeData(const QString &efile)
293
{
294
    QFile file(efile);
295
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
296
    {
297
        MainWindow::showMessage("Cannot open external data file");
298
        return;
299
    }
300
    QTextStream out(&file);
301
 
302
    // Print headings
151 david 303
    out << toCsv("Team Number");
304
    out << "," << toCsv("Team Name");
305
    out << "," <<  toCsv("Class Abr");
149 - 306
 
307
    for( int j = 1; j <= config.num_legs; j++ )
308
    {
151 david 309
        out << "," << toCsv("Competitor Name");
310
        out << "," << toCsv("Age");
149 - 311
    }
312
    out << endl;
313
 
314
    /*
315
     * Put the data into the file
316
     */
170 - 317
    team_type   team_buf;
149 - 318
    for(int i = config.min_team; i <= config.max_team; i++ )
319
    {
320
        if( valid_field( i ) && g_record( i, &team_buf ) )
321
        {
322
            /*
323
            **  Basic information
324
            **      - Team number
325
            **      - Full team name
326
            */
151 david 327
            out << toCsv(team_buf.numb);
328
            out << "," << toCsv(team_buf.name);
329
            out << "," << toCsv(team_buf.teamclass == 0 ? "" : config.team_class[team_buf.teamclass - 1].abr);
149 - 330
 
331
            for(int j = 1; j <= config.num_legs; j++ )
332
            {
151 david 333
                out << "," << toCsv(team_buf.members[j-1].name);
334
                out << "," << toCsv(team_buf.members[j-1].age);
149 - 335
            }
336
            out <<endl;
337
        }
338
    }
339
}
151 david 340
 
341
QStringList QmDialogLoadExternalTeams::splitCsvData( const QString str)
342
{
343
    QStringList results;
344
 
345
    const QChar *data = str.constData();
346
    while (!data->isNull())
347
    {
348
        QString result;
349
        bool quoted = false;
350
        /*
351
        **  Extract the next record
352
        */
353
        while ( TRUE )
354
        {
355
            QChar uch = *data;
356
 
357
            /*
358
            **  End of the field
359
            */
360
            if ( uch == '\n' || uch == '\r' || uch == '\0' )
361
                break;
362
 
363
            data++;
364
 
365
            /*
366
            ** Ugly character from MS CSV files
367
            */
368
            if ( uch == (char) 0xA0 )
369
            {
370
                continue;
371
            }
372
 
373
            if ( !quoted && uch == ',' )
374
            {
375
                break;
376
            }
377
 
378
            /*
379
            **  An unquoted " will start scanning for a matching quote
380
            */
381
            if ( !quoted && uch == '"' )
382
            {
383
                quoted = true;
384
                continue;
385
            }
386
 
387
            /*
388
            **  A quoted " may be an embedded quote or the end of a quote
389
            */
390
            if ( quoted && uch == '"' )
391
            {
392
                if ( *data != '"' )
393
                {
394
                    quoted = FALSE;
395
                    continue;
396
                }
397
 
398
                /*
399
                **  Skip one " and pick up the next
400
                */
401
                ++data;
402
            }
403
 
404
            /*
405
            **  Save this character
406
            */
407
            result += uch;
408
        }
409
 
410
        /*
411
        **  Clean up the extracted string
412
        */
413
        results += result.trimmed();
414
     }
415
    return results;
416
}
417
 
152 david 418
/*========================================================================
419
 *
420
 *  Generate team name file
421
 *
422
 *  Purpose:
423
 *      This function is called to Generate team name file
424
 *
425
 *      The file contains team number,Team name,Team class
426
 *      The operator is prompted to enter the name of the file
427
 *
428
 *  Parameters:
429
 *      None
430
 *
431
 *  Returns:
432
 *      Nothing
433
 *
434
 *========================================================================*/
435
 
436
void QmDialogLoadExternalTeams::storeTeamInfo(const QString &efile)
437
{
438
    QFile file(efile);
439
    if ( ! file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text) )
440
    {
441
        MainWindow::showMessage("Cannot open external team info file");
442
        return;
443
    }
444
    QTextStream out(&file);
445
 
446
     /*
447
     * Put the data into the file
448
     */
170 - 449
    team_type   team_buf;
152 david 450
    for(int i = config.min_team; i <= config.max_team; i++ )
451
    {
452
        if( valid_field( i ) && g_record( i, &team_buf ) )
453
        {
454
            /*
455
            **  Basic information
456
            **      - Team number
457
            **      - Full team name
458
            **      - Category
459
            */
460
            out.setFieldAlignment(QTextStream::AlignLeft);
461
            out.setFieldWidth(5);
462
            out << team_buf.numb;
463
            out.setFieldWidth(0);
464
            out << ",";
465
            out.setFieldWidth(MAX_TM_NAME+1);
466
            out << team_buf.name;
467
            out.setFieldWidth(0);
468
            out << ",";
469
            out << (team_buf.teamclass <= 0 ? "" : config.team_class[team_buf.teamclass - 1].abr);
470
            out <<endl;
471
        }
472
    }
473
}
474
 
151 david 475
QString QmDialogLoadExternalTeams::toCsv(const QString &str)
476
{
477
    QString result = QString(str);
478
    if ( result.contains("\"") || result.contains(",") )
479
    {
480
        result.replace("\"", "\"\"");
481
        result.prepend("\"");
482
        result.append("\"");
483
    }
484
    return result;
485
}
486
 
487
QString QmDialogLoadExternalTeams::toCsv(const int data)
488
{
489
    return QString::number(data);
490
}