Subversion Repositories svn1

Rev

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

Rev Author Line No. Line
171 david 1
#include "qmconfteams.h"
174 - 2
#include "qmconfig.h"
171 david 3
#include "ui_qmconfteams.h"
183 - 4
#include "consts.h"
5
#include "structs.h"
6
#include "proto.h"
7
#include "mainwindow.h"
8
#include "twinspinbox.h"
197 - 9
#include "qmeditaddendum.h"
380 david 10
#include "qmeditawards.h"
171 david 11
 
12
QmConfTeams::QmConfTeams(QWidget *parent) :
13
    QWidget(parent),
14
    ui(new Ui::QmConfigure)
15
{
16
    ui->setupUi(this);
385 david 17
 
18
    ui->editAddendum->setStyleSheet("QPushButton{font-size: 12px;font-family: Arial;}");
19
    ui->editAwardsFileName->setStyleSheet("QPushButton{font-size: 12px;font-family: Arial;}");
20
 
182 - 21
    connect(ui->pushButtonSave, SIGNAL(clicked()), this, SLOT(save()) );
22
    connect(ui->pushButtonRestore, SIGNAL(clicked()), this, SLOT(cancel()) );
197 - 23
    connect(ui->editAddendum, SIGNAL(clicked()), this, SLOT(editAddendum()));
380 david 24
    connect(ui->editAwardsFileName, SIGNAL(clicked()), this, SLOT(editAwards()));
25
    connect(ui->pushButtonExit, SIGNAL(clicked()), this, SLOT(quitApplication()));
171 david 26
 
380 david 27
 
180 - 28
    ui->spinBox_NeLeg->setMaximum(5);
29
    ui->spinBox_NeLeg->setMinimum(0);
30
    connect(ui->spinBox_NeLeg, SIGNAL(valueChanged(int)), this,SLOT(updateNe()));
31
    connect(ui->legNe1,SIGNAL(clicked()), this,SLOT(calcNe1()));
32
    connect(ui->legNe2,SIGNAL(clicked()), this,SLOT(calcNe2()));
33
    connect(ui->legNe3,SIGNAL(clicked()), this,SLOT(calcNe3()));
34
    connect(ui->legNe4,SIGNAL(clicked()), this,SLOT(calcNe4()));
35
    connect(ui->legNe5,SIGNAL(clicked()), this,SLOT(calcNe5()));
36
 
182 - 37
    connect(ui->eventName, SIGNAL(textChanged(QString)), this, SLOT(changed()));
38
    connect(ui->legName1, SIGNAL(textChanged(QString)), this, SLOT(changed()));
39
    connect(ui->legName2, SIGNAL(textChanged(QString)), this, SLOT(changed()));
40
    connect(ui->legName3, SIGNAL(textChanged(QString)), this, SLOT(changed()));
41
    connect(ui->legName4, SIGNAL(textChanged(QString)), this, SLOT(changed()));
42
    connect(ui->legName5, SIGNAL(textChanged(QString)), this, SLOT(changed()));
43
 
44
    connect(ui->ne_leg_abr, SIGNAL(textChanged(QString)), this, SLOT(changed()));
45
    connect(ui->addendum, SIGNAL(textChanged(QString)), this, SLOT(changed()));
380 david 46
    connect(ui->awardsFileName, SIGNAL(textChanged(QString)), this, SLOT(changed()));
182 - 47
    connect(ui->datafilename, SIGNAL(textChanged(QString)), this, SLOT(changed()));
48
    connect(ui->spinBox_LinesPerPage, SIGNAL(valueChanged(int)), this, SLOT(changed()));
49
    connect(ui->perfSkip, SIGNAL(stateChanged(int)), this, SLOT(changed()));
50
 
51
 
183 - 52
    ui->tn1->setLimits( 0, 999);
53
    ui->tn2->setLimits( 0, 999);
54
    ui->tn3->setLimits( 0, 999);
55
    ui->tn4->setLimits( 0, 999);
56
    ui->tn5->setLimits( 0, 999);
57
    ui->tn6->setLimits( 0, 999);
58
    ui->tn7->setLimits( 0, 999);
59
    ui->tn8->setLimits( 0, 999);
182 - 60
 
183 - 61
    connect(ui->tn1, SIGNAL(changed()), this, SLOT(changed()));
62
    connect(ui->tn2, SIGNAL(changed()), this, SLOT(changed()));
63
    connect(ui->tn3, SIGNAL(changed()), this, SLOT(changed()));
64
    connect(ui->tn4, SIGNAL(changed()), this, SLOT(changed()));
65
    connect(ui->tn5, SIGNAL(changed()), this, SLOT(changed()));
66
    connect(ui->tn6, SIGNAL(changed()), this, SLOT(changed()));
67
    connect(ui->tn7, SIGNAL(changed()), this, SLOT(changed()));
68
    connect(ui->tn8, SIGNAL(changed()), this, SLOT(changed()));
69
 
182 - 70
    populating = false;
71
    dirty = true;
380 david 72
    mustRestart = false;
73
    ui->pushButtonExit->hide();
171 david 74
    populate();
75
 
76
 
77
}
78
void QmConfTeams::populate(void)
79
{
182 - 80
    populating = true;
171 david 81
    ui->eventName->setText( config.event_name);
82
    ui->eventName->setMaxLength(MAX_EVENT_NAME);
83
 
84
    ui->addendum->setText(config.addendum);
85
    ui->addendum->setMaxLength(sizeof(config.addendum)-1);
86
 
87
    ui->datafilename->setText(config.datafilename);
197 - 88
    ui->datafilename->setMaxLength(sizeof(config.datafilename) - 1);
171 david 89
 
380 david 90
    ui->awardsFileName->setText(config.awardsfilename);
91
    ui->awardsFileName->setMaxLength(sizeof(config.awardsfilename) - 1);
92
 
171 david 93
    ui->legName1->setText(config.leg_name[0]);
94
    ui->legName1->setMaxLength(MAX_LEG_NAME);
95
    ui->legName2->setText(config.leg_name[1]);
96
    ui->legName2->setMaxLength(MAX_LEG_NAME);
97
    ui->legName3->setText(config.leg_name[2]);
98
    ui->legName3->setMaxLength(MAX_LEG_NAME);
99
    ui->legName4->setText(config.leg_name[3]);
100
    ui->legName4->setMaxLength(MAX_LEG_NAME);
101
    ui->legName5->setText(config.leg_name[4]);
102
    ui->legName5->setMaxLength(MAX_LEG_NAME);
103
 
183 - 104
    ui->tn1->setData( config.t_def[0].start, config.t_def[0].end);
105
    ui->tn2->setData( config.t_def[1].start, config.t_def[1].end);
106
    ui->tn3->setData( config.t_def[2].start, config.t_def[2].end);
107
    ui->tn4->setData( config.t_def[3].start, config.t_def[3].end);
108
    ui->tn5->setData( config.t_def[4].start, config.t_def[4].end);
109
    ui->tn6->setData( config.t_def[5].start, config.t_def[5].end);
110
    ui->tn7->setData( config.t_def[6].start, config.t_def[6].end);
111
    ui->tn8->setData( config.t_def[7].start, config.t_def[7].end);
171 david 112
 
113
    ui->spinBox_NeLeg->setValue(config.equestrian_leg);
180 - 114
    updateNe();
171 david 115
    ui->ne_leg_abr->setText(config.nonequestrian_class_abr);
116
    ui->ne_leg_abr->setMaxLength(sizeof(config.nonequestrian_class_abr)-1);
117
 
118
     ui->spinBox_LinesPerPage->setValue(config.lines_per_page);
119
     ui->perfSkip->setChecked( config.perf_skip );
182 - 120
    populating = false;
121
    updateChanged(false);
171 david 122
   }
123
 
180 - 124
void QmConfTeams::updateNe(void)
125
{
126
    int value = ui->spinBox_NeLeg->value();
127
    ui->legNe1->setChecked(value == 1);
128
    ui->legNe2->setChecked(value == 2);
129
    ui->legNe3->setChecked(value == 3);
130
    ui->legNe4->setChecked(value == 4);
131
    ui->legNe5->setChecked(value == 5);
182 - 132
    updateChanged(true);
180 - 133
}
134
 
135
void QmConfTeams::calcNe1(void){ui->spinBox_NeLeg->setValue(1); updateNe(); }
136
void QmConfTeams::calcNe2(void){ui->spinBox_NeLeg->setValue(2); updateNe(); }
137
void QmConfTeams::calcNe3(void){ui->spinBox_NeLeg->setValue(3); updateNe(); }
138
void QmConfTeams::calcNe4(void){ui->spinBox_NeLeg->setValue(4); updateNe(); }
139
void QmConfTeams::calcNe5(void){ui->spinBox_NeLeg->setValue(5); updateNe(); }
140
 
171 david 141
void QmConfTeams::save(void)
142
{
143
    int num_teams = 0;
144
 
145
    /*
146
    **    Copy original data
147
    */
176 - 148
    QmConfig    newcfg(config);
171 david 149
 
150
    /*
151
    **  Extract data from Widgets
152
    */
153
    strncpy(newcfg.event_name, qPrintable(ui->eventName->text()), sizeof(newcfg.event_name)) ;
197 - 154
    strncpy(newcfg.datafilename, qPrintable(ui->datafilename->text()), sizeof(newcfg.datafilename)) ;
155
    strncpy(newcfg.addendum, qPrintable(ui->addendum->text()), sizeof(newcfg.addendum)) ;
380 david 156
    strncpy(newcfg.awardsfilename, qPrintable(ui->awardsFileName->text()), sizeof(newcfg.awardsfilename)) ;
171 david 157
 
158
    strncpy(newcfg.leg_name[0], qPrintable(ui->legName1->text()), sizeof(newcfg.leg_name[0])) ;
159
    strncpy(newcfg.leg_name[1], qPrintable(ui->legName2->text()), sizeof(newcfg.leg_name[1])) ;
160
    strncpy(newcfg.leg_name[2], qPrintable(ui->legName3->text()), sizeof(newcfg.leg_name[2])) ;
161
    strncpy(newcfg.leg_name[3], qPrintable(ui->legName4->text()), sizeof(newcfg.leg_name[3])) ;
162
    strncpy(newcfg.leg_name[4], qPrintable(ui->legName5->text()), sizeof(newcfg.leg_name[4])) ;
163
 
183 - 164
    ui->tn1->getData(&newcfg.t_def[0].start, &newcfg.t_def[0].end);
165
    ui->tn2->getData(&newcfg.t_def[1].start, &newcfg.t_def[1].end);
166
    ui->tn3->getData(&newcfg.t_def[2].start, &newcfg.t_def[2].end);
167
    ui->tn4->getData(&newcfg.t_def[3].start, &newcfg.t_def[3].end);
168
    ui->tn5->getData(&newcfg.t_def[4].start, &newcfg.t_def[4].end);
169
    ui->tn6->getData(&newcfg.t_def[5].start, &newcfg.t_def[5].end);
170
    ui->tn7->getData(&newcfg.t_def[6].start, &newcfg.t_def[6].end);
171
    ui->tn8->getData(&newcfg.t_def[7].start, &newcfg.t_def[7].end);
171 david 172
 
173
    newcfg.equestrian_leg = ui->spinBox_NeLeg->value();
174
    strncpy(newcfg.nonequestrian_class_abr, qPrintable(ui->ne_leg_abr->text()), sizeof(newcfg.nonequestrian_class_abr)-1) ;
175
 
176
    newcfg.lines_per_page = ui->spinBox_LinesPerPage->value();
177
    newcfg.perf_skip = ui->perfSkip->isChecked();
178
 
179
    /*
180
    **  Sanity Test and Clean up
181
    */
182 - 182
    try
183
    {
184
        MainWindow::showMessage( "Saving Config");
171 david 185
 
182 - 186
        compact( newcfg.event_name );               /* Rip of leading white_space */
187
        for( int i = 0; i < MAX_LEGS; i++ )
188
            compact( newcfg.leg_name[i] );
189
        compact( newcfg.addendum );
190
        compact( newcfg.datafilename );
380 david 191
        compact( newcfg.awardsfilename );
182 - 192
 
193
        /*
171 david 194
     * Do all sorts of consistency tests on the data
195
     * Firstly - calculate the number of legs. Justify the data in the array
196
     */
197
 
182 - 198
        newcfg.num_legs = 0;
199
        for( int i = 0; i < MAX_LEGS; i++ )
200
            if( newcfg.leg_name[i][0] )
201
                newcfg.num_legs++;
202
        for( int i = newcfg.num_legs; i < MAX_LEGS; i++ )
171 david 203
        {
182 - 204
            if( newcfg.leg_name[i][0] )
205
            {
206
                throw( "Configuration error: Missing leg" );
207
            }
171 david 208
        }
209
 
210
 
182 - 211
        /*
171 david 212
     * Now do the team numbering stuff
213
     */
214
 
182 - 215
        for( int i = 0; i < MAX_TMS_SPLIT; i++ )
171 david 216
        {
182 - 217
            if( newcfg.t_def[i].start == 0 && newcfg.t_def[i].end == 0 )
218
                continue;
219
            if( newcfg.t_def[i].start > newcfg.t_def[i].end )
171 david 220
            {
182 - 221
                throw( tprintf( "Team definition error : End greater than start. %d to %d", newcfg.t_def[i].start, newcfg.t_def[i].end ));
222
 
171 david 223
            }
182 - 224
            for( int j = 0; j < i; j++ )
225
            {
226
                if( ( newcfg.t_def[i].start >= newcfg.t_def[j].start
227
                      && newcfg.t_def[i].start <= newcfg.t_def[j].end )
228
                    || ( newcfg.t_def[i].end >= newcfg.t_def[j].start
229
                         && newcfg.t_def[i].end <= newcfg.t_def[j].end ) )
230
                    {
231
                    throw (tprintf( "Team definition error : Overlapping definition. %d to %d",
232
                              newcfg.t_def[i].start, newcfg.t_def[i].end ));
233
                    }
234
            }
171 david 235
        }
236
 
182 - 237
        /*
171 david 238
     * Determine the number of team splits
239
     * There may be blank entries - these will be removed by sorting the array
240
     */
182 - 241
        qsort( ( char * ) newcfg.t_def, MAX_TMS_SPLIT, sizeof( *newcfg.t_def ), f_comp_int );
242
        for( newcfg.num_teams = 0; newcfg.num_teams < MAX_TMS_SPLIT; newcfg.num_teams++ )
243
            if( !newcfg.t_def[newcfg.num_teams].start )
244
                break;
171 david 245
 
182 - 246
        newcfg.min_team = newcfg.t_def[0].start;
247
        newcfg.max_team = newcfg.t_def[newcfg.num_teams - 1].end;
171 david 248
 
182 - 249
        /*
171 david 250
     **  Limit the number of entrants
251
     */
182 - 252
        for( int i = 0; i < MAX_TMS_SPLIT; i++ )
171 david 253
        {
182 - 254
            if( newcfg.t_def[i].start )
255
            {
256
                num_teams += newcfg.t_def[i].end - newcfg.t_def[i].start + 1;
257
            }
171 david 258
        }
259
#if defined(LIMIT_TEAMS) && (LIMIT_TEAMS > 0)
182 - 260
        if( num_teams > LIMIT_TEAMS )
261
        {
262
            throw( "Maximum number of teams exceeded - reduce number of teams" );
263
        }
171 david 264
#endif
265
 
266
#if defined (HI_TECH_C) || defined (__TURBOC__)
267
 
182 - 268
        /*
171 david 269
     * Ensure that the number of teams allocated can fit within available
270
     * Memory space. Ie: Can we malloc the report data structures
271
     */
182 - 272
        {
273
            long        sz;
171 david 274
 
182 - 275
            sz = sizeof( ty_s_data ) > sizeof( ty_s_aux ) ?
276
                 sizeof( ty_s_data ) : sizeof( ty_s_aux );
277
            if( sz * ( newcfg.max_team - newcfg.min_team + 2 ) > ( 1024L * 64L ) )
278
            {
279
                throw( "Too many teams: Reduce team spread." );
280
            }
171 david 281
        }
282
#endif
283
 
182 - 284
        /*
171 david 285
     **  Ensure the non-equestrian class and the equestrian leg are valid
286
     */
182 - 287
        if( newcfg.equestrian_leg )
171 david 288
        {
182 - 289
            if( newcfg.equestrian_leg > newcfg.num_legs )
290
            {
291
                throw( "Invalid non-equestrian leg number" );
292
            }
171 david 293
 
182 - 294
            /*
171 david 295
         **  Ensure that the entered non-equestrian class name does exist
296
         */
182 - 297
            newcfg.nonequestrian_class = newcfg.lookup_class( newcfg.nonequestrian_class_abr );
298
            if( newcfg.nonequestrian_class == 0 )
299
                MainWindow::showMessage( "WARNING: Non-equestrian class not found" );
300
        }
171 david 301
 
380 david 302
        //  Detect change in the number of legs configured
303
        //  Need to force application restart
304
        if ( config.num_legs != newcfg.num_legs || mustRestart ) {
305
            mustRestart = true;
306
        }
307
 
171 david 308
        config = newcfg;
176 - 309
        config.write_config();
182 - 310
        updateChanged(false);
311
 
171 david 312
    }
182 - 313
    catch ( const char * str )
314
    {
315
        MainWindow::showMessage(str);
316
    }
317
    catch ( ... )
318
    {
319
        MainWindow::showMessage ("QmConfTeams: Error of some form");
320
    }
171 david 321
}
322
 
323
void QmConfTeams::cancel(void)
324
{
325
    populate();
326
}
327
 
182 - 328
void QmConfTeams::changed(void)
329
{
330
    if ( populating )
331
        return;
332
    updateChanged(true);
333
}
171 david 334
 
182 - 335
void QmConfTeams::updateChanged(bool newDirty)
336
{
199 david 337
    ui->editAddendum->setEnabled(!ui->addendum->text().isEmpty());
182 - 338
    if (newDirty != dirty)
339
    {
340
        dirty = newDirty;
341
        if (dirty)
342
        {
343
            ui->Changed->setVisible(true);
344
            ui->pushButtonSave->setEnabled(true);
226 - 345
            ui->pushButtonSave->setStyleSheet("background-color: rgb(255, 0, 0);");
182 - 346
        }
347
        else
348
        {
349
            ui->Changed->setVisible(false);
350
            ui->pushButtonSave->setEnabled(false);
226 - 351
            ui->pushButtonSave->setStyleSheet("");
182 - 352
        }
353
    }
380 david 354
 
355
    if (mustRestart) {
356
        MainWindow::disableAllTabs();
357
        ui->pushButtonExit->show();
358
        ui->pushButtonExit->setEnabled(true);
359
        ui->pushButtonExit->setStyleSheet("background-color: rgb(255, 0, 0);");
360
    }
182 - 361
}
362
 
197 - 363
void QmConfTeams::editAddendum(void)
364
{
198 - 365
    QString fname = ui->addendum->text();
366
    if ( !fname.isEmpty() )
367
    {
199 david 368
        QmEditAddendum dialog(QmConfig::getAddendemFile(fname, true));
198 - 369
        dialog.exec();
370
    }
197 - 371
}
372
 
380 david 373
void QmConfTeams::editAwards(void)
374
{
375
    QString fname = ui->awardsFileName->text();
376
    if ( !fname.isEmpty() )
377
    {
378
        QmEditAwards dialog(QmConfig::getAddendemFile(fname, true));
379
        dialog.exec();
380
    }
381
}
382
 
171 david 383
QmConfTeams::~QmConfTeams()
384
{
385
    delete ui;
386
}
387
 
388
void QmConfTeams::changeEvent(QEvent *e)
389
{
390
    QWidget::changeEvent(e);
197 - 391
    //qDebug("QmConfTeams Event:%d", e->type());
171 david 392
    switch (e->type()) {
393
    case QEvent::LanguageChange:
394
        ui->retranslateUi(this);
395
        break;
396
    default:
397
        break;
398
    }
399
}
380 david 400
 
401
void QmConfTeams::quitApplication(void)
402
{
403
    QCoreApplication::quit();
404
}
405