Commit ed35ed81 authored by Johannes Bergmeier's avatar Johannes Bergmeier

Fixing loading and creating your own game

* Fixed showing cells as wrong when entering your own game
* Fixed crash when starting your own game or loading
* Some cleanup of class KSudoku to prevent further crashes
* Reomoved unneeded code in serializer

svn path=/trunk/KDE/kdegames/ksudoku/; revision=733926
parent 28d81608
......@@ -114,13 +114,12 @@ void KSudoku::updateStatusBar()
}
KSudoku::KSudoku()
: KXmlGuiWindow(), m_gameVariants(new GameVariantCollection(this, true)), m_autoDelCentralWidget(false)
: KXmlGuiWindow(), m_gameVariants(new GameVariantCollection(this, true))
{
setObjectName("ksudoku");
m_gameWidget = 0;
m_gameUI = 0;
activeWidget = 0;
m_selectValueMapper = new QSignalMapper(this);
connect(m_selectValueMapper, SIGNAL(mapped(int)), this, SLOT(selectValue(int)));
......@@ -144,10 +143,10 @@ KSudoku::KSudoku()
// Create ValueListWidget
m_valueListWidget = new ValueListWidget(wrapper);
wrapper->layout()->addWidget(m_valueListWidget);
// m_valueListWidget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
m_valueListWidget->setFixedWidth(60);
m_welcomeScreen = new WelcomeScreen(wrapper, m_gameVariants);
wrapper->layout()->addWidget(m_welcomeScreen);
connect(m_welcomeScreen, SIGNAL(newGameStarted(const Game&,GameVariant*)), this, SLOT(startGame(const Game&)));
showWelcomeScreen();
......@@ -165,6 +164,11 @@ KSudoku::KSudoku()
timer->start( 1000); //TODO PORT, false ); // 2 seconds single-shot timer
}
KSudoku::~KSudoku()
{
endCurrentGame();
}
void KSudoku::updateShapesList()
{
// TODO clear the list
......@@ -213,11 +217,11 @@ void KSudoku::updateShapesList()
}
}
KSudoku::~KSudoku()
{
}
void KSudoku::startGame(const Game& game) {
m_welcomeScreen->hide();
endCurrentGame();
KsView* view = new KsView(game, this);
view->setValueListWidget(m_valueListWidget);
......@@ -228,22 +232,15 @@ void KSudoku::startGame(const Game& game) {
connect(m_valueListWidget, SIGNAL(valueSelected(int)), view, SLOT(selectValue(int)));
connect(view, SIGNAL(valueSelected(int)), SLOT(updateStatusBar()));
m_welcomeScreen->hide();
QWidget* widget = view->widget();
m_gameUI = view;
wrapper->layout()->addWidget(widget);
widget->show();
Game game2(game);
connect(game2.interface(), SIGNAL(completed(bool,const QTime&,bool)), SLOT(onCompleted(bool,const QTime&,bool)));
connect(game2.interface(), SIGNAL(modified(bool)), SLOT(onModified(bool)));
connect(game.interface(), SIGNAL(completed(bool,const QTime&,bool)), SLOT(onCompleted(bool,const QTime&,bool)));
connect(game.interface(), SIGNAL(modified(bool)), SLOT(onModified(bool)));
widget->show();
wrapper->layout()->addWidget(widget);
activeWidget = widget;
m_autoDelCentralWidget = true;
adaptActions2View();
QSizePolicy policy(QSizePolicy::Expanding, QSizePolicy::Expanding);
......@@ -261,6 +258,16 @@ void KSudoku::startGame(const Game& game) {
m_valueListWidget->show();
}
void KSudoku::endCurrentGame() {
m_valueListWidget->hide();
delete m_gameUI;
m_gameUI = 0;
adaptActions2View();
}
void KSudoku::loadGame(const KUrl& Url) {
QString errorMsg;
Game game = ksudoku::Serializer::load(Url, this, &errorMsg);
......@@ -272,25 +279,10 @@ void KSudoku::loadGame(const KUrl& Url) {
startGame(game);
}
void KSudoku::setCentralWidget(QWidget* widget, bool autoDel) {
QWidget* oldWidget = activeWidget;
if(oldWidget) oldWidget->hide();
if(m_autoDelCentralWidget) delete oldWidget; //moving up here fixes a roxdoku window bug
m_autoDelCentralWidget = autoDel;
widget->show();
wrapper->layout()->addWidget(widget);
activeWidget = widget;
adaptActions2View();
}
void KSudoku::showWelcomeScreen() {
m_valueListWidget->hide();
delete m_gameUI;
m_gameUI = 0;
endCurrentGame();
setCentralWidget(m_welcomeScreen, false);
m_welcomeScreen->show();
}
void KSudoku::homepage()
......@@ -347,11 +339,7 @@ void KSudoku::dubPuzzle()
if(KMessageBox::questionYesNo(this, i18n("Do you want to play the puzzle now?")) == 3)
{
ksudoku::Game* newGame = new ksudoku::Game(puzzle);
// (new KSudoku(newGame))->show();
startGame(*newGame);
delete newGame;
startGame(ksudoku::Game(puzzle));
}
else
{
......@@ -781,18 +769,6 @@ Game KSudoku::currentGame() const {
}
ksudoku::KsView* KSudoku::currentView() const{
// if(ksudoku::KsView* view = dynamic_cast<KsView*>(m_tabs->currentPage()))
// return view;
// else
// return 0;
// TODO this might cause trouble as the central widget don't have to be a
// KsView instance
// if(activeWidget)
// if(centralWidget() == 0) return 0;
// return dynamic_cast<KsView*>(activeWidget);
return m_gameUI;
}
......
......@@ -103,12 +103,6 @@ public:
Game currentGame() const;
ksudoku::KsView* currentView() const;
// override central Widget
virtual void setCentralWidget(QWidget* widget) { setCentralWidget(widget, false); }
void setCentralWidget(QWidget* widget, bool autoDel);
QMap<QString, SKSolver*>& shapes(){return m_shapes;}
protected:
/**
* Overridden virtuals for Qt drag 'n drop (XDND)
......@@ -122,6 +116,7 @@ public slots:
void showWelcomeScreen();
void startGame(const Game& game);
void endCurrentGame();
private slots:
void gameNew();
......@@ -170,8 +165,6 @@ public:
void createCustomShape();
private:
QWidget* wrapper;
QWidget* activeWidget;
void createAction(const QString& text, const char* slot, const QString& name, const QString& icon);
void setupAccel();
void setupActions();
......@@ -180,9 +173,7 @@ private:
private:
// KTabWidget* m_tabs;
// GameSelectionDialog* m_gameSelDlg;
QWidget* wrapper;
QSignalMapper* m_selectValueMapper;
QSignalMapper* m_enterValueMapper;
......@@ -203,15 +194,6 @@ private:
KsView* m_gameUI;
bool m_autoDelCentralWidget;
QString m_defaultAction;
bool m_optionEnterOwnGame;
QMap<QString, SKSolver*> m_shapes;
QStringList m_shapes_paths;
QString m_shape_save_path;
Symbols m_symbols;
};
......
......@@ -208,7 +208,7 @@ int Game::size() const {
return m_private->puzzle->size();
}
GameIFace* Game::interface() {
GameIFace* Game::interface() const {
return m_private;
}
......@@ -418,7 +418,9 @@ ksudoku::ButtonState Game::buttonState(int index) const {
return GivenValue;
if(value(index) == 0)
return Marker;
if(value(index) != m_private->puzzle->solution(index))
if(value(index) == m_private->puzzle->solution(index))
return CorrectValue;
if(m_private->puzzle->solution(index))
return WrongValue;
return CorrectValue;
}
......@@ -431,7 +433,9 @@ CellInfo Game::cellInfo(int index) const {
return CellInfo(GivenValue, value(index));
if(value(index) == 0)
return CellInfo(m_private->state.markers(index));
if(value(index) != m_private->puzzle->solution(index))
if(value(index) == m_private->puzzle->solution(index))
return CellInfo(CorrectValue, value(index));
if(m_private->puzzle->solution(index))
return CellInfo(WrongValue, value(index));
return CellInfo(CorrectValue, value(index));
}
......
......@@ -118,7 +118,7 @@ public:
* Gets the the interface of the game. Use this if for connecting to signals or
* slots of the game
*/
GameIFace* interface();
GameIFace* interface() const;
Game& operator=(const Game& game);
......
......@@ -368,23 +368,23 @@ Game Serializer::load(const KUrl& url, QWidget* window, QString *errorMsg) {
child = child.nextSibling();
}
SKSolver* sk = (SKSolver*) game.puzzle()->solver();
QString name = ((GraphCustom*)sk->g)->name;
KSudoku* p = (KSudoku*) window;
if(!p->shapes().contains(name))
{
KStandardDirs myStdDir;
const QString destDir = myStdDir.saveLocation("data", /* TODO PORT kapp->instanceName() +*/ "ksudoku/", true);
KStandardDirs::makeDir(destDir);
QString path = destDir + name + ".xml";
KUrl url;
url.setPath(path);
Serializer::storeCustomShape( sk, url ,window );
p->updateShapesList();
}
// SKSolver* sk = (SKSolver*) game.puzzle()->solver();
// QString name = ((GraphCustom*)sk->g)->name;
// KSudoku* p = (KSudoku*) window;
// if(!p->shapes().contains(name))
// {
// KStandardDirs myStdDir;
// const QString destDir = myStdDir.saveLocation("data", /* TODO PORT kapp->instanceName() +*/ "ksudoku/", true);
// KStandardDirs::makeDir(destDir);
//
// QString path = destDir + name + ".xml";
// KUrl url;
// url.setPath(path);
//
// Serializer::storeCustomShape( sk, url ,window );
// p->updateShapesList();
//
// }
return game;
}
......
......@@ -50,6 +50,7 @@ KsView::KsView(const Game& game, QObject* parent)
KsView::~KsView()
{
delete m_viewWidget;
}
void KsView::createView() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment