2012-04-30 08:32:23 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* Eliot
|
|
|
|
* Copyright (C) 2012 Olivier Teulière
|
|
|
|
* 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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
#include <QtGui/QTableView>
|
2012-04-30 08:32:23 +02:00
|
|
|
#include <QtGui/QHeaderView>
|
2012-06-11 20:55:57 +02:00
|
|
|
#include <QtGui/QStandardItemModel>
|
2012-04-30 08:32:23 +02:00
|
|
|
#include <QtGui/QVBoxLayout>
|
|
|
|
#include <QtGui/QLabel>
|
2012-06-11 20:55:57 +02:00
|
|
|
#include <QtCore/QLocale>
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
#include "stats_widget.h"
|
|
|
|
#include "qtcommon.h"
|
|
|
|
#include "public_game.h"
|
|
|
|
#include "player.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "turn.h"
|
2012-06-18 23:00:41 +02:00
|
|
|
#include "game_params.h"
|
|
|
|
#include "settings.h"
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
|
|
INIT_LOGGER(qt, StatsWidget);
|
|
|
|
|
|
|
|
|
2012-06-12 20:52:36 +02:00
|
|
|
const QColor StatsWidget::WarningBrush(220, 220, 0);
|
2012-06-11 20:55:57 +02:00
|
|
|
const QColor StatsWidget::PenaltyBrush(220, 120, 0);
|
|
|
|
const QColor StatsWidget::SoloBrush(0, 200, 0);
|
|
|
|
const QColor StatsWidget::PassBrush(210, 210, 210);
|
|
|
|
const QColor StatsWidget::InvalidBrush(255, 0, 0);
|
|
|
|
|
|
|
|
|
2012-06-18 23:00:41 +02:00
|
|
|
const bool HORIZONTAL = false;
|
2012-06-11 20:55:57 +02:00
|
|
|
|
|
|
|
|
2012-04-30 08:32:23 +02:00
|
|
|
StatsWidget::StatsWidget(QWidget *parent, const PublicGame *iGame)
|
|
|
|
: QWidget(parent), m_game(iGame)
|
|
|
|
{
|
|
|
|
// Layout
|
|
|
|
setLayout(new QVBoxLayout);
|
|
|
|
|
|
|
|
// Create the table
|
2012-06-11 20:55:57 +02:00
|
|
|
m_table = new QTableView(this);
|
2012-04-30 08:32:23 +02:00
|
|
|
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
2012-06-18 23:00:41 +02:00
|
|
|
m_table->horizontalHeader()->setHighlightSections(false);
|
|
|
|
m_table->verticalHeader()->setHighlightSections(false);
|
2012-04-30 08:32:23 +02:00
|
|
|
m_table->horizontalHeader()->setMinimumSectionSize(15);
|
2012-06-11 20:55:57 +02:00
|
|
|
m_table->verticalHeader()->setMinimumSectionSize(15);
|
|
|
|
//m_table->setSortingEnabled(true);
|
2012-04-30 08:32:23 +02:00
|
|
|
layout()->addWidget(m_table);
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
m_model = new QStandardItemModel();
|
|
|
|
m_table->setModel(m_model);
|
|
|
|
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StatsWidget::setGame(const PublicGame *iGame)
|
|
|
|
{
|
|
|
|
m_game = iGame;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StatsWidget::refresh()
|
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
m_model->clear();
|
|
|
|
|
2012-06-18 21:40:56 +02:00
|
|
|
unsigned histSize = m_game == NULL ? 0 : m_game->getHistory().getSize();
|
|
|
|
unsigned nbPlayers = m_game == NULL ? 0 : m_game->getNbPlayers();
|
2012-04-30 08:32:23 +02:00
|
|
|
|
2012-06-18 23:00:41 +02:00
|
|
|
setModelSize(nbPlayers + 1, histSize + 8);
|
|
|
|
|
|
|
|
// Some fields are displayed only in some cases
|
|
|
|
const bool isArbit = m_game != NULL &&
|
|
|
|
m_game->getParams().getMode() == GameParams::kARBITRATION;
|
|
|
|
const bool canHaveSolos = m_game != NULL &&
|
|
|
|
m_game->getParams().getMode() == GameParams::kDUPLICATE &&
|
|
|
|
Settings::Instance().getInt("duplicate.solo-players") <= (int)m_game->getNbPlayers();
|
2012-04-30 08:32:23 +02:00
|
|
|
|
2012-06-18 23:00:41 +02:00
|
|
|
// Define columns (or rows, depending on the orientation)
|
2012-04-30 08:32:23 +02:00
|
|
|
int col = 0;
|
2012-06-18 23:00:41 +02:00
|
|
|
setSectionHidden(col, !isArbit);
|
|
|
|
setModelHeader(col++, _q("Table"), false);
|
|
|
|
|
2012-06-18 21:40:56 +02:00
|
|
|
for (unsigned i = 1; i <= histSize; ++i)
|
2012-06-18 23:00:41 +02:00
|
|
|
setModelHeader(col++, QString("#%1").arg(i), false);
|
|
|
|
|
|
|
|
setSectionHidden(col, !isArbit && !canHaveSolos);
|
|
|
|
setModelHeader(col++, _q("Sub-total"), false);
|
|
|
|
setSectionHidden(col, !isArbit);
|
|
|
|
setModelHeader(col++, _q("Warnings"), false);
|
|
|
|
setSectionHidden(col, !isArbit);
|
|
|
|
setModelHeader(col++, _q("Penalties"), false);
|
|
|
|
setSectionHidden(col, !isArbit && !canHaveSolos);
|
|
|
|
setModelHeader(col++, _q("Solo points"), false);
|
|
|
|
|
|
|
|
setModelHeader(col++, _q("Total"), false);
|
|
|
|
setModelHeader(col++, _q("Diff"), false);
|
|
|
|
setModelHeader(col++, _q("Game %"), false);
|
|
|
|
|
|
|
|
// Define the header for the Game pseudo-player
|
|
|
|
setModelHeader(0, _q("Game"), true);
|
2012-06-11 20:55:57 +02:00
|
|
|
|
2012-06-18 21:40:56 +02:00
|
|
|
if (m_game == NULL)
|
|
|
|
return;
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
QLocale locale;
|
2012-06-18 21:40:56 +02:00
|
|
|
const History &gHistory = m_game->getHistory();
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
// Game data
|
|
|
|
int gameTotal = 0;
|
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
const int row = 0;
|
2012-04-30 08:32:23 +02:00
|
|
|
int col = 0;
|
2012-05-30 08:14:18 +02:00
|
|
|
// Skip the table number
|
|
|
|
++col;
|
2012-04-30 08:32:23 +02:00
|
|
|
int score = 0;
|
|
|
|
for (unsigned j = 0; j < gHistory.getSize(); ++j)
|
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelTurnData(getIndex(row, col++), gHistory.getTurn(j), gHistory.getTurn(j));
|
2012-04-30 08:32:23 +02:00
|
|
|
score += gHistory.getTurn(j).getMove().getScore();
|
|
|
|
}
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelText(getIndex(row, col++), score, true);
|
2012-04-30 08:32:23 +02:00
|
|
|
// Skip the events columns
|
|
|
|
col += 3;
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelText(getIndex(row, col++), score, true);
|
2012-04-30 08:32:23 +02:00
|
|
|
// Skip the diff column
|
|
|
|
col += 1;
|
2012-06-12 20:52:36 +02:00
|
|
|
setModelText(getIndex(row, col++), locale.toString((double)100, 'f', 1) + "%", true);
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
gameTotal = score;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Players data
|
|
|
|
for (unsigned i = 0; i < nbPlayers; ++i)
|
|
|
|
{
|
|
|
|
const Player &player = m_game->getPlayer(i);
|
|
|
|
int col = 0;
|
2012-06-18 23:00:41 +02:00
|
|
|
setModelHeader(i + 1, qfw(player.getName()), true);
|
2012-04-30 08:32:23 +02:00
|
|
|
|
2012-05-30 08:14:18 +02:00
|
|
|
// Table number
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelText(getIndex(i + 1, col++), player.getTableNb());
|
2012-05-30 08:14:18 +02:00
|
|
|
|
2012-04-30 08:32:23 +02:00
|
|
|
int score = 0;
|
|
|
|
// Normal turns
|
|
|
|
for (unsigned j = 0; j < gHistory.getSize(); ++j)
|
|
|
|
{
|
|
|
|
const History &pHistory = player.getHistory();
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelTurnData(getIndex(i + 1, col++),
|
|
|
|
pHistory.getTurn(j), gHistory.getTurn(j));
|
2012-04-30 08:32:23 +02:00
|
|
|
score += pHistory.getTurn(j).getMove().getScore();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sub-total
|
2012-06-12 20:52:36 +02:00
|
|
|
setModelText(getIndex(i + 1, col++), score, score >= gameTotal);
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
// Events columns
|
|
|
|
for (int j = 0; j <= 2; ++j)
|
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelEventData(getIndex(i + 1, col++), j, player);
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Final score
|
|
|
|
score += player.getSoloPoints() + player.getPenaltyPoints();
|
2012-06-12 20:52:36 +02:00
|
|
|
setModelText(getIndex(i + 1, col++), score, score >= gameTotal);
|
2012-04-30 08:32:23 +02:00
|
|
|
|
|
|
|
// Diff with game total
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelText(getIndex(i + 1, col++), score - gameTotal);
|
2012-04-30 08:32:23 +02:00
|
|
|
// Global score percentage
|
2012-06-11 20:55:57 +02:00
|
|
|
setModelText(getIndex(i + 1, col++),
|
2012-06-12 20:52:36 +02:00
|
|
|
locale.toString(100. * score / gameTotal, 'f', 1) + "%",
|
|
|
|
score >= gameTotal);
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resize
|
2012-06-18 23:00:41 +02:00
|
|
|
m_table->resizeRowsToContents();
|
|
|
|
m_table->resizeColumnsToContents();
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
QModelIndex StatsWidget::getIndex(int row, int col) const
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
if (HORIZONTAL)
|
|
|
|
return m_model->index(row, col);
|
|
|
|
else
|
|
|
|
return m_model->index(col, row);
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-18 23:00:41 +02:00
|
|
|
void StatsWidget::setSectionHidden(int index, bool iHide)
|
|
|
|
{
|
|
|
|
if (HORIZONTAL)
|
|
|
|
m_table->setColumnHidden(index, iHide);
|
|
|
|
else
|
|
|
|
m_table->setRowHidden(index, iHide);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
void StatsWidget::setModelSize(int rowCount, int colCount)
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
m_model->setRowCount(HORIZONTAL ? rowCount : colCount);
|
|
|
|
m_model->setColumnCount(HORIZONTAL ? colCount : rowCount);
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-18 23:00:41 +02:00
|
|
|
void StatsWidget::setModelHeader(int index, const QString &iText, bool iPlayerNames)
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-18 23:00:41 +02:00
|
|
|
Qt::Orientation orientation;
|
|
|
|
if ((HORIZONTAL && iPlayerNames) || (!HORIZONTAL && !iPlayerNames))
|
|
|
|
orientation = Qt::Vertical;
|
|
|
|
else
|
|
|
|
orientation = Qt::Horizontal;
|
|
|
|
m_model->setHeaderData(index, orientation, iText);
|
|
|
|
m_model->setHeaderData(index, orientation, Qt::AlignCenter, Qt::TextAlignmentRole);
|
2012-06-11 20:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StatsWidget::setModelText(const QModelIndex &iIndex,
|
|
|
|
const QVariant &iData, bool useBoldFont)
|
|
|
|
{
|
|
|
|
m_model->setData(iIndex, iData);
|
|
|
|
m_model->setData(iIndex, Qt::AlignCenter, Qt::TextAlignmentRole);
|
|
|
|
if (useBoldFont)
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
QFont boldFont = font();
|
|
|
|
boldFont.setBold(true);
|
|
|
|
m_model->setData(iIndex, boldFont, Qt::FontRole);
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
2012-06-11 20:55:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StatsWidget::setModelTurnData(const QModelIndex &iIndex,
|
|
|
|
const Turn &iTurn, const Turn &iGameTurn)
|
|
|
|
{
|
|
|
|
// Set the text (score for the turn)
|
|
|
|
if (!iTurn.getMove().isNull())
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-11 20:55:57 +02:00
|
|
|
int score = iTurn.getMove().getScore();
|
|
|
|
setModelText(iIndex, QVariant(score),
|
|
|
|
score >= iGameTurn.getMove().getScore());
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
// Set the background c constolor
|
|
|
|
if (iTurn.getPenaltyPoints() != 0)
|
|
|
|
m_model->setData(iIndex, PenaltyBrush, Qt::BackgroundRole);
|
|
|
|
else if (iTurn.getWarningsNb() != 0)
|
|
|
|
m_model->setData(iIndex, WarningBrush, Qt::BackgroundRole);
|
|
|
|
else if (iTurn.getSoloPoints() != 0)
|
|
|
|
m_model->setData(iIndex, SoloBrush, Qt::BackgroundRole);
|
|
|
|
else if (iTurn.getMove().isNull())
|
|
|
|
m_model->setData(iIndex, PassBrush, Qt::BackgroundRole);
|
|
|
|
|
|
|
|
// Set the foreground color
|
|
|
|
if (iTurn.getMove().isInvalid())
|
|
|
|
m_model->setData(iIndex, InvalidBrush, Qt::ForegroundRole);
|
|
|
|
|
|
|
|
// Set the tooltip
|
|
|
|
const QString &tooltip = getTooltip(iTurn, iGameTurn);
|
|
|
|
m_model->setData(iIndex, tooltip, Qt::ToolTipRole);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void StatsWidget::setModelEventData(const QModelIndex &iIndex,
|
|
|
|
int iEvent, const Player &iPlayer)
|
|
|
|
{
|
|
|
|
QVariant text;
|
|
|
|
if (iEvent == 0 && iPlayer.getWarningsNb() != 0)
|
|
|
|
text = iPlayer.getWarningsNb();
|
|
|
|
else if (iEvent == 1 && iPlayer.getPenaltyPoints() != 0)
|
|
|
|
text = iPlayer.getPenaltyPoints();
|
|
|
|
else if (iEvent == 2 && iPlayer.getSoloPoints() != 0)
|
|
|
|
text = iPlayer.getSoloPoints();
|
|
|
|
setModelText(iIndex, text);
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 20:55:57 +02:00
|
|
|
QString StatsWidget::getTooltip(const Turn &iTurn, const Turn &iGameTurn) const
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
|
|
|
QString tooltip = _q("Rack: %1").arg(qfw(iTurn.getPlayedRack().toString()));
|
2012-06-12 20:52:36 +02:00
|
|
|
const Move &move = iTurn.getMove();
|
|
|
|
if (move.isValid())
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-12 20:52:36 +02:00
|
|
|
tooltip += "\n" + _q("Word: %1").arg(qfw(move.getRound().getWord()));
|
|
|
|
tooltip += "\n" + _q("Ref: %1").arg(qfw(move.getRound().getCoord().toString()));
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
2012-06-12 20:52:36 +02:00
|
|
|
else if (move.isInvalid())
|
2012-04-30 08:32:23 +02:00
|
|
|
{
|
2012-06-12 20:52:36 +02:00
|
|
|
tooltip += "\n" + _q("Invalid move (%1 - %2)")
|
|
|
|
.arg(qfw(move.getBadWord()))
|
|
|
|
.arg(qfw(move.getBadCoord()));
|
|
|
|
}
|
|
|
|
else if (move.isChangeLetters())
|
|
|
|
{
|
|
|
|
tooltip += "\n" + _q("Changing letters: %1").arg(qfw(move.getChangedLetters()));
|
2012-04-30 08:32:23 +02:00
|
|
|
}
|
2012-06-12 20:52:36 +02:00
|
|
|
else if (move.isPass())
|
|
|
|
{
|
|
|
|
tooltip += "\n" + _q("Passing turn");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tooltip += "\n" + _q("No move");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Points
|
|
|
|
int score = move.getScore();
|
|
|
|
int gameScore = iGameTurn.getMove().getScore();
|
2012-06-18 23:00:41 +02:00
|
|
|
if (move.isNull())
|
|
|
|
tooltip += _q("Points: %1").arg(score);
|
2012-06-12 20:52:36 +02:00
|
|
|
else
|
2012-06-18 23:00:41 +02:00
|
|
|
{
|
|
|
|
QString scoreString = _q("Points: %1 (%2)").arg(score);
|
|
|
|
if (score == gameScore)
|
|
|
|
tooltip += "\n" + scoreString.arg(_q("max"));
|
|
|
|
else
|
|
|
|
tooltip += "\n" + scoreString.arg(score - gameScore);
|
|
|
|
}
|
2012-06-12 20:52:36 +02:00
|
|
|
|
2012-04-30 08:32:23 +02:00
|
|
|
if (iTurn.getWarningsNb())
|
|
|
|
{
|
|
|
|
tooltip += "\n" + _q("Warnings: %1").arg(iTurn.getWarningsNb());
|
|
|
|
}
|
|
|
|
if (iTurn.getPenaltyPoints())
|
|
|
|
{
|
|
|
|
tooltip += "\n" + _q("Penalties: %1").arg(iTurn.getPenaltyPoints());
|
|
|
|
}
|
|
|
|
if (iTurn.getSoloPoints())
|
|
|
|
{
|
|
|
|
tooltip += "\n" + _q("Solo: %1").arg(iTurn.getSoloPoints());
|
|
|
|
}
|
|
|
|
return tooltip;
|
|
|
|
}
|
|
|
|
|
|
|
|
|