Subversion Repositories svn1

Rev

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