Subversion Repositories svn1

Rev

Rev 377 | 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"
359 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);
182 - 17
    connect(ui->pushButtonSave, SIGNAL(clicked()), this, SLOT(save()) );
18
    connect(ui->pushButtonRestore, SIGNAL(clicked()), this, SLOT(cancel()) );
197 - 19
    connect(ui->editAddendum, SIGNAL(clicked()), this, SLOT(editAddendum()));
359 david 20
    connect(ui->editAwardsFileName, SIGNAL(clicked()), this, SLOT(editAwards()));
361 david 21
    connect(ui->pushButtonExit, SIGNAL(clicked()), this, SLOT(quitApplication()));
171 david 22
 
359 david 23
 
180 - 24
    ui->spinBox_NeLeg->setMaximum(5);
25
    ui->spinBox_NeLeg->setMinimum(0);
26
    connect(ui->spinBox_NeLeg, SIGNAL(valueChanged(int)), this,SLOT(updateNe()));
27
    connect(ui->legNe1,SIGNAL(clicked()), this,SLOT(calcNe1()));
28
    connect(ui->legNe2,SIGNAL(clicked()), this,SLOT(calcNe2()));
29
    connect(ui->legNe3,SIGNAL(clicked()), this,SLOT(calcNe3()));
30
    connect(ui->legNe4,SIGNAL(clicked()), this,SLOT(calcNe4()));
31
    connect(ui->legNe5,SIGNAL(clicked()), this,SLOT(calcNe5()));
32
 
182 - 33
    connect(ui->eventName, SIGNAL(textChanged(QString)), this, SLOT(changed()));
34
    connect(ui->legName1, SIGNAL(textChanged(QString)), this, SLOT(changed()));
35
    connect(ui->legName2, SIGNAL(textChanged(QString)), this, SLOT(changed()));
36
    connect(ui->legName3, SIGNAL(textChanged(QString)), this, SLOT(changed()));
37
    connect(ui->legName4, SIGNAL(textChanged(QString)), this, SLOT(changed()));
38
    connect(ui->legName5, SIGNAL(textChanged(QString)), this, SLOT(changed()));
39
 
40
    connect(ui->ne_leg_abr, SIGNAL(textChanged(QString)), this, SLOT(changed()));
41
    connect(ui->addendum, SIGNAL(textChanged(QString)), this, SLOT(changed()));
359 david 42
    connect(ui->awardsFileName, SIGNAL(textChanged(QString)), this, SLOT(changed()));
182 - 43
    connect(ui->datafilename, SIGNAL(textChanged(QString)), this, SLOT(changed()));
44
    connect(ui->spinBox_LinesPerPage, SIGNAL(valueChanged(int)), this, SLOT(changed()));
45
    connect(ui->perfSkip, SIGNAL(stateChanged(int)), this, SLOT(changed()));
46
 
47
 
183 - 48
    ui->tn1->setLimits( 0, 999);
49
    ui->tn2->setLimits( 0, 999);
50
    ui->tn3->setLimits( 0, 999);
51
    ui->tn4->setLimits( 0, 999);
52
    ui->tn5->setLimits( 0, 999);
53
    ui->tn6->setLimits( 0, 999);
54
    ui->tn7->setLimits( 0, 999);
55
    ui->tn8->setLimits( 0, 999);
182 - 56
 
183 - 57
    connect(ui->tn1, SIGNAL(changed()), this, SLOT(changed()));
58
    connect(ui->tn2, SIGNAL(changed()), this, SLOT(changed()));
59
    connect(ui->tn3, SIGNAL(changed()), this, SLOT(changed()));
60
    connect(ui->tn4, SIGNAL(changed()), this, SLOT(changed()));
61
    connect(ui->tn5, SIGNAL(changed()), this, SLOT(changed()));
62
    connect(ui->tn6, SIGNAL(changed()), this, SLOT(changed()));
63
    connect(ui->tn7, SIGNAL(changed()), this, SLOT(changed()));
64
    connect(ui->tn8, SIGNAL(changed()), this, SLOT(changed()));
65
 
182 - 66
    populating = false;
67
    dirty = true;
361 david 68
    mustRestart = false;
69
    ui->pushButtonExit->hide();
171 david 70
    populate();
71
 
72
 
73
}
74
void QmConfTeams::populate(void)
75
{
182 - 76
    populating = true;
171 david 77
    ui->eventName->setText( config.event_name);
78
    ui->eventName->setMaxLength(MAX_EVENT_NAME);
79
 
80
    ui->addendum->setText(config.addendum);
81
    ui->addendum->setMaxLength(sizeof(config.addendum)-1);
82
 
83
    ui->datafilename->setText(config.datafilename);
197 - 84
    ui->datafilename->setMaxLength(sizeof(config.datafilename) - 1);
171 david 85
 
359 david 86
    ui->awardsFileName->setText(config.awardsfilename);
87
    ui->awardsFileName->setMaxLength(sizeof(config.awardsfilename) - 1);
88
 
171 david 89
    ui->legName1->setText(config.leg_name[0]);
90
    ui->legName1->setMaxLength(MAX_LEG_NAME);
91
    ui->legName2->setText(config.leg_name[1]);
92
    ui->legName2->setMaxLength(MAX_LEG_NAME);
93
    ui->legName3->setText(config.leg_name[2]);
94
    ui->legName3->setMaxLength(MAX_LEG_NAME);
95
    ui->legName4->setText(config.leg_name[3]);
96
    ui->legName4->setMaxLength(MAX_LEG_NAME);
97
    ui->legName5->setText(config.leg_name[4]);
98
    ui->legName5->setMaxLength(MAX_LEG_NAME);
99
 
183 - 100
    ui->tn1->setData( config.t_def[0].start, config.t_def[0].end);
101
    ui->tn2->setData( config.t_def[1].start, config.t_def[1].end);
102
    ui->tn3->setData( config.t_def[2].start, config.t_def[2].end);
103
    ui->tn4->setData( config.t_def[3].start, config.t_def[3].end);
104
    ui->tn5->setData( config.t_def[4].start, config.t_def[4].end);
105
    ui->tn6->setData( config.t_def[5].start, config.t_def[5].end);
106
    ui->tn7->setData( config.t_def[6].start, config.t_def[6].end);
107
    ui->tn8->setData( config.t_def[7].start, config.t_def[7].end);
171 david 108
 
109
    ui->spinBox_NeLeg->setValue(config.equestrian_leg);
180 - 110
    updateNe();
171 david 111
    ui->ne_leg_abr->setText(config.nonequestrian_class_abr);
112
    ui->ne_leg_abr->setMaxLength(sizeof(config.nonequestrian_class_abr)-1);
113
 
114
     ui->spinBox_LinesPerPage->setValue(config.lines_per_page);
115
     ui->perfSkip->setChecked( config.perf_skip );
182 - 116
    populating = false;
117
    updateChanged(false);
171 david 118
   }
119
 
180 - 120
void QmConfTeams::updateNe(void)
121
{
122
    int value = ui->spinBox_NeLeg->value();
123
    ui->legNe1->setChecked(value == 1);
124
    ui->legNe2->setChecked(value == 2);
125
    ui->legNe3->setChecked(value == 3);
126
    ui->legNe4->setChecked(value == 4);
127
    ui->legNe5->setChecked(value == 5);
182 - 128
    updateChanged(true);
180 - 129
}
130
 
131
void QmConfTeams::calcNe1(void){ui->spinBox_NeLeg->setValue(1); updateNe(); }
132
void QmConfTeams::calcNe2(void){ui->spinBox_NeLeg->setValue(2); updateNe(); }
133
void QmConfTeams::calcNe3(void){ui->spinBox_NeLeg->setValue(3); updateNe(); }
134
void QmConfTeams::calcNe4(void){ui->spinBox_NeLeg->setValue(4); updateNe(); }
135
void QmConfTeams::calcNe5(void){ui->spinBox_NeLeg->setValue(5); updateNe(); }
136
 
171 david 137
void QmConfTeams::save(void)
138
{
139
    int num_teams = 0;
140
 
141
    /*
142
    **    Copy original data
143
    */
176 - 144
    QmConfig    newcfg(config);
171 david 145
 
146
    /*
147
    **  Extract data from Widgets
148
    */
149
    strncpy(newcfg.event_name, qPrintable(ui->eventName->text()), sizeof(newcfg.event_name)) ;
197 - 150
    strncpy(newcfg.datafilename, qPrintable(ui->datafilename->text()), sizeof(newcfg.datafilename)) ;
151
    strncpy(newcfg.addendum, qPrintable(ui->addendum->text()), sizeof(newcfg.addendum)) ;
359 david 152
    strncpy(newcfg.awardsfilename, qPrintable(ui->awardsFileName->text()), sizeof(newcfg.awardsfilename)) ;
171 david 153
 
154
    strncpy(newcfg.leg_name[0], qPrintable(ui->legName1->text()), sizeof(newcfg.leg_name[0])) ;
155
    strncpy(newcfg.leg_name[1], qPrintable(ui->legName2->text()), sizeof(newcfg.leg_name[1])) ;
156
    strncpy(newcfg.leg_name[2], qPrintable(ui->legName3->text()), sizeof(newcfg.leg_name[2])) ;
157
    strncpy(newcfg.leg_name[3], qPrintable(ui->legName4->text()), sizeof(newcfg.leg_name[3])) ;
158
    strncpy(newcfg.leg_name[4], qPrintable(ui->legName5->text()), sizeof(newcfg.leg_name[4])) ;
159
 
183 - 160
    ui->tn1->getData(&newcfg.t_def[0].start, &newcfg.t_def[0].end);
161
    ui->tn2->getData(&newcfg.t_def[1].start, &newcfg.t_def[1].end);
162
    ui->tn3->getData(&newcfg.t_def[2].start, &newcfg.t_def[2].end);
163
    ui->tn4->getData(&newcfg.t_def[3].start, &newcfg.t_def[3].end);
164
    ui->tn5->getData(&newcfg.t_def[4].start, &newcfg.t_def[4].end);
165
    ui->tn6->getData(&newcfg.t_def[5].start, &newcfg.t_def[5].end);
166
    ui->tn7->getData(&newcfg.t_def[6].start, &newcfg.t_def[6].end);
167
    ui->tn8->getData(&newcfg.t_def[7].start, &newcfg.t_def[7].end);
171 david 168
 
169
    newcfg.equestrian_leg = ui->spinBox_NeLeg->value();
170
    strncpy(newcfg.nonequestrian_class_abr, qPrintable(ui->ne_leg_abr->text()), sizeof(newcfg.nonequestrian_class_abr)-1) ;
171
 
172
    newcfg.lines_per_page = ui->spinBox_LinesPerPage->value();
173
    newcfg.perf_skip = ui->perfSkip->isChecked();
174
 
175
    /*
176
    **  Sanity Test and Clean up
177
    */
182 - 178
    try
179
    {
180
        MainWindow::showMessage( "Saving Config");
171 david 181
 
182 - 182
        compact( newcfg.event_name );               /* Rip of leading white_space */
183
        for( int i = 0; i < MAX_LEGS; i++ )
184
            compact( newcfg.leg_name[i] );
185
        compact( newcfg.addendum );
186
        compact( newcfg.datafilename );
359 david 187
        compact( newcfg.awardsfilename );
182 - 188
 
189
        /*
171 david 190
     * Do all sorts of consistency tests on the data
191
     * Firstly - calculate the number of legs. Justify the data in the array
192
     */
193
 
182 - 194
        newcfg.num_legs = 0;
195
        for( int i = 0; i < MAX_LEGS; i++ )
196
            if( newcfg.leg_name[i][0] )
197
                newcfg.num_legs++;
198
        for( int i = newcfg.num_legs; i < MAX_LEGS; i++ )
171 david 199
        {
182 - 200
            if( newcfg.leg_name[i][0] )
201
            {
202
                throw( "Configuration error: Missing leg" );
203
            }
171 david 204
        }
205
 
206
 
182 - 207
        /*
171 david 208
     * Now do the team numbering stuff
209
     */
210
 
182 - 211
        for( int i = 0; i < MAX_TMS_SPLIT; i++ )
171 david 212
        {
182 - 213
            if( newcfg.t_def[i].start == 0 && newcfg.t_def[i].end == 0 )
214
                continue;
215
            if( newcfg.t_def[i].start > newcfg.t_def[i].end )
171 david 216
            {
182 - 217
                throw( tprintf( "Team definition error : End greater than start. %d to %d", newcfg.t_def[i].start, newcfg.t_def[i].end ));
218
 
171 david 219
            }
182 - 220
            for( int j = 0; j < i; j++ )
221
            {
222
                if( ( newcfg.t_def[i].start >= newcfg.t_def[j].start
223
                      && newcfg.t_def[i].start <= newcfg.t_def[j].end )
224
                    || ( newcfg.t_def[i].end >= newcfg.t_def[j].start
225
                         && newcfg.t_def[i].end <= newcfg.t_def[j].end ) )
226
                    {
227
                    throw (tprintf( "Team definition error : Overlapping definition. %d to %d",
228
                              newcfg.t_def[i].start, newcfg.t_def[i].end ));
229
                    }
230
            }
171 david 231
        }
232
 
182 - 233
        /*
171 david 234
     * Determine the number of team splits
235
     * There may be blank entries - these will be removed by sorting the array
236
     */
182 - 237
        qsort( ( char * ) newcfg.t_def, MAX_TMS_SPLIT, sizeof( *newcfg.t_def ), f_comp_int );
238
        for( newcfg.num_teams = 0; newcfg.num_teams < MAX_TMS_SPLIT; newcfg.num_teams++ )
239
            if( !newcfg.t_def[newcfg.num_teams].start )
240
                break;
171 david 241
 
182 - 242
        newcfg.min_team = newcfg.t_def[0].start;
243
        newcfg.max_team = newcfg.t_def[newcfg.num_teams - 1].end;
171 david 244
 
182 - 245
        /*
171 david 246
     **  Limit the number of entrants
247
     */
182 - 248
        for( int i = 0; i < MAX_TMS_SPLIT; i++ )
171 david 249
        {
182 - 250
            if( newcfg.t_def[i].start )
251
            {
252
                num_teams += newcfg.t_def[i].end - newcfg.t_def[i].start + 1;
253
            }
171 david 254
        }
255
#if defined(LIMIT_TEAMS) && (LIMIT_TEAMS > 0)
182 - 256
        if( num_teams > LIMIT_TEAMS )
257
        {
258
            throw( "Maximum number of teams exceeded - reduce number of teams" );
259
        }
171 david 260
#endif
261
 
262
#if defined (HI_TECH_C) || defined (__TURBOC__)
263
 
182 - 264
        /*
171 david 265
     * Ensure that the number of teams allocated can fit within available
266
     * Memory space. Ie: Can we malloc the report data structures
267
     */
182 - 268
        {
269
            long        sz;
171 david 270
 
182 - 271
            sz = sizeof( ty_s_data ) > sizeof( ty_s_aux ) ?
272
                 sizeof( ty_s_data ) : sizeof( ty_s_aux );
273
            if( sz * ( newcfg.max_team - newcfg.min_team + 2 ) > ( 1024L * 64L ) )
274
            {
275
                throw( "Too many teams: Reduce team spread." );
276
            }
171 david 277
        }
278
#endif
279
 
182 - 280
        /*
171 david 281
     **  Ensure the non-equestrian class and the equestrian leg are valid
282
     */
182 - 283
        if( newcfg.equestrian_leg )
171 david 284
        {
182 - 285
            if( newcfg.equestrian_leg > newcfg.num_legs )
286
            {
287
                throw( "Invalid non-equestrian leg number" );
288
            }
171 david 289
 
182 - 290
            /*
171 david 291
         **  Ensure that the entered non-equestrian class name does exist
292
         */
182 - 293
            newcfg.nonequestrian_class = newcfg.lookup_class( newcfg.nonequestrian_class_abr );
294
            if( newcfg.nonequestrian_class == 0 )
295
                MainWindow::showMessage( "WARNING: Non-equestrian class not found" );
296
        }
171 david 297
 
361 david 298
        //  Detect change in the number of legs configured
299
        //  Need to force application restart
300
        if ( config.num_legs != newcfg.num_legs || mustRestart ) {
301
            mustRestart = true;
302
        }
303
 
171 david 304
        config = newcfg;
176 - 305
        config.write_config();
182 - 306
        updateChanged(false);
307
 
171 david 308
    }
182 - 309
    catch ( const char * str )
310
    {
311
        MainWindow::showMessage(str);
312
    }
313
    catch ( 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
    }
361 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
 
359 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
}
361 david 400
 
401
void QmConfTeams::quitApplication(void)
402
{
403
    QCoreApplication::quit();
404
}
405