176 lines
5.8 KiB
C++
176 lines
5.8 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 <libqcross/cnonogram.h>
|
|
#include <libqcross/ccrosspackage.h>
|
|
|
|
#include "cgamewindow.h"
|
|
#include "ccrossfieldwidget.h"
|
|
#include "cnewgamedialog.h"
|
|
#include "chighscore.h"
|
|
|
|
namespace qcross {
|
|
using namespace libqcross;
|
|
//public
|
|
CGameWindow::CGameWindow(QWidget * parent) : QMainWindow(parent) {
|
|
m_Highscore = NULL;
|
|
m_PictureIndex = -1;
|
|
m_Picture = new CNonogram(QSize(10, 10));
|
|
m_Picture->updateNumbers();
|
|
|
|
m_Field = new CCrossFieldWidget(m_Picture, this);
|
|
m_Field->setTime(42 * 60 + 23);
|
|
|
|
setCentralWidget(m_Field);
|
|
// m_Field->showMessage(tr("Welcome to QCross!"));
|
|
|
|
createActions();
|
|
// statusBar()->show();
|
|
|
|
connect(m_Field, SIGNAL(solved()), this, SLOT(wonGame()));
|
|
connect(m_Field, SIGNAL(timeUp()), this, SLOT(lostGame()));
|
|
connect(m_Field, SIGNAL(markError()), this, SLOT(handleErrorMark()));
|
|
}
|
|
|
|
CGameWindow::~CGameWindow() {
|
|
m_Field->setPicture(NULL);
|
|
delete m_Picture;
|
|
}
|
|
|
|
//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_RestartGameAction = currentMenu->addAction(tr("&Restart"), this, SLOT(restartGame()), Qt::CTRL + Qt::Key_R);
|
|
m_RestartGameAction->setEnabled(false);
|
|
m_PauseGameAction = currentMenu->addAction(tr("&Pause"));
|
|
connect(m_PauseGameAction, SIGNAL(toggled(bool)), this, SLOT(pauseGame(bool)));
|
|
m_PauseGameAction->setShortcut(Qt::CTRL + Qt::Key_P);
|
|
m_PauseGameAction->setCheckable(true);
|
|
m_PauseGameAction->setEnabled(false);
|
|
currentMenu->addSeparator();
|
|
currentMenu->addAction(tr("&Quit"), this, SLOT(close()), Qt::CTRL + Qt::Key_Q);
|
|
// currentMenu->addSeparator();
|
|
// currentMenu->addAction(tr("debug: win"), this, SLOT(wonGame()));
|
|
|
|
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_Field->isPaused();
|
|
if (notPaused)
|
|
m_Field->pause();
|
|
|
|
CNewGameDialog dialog;
|
|
if (dialog.exec() == QDialog::Accepted) {
|
|
if (m_Highscore)
|
|
delete m_Highscore;
|
|
|
|
m_Field->setPicture(NULL);
|
|
if (m_Picture)
|
|
delete m_Picture;
|
|
|
|
m_Highscore = dialog.takeHighscore();
|
|
m_PictureIndex = m_Highscore ? dialog.nonogramIndex() : -1;
|
|
|
|
m_Picture = dialog.takeNonogram();
|
|
m_Picture->updateNumbers();
|
|
|
|
m_PauseGameAction->setEnabled(true);
|
|
m_RestartGameAction->setEnabled(true);
|
|
|
|
m_Field->setPicture(m_Picture);
|
|
|
|
m_Field->resume();
|
|
m_Field->showMessage(tr("Game started!"), 1000);
|
|
}
|
|
else if (notPaused)
|
|
m_Field->resume();
|
|
}
|
|
|
|
void CGameWindow::restartGame() {
|
|
m_Field->setTime(m_Picture->timeout());
|
|
|
|
m_PauseGameAction->setChecked(false);
|
|
m_Field->start();
|
|
|
|
m_Field->showMessage(tr("Game restarted."), 1000);
|
|
}
|
|
|
|
void CGameWindow::pauseGame(bool value) {
|
|
if (value) {
|
|
m_Field->showMessage(tr("Game paused."));
|
|
m_Field->pause();
|
|
}
|
|
else {
|
|
m_Field->showMessage(tr("Game resumed."), 1000);
|
|
m_Field->resume();
|
|
}
|
|
}
|
|
|
|
void CGameWindow::wonGame() {
|
|
m_PauseGameAction->setEnabled(false);
|
|
m_Field->showMessage(tr("Congratulations! You've solved the puzzle."));
|
|
if (m_Highscore) {
|
|
qDebug("attempting to save highscore");
|
|
(*m_Highscore)[m_PictureIndex] = m_Field->time();
|
|
m_Highscore->save();
|
|
}
|
|
}
|
|
|
|
void CGameWindow::lostGame() {
|
|
m_PauseGameAction->setEnabled(false);
|
|
m_Field->showMessage(tr("Too bad! Time's up."), 0, CCrossFieldWidget::Critical);
|
|
}
|
|
|
|
void CGameWindow::handleErrorMark() {
|
|
int timeDiff = (m_Field->errorCount() == 1) ? 2 : ((m_Field->errorCount() == 2) ? 4 : 8);
|
|
|
|
m_Field->showMessage(tr("Sorry this was not correct: -%1min").arg(timeDiff), 1000, CCrossFieldWidget::Warning);
|
|
|
|
m_Field->setTime(m_Field->time() - timeDiff*60);
|
|
}
|
|
|
|
void CGameWindow::about() {
|
|
QMessageBox::about(this, tr("About"), tr("This is a still unfancy gui for solving nonograms."));
|
|
}
|
|
}
|