Subversion Repositories svn1

Rev

Rev 180 | Rev 183 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "qmconfteams.h"
#include "qmconfig.h"
#include "ui_qmconfteams.h"
#include    "consts.h"
#include    "structs.h"
#include    "proto.h"
#include    "mainwindow.h"

QmConfTeams::QmConfTeams(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QmConfigure)
{
    ui->setupUi(this);
    connect(ui->pushButtonSave, SIGNAL(clicked()), this, SLOT(save()) );
    connect(ui->pushButtonRestore, SIGNAL(clicked()), this, SLOT(cancel()) );

    ui->spinBox_NeLeg->setMaximum(5);
    ui->spinBox_NeLeg->setMinimum(0);
    connect(ui->spinBox_NeLeg, SIGNAL(valueChanged(int)), this,SLOT(updateNe()));
    connect(ui->legNe1,SIGNAL(clicked()), this,SLOT(calcNe1()));
    connect(ui->legNe2,SIGNAL(clicked()), this,SLOT(calcNe2()));
    connect(ui->legNe3,SIGNAL(clicked()), this,SLOT(calcNe3()));
    connect(ui->legNe4,SIGNAL(clicked()), this,SLOT(calcNe4()));
    connect(ui->legNe5,SIGNAL(clicked()), this,SLOT(calcNe5()));

    connect(ui->eventName, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->legName1, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->legName2, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->legName3, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->legName4, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->legName5, SIGNAL(textChanged(QString)), this, SLOT(changed()));

    connect(ui->ne_leg_abr, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->addendum, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->datafilename, SIGNAL(textChanged(QString)), this, SLOT(changed()));
    connect(ui->spinBox_LinesPerPage, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->perfSkip, SIGNAL(stateChanged(int)), this, SLOT(changed()));

    connect(ui->spinBox_1l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_1u, SIGNAL(valueChanged(int)), this, SLOT(changed()));

    connect(ui->spinBox_2l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_2u, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_3l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_3u, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_4l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_4u, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_5l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_5u, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_6l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_6u, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_7l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_7u, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_8l, SIGNAL(valueChanged(int)), this, SLOT(changed()));
    connect(ui->spinBox_8u, SIGNAL(valueChanged(int)), this, SLOT(changed()));

    populating = false;
    dirty = true;
    populate();


}
void QmConfTeams::populate(void)
{
    populating = true;
    ui->eventName->setText( config.event_name);
    ui->eventName->setMaxLength(MAX_EVENT_NAME);

    ui->addendum->setText(config.addendum);
    ui->addendum->setMaxLength(sizeof(config.addendum)-1);

    ui->datafilename->setText(config.datafilename);
    ui->datafilename->setMaxLength(sizeof(config.datafilename - 1));

    ui->legName1->setText(config.leg_name[0]);
    ui->legName1->setMaxLength(MAX_LEG_NAME);
    ui->legName2->setText(config.leg_name[1]);
    ui->legName2->setMaxLength(MAX_LEG_NAME);
    ui->legName3->setText(config.leg_name[2]);
    ui->legName3->setMaxLength(MAX_LEG_NAME);
    ui->legName4->setText(config.leg_name[3]);
    ui->legName4->setMaxLength(MAX_LEG_NAME);
    ui->legName5->setText(config.leg_name[4]);
    ui->legName5->setMaxLength(MAX_LEG_NAME);

    ui->spinBox_1l->setValue( config.t_def[0].start);
    ui->spinBox_1u->setValue( config.t_def[0].end);
    ui->spinBox_2l->setValue( config.t_def[1].start);
    ui->spinBox_2u->setValue( config.t_def[1].end);
    ui->spinBox_3l->setValue( config.t_def[2].start);
    ui->spinBox_3u->setValue( config.t_def[2].end);
    ui->spinBox_4l->setValue( config.t_def[3].start);
    ui->spinBox_4u->setValue( config.t_def[3].end);
    ui->spinBox_5l->setValue( config.t_def[4].start);
    ui->spinBox_5u->setValue( config.t_def[4].end);
    ui->spinBox_6l->setValue( config.t_def[5].start);
    ui->spinBox_6u->setValue( config.t_def[5].end);
    ui->spinBox_7l->setValue( config.t_def[6].start);
    ui->spinBox_7u->setValue( config.t_def[6].end);
    ui->spinBox_8l->setValue( config.t_def[7].start);
    ui->spinBox_8u->setValue( config.t_def[7].end);

    ui->spinBox_NeLeg->setValue(config.equestrian_leg);
    updateNe();
    ui->ne_leg_abr->setText(config.nonequestrian_class_abr);
    ui->ne_leg_abr->setMaxLength(sizeof(config.nonequestrian_class_abr)-1);

     ui->spinBox_LinesPerPage->setValue(config.lines_per_page);
     ui->perfSkip->setChecked( config.perf_skip );
    populating = false;
    updateChanged(false);
   }

void QmConfTeams::updateNe(void)
{
    int value = ui->spinBox_NeLeg->value();
    ui->legNe1->setChecked(value == 1);
    ui->legNe2->setChecked(value == 2);
    ui->legNe3->setChecked(value == 3);
    ui->legNe4->setChecked(value == 4);
    ui->legNe5->setChecked(value == 5);
    updateChanged(true);
}

void QmConfTeams::calcNe1(void){ui->spinBox_NeLeg->setValue(1); updateNe(); }
void QmConfTeams::calcNe2(void){ui->spinBox_NeLeg->setValue(2); updateNe(); }
void QmConfTeams::calcNe3(void){ui->spinBox_NeLeg->setValue(3); updateNe(); }
void QmConfTeams::calcNe4(void){ui->spinBox_NeLeg->setValue(4); updateNe(); }
void QmConfTeams::calcNe5(void){ui->spinBox_NeLeg->setValue(5); updateNe(); }

void QmConfTeams::save(void)
{
    int num_teams = 0;

    /*
    **    Copy original data
    */
    QmConfig    newcfg(config);

    /*
    **  Extract data from Widgets
    */
    strncpy(newcfg.event_name, qPrintable(ui->eventName->text()), sizeof(newcfg.event_name)) ;
    strncpy(newcfg.datafilename, qPrintable(ui->datafilename->text()), sizeof(newcfg.datafilename-1)) ;
    strncpy(newcfg.addendum, qPrintable(ui->addendum->text()), sizeof(newcfg.addendum-1)) ;

    strncpy(newcfg.leg_name[0], qPrintable(ui->legName1->text()), sizeof(newcfg.leg_name[0])) ;
    strncpy(newcfg.leg_name[1], qPrintable(ui->legName2->text()), sizeof(newcfg.leg_name[1])) ;
    strncpy(newcfg.leg_name[2], qPrintable(ui->legName3->text()), sizeof(newcfg.leg_name[2])) ;
    strncpy(newcfg.leg_name[3], qPrintable(ui->legName4->text()), sizeof(newcfg.leg_name[3])) ;
    strncpy(newcfg.leg_name[4], qPrintable(ui->legName5->text()), sizeof(newcfg.leg_name[4])) ;

    newcfg.t_def[0].start = ui->spinBox_1l->value();
    newcfg.t_def[0].end = ui->spinBox_1u->value();

    newcfg.t_def[1].start = ui->spinBox_2l->value();
    newcfg.t_def[1].end   = ui->spinBox_2u->value();

    newcfg.t_def[2].start = ui->spinBox_3l->value();
    newcfg.t_def[2].end   = ui->spinBox_3u->value();

    newcfg.t_def[3].start = ui->spinBox_4l->value();
    newcfg.t_def[3].end   = ui->spinBox_4u->value();

    newcfg.t_def[4].start = ui->spinBox_5l->value();
    newcfg.t_def[4].end   = ui->spinBox_5u->value();

    newcfg.t_def[5].start = ui->spinBox_6l->value();
    newcfg.t_def[5].end   = ui->spinBox_6u->value();

    newcfg.t_def[6].start = ui->spinBox_7l->value();
    newcfg.t_def[6].end   = ui->spinBox_7u->value();

    newcfg.t_def[7].start = ui->spinBox_8l->value();
    newcfg.t_def[7].end   = ui->spinBox_8u->value();

    newcfg.equestrian_leg = ui->spinBox_NeLeg->value();
    strncpy(newcfg.nonequestrian_class_abr, qPrintable(ui->ne_leg_abr->text()), sizeof(newcfg.nonequestrian_class_abr)-1) ;

    newcfg.lines_per_page = ui->spinBox_LinesPerPage->value();
    newcfg.perf_skip = ui->perfSkip->isChecked();

    /*
    **  Sanity Test and Clean up
    */
    try
    {
        MainWindow::showMessage( "Saving Config");

        compact( newcfg.event_name );               /* Rip of leading white_space */
        for( int i = 0; i < MAX_LEGS; i++ )
            compact( newcfg.leg_name[i] );
        compact( newcfg.addendum );
        compact( newcfg.datafilename );

        /*
     * Do all sorts of consistency tests on the data
     * Firstly - calculate the number of legs. Justify the data in the array
     */

        newcfg.num_legs = 0;
        for( int i = 0; i < MAX_LEGS; i++ )
            if( newcfg.leg_name[i][0] )
                newcfg.num_legs++;
        for( int i = newcfg.num_legs; i < MAX_LEGS; i++ )
        {
            if( newcfg.leg_name[i][0] )
            {
                throw( "Configuration error: Missing leg" );
            }
        }


        /*
     * Now do the team numbering stuff
     */

        for( int i = 0; i < MAX_TMS_SPLIT; i++ )
        {
            if( newcfg.t_def[i].start == 0 && newcfg.t_def[i].end == 0 )
                continue;
            if( newcfg.t_def[i].start > newcfg.t_def[i].end )
            {
                throw( tprintf( "Team definition error : End greater than start. %d to %d", newcfg.t_def[i].start, newcfg.t_def[i].end ));

            }
            for( int j = 0; j < i; j++ )
            {
                if( ( newcfg.t_def[i].start >= newcfg.t_def[j].start
                      && newcfg.t_def[i].start <= newcfg.t_def[j].end )
                    || ( newcfg.t_def[i].end >= newcfg.t_def[j].start
                         && newcfg.t_def[i].end <= newcfg.t_def[j].end ) )
                    {
                    throw (tprintf( "Team definition error : Overlapping definition. %d to %d",
                              newcfg.t_def[i].start, newcfg.t_def[i].end ));
                    }
            }
        }

        /*
     * Determine the number of team splits
     * There may be blank entries - these will be removed by sorting the array
     */
        qsort( ( char * ) newcfg.t_def, MAX_TMS_SPLIT, sizeof( *newcfg.t_def ), f_comp_int );
        for( newcfg.num_teams = 0; newcfg.num_teams < MAX_TMS_SPLIT; newcfg.num_teams++ )
            if( !newcfg.t_def[newcfg.num_teams].start )
                break;

        newcfg.min_team = newcfg.t_def[0].start;
        newcfg.max_team = newcfg.t_def[newcfg.num_teams - 1].end;

        /*
     **  Limit the number of entrants
     */
        for( int i = 0; i < MAX_TMS_SPLIT; i++ )
        {
            if( newcfg.t_def[i].start )
            {
                num_teams += newcfg.t_def[i].end - newcfg.t_def[i].start + 1;
            }
        }
#if defined(LIMIT_TEAMS) && (LIMIT_TEAMS > 0)
        if( num_teams > LIMIT_TEAMS )
        {
            throw( "Maximum number of teams exceeded - reduce number of teams" );
        }
#endif

#if defined (HI_TECH_C) || defined (__TURBOC__)

        /*
     * Ensure that the number of teams allocated can fit within available
     * Memory space. Ie: Can we malloc the report data structures
     */
        {
            long        sz;

            sz = sizeof( ty_s_data ) > sizeof( ty_s_aux ) ?
                 sizeof( ty_s_data ) : sizeof( ty_s_aux );
            if( sz * ( newcfg.max_team - newcfg.min_team + 2 ) > ( 1024L * 64L ) )
            {
                throw( "Too many teams: Reduce team spread." );
            }
        }
#endif

        /*
     **  Ensure the non-equestrian class and the equestrian leg are valid
     */
        if( newcfg.equestrian_leg )
        {
            if( newcfg.equestrian_leg > newcfg.num_legs )
            {
                throw( "Invalid non-equestrian leg number" );
            }

            /*
         **  Ensure that the entered non-equestrian class name does exist
         */
            newcfg.nonequestrian_class = newcfg.lookup_class( newcfg.nonequestrian_class_abr );
            if( newcfg.nonequestrian_class == 0 )
                MainWindow::showMessage( "WARNING: Non-equestrian class not found" );
        }

        config = newcfg;
        config.write_config();
        updateChanged(false);

    }
    catch ( const char * str )
    {
        MainWindow::showMessage(str);
    }
    catch ( char * str )
    {
        MainWindow::showMessage(str);
    }
    catch ( ... )
    {
        MainWindow::showMessage ("QmConfTeams: Error of some form");
    }
}

void QmConfTeams::cancel(void)
{
    populate();
}

void QmConfTeams::changed(void)
{
    if ( populating )
        return;
    updateChanged(true);
}

void QmConfTeams::updateChanged(bool newDirty)
{
    if (newDirty != dirty)
    {
        dirty = newDirty;
        if (dirty)
        {
            ui->Changed->setVisible(true);
            ui->pushButtonSave->setEnabled(true);
        }
        else
        {
            ui->Changed->setVisible(false);
            ui->pushButtonSave->setEnabled(false);
        }
    }
}

QmConfTeams::~QmConfTeams()
{
    delete ui;
}

void QmConfTeams::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    qDebug("QmConfTeams Event:%d", e->type());
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}