Subversion Repositories svn1-original

Rev

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

#include "qmdisqualified.h"
#include "consts.h"
#include "structs.h"
#include "proto.h"
#include <QHeaderView>
#include <QLabel>
#include <QGroupBox>
#include "mainwindow.h"

qmDisqualified::qmDisqualified(QWidget *parent) :
    QWidget(parent)

{
    inPopulate = false;
    dirty = true;
    resize ( 600,400);

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

    QGroupBox *groupBox = new QGroupBox("Disqualified and Non Equestrian");
    verticalLayout->addWidget(groupBox);

    QVBoxLayout *verticalLayout2 = new QVBoxLayout(groupBox);

    tableWidget = new QTableWidget(groupBox);
    tableWidget->setObjectName(QString::fromUtf8("tableWidget"));
    //tableWidget->setGeometry(QRect(10, 20, 501, 421));
    tableWidget->setAlternatingRowColors(true);
    tableWidget->horizontalHeader()->setProperty("showSortIndicator", QVariant(true));
    tableWidget->verticalHeader()->setDefaultSectionSize(20);
    tableWidget->verticalHeader()->setProperty("showSortIndicator", QVariant(false));
    verticalLayout2->addWidget(tableWidget);

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

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

    QHBoxLayout *horizontalLayout1;
    QSpacerItem *horizontalSpacer1;

    horizontalLayout1 = new QHBoxLayout();
    horizontalLayout1->setContentsMargins(5, 5, 5, 5);
    groupBox2->setLayout(horizontalLayout1);
    groupBox2->setToolTip("Set or clear one or more attributes for the specified team."
                           "\nThe attributes have three states: Set,Clear and Do Nothing."
                           "\nSelect the required attributes to change. Enter the team number"
                           "\nand use 'Set' or <Return> to Set/Clear the selected attributes.");

    cb_disq = new QCheckBox("Disq");
    cb_disq->setTristate(true);
    horizontalLayout1->addWidget(cb_disq);

    cb_ne = new QCheckBox("Non Equest");
    cb_ne->setTristate(true);
    horizontalLayout1->addWidget(cb_ne);

    cb_enable = new QCheckBox("Enable");
    cb_enable->setTristate(true);
    horizontalLayout1->addWidget(cb_enable);

    cb_vetcheck = new QCheckBox("Vet Check");
    cb_vetcheck->setTristate(true);
    horizontalLayout1->addWidget(cb_vetcheck);
        
    horizontalLayout1->addWidget(new QLabel("Team"));
    teamNumber = new QmTeamSelector();
    horizontalLayout1->addWidget(teamNumber);

    setButton = new QPushButton("Set");
    horizontalLayout1->addWidget(setButton);

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

    buttonBox = new QDialogButtonBox();
    pb_restore = buttonBox->addButton("Restore",QDialogButtonBox::ActionRole );
    pb_save = buttonBox->addButton("Save",QDialogButtonBox::ActionRole );
    horizontalLayout->addWidget(buttonBox);

    connect(pb_save, SIGNAL(clicked()), this, SLOT(save()) );
    connect(pb_restore, SIGNAL(clicked()), this, SLOT(cancel()) );
    connect(setButton, SIGNAL(clicked()), this, SLOT(bulk_set()) );
    connect(teamNumber,SIGNAL(teamSelected()), this, SLOT(bulk_set()) );
    connect(teamNumber, SIGNAL(valueChanged(int)), this, SLOT(center_display(int)));
    connect(tableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(tableItemChanged(QTableWidgetItem *)));
    updateChanged (false);
}

qmDisqualified::~qmDisqualified()
{

}

void qmDisqualified::save(void)
{
    team_type team_buf;
    for ( int ii = 0; ii < tableWidget->rowCount(); ii++)
    {
        QTableWidgetItem *item;
        item = tableWidget->item(ii, 0);
        if ( item )
        {
            if ( item->data(Qt::UserRole + 1).toBool() )
            {
                int team = item->data(Qt::EditRole).toInt();
                if ( team )
                {
                    //qDebug("Detected change:%d", team);
                    g_record( team, &team_buf );

                    item = tableWidget->item(ii,2);
                    team_buf.flags.disqualified =  ( item->checkState () == Qt::Checked);

                    item = tableWidget->item(ii,3);
                    team_buf.flags.non_equestrian =  ( item->checkState () == Qt::Checked);

                    item = tableWidget->item(ii,4);
                    team_buf.flags.valid =  ( item->checkState () == Qt::Checked);

                    item = tableWidget->item(ii,5);
                    team_buf.flags.vet_check =  ( item->checkState () == Qt::Checked);

                    put_team_record( team, &team_buf );
                }
            }
        }
    }
    populate();

}

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

void qmDisqualified::populate(void)
{
    team_type team_buf;
    inPopulate = TRUE;

    /*
    ** Delete existing entries in the table
    */
    tableWidget->clearContents();
    tableWidget->setRowCount(config.max_team+1);
    tableWidget->setColumnCount(7);
    tableWidget->setSortingEnabled(FALSE);

    /*
    ** Scan all the team data
    */
    for ( int team = config.min_team; team <= config.max_team; team++)
    {
        tableWidget->hideRow ( team );
        if( valid_field( team ) )
        {
            g_record( team, &team_buf );
            if( team_buf.flags.valid || true )
            {
                qmDisqualifiedItem *item;

                tableWidget->showRow( team );

                item = new qmDisqualifiedItem(team);
                item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
                tableWidget->setItem(team, 0, item );

                item = new qmDisqualifiedItem(team_buf.name);
                item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
                tableWidget->setItem(team, 1, item );

                tableWidget->setItem(team, 2, new qmDisqualifiedItem("Disq", team_buf.flags.disqualified, team) );
                tableWidget->setItem(team, 3, new qmDisqualifiedItem("Non Equest",team_buf.flags.non_equestrian,team) );
                tableWidget->setItem(team, 4, new qmDisqualifiedItem("Enable", team_buf.flags.valid,team) );
                tableWidget->setItem(team, 5, new qmDisqualifiedItem("VetCheck", team_buf.flags.vet_check,team) );

                item = new qmDisqualifiedItem("Bad Times", team_buf.flags.bad_times);
                item->setFlags(Qt::ItemIsSelectable/* | Qt::ItemIsEnabled*/);
                tableWidget->setItem(team, 6, item  );
            }
        }
    }
    tableWidget->sortByColumn(0,Qt::AscendingOrder);
    tableWidget->setSortingEnabled(TRUE);
    tableWidget->resizeColumnsToContents();

    /*
     ** Init the bulk_set checkboxes to tristate
     */
    cb_disq->setCheckState(Qt::PartiallyChecked);
    cb_enable->setCheckState(Qt::PartiallyChecked);
    cb_ne->setCheckState(Qt::PartiallyChecked);
    cb_vetcheck->setCheckState(Qt::PartiallyChecked);

    teamNumber->setMaximum(config.max_team);
    teamNumber->setMinimum(config.min_team);

    inPopulate = FALSE;
    updateChanged(false);
}

void qmDisqualified::tableItemChanged(QTableWidgetItem *item)
{
    if ( !inPopulate )
    {
        if ( item->column() )
        {
            //qDebug ("DataChanged");
            item->setData(Qt::BackgroundRole, QBrush(QColor(255,0,0,50)));

            // Mark first item in row to help detect changes
            int row = item->row();
            item->tableWidget()->item(row,0)->setData(Qt::UserRole + 1, true );
            item->tableWidget()->item(row,0)->setData(Qt::BackgroundRole, QBrush(QColor(255,0,0,50)));
        }

        updateChanged(true);
    }
}

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

void qmDisqualified::showEvent ( QShowEvent * event )
{
    //qDebug("qmDisqualified::showEvent");
    if ( ! event->spontaneous() && !dirty )
    {
        populate();
    }
}

/*----------------------------------------------------------------------------
** FUNCTION           : bulk_set
**
** DESCRIPTION        : Set multiple attributes on the current team
**
**
** INPUTS             : Nothing
**
** RETURNS            : Nothing
**
----------------------------------------------------------------------------*/

void qmDisqualified::bulk_set(void)
{
    bool team_found = FALSE;
    bool item_set = FALSE;

    //qDebug("qmDisqualified::bulk_set:%d", teamNumber->value());
    for ( int ii = 0; ii < tableWidget->rowCount(); ii++)
    {
        QTableWidgetItem *item;
        item = tableWidget->item(ii, 0);
        if ( item )
        {
            int team = item->data(Qt::EditRole).toInt();
            if ( team && team == teamNumber->value() )
            {
                tableWidget->scrollToItem(item,QAbstractItemView::PositionAtCenter);
                //qDebug("Found Entry:%d", team);

                enum Qt::CheckState state;
                state = cb_disq->checkState();
                if (state != Qt::PartiallyChecked)
                {
                    item = tableWidget->item(ii,2);
                    item->setCheckState(state);
                    item_set = TRUE;
                }

                state = cb_ne->checkState();
                if (state != Qt::PartiallyChecked)
                {
                    item = tableWidget->item(ii,3);
                    item->setCheckState(state);
                    item_set = TRUE;
                }

                state = cb_enable->checkState();
                if (state != Qt::PartiallyChecked)
                {
                    item = tableWidget->item(ii,4);
                    item->setCheckState(state);
                    item_set = TRUE;
                }

                state = cb_vetcheck->checkState();
                if (state != Qt::PartiallyChecked)
                {
                    item = tableWidget->item(ii,5);
                    item->setCheckState(state);
                    item_set = TRUE;
                }
                team_found = TRUE;
                break;
            }
        }
    }

    /*
    **  Report to user
    */
    if ( team_found)
    {
        if (item_set )
            MainWindow::showMessage("Values set");
        else
            MainWindow::showMessage("Nothing selected to set");
    }
    else
    {
        MainWindow::showMessage("Team not found");
    }
}

/*----------------------------------------------------------------------------
** FUNCTION           : center_display
**
** DESCRIPTION        : Place the specified team in the center of the display
**                      If not found then place close item in display
**
**
** INPUTS             : teamNumber
**
** RETURNS            : Nothing
**
----------------------------------------------------------------------------*/

void qmDisqualified::center_display(int teamNumber)
{
    QTableWidgetItem *item = NULL;
    for ( int ii = 0; ii < tableWidget->rowCount(); ii++)
    {
        item = tableWidget->item(ii, 0);
        if ( item )
        {
            int team = item->data(Qt::EditRole).toInt();
            if ( team && team >= teamNumber )
            {
                break;
            }
        }
    }
    if ( item )
    {
        tableWidget->scrollToItem(item,QAbstractItemView::PositionAtCenter);
    }
}

qmDisqualifiedItem::qmDisqualifiedItem ( const QString & text, int type ) :  QTableWidgetItem(text, type+QTableWidgetItem::UserType)
{
    number = 0;
}
qmDisqualifiedItem::qmDisqualifiedItem ( int value, int type ) :  QTableWidgetItem(type+QTableWidgetItem::UserType)
{
    number = value;
    setData(0,value);
}
qmDisqualifiedItem::qmDisqualifiedItem ( const QString & text, bool checked, int num, int type ) : QTableWidgetItem(text, type+QTableWidgetItem::UserType)
{
    setCheckState(checked ? Qt::Checked : Qt::Unchecked);
    number = num;
}

// Special sorting, based on type
//      type == 0 : Text
//      type == 1 : Number
//      type == 2 : Checked
//
bool qmDisqualifiedItem::operator< ( const QTableWidgetItem & other ) const
{
    const qmDisqualifiedItem * other_widget = dynamic_cast<const qmDisqualifiedItem*>(&other);
    if (other_widget && other_widget->type() == QTableWidgetItem::UserType + 2 )
    {
        if (other_widget->checkState() == checkState() )
        {
//            qDebug ("Same(%d): %d : %d", other_widget->number < number, other_widget->number, number);
            if (this->tableWidget()->horizontalHeader()->sortIndicatorOrder() != Qt::AscendingOrder )
            {
                return other_widget->number < number;
            }
            else
            {
                return number < other_widget->number;
            }
        }
        return (other_widget->checkState() < checkState() );
    }

    return ( QTableWidgetItem::operator< (other) );
}