From d8780822c20d22d06c896340dc2712f48489bb93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sat, 26 Jan 2008 10:10:50 +0000 Subject: [PATCH] - Fixed a bug due to the reject sign '-' preventing to load a saved training game - Qt interface: Loading and saving a game is now supported (it only works in training mode, due to limitations in the core) --- game/game.cpp | 2 +- game/pldrack.cpp | 12 ++- qt/main_window.cpp | 179 +++++++++++++++++++++++++++---------------- qt/main_window.h | 12 +-- qt/ui/main_window.ui | 61 +++++++++++---- 5 files changed, 177 insertions(+), 89 deletions(-) diff --git a/game/game.cpp b/game/game.cpp index 785187c..a9422b7 100644 --- a/game/game.cpp +++ b/game/game.cpp @@ -477,7 +477,7 @@ int Game::helperSetRackManual(unsigned int p, bool iCheck, const wstring &iLette { ASSERT(p < getNPlayers(), "Wrong player number"); - if (!m_dic.validateLetters(iLetters, L"+")) + if (!m_dic.validateLetters(iLetters, L"+-")) return 3; PlayedRack pld; diff --git a/game/pldrack.cpp b/game/pldrack.cpp index b480fd0..6fdad3f 100644 --- a/game/pldrack.cpp +++ b/game/pldrack.cpp @@ -140,8 +140,18 @@ void PlayedRack::setManual(const wstring& iLetters) if (iLetters.empty()) return; + // Handle the reject sign + unsigned int begin; + if (iLetters[0] == L'-') + { + setReject(); + begin = 1; + } + else + begin = 0; + unsigned int i; - for (i = 0; i < iLetters.size() && iLetters[i] != L'+'; i++) + for (i = begin; i < iLetters.size() && iLetters[i] != L'+'; i++) { addOld(Tile(iLetters[i])); } diff --git a/qt/main_window.cpp b/qt/main_window.cpp index 51b7fc7..33d3679 100644 --- a/qt/main_window.cpp +++ b/qt/main_window.cpp @@ -20,6 +20,8 @@ #include "config.h" +#include +#include #include #include #include @@ -28,6 +30,7 @@ #include "dic.h" #include "encoding.h" #include "header.h" +#include "game_factory.h" #include "game.h" #include "freegame.h" #include "player.h" @@ -119,67 +122,6 @@ MainWindow::~MainWindow() } -void MainWindow::on_action_About_triggered() -{ - QString msg; - msg.sprintf("Eliot %s\n\n", VERSION); - msg += _q( \ - "Copyright (C) 1999-2008 - Antoine Fraboulet & Olivier Teuliere\n\n" \ - "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."); - // QMessageBox::about() doesn't add the nice information icon, so we create - // the box manually (not much work...) - QMessageBox *aboutBox = new QMessageBox(QMessageBox::Information, - _q("About Eliot"), msg, QMessageBox::Ok, this); - aboutBox->exec(); -} - - -void MainWindow::on_action_Bag_triggered() -{ - if (m_bagWindow == NULL) - { - // Create the bag window - BagWidget *bagWidget = new BagWidget(NULL); - bagWidget->setGame(m_game); - m_bagWindow = new AuxWindow(*bagWidget, m_ui.action_Bag); - QObject::connect(this, SIGNAL(gameChanged(const Game*)), - bagWidget, SLOT(setGame(const Game*))); - QObject::connect(this, SIGNAL(gameUpdated()), - bagWidget, SLOT(refresh())); - // XXX - m_bagWindow->move(20, 20); - } - if (m_bagWindow->isVisible()) - m_bagWindow->hide(); - else - m_bagWindow->show(); -} - - -void MainWindow::on_action_ChooseDic_triggered() -{ - QString fileName = - QFileDialog::getOpenFileName(this, _q("Choose a dictionary"), "", "*.dawg"); - if (!fileName.isEmpty()) - { - try - { - Dictionary *dic = new Dictionary(qtl(fileName)); - delete m_dic; - m_dic = dic; - emit dicChanged(fileName, qfw(m_dic->getHeader().getName())); - } - catch (std::exception &e) - { - displayErrorMsg(e.what()); - } - } -} - - void MainWindow::destroyCurrentGame() { if (m_game == NULL) @@ -197,7 +139,16 @@ void MainWindow::destroyCurrentGame() } -void MainWindow::on_action_New_Game_triggered() +void MainWindow::displayErrorMsg(QString iMsg, QString iContext) +{ + if (iContext == "") + iContext = PACKAGE_NAME; + + QMessageBox::warning(this, iContext, iMsg); +} + + +void MainWindow::on_action_GameNew_triggered() { if (m_dic == NULL) { @@ -229,7 +180,47 @@ void MainWindow::on_action_New_Game_triggered() } -void MainWindow::on_action_Preferences_triggered() +void MainWindow::on_action_GameLoad_triggered() +{ + if (m_dic == NULL) + { + displayErrorMsg(_q("You have to select a dictionary first!")); + return; + } + + QString fileName = QFileDialog::getOpenFileName(this, _q("Load a game")); + if (fileName != "") + { + destroyCurrentGame(); + m_game = GameFactory::Instance()->load(qtl(fileName), *m_dic); + if (m_game == NULL) + { + displayErrorMsg(_q("Error while loading the game")); + return; + } + m_ui.groupBoxPlayers->show(); + emit gameChangedNonConst(m_game); + emit gameChanged(m_game); + emit gameUpdated(); + } +} + + +void MainWindow::on_action_GameSaveAs_triggered() +{ + if (m_game == NULL) + return; + + QString fileName = QFileDialog::getSaveFileName(this, _q("Save a game")); + if (fileName != "") + { + ofstream fout(qtl(fileName)); + m_game->save(fout); + } +} + + +void MainWindow::on_action_SettingsPreferences_triggered() { if (m_prefsDialog == NULL) m_prefsDialog = new PrefsDialog(this); @@ -237,11 +228,63 @@ void MainWindow::on_action_Preferences_triggered() } -void MainWindow::displayErrorMsg(QString iMsg, QString iContext) +void MainWindow::on_action_SettingsChooseDic_triggered() { - if (iContext == "") - iContext = PACKAGE_NAME; - - QMessageBox::warning(this, iContext, iMsg); + QString fileName = + QFileDialog::getOpenFileName(this, _q("Choose a dictionary"), "", "*.dawg"); + if (!fileName.isEmpty()) + { + try + { + Dictionary *dic = new Dictionary(qtl(fileName)); + delete m_dic; + m_dic = dic; + emit dicChanged(fileName, qfw(m_dic->getHeader().getName())); + } + catch (std::exception &e) + { + displayErrorMsg(e.what()); + } + } +} + + +void MainWindow::on_action_WindowsBag_triggered() +{ + if (m_bagWindow == NULL) + { + // Create the bag window + BagWidget *bagWidget = new BagWidget(NULL); + bagWidget->setGame(m_game); + m_bagWindow = new AuxWindow(*bagWidget, m_ui.action_WindowsBag); + QObject::connect(this, SIGNAL(gameChanged(const Game*)), + bagWidget, SLOT(setGame(const Game*))); + QObject::connect(this, SIGNAL(gameUpdated()), + bagWidget, SLOT(refresh())); + // XXX + m_bagWindow->move(20, 20); + } + if (m_bagWindow->isVisible()) + m_bagWindow->hide(); + else + m_bagWindow->show(); +} + + +void MainWindow::on_action_HelpAbout_triggered() +{ + QString msg; + msg.sprintf("Eliot %s\n\n", VERSION); + msg += _q( \ + "Copyright (C) 1999-2008 - Antoine Fraboulet & Olivier Teuliere\n\n" \ + "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."); + // QMessageBox::about() doesn't add the nice information icon, so we create + // the box manually (not much work...) + QMessageBox *aboutBox = new QMessageBox(QMessageBox::Information, + _q("About Eliot"), msg, QMessageBox::Ok, this); + aboutBox->exec(); } diff --git a/qt/main_window.h b/qt/main_window.h index 26aa115..001e9e3 100644 --- a/qt/main_window.h +++ b/qt/main_window.h @@ -54,11 +54,13 @@ public slots: void displayErrorMsg(QString iMsg, QString iContext = ""); private slots: - void on_action_About_triggered(); - void on_action_Bag_triggered(); - void on_action_ChooseDic_triggered(); - void on_action_New_Game_triggered(); - void on_action_Preferences_triggered(); + void on_action_GameNew_triggered(); + void on_action_GameLoad_triggered(); + void on_action_GameSaveAs_triggered(); + void on_action_SettingsChooseDic_triggered(); + void on_action_SettingsPreferences_triggered(); + void on_action_WindowsBag_triggered(); + void on_action_HelpAbout_triggered(); private: /// Current dictionary diff --git a/qt/ui/main_window.ui b/qt/ui/main_window.ui index 9fc9a58..2049bec 100644 --- a/qt/ui/main_window.ui +++ b/qt/ui/main_window.ui @@ -70,29 +70,32 @@ _("&Game") - + + - + + + &Settings - - + + _("&Help") - + _("&Windows") - + @@ -111,12 +114,12 @@ false - + _("Choose dictionary...") - + _("&About...") @@ -124,37 +127,67 @@ About Eliot - + _("&Quit") - + - _("&New game...") + _("New...") Ctrl+N - + true _("&Bag") + + Ctrl+B + - + _("Preferences...") + + Ctrl+P + + + + + _("Save") + + + Ctrl+S + + + + + _("Load...") + + + Ctrl+O + + + + + _("Save as...") + + + Ctrl+S + - action_Quit + action_GameQuit triggered() MainWindow close()