190 lines
6.3 KiB
C++
190 lines
6.3 KiB
C++
/***************************************************************************
|
|
* Copyright (C) 2008 by Oliver Groß *
|
|
* z.o.gross@gmx.de *
|
|
* *
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program; if not, write to the *
|
|
* Free Software Foundation, Inc., *
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
***************************************************************************/
|
|
#include <QApplication>
|
|
#include <QStatusBar>
|
|
#include <QMenuBar>
|
|
#include <QMenu>
|
|
#include <QToolBar>
|
|
#include <QAction>
|
|
#include <QFileDialog>
|
|
#include <QMessageBox>
|
|
#include <QDir>
|
|
#include <QInputDialog>
|
|
|
|
#include <libqnono/cnonogram.h>
|
|
#include <libqnono/ccrosspackage.h>
|
|
|
|
#include "constants.h"
|
|
#include "cgamewindow.h"
|
|
#include "ccrossfieldwidget.h"
|
|
#include "cnewgamedialog.h"
|
|
#include "chighscore.h"
|
|
|
|
namespace qcross {
|
|
using namespace libqnono;
|
|
//public
|
|
CGameWindow::CGameWindow(QWidget * parent) : QMainWindow(parent) {
|
|
m_Field = new CCrossFieldWidget(this);
|
|
m_Nonogram = m_Field->nonogram();
|
|
|
|
setCentralWidget(m_Field);
|
|
m_Field->showMessage(tr("Welcome to QCross!"));
|
|
|
|
createActions();
|
|
|
|
connect(m_Nonogram, SIGNAL(won(int)), this, SLOT(wonGame(int)));
|
|
connect(m_Nonogram, SIGNAL(timeup()), this, SLOT(lostGame()));
|
|
|
|
connect(m_Nonogram, SIGNAL(loaded()), SLOT(updateStates()));
|
|
connect(m_Nonogram, SIGNAL(restarted()), SLOT(updateStates()));
|
|
connect(m_Nonogram, SIGNAL(paused(int)), SLOT(updateStates()));
|
|
connect(m_Nonogram, SIGNAL(resumed(int)), SLOT(updateStates()));
|
|
}
|
|
|
|
CGameWindow::~CGameWindow() {
|
|
}
|
|
|
|
//protected
|
|
void CGameWindow::createActions() {
|
|
QToolBar * currentToolBar;
|
|
QMenu * currentMenu;
|
|
|
|
//Game
|
|
currentMenu = menuBar()->addMenu(tr("&Game"));
|
|
currentMenu->addAction(tr("&New..."), this, SLOT(newGame()), Qt::CTRL + Qt::Key_N);
|
|
currentMenu->addSeparator();
|
|
m_SaveGameAction = currentMenu->addAction(tr("&Save..."), this, SLOT(saveGame()), Qt::CTRL + Qt::Key_N);
|
|
m_SaveGameAction->setEnabled(false);
|
|
currentMenu->addAction(tr("&Load..."), this, SLOT(loadGame()), Qt::CTRL + Qt::Key_N);
|
|
currentMenu->addSeparator();
|
|
m_RestartGameAction = currentMenu->addAction(tr("&Restart"), m_Nonogram, SLOT(restart()), Qt::CTRL + Qt::Key_R);
|
|
m_RestartGameAction->setEnabled(false);
|
|
m_PauseGameAction = currentMenu->addAction(tr("&Pause"));
|
|
m_PauseGameAction->setEnabled(false);
|
|
m_PauseGameAction->setCheckable(true);
|
|
m_PauseGameAction->setShortcut(Qt::CTRL + Qt::Key_P);
|
|
connect(m_PauseGameAction, SIGNAL(toggled(bool)), this, SLOT(pauseGame(bool)));
|
|
currentMenu->addSeparator();
|
|
currentMenu->addAction(tr("&Quit"), this, SLOT(close()), Qt::CTRL + Qt::Key_Q);
|
|
currentMenu->addSeparator();
|
|
currentMenu->addAction(tr("debug: solve"), m_Nonogram, SLOT(solve()));
|
|
|
|
currentToolBar = addToolBar(currentMenu->title());
|
|
currentToolBar->addActions(currentMenu->actions());
|
|
|
|
//Help
|
|
currentMenu = menuBar()->addMenu(tr("&Help"));
|
|
currentMenu->addAction(tr("&About"), this, SLOT(about()));
|
|
currentMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt()));
|
|
}
|
|
|
|
void CGameWindow::newGame() {
|
|
bool notPaused = !m_Nonogram->isPaused();
|
|
if (notPaused)
|
|
m_Nonogram->pause();
|
|
|
|
CNewGameDialog dialog;
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
m_Nonogram->start(dialog.getProblem());
|
|
return;
|
|
}
|
|
|
|
if (notPaused)
|
|
m_Nonogram->resume();
|
|
}
|
|
|
|
|
|
void CGameWindow::saveGame() {
|
|
bool notPaused = !m_Nonogram->isPaused();
|
|
if (notPaused)
|
|
m_Nonogram->pause();
|
|
|
|
QString fileName = QFileDialog::getSaveFileName(this, tr("Save current game"),
|
|
QDir::homePath(), tr("QCross Saved games (*.csg)"));
|
|
|
|
m_Nonogram->saveGame(fileName);
|
|
|
|
if (notPaused)
|
|
m_Nonogram->resume();
|
|
}
|
|
|
|
void CGameWindow::loadGame() {
|
|
bool notPaused = !m_Nonogram->isPaused();
|
|
if (notPaused)
|
|
m_Nonogram->pause();
|
|
|
|
QString fileName = QFileDialog::getOpenFileName(this, tr("Open saved game"),
|
|
QDir::homePath(), tr("QCross Saved games (*.csg)"));
|
|
|
|
if (m_Nonogram->loadGame(fileName)) {
|
|
return;
|
|
}
|
|
|
|
if (notPaused)
|
|
m_Nonogram->resume();
|
|
}
|
|
|
|
void CGameWindow::pauseGame(bool value) {
|
|
if (value == m_Nonogram->isPaused())
|
|
return;
|
|
|
|
if (value) {
|
|
m_Nonogram->pause();
|
|
}
|
|
else {
|
|
m_Nonogram->resume();
|
|
}
|
|
}
|
|
|
|
void CGameWindow::wonGame(int score) {
|
|
m_PauseGameAction->setEnabled(false);
|
|
if (score < 0) return;
|
|
const NonogramProblem &problem(m_Nonogram->problem());
|
|
if (!problem.packageIdentifier().isEmpty() && problem.packageIndex() >= 0) {
|
|
CHighscore highscore;
|
|
highscore.setPackageIdentifier(problem.packageIdentifier());
|
|
if (highscore.open()) {
|
|
int oldScore = highscore[problem.packageIndex()];
|
|
if (0 == oldScore || (problem.timeout() > 0 ? score > oldScore : score < oldScore)) {
|
|
qDebug("attempting to save highscore");
|
|
highscore[problem.packageIndex()] = score;
|
|
highscore.save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void CGameWindow::lostGame() {
|
|
m_PauseGameAction->setEnabled(false);
|
|
}
|
|
|
|
void CGameWindow::about() {
|
|
QMessageBox::about(this, tr("About"), tr("This is a still unfancy gui for solving nonograms."));
|
|
}
|
|
|
|
void CGameWindow::updateStates() {
|
|
bool valid = m_Nonogram->valid();
|
|
m_SaveGameAction->setEnabled(valid);
|
|
m_PauseGameAction->setEnabled(valid);
|
|
m_RestartGameAction->setEnabled(valid);
|
|
m_PauseGameAction->setChecked(m_Nonogram->isPaused());
|
|
}
|
|
}
|