Subversion Repositories svn1

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
103 - 1
#include "qmconfwinners.h"
174 - 2
#include "qmconfig.h"
181 - 3
#include "mainwindow.h"
103 - 4
 
104 - 5
#include    "consts.h"
6
#include    "structs.h"
7
#include    "proto.h"
8
 
380 david 9
#include <QDialogButtonBox>
10
#include <QTableWidgetItem>
11
#include <QVBoxLayout>
12
#include <QtCore/QVariant>
13
#include <QAction>
14
#include <QApplication>
15
#include <QButtonGroup>
16
#include <QGroupBox>
17
#include <QHeaderView>
18
#include <QLabel>
19
#include <QPushButton>
20
#include <QTableWidget>
21
#include <QWidget>
104 - 22
 
103 - 23
QmConfWinners::QmConfWinners(QWidget *parent) :
380 david 24
    QWidget(parent)
103 - 25
{
104 - 26
 
181 - 27
    dirty = true;
28
    populating = false;
29
 
380 david 30
    QVBoxLayout *verticalLayout = new QVBoxLayout(this);
31
    verticalLayout->setContentsMargins(0, 0, 0, 0);
181 - 32
 
380 david 33
    QGroupBox *groupBox = new QGroupBox("Hall of Fame");
34
    verticalLayout->addWidget(groupBox);
35
    QVBoxLayout *verticalLayout2 = new QVBoxLayout(groupBox);
36
    tableWidget = new QTableWidget(groupBox);
37
 
38
    tableWidget->setAlternatingRowColors(true);
39
    tableWidget->setRowCount(MAX_FAME);
40
    tableWidget->setColumnCount(1);
41
    tableWidget->horizontalHeader()->setVisible(false);
42
    tableWidget->horizontalHeader()->setCascadingSectionResizes(false);
43
    tableWidget->horizontalHeader()->setStretchLastSection(true);
44
    tableWidget->verticalHeader()->setDefaultSectionSize(20);
45
    tableWidget->verticalHeader()->setStretchLastSection(false);
46
    verticalLayout2->addWidget(tableWidget);
47
 
48
    QSpacerItem *verticalSpacer1;
49
    verticalSpacer1 = new QSpacerItem(0, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
50
    verticalLayout2->addItem(verticalSpacer1);
51
 
52
    QHBoxLayout *horizontalLayout;
53
    horizontalLayout = new QHBoxLayout();
54
    horizontalLayout->setContentsMargins(0, 0, 5, 5);
55
    verticalLayout->addLayout(horizontalLayout);
56
 
57
    QDialogButtonBox *buttonBox = new QDialogButtonBox();
58
    pushButtonRestore = buttonBox->addButton("Restore",QDialogButtonBox::ActionRole );
59
    pushButtonSave = buttonBox->addButton("Save",QDialogButtonBox::ActionRole );
60
    horizontalLayout->addWidget(buttonBox);
61
 
62
    connect(pushButtonSave, SIGNAL(clicked()), this, SLOT(save()) );
63
    connect(pushButtonRestore, SIGNAL(clicked()), this, SLOT(cancel()) );
64
    connect(tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(changed()));
65
 
104 - 66
    populate();
103 - 67
}
68
 
69
QmConfWinners::~QmConfWinners()
70
{
71
}
72
 
104 - 73
void QmConfWinners::populate(void)
74
{
181 - 75
    populating = true;
104 - 76
    for ( int ii = 0; ii < MAX_FAME; ii++)
77
    {
380 david 78
        tableWidget->setItem(ii, 0, new QTableWidgetItem(config.hall_fame[ii] ));
104 - 79
    }
181 - 80
    populating = false;
81
    updateChanged(false);
104 - 82
}
83
 
84
void QmConfWinners::save(void)
85
{
86
    /*
87
    **    Copy original data
88
    */
176 - 89
     QmConfig newcfg(config);
104 - 90
 
91
    /*
92
    **  Extract the data from the Widgets
93
    */
94
    for ( int ii = 0; ii < MAX_FAME; ii++)
95
    {
380 david 96
        QTableWidgetItem *item = tableWidget->item ( ii, 0 );
104 - 97
        if ( item )
98
        {
99
            strncpy(newcfg.hall_fame[ii], qPrintable(item->text()), sizeof(newcfg.hall_fame[ii])-1);
100
        }
101
    }
102
 
103
    newcfg.num_fame = 0;
104
    for( int i = 0; i < MAX_FAME; i++ )
105
        if( newcfg.hall_fame[i][0] )
106
            newcfg.num_fame++;
107
 
181 - 108
    // Sanity Check
109
    try
104 - 110
    {
181 - 111
        MainWindow::showMessage( "Saving Config");
112
        for( int i = newcfg.num_fame; i < MAX_FAME; i++ )
113
            if( newcfg.hall_fame[i][0] )
114
            {
115
                throw ( "Configuration error: Missing Fame name. Gaps not allowed" );
116
            }
117
 
104 - 118
        config = newcfg;
176 - 119
        config.write_config();
181 - 120
        updateChanged(false);
104 - 121
    }
181 - 122
    catch ( const char * str)
123
    {
124
        MainWindow::showMessage(str);
125
    }
126
 
104 - 127
}
128
 
129
void QmConfWinners::cancel(void)
130
{
131
    populate();
132
}
181 - 133
 
134
void QmConfWinners::changed(void)
135
{
136
    if ( populating )
137
        return;
138
    updateChanged(true);
139
}
140
 
141
void QmConfWinners::updateChanged(bool newDirty)
142
{
143
    if (newDirty != dirty)
144
    {
145
        dirty = newDirty;
146
        if (dirty)
147
        {
380 david 148
            pushButtonSave->setEnabled(true);
149
            pushButtonSave->setStyleSheet("background-color: rgb(255, 0, 0);");
150
            MainWindow::showMessage("Winners Data Changed");
181 - 151
        }
152
        else
153
        {
380 david 154
            pushButtonSave->setEnabled(false);
155
            pushButtonSave->setStyleSheet("");
156
            MainWindow::showMessage("");
181 - 157
        }
158
    }
159
}
160