2008-01-20 19:40:12 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
2012-01-17 00:27:32 +01:00
|
|
|
* Copyright (C) 2008-2012 Olivier Teulière
|
2008-01-20 19:40:12 +01:00
|
|
|
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-01-27 00:03:32 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-01-26 20:12:47 +01:00
|
|
|
#include <cmath>
|
2008-01-20 19:40:12 +01:00
|
|
|
#include <QtGui/QTreeView>
|
2008-01-22 16:30:20 +01:00
|
|
|
#include <QtGui/QTabWidget>
|
2008-01-20 19:40:12 +01:00
|
|
|
#include <QtGui/QStandardItemModel>
|
2008-01-26 17:58:46 +01:00
|
|
|
#include <QtCore/QSettings>
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
#include "history_widget.h"
|
2011-07-28 22:55:48 +02:00
|
|
|
#include "custom_popup.h"
|
2008-01-26 17:58:46 +01:00
|
|
|
#include "prefs_dialog.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
#include "qtcommon.h"
|
2008-11-30 21:53:44 +01:00
|
|
|
#include "public_game.h"
|
2008-01-22 16:30:20 +01:00
|
|
|
#include "player.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
#include "history.h"
|
|
|
|
#include "turn.h"
|
|
|
|
#include "move.h"
|
2012-01-17 21:24:41 +01:00
|
|
|
#include "game_params.h"
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
2012-02-18 22:26:52 +01:00
|
|
|
INIT_LOGGER(qt, HistoryWidget);
|
|
|
|
|
|
|
|
|
2008-01-22 16:30:20 +01:00
|
|
|
HistoryWidget::HistoryWidget(QWidget *parent)
|
2012-01-26 20:12:47 +01:00
|
|
|
: QTreeView(parent), m_history(NULL), m_forPlayer(false), m_isFreeGame(false)
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
|
|
|
// Create the tree view
|
|
|
|
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
setRootIsDecorated(false);
|
|
|
|
|
2011-07-28 22:55:48 +02:00
|
|
|
// Add a context menu for the results
|
|
|
|
m_customPopup = new CustomPopup(this);
|
|
|
|
QObject::connect(m_customPopup, SIGNAL(popupCreated(QMenu&, const QPoint&)),
|
|
|
|
this, SLOT(populateMenu(QMenu&, const QPoint&)));
|
|
|
|
QObject::connect(m_customPopup, SIGNAL(requestDefinition(QString)),
|
|
|
|
this, SIGNAL(requestDefinition(QString)));
|
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
// Associate the model to the view
|
|
|
|
m_model = new QStandardItemModel(this);
|
|
|
|
setModel(m_model);
|
2012-01-26 20:12:47 +01:00
|
|
|
m_model->setColumnCount(7);
|
2008-01-24 21:18:00 +01:00
|
|
|
m_model->setHeaderData(0, Qt::Horizontal, _q("Turn"), Qt::DisplayRole);
|
|
|
|
m_model->setHeaderData(1, Qt::Horizontal, _q("Rack"), Qt::DisplayRole);
|
|
|
|
m_model->setHeaderData(2, Qt::Horizontal, _q("Word"), Qt::DisplayRole);
|
|
|
|
m_model->setHeaderData(3, Qt::Horizontal, _q("Ref"), Qt::DisplayRole);
|
|
|
|
m_model->setHeaderData(4, Qt::Horizontal, _q("Points"), Qt::DisplayRole);
|
2008-01-20 19:40:12 +01:00
|
|
|
updateModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-22 16:30:20 +01:00
|
|
|
void HistoryWidget::setHistory(const History *iHistory,
|
2008-11-30 21:53:44 +01:00
|
|
|
const PublicGame *iGame,
|
2008-01-22 16:30:20 +01:00
|
|
|
bool iIsForPlayer)
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
|
|
|
m_history = iHistory;
|
2008-01-26 17:58:46 +01:00
|
|
|
m_game = iGame;
|
2008-01-22 16:30:20 +01:00
|
|
|
m_forPlayer = iIsForPlayer;
|
2012-01-26 20:12:47 +01:00
|
|
|
m_isFreeGame = (iGame != 0 && iGame->getMode() == PublicGame::kFREEGAME);
|
2008-01-22 16:30:20 +01:00
|
|
|
updateModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HistoryWidget::refresh()
|
|
|
|
{
|
2008-01-20 19:40:12 +01:00
|
|
|
updateModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-28 22:55:48 +02:00
|
|
|
void HistoryWidget::populateMenu(QMenu &iMenu, const QPoint &iPoint)
|
|
|
|
{
|
|
|
|
const QModelIndex &index = indexAt(iPoint);
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the selected word
|
|
|
|
const QModelIndex &wordIndex = m_model->index(index.row(), 2);
|
|
|
|
QString selectedWord = m_model->data(wordIndex).toString();
|
|
|
|
|
|
|
|
if (selectedWord != "")
|
|
|
|
m_customPopup->addShowDefinitionEntry(iMenu, selectedWord);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
void HistoryWidget::updateModel()
|
|
|
|
{
|
2008-01-24 21:18:00 +01:00
|
|
|
m_model->removeRows(0, m_model->rowCount());
|
2008-01-22 16:30:20 +01:00
|
|
|
if (m_forPlayer)
|
|
|
|
{
|
2012-01-26 20:12:47 +01:00
|
|
|
// Display the cumulative score and percentage
|
2012-01-20 12:57:11 +01:00
|
|
|
m_model->setHeaderData(5, Qt::Horizontal, _q("Total"), Qt::DisplayRole);
|
2012-01-26 20:12:47 +01:00
|
|
|
if (m_isFreeGame)
|
|
|
|
m_model->setHeaderData(6, Qt::Horizontal, "", Qt::DisplayRole);
|
|
|
|
else
|
|
|
|
m_model->setHeaderData(6, Qt::Horizontal, _q("Game %"), Qt::DisplayRole);
|
2008-01-22 16:30:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-26 20:12:47 +01:00
|
|
|
if (m_isFreeGame)
|
|
|
|
m_model->setHeaderData(5, Qt::Horizontal, _q("Player"), Qt::DisplayRole);
|
|
|
|
else
|
|
|
|
m_model->setHeaderData(5, Qt::Horizontal, _q("Total"), Qt::DisplayRole);
|
|
|
|
m_model->setHeaderData(6, Qt::Horizontal, "", Qt::DisplayRole);
|
2008-01-22 16:30:20 +01:00
|
|
|
}
|
2008-01-20 19:40:12 +01:00
|
|
|
|
2008-01-26 17:58:46 +01:00
|
|
|
if (m_history != NULL && m_history->getSize() != 0)
|
2008-01-20 19:40:12 +01:00
|
|
|
{
|
2008-01-26 17:58:46 +01:00
|
|
|
// Should we align the rack with its solution?
|
2012-02-02 09:02:20 +01:00
|
|
|
QSettings qs;
|
2008-01-26 17:58:46 +01:00
|
|
|
bool align = qs.value(PrefsDialog::kINTF_ALIGN_HISTORY).toBool();
|
|
|
|
|
|
|
|
if (!align)
|
|
|
|
m_model->insertRow(0);
|
|
|
|
|
2012-01-20 12:57:11 +01:00
|
|
|
int totalScore = 0;
|
2012-01-26 20:12:47 +01:00
|
|
|
int gameScore = 0;
|
2008-01-20 19:40:12 +01:00
|
|
|
for (unsigned int i = 0; i < m_history->getSize(); ++i)
|
|
|
|
{
|
|
|
|
int rowNum = m_model->rowCount();
|
|
|
|
m_model->insertRow(rowNum);
|
2008-01-26 17:58:46 +01:00
|
|
|
int prevRowNum;
|
|
|
|
if (align)
|
|
|
|
prevRowNum = rowNum;
|
|
|
|
else
|
|
|
|
prevRowNum = rowNum - 1;
|
2008-01-20 19:40:12 +01:00
|
|
|
|
|
|
|
QColor color = Qt::black;
|
|
|
|
|
|
|
|
const Turn& t = m_history->getTurn(i);
|
|
|
|
const Move& m = t.getMove();
|
2008-01-26 17:58:46 +01:00
|
|
|
|
|
|
|
// Set data common to all moves
|
|
|
|
m_model->setData(m_model->index(prevRowNum, 0), i + 1);
|
|
|
|
m_model->setData(m_model->index(prevRowNum, 1),
|
2008-01-20 19:40:12 +01:00
|
|
|
qfw(t.getPlayedRack().toString()));
|
|
|
|
m_model->setData(m_model->index(rowNum, 4), m.getScore());
|
2012-01-20 12:57:11 +01:00
|
|
|
totalScore += m.getScore();
|
2012-01-26 20:12:47 +01:00
|
|
|
if (m_game != NULL)
|
|
|
|
{
|
|
|
|
gameScore += m_game->getHistory().getTurn(i).getMove().getScore();
|
|
|
|
}
|
|
|
|
if (m_forPlayer)
|
|
|
|
{
|
|
|
|
m_model->setData(m_model->index(rowNum, 5), totalScore);
|
2012-01-26 22:04:00 +01:00
|
|
|
if (!m_isFreeGame && gameScore != 0)
|
2012-01-26 20:12:47 +01:00
|
|
|
{
|
|
|
|
int percentage = totalScore * 100 / gameScore;
|
|
|
|
m_model->setData(m_model->index(rowNum, 6),
|
|
|
|
QString("%1%").arg(percentage));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_isFreeGame)
|
2008-01-26 17:58:46 +01:00
|
|
|
{
|
|
|
|
const wstring &name = m_game->getPlayer(t.getPlayer()).getName();
|
|
|
|
m_model->setData(m_model->index(rowNum, 5), qfw(name));
|
|
|
|
}
|
2012-01-20 12:57:11 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_model->setData(m_model->index(rowNum, 5), totalScore);
|
|
|
|
}
|
2008-01-26 17:58:46 +01:00
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
// Set the rest
|
|
|
|
if (m.getType() == Move::VALID_ROUND)
|
|
|
|
{
|
|
|
|
const Round &r = m.getRound();
|
|
|
|
wstring coord = r.getCoord().toString();
|
|
|
|
m_model->setData(m_model->index(rowNum, 2), qfw(r.getWord()));
|
|
|
|
m_model->setData(m_model->index(rowNum, 3), qfw(coord));
|
|
|
|
color = Qt::black;
|
|
|
|
}
|
|
|
|
else if (m.getType() == Move::INVALID_WORD)
|
|
|
|
{
|
|
|
|
m_model->setData(m_model->index(rowNum, 2),
|
|
|
|
"<" + qfw(m.getBadWord()) + ">");
|
|
|
|
m_model->setData(m_model->index(rowNum, 3), qfw(m.getBadCoord()));
|
|
|
|
color = Qt::red;
|
|
|
|
}
|
2012-01-17 00:27:32 +01:00
|
|
|
else if (m.getType() == Move::NO_MOVE)
|
|
|
|
{
|
|
|
|
m_model->setData(m_model->index(rowNum, 2), _q("(NO MOVE)"));
|
|
|
|
color = Qt::blue;
|
|
|
|
}
|
2008-01-20 19:40:12 +01:00
|
|
|
else if (m.getType() == Move::PASS)
|
|
|
|
{
|
|
|
|
m_model->setData(m_model->index(rowNum, 2), _q("(PASS)"));
|
|
|
|
color = Qt::blue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_model->setData(m_model->index(rowNum, 2),
|
|
|
|
"[-" + qfw(m.getChangedLetters()) + "]");
|
|
|
|
color = Qt::blue;
|
|
|
|
}
|
2008-01-26 17:58:46 +01:00
|
|
|
|
2008-01-20 19:40:12 +01:00
|
|
|
// Set the color of the text
|
|
|
|
for (int col = 0; col < 6; ++col)
|
|
|
|
{
|
2008-01-27 21:17:10 +01:00
|
|
|
int row = rowNum;
|
|
|
|
if (!align && col < 2)
|
|
|
|
row = prevRowNum;
|
|
|
|
m_model->setData(m_model->index(row, col),
|
2008-01-20 19:40:12 +01:00
|
|
|
QBrush(color), Qt::ForegroundRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeColumnToContents(0);
|
|
|
|
resizeColumnToContents(3);
|
|
|
|
resizeColumnToContents(4);
|
|
|
|
resizeColumnToContents(5);
|
|
|
|
}
|
|
|
|
|
2008-01-22 16:30:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
HistoryTabWidget::HistoryTabWidget(QWidget *parent)
|
|
|
|
: QTabWidget(parent), m_game(NULL)
|
|
|
|
{
|
|
|
|
m_gameHistoryWidget = new HistoryWidget(NULL);
|
|
|
|
insertTab(0, m_gameHistoryWidget, _q("&Game"));
|
|
|
|
//setMinimalSize(300, 100);
|
|
|
|
|
|
|
|
setGame(m_game);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-30 21:53:44 +01:00
|
|
|
void HistoryTabWidget::setGame(const PublicGame *iGame)
|
2008-01-22 16:30:20 +01:00
|
|
|
{
|
|
|
|
m_game = iGame;
|
|
|
|
|
2008-01-26 17:58:46 +01:00
|
|
|
// Keep only the Game tab, because it is nicer to have something, even
|
|
|
|
// if it is empty
|
|
|
|
int nbTabs = count();
|
|
|
|
for (int i = nbTabs - 1; i > 0; --i)
|
2008-01-22 16:30:20 +01:00
|
|
|
{
|
2008-01-26 17:58:46 +01:00
|
|
|
setCurrentIndex(i);
|
|
|
|
// Cut all the connections with the page (needed because removeTab()
|
|
|
|
// doesn't really destroy the widget)
|
|
|
|
disconnect(currentWidget());
|
|
|
|
removeTab(i);
|
|
|
|
}
|
2008-01-22 16:30:20 +01:00
|
|
|
|
2008-01-26 17:58:46 +01:00
|
|
|
if (m_game == NULL)
|
|
|
|
{
|
2008-01-22 16:30:20 +01:00
|
|
|
// Tell the remaining tab that there is no more history to display
|
2012-01-17 21:24:41 +01:00
|
|
|
m_gameHistoryWidget->setHistory(NULL, NULL, false);
|
2008-01-22 16:30:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Refresh the Game tab
|
2012-01-26 20:12:47 +01:00
|
|
|
m_gameHistoryWidget->setHistory(&m_game->getHistory(), m_game, false);
|
2008-01-22 16:30:20 +01:00
|
|
|
QObject::connect(this, SIGNAL(refreshSignal()),
|
|
|
|
m_gameHistoryWidget, SLOT(refresh()));
|
2011-07-28 22:55:48 +02:00
|
|
|
QObject::connect(m_gameHistoryWidget, SIGNAL(requestDefinition(QString)),
|
|
|
|
this, SIGNAL(requestDefinition(QString)));
|
2008-01-22 16:30:20 +01:00
|
|
|
|
2008-01-26 17:58:46 +01:00
|
|
|
// In training mode, the players history is completely useless
|
2008-11-30 21:53:44 +01:00
|
|
|
if (m_game->getMode() == PublicGame::kTRAINING)
|
2008-01-26 17:58:46 +01:00
|
|
|
return;
|
|
|
|
|
2008-01-22 16:30:20 +01:00
|
|
|
// Add one history tab per player
|
2008-11-30 21:53:44 +01:00
|
|
|
for (unsigned int i = 0; i < m_game->getNbPlayers(); ++i)
|
2008-01-22 16:30:20 +01:00
|
|
|
{
|
|
|
|
const Player &player = m_game->getPlayer(i);
|
|
|
|
HistoryWidget *h = new HistoryWidget(NULL);
|
2008-01-26 17:58:46 +01:00
|
|
|
h->setHistory(&player.getHistory(), m_game, true);
|
2008-01-22 16:30:20 +01:00
|
|
|
QObject::connect(this, SIGNAL(refreshSignal()), h, SLOT(refresh()));
|
2011-07-28 22:55:48 +02:00
|
|
|
QObject::connect(h, SIGNAL(requestDefinition(QString)),
|
|
|
|
this, SIGNAL(requestDefinition(QString)));
|
2008-01-22 16:30:20 +01:00
|
|
|
addTab(h, qfw(player.getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HistoryTabWidget::refresh()
|
|
|
|
{
|
|
|
|
emit refreshSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QSize HistoryTabWidget::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(500, 300);
|
|
|
|
}
|
|
|
|
|