Subversion Repositories svn1-original

Rev

Rev 186 | Rev 201 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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