mirror of
git://git.savannah.nongnu.org/eliot.git
synced 2025-01-30 20:34:27 +01:00
HistoryWidget: simplified the handling of columns
This commit is contained in:
parent
7e37cde0c0
commit
cc0fe39c76
2 changed files with 79 additions and 59 deletions
|
@ -47,6 +47,15 @@ INIT_LOGGER(qt, HistoryWidget);
|
||||||
HistoryWidget::HistoryWidget(QWidget *parent)
|
HistoryWidget::HistoryWidget(QWidget *parent)
|
||||||
: QTreeView(parent), m_history(NULL), m_forPlayer(false), m_isFreeGame(false)
|
: QTreeView(parent), m_history(NULL), m_forPlayer(false), m_isFreeGame(false)
|
||||||
{
|
{
|
||||||
|
m_colTurn = 0;
|
||||||
|
m_colRack = 1;
|
||||||
|
m_colWord = 2;
|
||||||
|
m_colRef = 3;
|
||||||
|
m_colPoints = 4;
|
||||||
|
m_colTotal = -1;
|
||||||
|
m_colPercent = -1;
|
||||||
|
m_colPlayer = -1;
|
||||||
|
|
||||||
// Create the tree view
|
// Create the tree view
|
||||||
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||||
setRootIsDecorated(false);
|
setRootIsDecorated(false);
|
||||||
|
@ -61,13 +70,12 @@ HistoryWidget::HistoryWidget(QWidget *parent)
|
||||||
// Associate the model to the view
|
// Associate the model to the view
|
||||||
m_model = new QStandardItemModel(this);
|
m_model = new QStandardItemModel(this);
|
||||||
setModel(m_model);
|
setModel(m_model);
|
||||||
m_model->setColumnCount(7);
|
m_model->setColumnCount(5);
|
||||||
m_model->setHeaderData(0, Qt::Horizontal, _q("Turn"), Qt::DisplayRole);
|
m_model->setHeaderData(m_colTurn, Qt::Horizontal, _q("Turn"));
|
||||||
m_model->setHeaderData(1, Qt::Horizontal, _q("Rack"), Qt::DisplayRole);
|
m_model->setHeaderData(m_colRack, Qt::Horizontal, _q("Rack"));
|
||||||
m_model->setHeaderData(2, Qt::Horizontal, _q("Word"), Qt::DisplayRole);
|
m_model->setHeaderData(m_colWord, Qt::Horizontal, _q("Word"));
|
||||||
m_model->setHeaderData(3, Qt::Horizontal, _q("Ref"), Qt::DisplayRole);
|
m_model->setHeaderData(m_colRef, Qt::Horizontal, _q("Ref"));
|
||||||
m_model->setHeaderData(4, Qt::Horizontal, _q("Points"), Qt::DisplayRole);
|
m_model->setHeaderData(m_colPoints, Qt::Horizontal, _q("Points"));
|
||||||
updateModel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -79,7 +87,27 @@ void HistoryWidget::setHistory(const History *iHistory,
|
||||||
m_game = iGame;
|
m_game = iGame;
|
||||||
m_forPlayer = iIsForPlayer;
|
m_forPlayer = iIsForPlayer;
|
||||||
m_isFreeGame = (iGame != 0 && iGame->getMode() == PublicGame::kFREEGAME);
|
m_isFreeGame = (iGame != 0 && iGame->getMode() == PublicGame::kFREEGAME);
|
||||||
updateModel();
|
|
||||||
|
int currColumn = m_colPoints + 1;
|
||||||
|
if (m_forPlayer)
|
||||||
|
{
|
||||||
|
m_colTotal = currColumn++;
|
||||||
|
m_colPlayer = -1;
|
||||||
|
m_colPercent = m_isFreeGame ? -1 : currColumn++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_colTotal = m_isFreeGame ? -1 : currColumn++;
|
||||||
|
m_colPlayer = m_isFreeGame ? currColumn++ : -1;
|
||||||
|
m_colPercent = -1;
|
||||||
|
}
|
||||||
|
m_model->setColumnCount(currColumn);
|
||||||
|
|
||||||
|
m_model->setHeaderData(m_colTotal, Qt::Horizontal, _q("Total"));
|
||||||
|
m_model->setHeaderData(m_colPercent, Qt::Horizontal, _q("Game %"));
|
||||||
|
m_model->setHeaderData(m_colPlayer, Qt::Horizontal, _q("Player"));
|
||||||
|
|
||||||
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -96,7 +124,7 @@ void HistoryWidget::populateMenu(QMenu &iMenu, const QPoint &iPoint)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Find the selected word
|
// Find the selected word
|
||||||
const QModelIndex &wordIndex = m_model->index(index.row(), 2);
|
const QModelIndex &wordIndex = m_model->index(index.row(), m_colWord);
|
||||||
QString selectedWord = m_model->data(wordIndex).toString();
|
QString selectedWord = m_model->data(wordIndex).toString();
|
||||||
|
|
||||||
if (selectedWord != "")
|
if (selectedWord != "")
|
||||||
|
@ -107,23 +135,6 @@ void HistoryWidget::populateMenu(QMenu &iMenu, const QPoint &iPoint)
|
||||||
void HistoryWidget::updateModel()
|
void HistoryWidget::updateModel()
|
||||||
{
|
{
|
||||||
m_model->removeRows(0, m_model->rowCount());
|
m_model->removeRows(0, m_model->rowCount());
|
||||||
if (m_forPlayer)
|
|
||||||
{
|
|
||||||
// Display the cumulative score and percentage
|
|
||||||
m_model->setHeaderData(5, Qt::Horizontal, _q("Total"), Qt::DisplayRole);
|
|
||||||
if (m_isFreeGame)
|
|
||||||
m_model->setHeaderData(6, Qt::Horizontal, "", Qt::DisplayRole);
|
|
||||||
else
|
|
||||||
m_model->setHeaderData(6, Qt::Horizontal, _q("Game %"), Qt::DisplayRole);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_history != NULL && m_history->getSize() != 0)
|
if (m_history != NULL && m_history->getSize() != 0)
|
||||||
{
|
{
|
||||||
|
@ -152,33 +163,24 @@ void HistoryWidget::updateModel()
|
||||||
const Move& m = t.getMove();
|
const Move& m = t.getMove();
|
||||||
|
|
||||||
// Set data common to all moves
|
// Set data common to all moves
|
||||||
m_model->setData(m_model->index(prevRowNum, 0), i + 1);
|
setCellData(prevRowNum, m_colTurn, i + 1);
|
||||||
m_model->setData(m_model->index(prevRowNum, 1),
|
setCellData(prevRowNum, m_colRack, qfw(t.getPlayedRack().toString()));
|
||||||
qfw(t.getPlayedRack().toString()));
|
setCellData(rowNum, m_colPoints, m.getScore());
|
||||||
m_model->setData(m_model->index(rowNum, 4), m.getScore());
|
|
||||||
totalScore += m.getScore();
|
totalScore += m.getScore();
|
||||||
if (m_game != NULL)
|
setCellData(rowNum, m_colTotal, totalScore);
|
||||||
|
if (m_game->getHistory().getSize() > i)
|
||||||
{
|
{
|
||||||
gameScore += m_game->getHistory().getTurn(i).getMove().getScore();
|
gameScore += m_game->getHistory().getTurn(i).getMove().getScore();
|
||||||
}
|
}
|
||||||
if (m_forPlayer)
|
if (gameScore != 0)
|
||||||
{
|
|
||||||
m_model->setData(m_model->index(rowNum, 5), totalScore);
|
|
||||||
if (!m_isFreeGame && gameScore != 0)
|
|
||||||
{
|
{
|
||||||
int percentage = totalScore * 100 / gameScore;
|
int percentage = totalScore * 100 / gameScore;
|
||||||
m_model->setData(m_model->index(rowNum, 6),
|
setCellData(rowNum, m_colPercent, QString("%1%").arg(percentage));
|
||||||
QString("%1%").arg(percentage));
|
|
||||||
}
|
}
|
||||||
}
|
if (m_isFreeGame)
|
||||||
else if (m_isFreeGame)
|
|
||||||
{
|
{
|
||||||
const wstring &name = m_game->getPlayer(t.getPlayer()).getName();
|
const wstring &name = m_game->getPlayer(t.getPlayer()).getName();
|
||||||
m_model->setData(m_model->index(rowNum, 5), qfw(name));
|
setCellData(rowNum, m_colPlayer, qfw(name));
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_model->setData(m_model->index(rowNum, 5), totalScore);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the rest
|
// Set the rest
|
||||||
|
@ -186,36 +188,35 @@ void HistoryWidget::updateModel()
|
||||||
{
|
{
|
||||||
const Round &r = m.getRound();
|
const Round &r = m.getRound();
|
||||||
wstring coord = r.getCoord().toString();
|
wstring coord = r.getCoord().toString();
|
||||||
m_model->setData(m_model->index(rowNum, 2), qfw(r.getWord()));
|
setCellData(rowNum, m_colWord, qfw(r.getWord()));
|
||||||
m_model->setData(m_model->index(rowNum, 3), qfw(coord));
|
setCellData(rowNum, m_colRef, qfw(coord));
|
||||||
color = Qt::black;
|
color = Qt::black;
|
||||||
}
|
}
|
||||||
else if (m.getType() == Move::INVALID_WORD)
|
else if (m.getType() == Move::INVALID_WORD)
|
||||||
{
|
{
|
||||||
m_model->setData(m_model->index(rowNum, 2),
|
setCellData(rowNum, m_colWord, "<" + qfw(m.getBadWord()) + ">");
|
||||||
"<" + qfw(m.getBadWord()) + ">");
|
setCellData(rowNum, m_colRef, qfw(m.getBadCoord()));
|
||||||
m_model->setData(m_model->index(rowNum, 3), qfw(m.getBadCoord()));
|
|
||||||
color = Qt::red;
|
color = Qt::red;
|
||||||
}
|
}
|
||||||
else if (m.getType() == Move::NO_MOVE)
|
else if (m.getType() == Move::NO_MOVE)
|
||||||
{
|
{
|
||||||
m_model->setData(m_model->index(rowNum, 2), _q("(NO MOVE)"));
|
setCellData(rowNum, m_colWord, _q("(NO MOVE)"));
|
||||||
color = Qt::blue;
|
color = Qt::blue;
|
||||||
}
|
}
|
||||||
else if (m.getType() == Move::PASS)
|
else if (m.getType() == Move::PASS)
|
||||||
{
|
{
|
||||||
m_model->setData(m_model->index(rowNum, 2), _q("(PASS)"));
|
setCellData(rowNum, m_colWord, _q("(PASS)"));
|
||||||
color = Qt::blue;
|
color = Qt::blue;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
m_model->setData(m_model->index(rowNum, 2),
|
setCellData(rowNum, m_colWord,
|
||||||
"[-" + qfw(m.getChangedLetters()) + "]");
|
"[-" + qfw(m.getChangedLetters()) + "]");
|
||||||
color = Qt::blue;
|
color = Qt::blue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the color of the text
|
// Set the color of the text
|
||||||
for (int col = 0; col < 6; ++col)
|
for (int col = 0; col < m_model->columnCount(); ++col)
|
||||||
{
|
{
|
||||||
int row = rowNum;
|
int row = rowNum;
|
||||||
if (!align && col < 2)
|
if (!align && col < 2)
|
||||||
|
@ -226,10 +227,17 @@ void HistoryWidget::updateModel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resizeColumnToContents(0);
|
resizeColumnToContents(m_colTurn);
|
||||||
resizeColumnToContents(3);
|
resizeColumnToContents(m_colRef);
|
||||||
resizeColumnToContents(4);
|
resizeColumnToContents(m_colPoints);
|
||||||
resizeColumnToContents(5);
|
resizeColumnToContents(m_colTotal);
|
||||||
|
resizeColumnToContents(m_colPlayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void HistoryWidget::setCellData(int iRow, int iCol, const QVariant &iData)
|
||||||
|
{
|
||||||
|
m_model->setData(m_model->index(iRow, iCol), iData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -68,6 +68,15 @@ private:
|
||||||
bool m_forPlayer;
|
bool m_forPlayer;
|
||||||
bool m_isFreeGame;
|
bool m_isFreeGame;
|
||||||
|
|
||||||
|
int m_colTurn;
|
||||||
|
int m_colRack;
|
||||||
|
int m_colWord;
|
||||||
|
int m_colRef;
|
||||||
|
int m_colPoints;
|
||||||
|
int m_colTotal;
|
||||||
|
int m_colPercent;
|
||||||
|
int m_colPlayer;
|
||||||
|
|
||||||
/// Model of the history
|
/// Model of the history
|
||||||
QStandardItemModel *m_model;
|
QStandardItemModel *m_model;
|
||||||
|
|
||||||
|
@ -76,6 +85,9 @@ private:
|
||||||
|
|
||||||
/// Force synchronizing the model with the contents of the history
|
/// Force synchronizing the model with the contents of the history
|
||||||
void updateModel();
|
void updateModel();
|
||||||
|
|
||||||
|
/// Helper method to set the data in a table cell
|
||||||
|
void setCellData(int iRow, int iCol, const QVariant &iData);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue