summaryrefslogtreecommitdiff
path: root/src/mainwindow.cc
blob: adb45e6210cb6114c3bde6641a0a9ed12c51493c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

#include "mainwindow.h"
#include "solverwindow.h"

#include "config.h"

/* XPM */
const char *icon_xpm[] = {
"16 16 2 1",
" 	c #000000",
".	c #FFFFFF",
"                ",
" .... .... .... ",
" .... .... .... ",
" .... .... .... ",
" .... .... .... ",
"                ",
" .... .... .... ",
" .... .... .... ",
" .... .... .... ",
" .... .... .... ",
"                ",
" .... .... .... ",
" .... .... .... ",
" .... .... .... ",
" .... .... .... ",
"                "};


MainWindow::MainWindow()
{
	
	setWindowTitle(tr("Sudoku"));
	setWindowIcon(QIcon(QPixmap(icon_xpm)));

	mainwindow_solverwindow = new SolverWindow();	
	setCentralWidget(mainwindow_solverwindow);
	
	initActions();
	
	initMenus();
	
	updateTitle();
}


void MainWindow::initActions()
{
	// Game -> New
	action_new = new QAction(tr("&New"), this);
	action_new->setShortcuts(QKeySequence::New);
	action_new->setStatusTip(tr("Start a new game"));
	connect(action_new, SIGNAL(triggered()), this, SLOT(doNew()));
	
	// Game -> Load
	action_open = new QAction(tr("&Open..."), this);
	action_open->setShortcuts(QKeySequence::Open);
	action_open->setStatusTip(tr("Open a previously saved game"));
	connect(action_open, SIGNAL(triggered()), this, SLOT(doOpen()));
	
	// Game -> Save
	action_save = new QAction(tr("&Save"), this);
	action_save->setShortcuts(QKeySequence::Save);
	action_save->setStatusTip(tr("Save the current game"));
	connect(action_save, SIGNAL(triggered()), this, SLOT(doSave()));

	// Game -> Save As
	action_saveas = new QAction(tr("Save &As..."), this);
	action_saveas->setStatusTip(tr("Save the current game to a new file"));
	connect(action_saveas, SIGNAL(triggered()), this, SLOT(doSaveAs()));
	
	// Game -> Revert
	action_revert = new QAction(tr("&Revert"), this);
	action_revert->setStatusTip(tr("Reload the current game from file"));
	connect(action_revert, SIGNAL(triggered()), this, SLOT(doRevert()));
	
	// Game -> Quit
	action_quit = new QAction(tr("&Quit"), this);
	action_quit->setShortcuts(QKeySequence::Quit);
	action_quit->setStatusTip(tr("Exit the application"));
	connect(action_quit, SIGNAL(triggered()), this, SLOT(doQuit()));
	
	// Move -> Hint
	action_hint = new QAction(tr("Hint"), this);
	action_hint->setStatusTip(tr("Give a hint"));
	// TODO connect()
	
	// Move -> Step
	action_step = new QAction(tr("Step"), this);
	action_step->setStatusTip(tr("Solve a single cell"));
	connect(action_step, SIGNAL(triggered()), mainwindow_solverwindow, SLOT(doStep()));
	
	// Move -> Step
	action_guess = new QAction(tr("Guess"), this);
	action_guess->setStatusTip(tr("Solve a single cell with guessing"));
	connect(action_guess, SIGNAL(triggered()), mainwindow_solverwindow, SLOT(doGuess()));
	
	// Move -> Solve
	action_solve = new QAction(tr("Solve rules"), this);
	action_solve->setStatusTip(tr("Solve sudoku rules"));
	connect(action_solve, SIGNAL(triggered()), mainwindow_solverwindow, SLOT(doSolve()));

	// Move -> Search
	action_search = new QAction(tr("Find solution"), this);
	action_search->setStatusTip(tr("Find a solution"));
	connect(action_search, SIGNAL(triggered()), mainwindow_solverwindow, SLOT(doSearch()));
	
	// Move -> Validate
	action_validate = new QAction(tr("Validate"), this);
	action_validate ->setStatusTip(tr("Validate the sudoku"));
	connect(action_validate, SIGNAL(triggered()), this, SLOT(doValidate()));
	
	// Help -> About
	action_about = new QAction(tr("About..."), this);
	action_about ->setStatusTip(tr("About %1").arg(PACKAGE_NAME));
	connect(action_about, SIGNAL(triggered()), this, SLOT(doAbout()));
}

void MainWindow::initMenus()
{
	mainwindow_gamemenu = menuBar()->addMenu(tr("&Game"));
	mainwindow_gamemenu->addAction(action_new);
	mainwindow_gamemenu->addAction(action_open);
	mainwindow_gamemenu->addSeparator();
	mainwindow_gamemenu->addAction(action_save);
	mainwindow_gamemenu->addAction(action_saveas);
	mainwindow_gamemenu->addAction(action_revert);
	mainwindow_gamemenu->addSeparator();
	mainwindow_gamemenu->addAction(action_quit);
	
	mainwindow_movemenu = menuBar()->addMenu(tr("&Move"));
	//mainwindow_movemenu->addAction(action_hint);
	mainwindow_movemenu->addAction(action_step);
	mainwindow_movemenu->addAction(action_guess);
	mainwindow_movemenu->addSeparator();
	mainwindow_movemenu->addAction(action_solve);
	mainwindow_movemenu->addAction(action_search);
	mainwindow_movemenu->addSeparator();
	mainwindow_movemenu->addAction(action_validate);
	
	mainwindow_helpmenu = menuBar()->addMenu(tr("&Help"));
	mainwindow_helpmenu->addAction(action_about);
}

void MainWindow::updateTitle()
{
	if (mainwindow_solverwindow->filename().isEmpty()) {
		setWindowTitle(PACKAGE_NAME);
		
		action_revert->setEnabled(false);
		
	} else {
		setWindowTitle(mainwindow_solverwindow->filename() + " - " + PACKAGE_NAME);
		
		action_revert->setEnabled(true);
	}
}

void MainWindow::doNew()
{
	mainwindow_solverwindow->doNew();
	updateTitle();
}

void MainWindow::doSave()
{
	mainwindow_solverwindow->doSave();
	updateTitle();
}

void MainWindow::doSaveAs()
{
	mainwindow_solverwindow->doSaveAs();
	updateTitle();
}

void MainWindow::doOpen()
{
	mainwindow_solverwindow->doOpen();
	updateTitle();
}

void MainWindow::doRevert()
{
	mainwindow_solverwindow->doRevert();
	updateTitle();
}

void MainWindow::doValidate()
{
	mainwindow_solverwindow->doValidate();
}

void MainWindow::doQuit()
{
	if (QMessageBox::question(this, tr("Quit"), tr("Exit the application?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
		qApp->closeAllWindows();
	}
}

void MainWindow::doAbout()
{
	QMessageBox::information(this, tr("About %1").arg(PACKAGE_NAME),
		tr("%1 version %2\n\n"
		   "This application was written by Stijn 'Ingar' Buys and "
		   "is available under the terms and conditions of the GNU Public License, version 3 or higher.")
		.arg(PACKAGE_NAME)
		.arg(PACKAGE_VERSION));
}