Subversion Repositories svn1-original

Rev

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

#include "qmconfwinners.h"
#include "qmconfig.h"
#include "ui_qmconfwinners.h"
#include "mainwindow.h"

#include    "consts.h"
#include    "structs.h"
#include    "proto.h"

#include <QDialogButtonBox>
#include <QTableWidgetItem>
#include <QVBoxLayout>

QmConfWinners::QmConfWinners(QWidget *parent) :
    QWidget(parent)
{

    dirty = true;
    populating = false;

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

    QGroupBox *groupBox = new QGroupBox("Hall of Fame");
    verticalLayout->addWidget(groupBox);
    QVBoxLayout *verticalLayout2 = new QVBoxLayout(groupBox);
    tableWidget = new QTableWidget(groupBox);

    tableWidget->setAlternatingRowColors(true);
    tableWidget->setRowCount(MAX_FAME);
    tableWidget->setColumnCount(1);
    tableWidget->horizontalHeader()->setVisible(false);
    tableWidget->horizontalHeader()->setCascadingSectionResizes(false);
    tableWidget->horizontalHeader()->setStretchLastSection(true);
    tableWidget->verticalHeader()->setDefaultSectionSize(20);
    tableWidget->verticalHeader()->setStretchLastSection(false);
    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()), this, SLOT(save()) );
    connect(pushButtonRestore, SIGNAL(clicked()), this, SLOT(cancel()) );
    connect(tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(changed()));

    populate();
}

QmConfWinners::~QmConfWinners()
{
}

void QmConfWinners::populate(void)
{
    populating = true;
    for ( int ii = 0; ii < MAX_FAME; ii++)
    {
        tableWidget->setItem(ii, 0, new QTableWidgetItem(config.hall_fame[ii] ));
    }
    populating = false;
    updateChanged(false);
}

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

    /*
    **  Extract the data from the Widgets
    */
    for ( int ii = 0; ii < MAX_FAME; ii++)
    {
        QTableWidgetItem *item = tableWidget->item ( ii, 0 );
        if ( item )
        {
            strncpy(newcfg.hall_fame[ii], qPrintable(item->text()), sizeof(newcfg.hall_fame[ii])-1);
        }
    }

    newcfg.num_fame = 0;
    for( int i = 0; i < MAX_FAME; i++ )
        if( newcfg.hall_fame[i][0] )
            newcfg.num_fame++;

    // Sanity Check
    try
    {
        MainWindow::showMessage( "Saving Config");
        for( int i = newcfg.num_fame; i < MAX_FAME; i++ )
            if( newcfg.hall_fame[i][0] )
            {
                throw ( "Configuration error: Missing Fame name. Gaps not allowed" );
            }

        config = newcfg;
        config.write_config();
        updateChanged(false);
    }
    catch ( const char * str)
    {
        MainWindow::showMessage(str);
    }

}

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

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

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