Subversion Repositories svn1-original

Rev

Rev 381 | 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"
#include "twinspinbox.h"
#include "qmeditaddendum.h"
#include "qmeditawards.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()) );
    connect(ui->editAddendum, SIGNAL(clicked()), this, SLOT(editAddendum()));
    connect(ui->editAwardsFileName, SIGNAL(clicked()), this, SLOT(editAwards()));
    connect(ui->pushButtonExit, SIGNAL(clicked()), this, SLOT(quitApplication()));


    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->awardsFileName, 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()));


    ui->tn1->setLimits( 0, 999);
    ui->tn2->setLimits( 0, 999);
    ui->tn3->setLimits( 0, 999);
    ui->tn4->setLimits( 0, 999);
    ui->tn5->setLimits( 0, 999);
    ui->tn6->setLimits( 0, 999);
    ui->tn7->setLimits( 0, 999);
    ui->tn8->setLimits( 0, 999);

    connect(ui->tn1, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn2, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn3, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn4, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn5, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn6, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn7, SIGNAL(changed()), this, SLOT(changed()));
    connect(ui->tn8, SIGNAL(changed()), this, SLOT(changed()));

    populating = false;
    dirty = true;
    mustRestart = false;
    ui->pushButtonExit->hide();
    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->awardsFileName->setText(config.awardsfilename);
    ui->awardsFileName->setMaxLength(sizeof(config.awardsfilename) - 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->tn1->setData( config.t_def[0].start, config.t_def[0].end);
    ui->tn2->setData( config.t_def[1].start, config.t_def[1].end);
    ui->tn3->setData( config.t_def[2].start, config.t_def[2].end);
    ui->tn4->setData( config.t_def[3].start, config.t_def[3].end);
    ui->tn5->setData( config.t_def[4].start, config.t_def[4].end);
    ui->tn6->setData( config.t_def[5].start, config.t_def[5].end);
    ui->tn7->setData( config.t_def[6].start, config.t_def[6].end);
    ui->tn8->setData( config.t_def[7].start, 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)) ;
    strncpy(newcfg.addendum, qPrintable(ui->addendum->text()), sizeof(newcfg.addendum)) ;
    strncpy(newcfg.awardsfilename, qPrintable(ui->awardsFileName->text()), sizeof(newcfg.awardsfilename)) ;

    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])) ;

    ui->tn1->getData(&newcfg.t_def[0].start, &newcfg.t_def[0].end);
    ui->tn2->getData(&newcfg.t_def[1].start, &newcfg.t_def[1].end);
    ui->tn3->getData(&newcfg.t_def[2].start, &newcfg.t_def[2].end);
    ui->tn4->getData(&newcfg.t_def[3].start, &newcfg.t_def[3].end);
    ui->tn5->getData(&newcfg.t_def[4].start, &newcfg.t_def[4].end);
    ui->tn6->getData(&newcfg.t_def[5].start, &newcfg.t_def[5].end);
    ui->tn7->getData(&newcfg.t_def[6].start, &newcfg.t_def[6].end);
    ui->tn8->getData(&newcfg.t_def[7].start, &newcfg.t_def[7].end);

    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 );
        compact( newcfg.awardsfilename );

        /*
     * 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" );
        }

        //  Detect change in the number of legs configured
        //  Need to force application restart
        if ( config.num_legs != newcfg.num_legs || mustRestart ) {
            mustRestart = true;
        }

        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)
{
    ui->editAddendum->setEnabled(!ui->addendum->text().isEmpty());
    if (newDirty != dirty)
    {
        dirty = newDirty;
        if (dirty)
        {
            ui->Changed->setVisible(true);
            ui->pushButtonSave->setEnabled(true);
            ui->pushButtonSave->setStyleSheet("background-color: rgb(255, 0, 0);");
        }
        else
        {
            ui->Changed->setVisible(false);
            ui->pushButtonSave->setEnabled(false);
            ui->pushButtonSave->setStyleSheet("");
        }
    }

    if (mustRestart) {
        MainWindow::disableAllTabs();
        ui->pushButtonExit->show();
        ui->pushButtonExit->setEnabled(true);
        ui->pushButtonExit->setStyleSheet("background-color: rgb(255, 0, 0);");
    }
}

void QmConfTeams::editAddendum(void)
{
    QString fname = ui->addendum->text();
    if ( !fname.isEmpty() )
    {
        QmEditAddendum dialog(QmConfig::getAddendemFile(fname, true));
        dialog.exec();
    }
}

void QmConfTeams::editAwards(void)
{
    QString fname = ui->awardsFileName->text();
    if ( !fname.isEmpty() )
    {
        QmEditAwards dialog(QmConfig::getAddendemFile(fname, true));
        dialog.exec();
    }
}

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;
    }
}

void QmConfTeams::quitApplication(void)
{
    QCoreApplication::quit();
}