diff --git a/game/public_game.cpp b/game/public_game.cpp index e2e985e..520cc1b 100644 --- a/game/public_game.cpp +++ b/game/public_game.cpp @@ -159,6 +159,12 @@ int PublicGame::play(const wstring &iWord, const wstring &iCoord) } +int PublicGame::checkPlayedWord(const wstring &iWord, const wstring &iCoord, Move &oMove) const +{ + return m_game.checkPlayedWord(iCoord, iWord, oMove, true); +} + + int PublicGame::computePoints(const wstring &iWord, const wstring &iCoord) const { Move move; diff --git a/game/public_game.h b/game/public_game.h index bad29b4..491baac 100644 --- a/game/public_game.h +++ b/game/public_game.h @@ -158,6 +158,10 @@ public: */ int play(const wstring &iWord, const wstring &iCoord); + // TODO: doc + int checkPlayedWord(const wstring &iWord, const wstring &iCoord, + Move &oMove) const; + /** * Compute the points for playing the word iWord at coordinates iCoord. * A negative return value indicates an error (same codes as for the diff --git a/qt/main_window.cpp b/qt/main_window.cpp index c320784..468711d 100644 --- a/qt/main_window.cpp +++ b/qt/main_window.cpp @@ -161,8 +161,9 @@ MainWindow::MainWindow(QWidget *iParent) // Rack widget below the board RackWidget *rackWidget = new RackWidget; - QObject::connect(&m_playModel, SIGNAL(moveChanged(const wstring&, const Coord&)), - rackWidget, SLOT(refresh())); + // TODO +// QObject::connect(&m_playModel, SIGNAL(moveChanged(const wstring&, const Coord&)), +// rackWidget, SLOT(refresh())); rackWidget->setFrameStyle(QFrame::WinPanel | QFrame::Raised); QObject::connect(this, SIGNAL(gameChanged(const PublicGame*)), rackWidget, SLOT(setGame(const PublicGame*))); diff --git a/qt/play_model.cpp b/qt/play_model.cpp index 8c2d4bc..143cde4 100644 --- a/qt/play_model.cpp +++ b/qt/play_model.cpp @@ -20,23 +20,22 @@ #include "play_model.h" +#include "encoding.h" +#include "debug.h" + INIT_LOGGER(qt, PlayModel); PlayModel::PlayModel() { - QObject::connect(this, SIGNAL(coordChanged(const Coord&, const Coord&)), - this, SLOT(onMoveChanged())); - QObject::connect(this, SIGNAL(wordChanged(const wstring&, const wstring&)), - this, SLOT(onMoveChanged())); } void PlayModel::clear() { setCoord(Coord()); - setWord(L""); + setMove(Move()); } @@ -52,20 +51,20 @@ void PlayModel::setCoord(const Coord &iCoord) } -void PlayModel::setWord(const wstring &iWord) +void PlayModel::setMove(const Move &iMove) { + ASSERT(iMove.isValid() || iMove.isInvalid() || iMove.isNull(), + "Unexpected move type"); + + LOG_DEBUG("Setting PlayModel move to " << lfw(iMove.toString())); + // Avoid useless work - if (iWord == m_currWord) - return; + // TODO + //if (iMove == m_currMove) + // return; - m_prevWord = m_currWord; - m_currWord = iWord; - emit wordChanged(iWord, m_prevWord); -} - - -void PlayModel::onMoveChanged() -{ - emit moveChanged(m_currWord, m_currCoord); + m_prevMove = m_currMove; + m_currMove = iMove; + emit moveChanged(iMove, m_prevMove); } diff --git a/qt/play_model.h b/qt/play_model.h index e0de5ca..66db919 100644 --- a/qt/play_model.h +++ b/qt/play_model.h @@ -25,6 +25,7 @@ #include #include "coord.h" +#include "move.h" #include "logging.h" using std::wstring; @@ -58,24 +59,19 @@ public: void setCoord(const Coord &iCoord); const Coord & getCoord() const { return m_currCoord; } - void setWord(const wstring &iWord); - const wstring & getWord() const { return m_currWord; } + void setMove(const Move &iMove); + const Move &getMove() const { return m_currMove; } signals: void coordChanged(const Coord &iNewCoord, const Coord &iOldCoord); - void wordChanged(const wstring &iNewWord, const wstring &iOldWord); - /// Emitted whenever coordChanged() or wordChanged() is emitted - void moveChanged(const wstring &iWord, const Coord &iCoord); - -private slots: - void onMoveChanged(); + void moveChanged(const Move &iMove, const Move &iOldMove); private: Coord m_currCoord; Coord m_prevCoord; - wstring m_currWord; - wstring m_prevWord; + Move m_currMove; + Move m_prevMove; }; #endif diff --git a/qt/play_word_mediator.cpp b/qt/play_word_mediator.cpp index 3467dcc..5a12d58 100644 --- a/qt/play_word_mediator.cpp +++ b/qt/play_word_mediator.cpp @@ -54,7 +54,7 @@ PlayWordMediator::PlayWordMediator(QObject *parent, QLineEdit &iEditPlay, // Set all the connections QObject::connect(&m_lineEditPlay, SIGNAL(textChanged(const QString&)), - this, SLOT(updatePointsAndState())); + this, SLOT(onWordChanged(const QString&))); QObject::connect(&m_lineEditPlay, SIGNAL(returnPressed()), this, SLOT(playWord())); QObject::connect(&m_lineEditCoord, SIGNAL(textChanged(const QString&)), @@ -106,7 +106,8 @@ bool PlayWordMediator::GetPlayedWord(QLineEdit &iEditWord, { // Cannot parse the string... *oPlayedWord = wfq(word); - *oProblemCause = _q("Cannot play word: misplaced parentheses"); + if (oProblemCause) + *oProblemCause = _q("Cannot play word: misplaced parentheses"); return false; } @@ -219,6 +220,18 @@ void PlayWordMediator::onCoordChanged(const QString &iText) } +void PlayWordMediator::onWordChanged(const QString &iText) +{ + wstring playedWord; + GetPlayedWord(m_lineEditPlay, m_game->getDic(), &playedWord, NULL); + + Move move; + m_game->checkPlayedWord(playedWord, wfq(m_lineEditCoord.text()), move); + m_playModel.setMove(move); + updatePointsAndState(); +} + + void PlayWordMediator::updateCoord(const Coord &iNewCoord) { // Ignore updates to non-visible controls (which happens when there diff --git a/qt/play_word_mediator.h b/qt/play_word_mediator.h index facb90f..34cd0bf 100644 --- a/qt/play_word_mediator.h +++ b/qt/play_word_mediator.h @@ -91,6 +91,7 @@ private slots: void playWord(); void updatePointsAndState(); void onCoordChanged(const QString &iText); + void onWordChanged(const QString &iText); void updateCoord(const Coord &iNewCoord); private: