Subversion Repositories svn1-original

Rev

Rev 382 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "qmdatacheck.h"
#include <QtCore/QVariant>
#include <QAction>
#include <QApplication>
#include <QButtonGroup>
#include <QDialogButtonBox>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QListWidget>
#include <QSpacerItem>
#include <QSpinBox>
#include <QWidget>
#include    "consts.h"
#include    "structs.h"
#include    "proto.h"
#include "QString"
#include "mainwindow.h"
#include "qmdialogteameditor.h"

const char       *qck_err_mess[] = {
    "No error",
    "Invalid start time",
    "Invalid end time",
    "End before start",
    "Team data not entered",
    "NE with named competitor"      // No longer used
};

qmDataCheck::qmDataCheck(QWidget *parent) :
    QWidget(parent)
{

    resize ( 600,400);

    QVBoxLayout *verticalLayout = new QVBoxLayout(this);
    verticalLayout->setContentsMargins(0, 0, 0, 0);

    QGroupBox *groupBox = new QGroupBox("Data Check");
    verticalLayout->addWidget(groupBox);

    QVBoxLayout *verticalLayout2 = new QVBoxLayout(groupBox);

    listWidget = new QListWidget(groupBox);
    verticalLayout2->addWidget(listWidget);

    QHBoxLayout *horizontalLayout;
    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setContentsMargins(0, 0, 0, 5);

    QGroupBox *groupBox2 = new QGroupBox("Tests");
    verticalLayout2->addLayout(horizontalLayout);
    horizontalLayout->addWidget(groupBox2);

    QHBoxLayout *horizontalLayout1;

    horizontalLayout1 = new QHBoxLayout();
    horizontalLayout1->setContentsMargins(5, 5, 5, 5);
    groupBox2->setLayout(horizontalLayout1);
    groupBox2->setToolTip("Enable Tests to be performed");

    cb_all = new QCheckBox("All");
    cb_all->setChecked(true);
    cb_all->setToolTip("Enable all tests");
    horizontalLayout1->addWidget(cb_all);

    QFrame *line = new QFrame();
    line->setObjectName(QString::fromUtf8("line"));
    line->setGeometry(QRect(320, 150, 118, 3));
    line->setFrameShape(QFrame::VLine);
    line->setFrameShadow(QFrame::Sunken);
    horizontalLayout1->addWidget(line);

    cb_teamName = new QCheckBox("Team Names");
    cb_teamName->setChecked(true);
    cb_teamName->setToolTip("Check that teams have a name");
    horizontalLayout1->addWidget(cb_teamName);

    cb_class = new QCheckBox("Class");
    cb_class->setChecked(true);
    cb_class->setToolTip("Check that teams have a valid class");
    horizontalLayout1->addWidget(cb_class);

    cb_compNames = new QCheckBox("Names");
    cb_compNames->setChecked(true);
    cb_compNames->setToolTip("Check that competitors have names");
    horizontalLayout1->addWidget(cb_compNames);

    cb_eqNames = new QCheckBox("EQ/Names");
    cb_eqNames->setChecked(true);
    cb_eqNames->setToolTip("Check that Equestrian teams do have a\nteam member for the equestrian leg");
    horizontalLayout1->addWidget(cb_eqNames);

    cb_neNames = new QCheckBox("NE/Names");
    cb_neNames->setChecked(true);
    cb_neNames->setToolTip("Check that NE teams do not have a\nteam member for the equestrian leg");
    horizontalLayout1->addWidget(cb_neNames);

    cb_checkLegs = new QCheckBox("Leg Times");
    cb_checkLegs->setChecked(true);
    cb_checkLegs->setToolTip("Check Leg Start and End times");
    horizontalLayout1->addWidget(cb_checkLegs);

    horizontalLayout1->addWidget(new QLabel("Leg"));
    leg = new QmTeamSelector();
    horizontalLayout1->addWidget(leg);

    QSpacerItem *horizontalSpacer1;
    horizontalSpacer1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    horizontalLayout->addItem(horizontalSpacer1);

    buttonBox = new QDialogButtonBox();
    pb_check = buttonBox->addButton("Check",QDialogButtonBox::ActionRole );
    pb_cancel = buttonBox->addButton("Clear",QDialogButtonBox::ActionRole );
    verticalLayout2->addWidget(buttonBox);

    leg->setMinimum(0);
    leg->setMaximum(config.num_legs);
    leg->setValue(config.num_legs);

    connect(pb_check, SIGNAL(clicked()), this, SLOT(populate()));
    connect(pb_cancel, SIGNAL(clicked()), this, SLOT(cancel()) );
    connect(listWidget,SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(selected(QListWidgetItem *)));
    connect(cb_all, SIGNAL(clicked()), this, SLOT(checkAllTests()));
}

qmDataCheck::~qmDataCheck()
{

}

/*----------------------------------------------------------------------------
** FUNCTION           : addItem
**
** DESCRIPTION        : Add an entry to the list of errored items
**
**
** INPUTS             : team            - Team
**                      leg             - May be 0
**                      msg             - Text error message
**
** RETURNS            : Nothing
**
----------------------------------------------------------------------------*/

void qmDataCheck::addItem(int team, int leg, QString msg)
{
    qmDataCheckItem *item = new qmDataCheckItem(team, leg);
    item->setData(Qt::EditRole, msg);
    listWidget->addItem(item);
}

void qmDataCheck::populate(void)
{
    int leg_end = leg->value();
    if ( leg_end < 0 || leg_end > config.num_legs)
    {
        MainWindow::showMessage("Bad Leg Number selected");
        return;
    }
    listWidget->clear();

    /*
     * Start the testing
     * Read each team into memory and start the testing
     */
    int count_bad = 0;
    int neindex = config.equestrian_leg - 1;

    for( int tm = config.min_team; tm <= config.max_team; tm++ )
    {
        int badData = 0;
        team_type team_buf;
        if( valid_field( tm ) )
        {
            g_record( tm, &team_buf );
            if (team_buf.flags.valid)
            {
                // Test for class not defined
                if ( cb_class->isChecked() && (team_buf.teamclass <= 0))
                {
                    addItem(tm, 0, QString("Team %1, No Class specified").arg(QString::number(tm) ) );
                    badData = 1;
                }

                if ( cb_teamName->isChecked() && (strlen (team_buf.name) <= 0))
                {
                    addItem(tm, 0, QString("Team %1, No Team Name specified").arg(QString::number(tm) ) );
                    badData = 1;
                }

                if (cb_compNames->isChecked() ) {
                    for (int leg = 0; leg <= config.num_legs; leg++) {

                        if (config.equestrian_leg && leg == neindex) {
                            continue;
                        }

                        char *nptr = team_buf.members[leg].name;
                        bool validTeamName = ( *nptr && strcmp(nptr, "0") != 0
                                            && strcmp(nptr, "-") != 0
                                            && stricmp(nptr, "N/A") != 0 );

                       if (!validTeamName) {
                            QString msg = QString("Team %1, No competitor name for leg: %2").arg(QString::number(tm)).arg(config.leg_name[leg]);
                            if (nptr && strlen(nptr)) {
                                msg.append(QString(". Have \"%1\"").arg(nptr) );
                            }
                            addItem(tm, 0, msg );
                            badData = 1;
                        }
                    }
                }


                // Test for NE Team with a team name

                if ((cb_neNames->isChecked() || cb_eqNames->isChecked()) && config.equestrian_leg )
                {
                    char *nptr = team_buf.members[neindex].name;
                    bool validTeamName = ( *nptr && strcmp(nptr, "0") != 0
                                        && strcmp(nptr, "-") != 0
                                        && stricmp(nptr, "N/A") != 0 );

                    if (cb_neNames->isChecked() && team_buf.flags.non_equestrian && validTeamName && stricmp(nptr, "NE") != 0 )
                    {
                        addItem(tm, 0, QString("Team %1, NE with named competitor: %2").arg(QString::number(tm)).arg(nptr) );
                        badData = 1;
                    }

                    if (cb_eqNames->isChecked() && !team_buf.flags.non_equestrian && !validTeamName )
                    {
                        addItem(tm, 0, QString("Team %1, possible NE team").arg(QString::number(tm)) );
                        badData = 1;
                    }

                    // Test for Equestrain Team with NE name - perhaps is really an NE team
                    if (cb_eqNames->isChecked() && !team_buf.flags.non_equestrian && stricmp(nptr, "NE") == 0 )
                    {
                        addItem(tm, 0, QString("Team %1, possible NE team as competitor is called %2").arg(QString::number(tm)).arg(nptr) );
                        badData = 1;
                    }
                }

                // Test Leg Data if required
                if (cb_checkLegs->isChecked() && leg_end &&  !team_buf.flags.disqualified)
                {
                    test_times( &team_buf, leg_end );
                    if( check_error.type > 0 )
                    {
                        addItem(tm, check_error.leg, QString("Team %1, Leg %2: %3").arg(QString::number(tm)).arg(QString::number(check_error.leg)).arg(qck_err_mess[check_error.type]));
                        badData = 1;
                    }
                }
            }
        }
        else
        {
            //
            // Team not within the configured range
            // Warn if the team has time data - may be a miss configuration
            //
            int i;
            g_record( tm, &team_buf );
            for( i = 0; i <= config.num_legs; i++ )
            {
                if ( team_buf.leg[i].start > 0 || team_buf.leg[i].end > 0 )
                {
                    addItem(tm, 0, QString("Team %1, Unconfigured team has time information").arg(QString::number(tm) ) );
                    badData = 1;
                    break;
                }
            }

        }

        //
        //  Count teams with wonky data
        //
        if (badData)
            count_bad++;
    }
    if( count_bad )
        MainWindow::showMessage(QString("%1 teams with inconsistent data").arg(count_bad));
    else
        MainWindow::showMessage("All team data correct");
}

void qmDataCheck::cancel(void)
{
    listWidget->clear();
}

void qmDataCheck::selected(QListWidgetItem *item)
{
    //qDebug("Item selected");
    qmDataCheckItem *myitem = dynamic_cast<qmDataCheckItem *>(item);
    if ( myitem )
    {
        //qDebug("   Team:%d, Leg: %d", myitem->team, myitem->leg);
        qmDialogTeamEditor dialog(myitem->team, this);
        dialog.exec();
    }
}

void qmDataCheck::checkAllTests(void)
{
    bool all_state = cb_all->isChecked();

    cb_checkLegs->setChecked(all_state);
    cb_class->setChecked(all_state);
    cb_eqNames->setChecked(all_state);
    cb_neNames->setChecked(all_state);
    cb_teamName->setChecked(all_state);
    cb_compNames->setChecked(all_state);
}