~ubuntu-branches/ubuntu/quantal/kdegames/quantal

« back to all changes in this revision

Viewing changes to ksudoku/src/gui/welcomescreen.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:50 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20111215141750-6tj6brf4azhrt915
Tags: 4:4.7.90-0ubuntu1
new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include <KMessageBox>
22
22
 
 
23
#include <QDebug>
 
24
 
23
25
#include "ksudokugame.h"
 
26
#include "globals.h"
24
27
 
25
28
Q_DECLARE_METATYPE(ksudoku::GameVariant*)
26
29
 
34
37
        setupUi(this);
35
38
        gameListWidget->setModel(m_collection);
36
39
        gameListWidget->setItemDelegate(delegate);
37
 
    gameListWidget->setVerticalScrollMode(QListView::ScrollPerPixel);
38
 
        
 
40
        gameListWidget->setVerticalScrollMode(QListView::ScrollPerPixel);
 
41
        gameListWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
 
42
        gameListWidget->setSelectionMode(QAbstractItemView::SingleSelection);
 
43
 
 
44
        // Get the previous puzzle configuration.
 
45
        KConfigGroup gameGroup (KGlobal::config(), "KSudokuGame");
 
46
        m_selectedPuzzle = gameGroup.readEntry("SelectedPuzzle", 0);
 
47
        m_difficulty     = gameGroup.readEntry("Difficulty", (int) VeryEasy);
 
48
        m_symmetry       = gameGroup.readEntry("Symmetry"  , (int) CENTRAL);
 
49
 
 
50
        // This has to be a deferred call (presumably to allow the view's setup
 
51
        // to complete), otherwise the selection fails to appear.
 
52
        QMetaObject::invokeMethod (this, "setSelectedVariant",
 
53
                                   Qt::QueuedConnection,
 
54
                                   Q_ARG (int, m_selectedPuzzle));
 
55
 
39
56
        connect(gameListWidget->selectionModel(), SIGNAL(currentChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(onCurrentVariantChange()));
40
57
        
41
58
        connect(getNewGameButton, SIGNAL(clicked(bool)), this, SLOT(getNewVariant()));
42
59
        // TODO disabled due to missing per-game config dialog
43
60
//      connect(configureGameButton, SIGNAL(clicked(bool)), this, SLOT(configureVariant()));
 
61
        // connect(playGameButton, SIGNAL(clicked(bool)), this, SLOT(playVariant()));                   // Disable old create-game code.
 
62
        // connect(gameListWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(playVariant()));      // Disable old create-game code.
 
63
 
44
64
        connect(startEmptyButton, SIGNAL(clicked(bool)), this, SLOT(startEmptyGame()));
45
 
        connect(playGameButton, SIGNAL(clicked(bool)), this, SLOT(playVariant()));
46
 
        connect(gameListWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(playVariant()));
 
65
        connect(puzzleGeneratorButton, SIGNAL(clicked(bool)), this, SLOT(generatePuzzle()));
 
66
        connect(gameListWidget, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(generatePuzzle()));
47
67
 
48
68
        // GHNS is not implemented yet, so don't show an unuseful button
49
69
        getNewGameButton->hide();
50
70
}
51
71
 
52
72
GameVariant* WelcomeScreen::selectedVariant() const {
53
 
        QModelIndex index = gameListWidget->selectionModel()->currentIndex();
 
73
        QModelIndex index = gameListWidget->currentIndex();
54
74
        return m_collection->variant(index);
55
75
}
56
76
 
 
77
void WelcomeScreen::setSelectedVariant(int row) {
 
78
        gameListWidget->setCurrentIndex(gameListWidget->model()->index(row,0));
 
79
}
 
80
 
57
81
int WelcomeScreen::difficulty() const {
58
 
        return difficultySlider->value();
 
82
        return m_difficulty;
59
83
}
60
84
 
61
85
void WelcomeScreen::setDifficulty(int difficulty) {
62
 
        difficultySlider->setValue(difficulty);
 
86
        m_difficulty = difficulty;
 
87
}
 
88
 
 
89
int WelcomeScreen::symmetry() const {
 
90
        return m_symmetry;
 
91
}
 
92
 
 
93
void WelcomeScreen::setSymmetry(int symmetry) {
 
94
        m_symmetry = symmetry;
63
95
}
64
96
 
65
97
void WelcomeScreen::onCurrentVariantChange() {
66
 
        QItemSelectionModel* selection = gameListWidget->selectionModel();
67
 
        selection->select(selection->currentIndex(), QItemSelectionModel::Select);
68
 
        
69
98
        GameVariant* variant = selectedVariant();
70
99
        if(!variant) {
71
100
                // TODO disabled due to missing per-game config dialog
72
101
//              configureGameButton->setEnabled(false);
73
 
                playGameButton->setEnabled(false);
 
102
                // playGameButton->setEnabled(false);
 
103
                puzzleGeneratorButton->setEnabled(false);
74
104
                return;
75
105
        }
76
106
        
77
107
        // TODO disabled due to missing per-game config dialog
78
108
//      configureGameButton->setEnabled(variant->canConfigure());
79
109
        startEmptyButton->setEnabled(variant->canStartEmpty());
80
 
        playGameButton->setEnabled(true);
 
110
        // playGameButton->setEnabled(true);
 
111
        puzzleGeneratorButton->setEnabled(true);
81
112
}
82
113
 
83
114
void WelcomeScreen::getNewVariant() {
101
132
}
102
133
 
103
134
void WelcomeScreen::playVariant() {
104
 
        GameVariant* variant = selectedVariant();
105
 
        if(!variant) return;
106
 
        
107
 
        Game game = variant->createGame(difficulty());
108
 
        
 
135
        return;         // Disable old game-creation code.
 
136
        GameVariant* variant = selectedVariant();
 
137
        if(!variant) return;
 
138
        
 
139
        Game game = variant->createGame(difficulty(), 0);
 
140
 
 
141
        emit newGameStarted(game, variant);
 
142
}
 
143
 
 
144
void WelcomeScreen::generatePuzzle() {
 
145
        GameVariant* variant = selectedVariant();
 
146
        if(!variant) return;
 
147
 
 
148
        Game game = variant->createGame(difficulty(), symmetry());
 
149
 
 
150
        // Save the selected puzzle configuration.
 
151
        QModelIndex index = gameListWidget->currentIndex();
 
152
        m_selectedPuzzle = index.row();
 
153
 
 
154
        KConfigGroup gameGroup (KGlobal::config(), "KSudokuGame");
 
155
        gameGroup.writeEntry("SelectedPuzzle", m_selectedPuzzle);
 
156
        gameGroup.writeEntry("Difficulty", m_difficulty);
 
157
        gameGroup.writeEntry("Symmetry"  , m_symmetry);
 
158
        gameGroup.sync();               // Ensure that the entry goes to disk.
 
159
 
109
160
        emit newGameStarted(game, variant);
110
161
}
111
162