Subversion Repositories svn1

Rev

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