1 /***********************************************************************
2 *
3 * Copyright (C) 2007, 2008, 2009, 2012, 2014 Graeme Gott <graeme@gottcode.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***********************************************************************/
19
20 #include "new_game_dialog.h"
21
22 #include <QComboBox>
23 #include <QDialogButtonBox>
24 #include <QFormLayout>
25 #include <QLabel>
26 #include <QSettings>
27 #include <QSpinBox>
28 #include <QVBoxLayout>
29
30 // ============================================================================
31
NewGameDialog(QWidget * parent)32 NewGameDialog::NewGameDialog(QWidget* parent)
33 : QDialog(parent)
34 {
35 setWindowTitle(tr("New Game"));
36
37 // Create widgets
38 m_mazes_preview = new QLabel(this);
39
40 m_mazes_algorithm = new QComboBox(this);
41 m_mazes_algorithm->setInsertPolicy(QComboBox::InsertAlphabetically);
42 connect(m_mazes_algorithm, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &NewGameDialog::algorithmSelected);
43 m_mazes_algorithm->addItem(tr("Hunt and Kill"), 0);
44 m_mazes_algorithm->addItem(tr("Kruskal"), 1);
45 m_mazes_algorithm->addItem(tr("Prim"), 2);
46 m_mazes_algorithm->addItem(tr("Recursive Backtracker"), 3);
47 m_mazes_algorithm->addItem(tr("Stack"), 4);
48 m_mazes_algorithm->addItem(tr("Stack 2"), 5);
49 m_mazes_algorithm->addItem(tr("Stack 3"), 6);
50 m_mazes_algorithm->addItem(tr("Stack 4"), 7);
51 m_mazes_algorithm->addItem(tr("Stack 5"), 8);
52
53 m_mazes_targets = new QSpinBox(this);
54 m_mazes_targets->setRange(1, 99);
55
56 m_mazes_size = new QSpinBox(this);
57 m_mazes_size->setRange(10, 99);
58
59 QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
60 connect(buttons, &QDialogButtonBox::accepted, this, &NewGameDialog::accept);
61 connect(buttons, &QDialogButtonBox::rejected, this, &NewGameDialog::reject);
62
63 // Load settings
64 QSettings settings;
65 int algorithm = settings.value("New/Algorithm", 4).toInt();
66 m_mazes_algorithm->setCurrentIndex(m_mazes_algorithm->findData(algorithm));
67 m_mazes_targets->setValue(settings.value("New/Targets", 3).toInt());
68 m_mazes_size->setValue(settings.value("New/Size", 50).toInt());
69
70 // Lay out dialog
71 QFormLayout* contents_layout = new QFormLayout;
72 contents_layout->addRow("", m_mazes_preview);
73 contents_layout->addRow(tr("Algorithm:"), m_mazes_algorithm);
74 contents_layout->addRow(tr("Targets:"), m_mazes_targets);
75 contents_layout->addRow(tr("Size:"), m_mazes_size);
76
77 QVBoxLayout* layout = new QVBoxLayout(this);
78 layout->addLayout(contents_layout);
79 layout->addWidget(buttons);
80 }
81
82 // ============================================================================
83
accept()84 void NewGameDialog::accept()
85 {
86 QSettings settings;
87
88 settings.setValue("New/Algorithm", m_mazes_algorithm->itemData(m_mazes_algorithm->currentIndex()));
89 settings.setValue("New/Targets", m_mazes_targets->value());
90 settings.setValue("New/Size", m_mazes_size->value());
91
92 QDialog::accept();
93 }
94
95 // ============================================================================
96
algorithmSelected(int index)97 void NewGameDialog::algorithmSelected(int index)
98 {
99 if (index != -1) {
100 m_mazes_preview->setPixmap( QString(":/preview%1.png").arg( m_mazes_algorithm->itemData(index).toInt()) );
101 }
102 }
103
104 // ============================================================================
105