diff --git a/qt/Makefile.am b/qt/Makefile.am index 015cea8..6befe35 100644 --- a/qt/Makefile.am +++ b/qt/Makefile.am @@ -76,6 +76,7 @@ eliot_SOURCES = \ dic_tools_widget.cpp dic_tools_widget.h \ players_table_helper.cpp players_table_helper.h \ fav_players.cpp fav_players.h \ + hints_dialog.cpp hints_dialog.h \ new_game.cpp new_game.h \ score_widget.cpp score_widget.h \ dic_wizard.cpp dic_wizard.h \ @@ -117,6 +118,7 @@ nodist_eliot_SOURCES = \ custom_popup.moc.cpp \ players_table_helper.moc.cpp \ fav_players.moc.cpp \ + hints_dialog.moc.cpp \ new_game.moc.cpp \ dic_tools_widget.moc.cpp \ arbitration_widget.moc.cpp \ diff --git a/qt/hints_dialog.cpp b/qt/hints_dialog.cpp new file mode 100644 index 0000000..d50f8b0 --- /dev/null +++ b/qt/hints_dialog.cpp @@ -0,0 +1,144 @@ +/***************************************************************************** + * Eliot + * Copyright (C) 2013 Olivier Teulière + * Authors: Olivier Teulière + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + *****************************************************************************/ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "hints_dialog.h" +#include "qtcommon.h" + +#include "hints.h" +#include "debug.h" + + +using namespace std; + +INIT_LOGGER(qt, HintsDialog); + + +struct CostComparator : public binary_function +{ + bool operator()(const AbstractHint *iHint1, const AbstractHint *iHint2) const + { + return iHint1->getCost() < iHint2->getCost(); + } +}; + + +HintWidget::HintWidget(const AbstractHint &iHint, QWidget *parent) + : QWidget(parent), m_hint(iHint) +{ + QHBoxLayout *layout = new QHBoxLayout(this); + + QLabel *label = new QLabel(qfl(m_hint.getName())); + label->setToolTip(qfl(m_hint.getDescription())); + layout->addWidget(label); + + layout->addStretch(); + + QPushButton *button = new QPushButton(_q("Show")); + button->setToolTip(qfl(m_hint.getDescription())); + QObject::connect(button, SIGNAL(clicked()), + this, SLOT(buttonClicked())); + layout->addWidget(button); + + setContentsMargins(0, 0, 0, 0); +} + + +void HintWidget::buttonClicked() +{ + emit hintRequested(m_hint); +} + + + +HintsDialog::HintsDialog(QWidget *parent) + : QDialog(parent), m_move(NULL) +{ + initializeHints(); + + QVBoxLayout *vLayout = new QVBoxLayout(this); + Q_FOREACH(const AbstractHint *hint, m_allHints) + { + HintWidget *hintWidget = new HintWidget(*hint); + QObject::connect(hintWidget, SIGNAL(hintRequested(const AbstractHint&)), + this, SLOT(showHint(const AbstractHint&))); + vLayout->addWidget(hintWidget); + } + + QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel); + vLayout->addWidget(buttonBox); + connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject())); + + setWindowTitle(_q("Hints")); +} + + +HintsDialog::~HintsDialog() +{ + Q_FOREACH(const AbstractHint *hint, m_allHints) + { + delete hint; + } +} + + +void HintsDialog::initializeHints() +{ + m_allHints.push_back(new ScoreHint); + m_allHints.push_back(new OrientationHint); + m_allHints.push_back(new PositionHint); + m_allHints.push_back(new LengthHint); + m_allHints.push_back(new BoardLettersHint); + m_allHints.push_back(new WordLettersHint); + m_allHints.push_back(new FirstLetterHint); + + // Sort them by increasing cost + CostComparator costComp; + std::sort(m_allHints.begin(), m_allHints.end(), costComp); +} + + +void HintsDialog::setMove(const Move &iMove) +{ + m_move = &iMove; +} + + +void HintsDialog::showHint(const AbstractHint &iHint) +{ + ASSERT(m_move != 0, "No move defined"); + // Show the hint in a message box + const string & result = iHint.giveHint(*m_move); + QMessageBox::information(this, _q("Hint"), qfl(result)); + + // Notify of the hint usage (most likely to "pay" the cost of the hint) + emit hintUsed(iHint); +} + + diff --git a/qt/hints_dialog.h b/qt/hints_dialog.h new file mode 100644 index 0000000..dbf42bf --- /dev/null +++ b/qt/hints_dialog.h @@ -0,0 +1,94 @@ +/***************************************************************************** + * Eliot + * Copyright (C) 2013 Olivier Teulière + * Authors: Olivier Teulière + * + * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + *****************************************************************************/ + +#ifndef HINTS_DIALOG_H_ +#define HINTS_DIALOG_H_ + +#include + +#include + +#include "logging.h" + + +using std::vector; + +class AbstractHint; +class Move; + + +/** + * Hint widget, used in the HintsDialog class + */ +class HintWidget: public QWidget +{ + Q_OBJECT; + DEFINE_LOGGER(); + +public: + explicit HintWidget(const AbstractHint &iHint, QWidget *parent = 0); + + const AbstractHint & getHint() const { return m_hint; } + +signals: + void hintRequested(const AbstractHint &iHint); + +private slots: + void buttonClicked(); + +private: + const AbstractHint &m_hint; + +}; + + +/** + * Hints dialog + */ +class HintsDialog: public QDialog +{ + Q_OBJECT; + DEFINE_LOGGER(); + +public: + explicit HintsDialog(QWidget *parent = 0); + ~HintsDialog(); + +public slots: + void setMove(const Move &iMove); + +signals: + void hintUsed(const AbstractHint &iHint); + void notifyProblem(QString msg); + +private slots: + void showHint(const AbstractHint &iHint); + +private: + const Move *m_move; + vector m_allHints; + + /// Initialize the m_allHints vector + void initializeHints(); + +}; + +#endif +