Subversion Repositories svn1-original

Rev

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

#include "qmconfclass.h"
#include "qmconfig.h"
#include "ui_qmconfclass.h"
#include    "consts.h"
#include    "structs.h"
#include    "proto.h"
#include "mainwindow.h"
#include "qmTableWidgetItem.h"

#include <QDialogButtonBox>
#include "QTableWidgetItem"
#include "spinboxdelegate.h"
#include "timedelegate.h"
#include "textdelegate.h"
#include <QMenu>

#define COL_ABR 0
#define COL_CLASS 1
#define COL_TIME 2
#define COL_WINNERS 3
#define COL_NE 4
#define COL_NE_WINNERS 5
#define COL_ST_TOTAL 6
#define COL_ST_VALID 7
#define COL_ST_DISQ 8
#define COL_ST_NONEQ 9
#define COL_ST_VET 10
#define COL_ST_CEV 11
#define COL_ST_CNE 12
#define COL_INDEX 13

#define COL_COUNT 14


QmConfClass::QmConfClass(QWidget *parent) :
    QWidget(parent)
{
    populating = false;
    //resize ( 600,400);

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

    QGroupBox *groupBox = new QGroupBox("Class");
    verticalLayout->addWidget(groupBox);
    QVBoxLayout *verticalLayout2 = new QVBoxLayout(groupBox);
    tableWidget = new QTableWidget(groupBox);
    tableWidget->setAlternatingRowColors(true);
    tableWidget->setRowCount(config.num_class + 1);
    tableWidget->setColumnCount(COL_COUNT);
    tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    tableWidget->horizontalHeader()->setVisible(true);
    tableWidget->horizontalHeader()->setDefaultSectionSize(70);
    tableWidget->horizontalHeader()->setHighlightSections(true);
    //tableWidget->horizontalHeader()->setStretchLastSection(true);
    tableWidget->verticalHeader()->setVisible(true);
    tableWidget->verticalHeader()->setDefaultSectionSize(20);
    //tableWidget->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::MinimumExpanding );
    //tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    verticalLayout2->addWidget(tableWidget);

    //QSpacerItem *verticalSpacer1;
    //verticalSpacer1 = new QSpacerItem(0, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
    //verticalLayout2->addItem(verticalSpacer1);

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

    QDialogButtonBox *buttonBox = new QDialogButtonBox();
    pushButtonRestore = buttonBox->addButton("Restore",QDialogButtonBox::ActionRole );
    pushButtonSave = buttonBox->addButton("Save",QDialogButtonBox::ActionRole );
    horizontalLayout->addWidget(buttonBox);


    connect(pushButtonSave, SIGNAL(clicked(bool)), this, SLOT(save()) );
    connect(pushButtonRestore, SIGNAL(clicked(bool)), this, SLOT(cancel()) );

    QStringList labels;
    labels << "Abr" << "Full Name" << "Start Time" << "Winners" << "NE" << "NE Winners";

    labels << "Total" << "Valid" <<"Disqual" << "NonEq" << "VetCheck" << "Comp Ev" << " Comp NE";

    labels << "Index";

    tableWidget->setHorizontalHeaderLabels(labels);
    tableWidget->resizeColumnsToContents();

    connect(tableWidget, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(ctxMenu(const QPoint &)));
    connect(tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(cellChanged(int,int)));

    /*
    **  Setup delegated for specialised editing
    */
    tableWidget->setItemDelegateForColumn(COL_ABR, new textDelegate(2));
    tableWidget->setItemDelegateForColumn(COL_CLASS, new textDelegate(LEN_CLASS_NAME));
    tableWidget->setItemDelegateForColumn(COL_TIME, new timeDelegate());
    tableWidget->setItemDelegateForColumn(COL_WINNERS, new SpinBoxDelegate(0,50));
    tableWidget->setItemDelegateForColumn(COL_NE_WINNERS, new SpinBoxDelegate(0,50));

    populate();
}

/*----------------------------------------------------------------------------
** FUNCTION           : populate
**
** DESCRIPTION        : Populate the display with information
**
----------------------------------------------------------------------------*/

void QmConfClass::populate(void)
{
    bool    reportDataLoaded = false;
    t_class_summary data;

    populating = true;
    tableWidget->clearContents();
    tableWidget->setSortingEnabled(false);
    tableWidget->setRowCount(config.num_class + 1);

    /*
    ** Load Report Data if we can
    */
    if( load_report_data() )
    {
        calc_class_summary( & data );
        reportDataLoaded = true;
    }

    /*
    **  Add all the information
    */
    int ii = 0;
    int entryIndex = 0;
    for ( ii = 0; ii < MAX_CLASS; ii++)
    {
        if ( config.team_class[ii].abr[0] )
        {
            // Keep the config item index entry
            //  Will need it when we write stiff out
            tableWidget->setItem(entryIndex, COL_INDEX, new qmTwiNumber(ii));

            tableWidget->setItem(entryIndex, COL_ABR, new QTableWidgetItem(config.team_class[ii].abr) );
            tableWidget->setItem(entryIndex, COL_CLASS, new QTableWidgetItem(config.team_class[ii].full_name ));

            QTableWidgetItem *item;
            item = new QTableWidgetItem();
            item->setData(Qt::EditRole,QTime().addSecs(config.team_class[ii].start) );
            tableWidget->setItem(entryIndex, COL_TIME, item);

            item = new qmTwiEditNumber(config.class_winners[ii]);
            tableWidget->setItem(entryIndex, COL_WINNERS, item);

            item = new QTableWidgetItem();
            item->setCheckState(config.nonequestrian_class == 1 + ii ? Qt::Checked : Qt::Unchecked);
            tableWidget->setItem(entryIndex, COL_NE, item);

            item = new qmTwiEditNumber(config.class_ne_winners[ii]);
            tableWidget->setItem(entryIndex, COL_NE_WINNERS, item);

            if (reportDataLoaded)
            {
                tableWidget->setItem(entryIndex,COL_ST_TOTAL, new qmTwiNumber(data.teamclass[ii+1].total) );
                tableWidget->setItem(entryIndex,COL_ST_VALID, new qmTwiNumber(data.teamclass[ii+1].valid) );
                tableWidget->setItem(entryIndex,COL_ST_DISQ,  new qmTwiNumber(data.teamclass[ii+1].disqualified));
                tableWidget->setItem(entryIndex,COL_ST_NONEQ, new qmTwiNumber(data.teamclass[ii+1].non_equestrian));
                tableWidget->setItem(entryIndex,COL_ST_VET,   new qmTwiNumber(data.teamclass[ii+1].vet_check));
                tableWidget->setItem(entryIndex,COL_ST_CEV,   new qmTwiNumber(data.teamclass[ii+1].valid_ev));
                tableWidget->setItem(entryIndex,COL_ST_CNE,   new qmTwiNumber(data.teamclass[ii+1].valid_ne));
            }
            entryIndex++;
        }
    }

    //  Insert Totals
    //      Flag that items on this this row is not to be sorted - or at least 
    //      sorted at the end
    //
    if (reportDataLoaded)
    {
        tableWidget->setItem(entryIndex,COL_ABR,      new qmTwiString("Totals", 1));
        tableWidget->setItem(entryIndex,COL_ST_TOTAL, new qmTwiNumber(data.total.total, 1));
        tableWidget->setItem(entryIndex,COL_ST_VALID, new qmTwiNumber(data.total.valid, 1));
        tableWidget->setItem(entryIndex,COL_ST_DISQ,  new qmTwiNumber(data.total.disqualified, 1));
        tableWidget->setItem(entryIndex,COL_ST_NONEQ, new qmTwiNumber(data.total.non_equestrian, 1));
        tableWidget->setItem(entryIndex,COL_ST_VET,   new qmTwiNumber(data.total.vet_check, 1));
        tableWidget->setItem(entryIndex,COL_ST_CEV,   new qmTwiNumber(data.total.valid_ev, 1));
        tableWidget->setItem(entryIndex,COL_ST_CNE,   new qmTwiNumber(data.total.valid_ne, 1));
    }

    tableWidget->resizeColumnsToContents();
    tableWidget->resizeRowsToContents();
    tableWidget->setSortingEnabled(true);

    updateChanged(false);
    populating = false;
}

void QmConfClass::save(void)
{
    /*
    **    Copy original data
    */
    QmConfig newcfg(config);

    /*
    **  Extract the data from the Widgets
    */
    for (int entryIndex = 0; entryIndex < tableWidget->rowCount(); entryIndex++)
    {

        // Check that this is a real Entry or the 'Totals' header
        //      Real rows have a COL_INDEX entr
        //
        QTableWidgetItem *item = tableWidget->item ( entryIndex, COL_INDEX );
        if (! item)
        {
            qDebug("Ignore row:%d", entryIndex);
            continue;
        }
        int ii = item->text().toInt();
        qDebug("Processing: %d", ii);

        if (ii >= MAX_CLASS || ii < 0)
        {
            qDebug("Ignore row:%d. Class out of rande", entryIndex);
            continue;
        }

        item = tableWidget->item ( entryIndex, COL_ABR );
        if ( item )
        {
            strncpy(newcfg.team_class[ii].abr, qPrintable(item->text()), sizeof(newcfg.team_class[ii].abr)-1);
        }
        else
        {
            *newcfg.team_class[ii].abr = 0;
        }

        item = tableWidget->item ( entryIndex, COL_CLASS );
        if ( item )
        {
            strncpy(newcfg.team_class[ii].full_name, qPrintable(item->text()), sizeof(newcfg.team_class[ii].full_name)-1);
        }
        else
        {
            *newcfg.team_class[ii].full_name = 0;
        }

        item = tableWidget->item( entryIndex, COL_TIME );
        if ( item )
        {
            QVariant data = item->data(Qt::EditRole);
            if (data.isValid())
            {
                newcfg.team_class[ii].start = QTime(0,0,0).secsTo(item->data(Qt::EditRole).toTime());
            }
            else
            {
                newcfg.team_class[ii].start = -1;
            }
        }
        else
        {
            newcfg.team_class[ii].start = -1;
        }

        item = tableWidget->item( entryIndex, COL_WINNERS );
        if ( item )
        {
            newcfg.class_winners[ii] = item->data(Qt::EditRole).toInt();
        }
        else
        {
            newcfg.class_winners[ii] = 0;
        }

        item = tableWidget->item( entryIndex, COL_NE );
        if ( item )
        {
            if ( item->checkState() == Qt::Checked)
            {
                newcfg.nonequestrian_class = ii;
                strncpy(newcfg.nonequestrian_class_abr, newcfg.team_class[ii].abr, sizeof(newcfg.team_class[ii].abr)-1);
            }
        }

        item = tableWidget->item( entryIndex, COL_NE_WINNERS );
        if ( item )
        {
            newcfg.class_ne_winners[ii] = item->data(Qt::EditRole).toInt();
        }
        else
        {
            newcfg.class_ne_winners[ii] = 0;
        }
    }
    
    // Validate the data
    try
    {
        MainWindow::showMessage( "Saving Config");

        /*
         **  Now do the Class definitions
         */

        for( int i = 0; i < MAX_CLASS; i++ )
        {
            compact( newcfg.team_class[i].abr );
            compact( newcfg.team_class[i].full_name );
        }

        for( int i = 0; i < MAX_CLASS; i++ )
        {
            if( ( newcfg.team_class[i].abr[0] == '\0' ) != ( newcfg.team_class[i].full_name[0] == '\0' ) )
            {
                throw( "Configuration error. Class without description" );

            }
            if( newcfg.team_class[i].abr[0] != '\0' && newcfg.team_class[i].start < 0L )
            {
                throw( "Configuration error. Bad start time on class" );

            }
        }

        newcfg.num_class = 0;
        for( int i = 0; i < MAX_CLASS; i++ )
            if( newcfg.team_class[i].full_name[0] )
                newcfg.num_class++;

        for( int i = newcfg.num_class; i < MAX_CLASS; i++ )
            if( newcfg.team_class[i].full_name[0] )
            {
                throw( "Configuration error: Missing Class name. Gaps not allowed" );

            }

        if( newcfg.num_class == 0 )
        {
            throw( "Error: No categories defined" );

        }

        newcfg.nonequestrian_class = newcfg.lookup_class( newcfg.nonequestrian_class_abr );
        if( newcfg.equestrian_leg && newcfg.nonequestrian_class == 0 )
            MainWindow::showMessage( "WARNING: Non-equestrian class not found" );

        /*
        **  Sanity test of the data
        */
        for( int i = 0; i < MAX_CLASS; i++ )
        {
            if( newcfg.team_class[i].abr[0] != '\0' && newcfg.class_winners[i] == 0 )
            {

                MainWindow::showMessage( QString("Warning: Class without winners:") + newcfg.team_class[i].abr );
            }
        }

        /*
        ** Cannot mix winners for NE class and NE Winners for each class
        */
        if (newcfg.nonequestrian_class)
        {
            newcfg.class_ne_winners_by_class = false;
            for( int i = 0; i < MAX_CLASS; i++ )
            {
                if ( newcfg.class_ne_winners[i])
                {
                    newcfg.class_ne_winners_by_class = true;
                    break;
                }
            }
            if (newcfg.class_winners[newcfg.nonequestrian_class - 1] && newcfg.class_ne_winners_by_class )
            {
                MainWindow::showMessage( QString("Should not mix NE winners by each class and by NE Class"));
                //throw( "Error: Cannot mix NE winners by each class and by NE Class" );
            }
        }


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

    }
    catch (const char * str )
    {
        MainWindow::showMessage(str);
    }

}

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

QmConfClass::~QmConfClass()
{

}

void QmConfClass::ctxMenu(const QPoint & pos)
{
    qDebug("Context Menu");
    QMenu *menu = new QMenu;
    QTableWidgetItem *item = tableWidget->itemAt(pos);
    menu->addAction(tr("New Category"), this, SLOT(ctxMenuAddRow()));
    if (item)
    {
        menu->addAction(tr("Delete Category"), this, SLOT(ctxMenuDeleteRow()));
    }
   menu->exec(tableWidget->mapToGlobal(pos));

}

void QmConfClass::ctxMenuDeleteRow(void)
{
    //qDebug ("DELETE ROW: %d", tableWidget->currentRow () );
    tableWidget->removeRow(tableWidget->currentRow ());
    updateChanged(true);
}

void QmConfClass::ctxMenuAddRow(void)
{
    tableWidget->setRowCount( 1+ tableWidget->rowCount());
}

void QmConfClass::cellChanged(int row,int col)
{
    if ( populating )
        return;
    updateChanged(true);
    populating = true;
    //qDebug("Cell changed: %d, %d", row, col);
    if (col == COL_NE)
    {
        for (int ii = 0; ii < tableWidget->rowCount(); ii++)
        {
            QTableWidgetItem *item = tableWidget->item(ii, COL_NE);
            if (item)
            {
                item->setCheckState(ii == row ? Qt::Checked : Qt::Unchecked);
            }
        }
    }
    populating = false;
}

void QmConfClass::updateChanged(bool newDirty)
{
    if (newDirty != dirty)
    {
        dirty = newDirty;
        if (dirty)
        {
            pushButtonSave->setEnabled(true);
            pushButtonSave->setStyleSheet("background-color: rgb(255, 0, 0);");
        }
        else
        {
            pushButtonSave->setEnabled(false);
            pushButtonSave->setStyleSheet("");
        }
    }
}

void QmConfClass::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {

    default:
        break;
    }
}