mirror of
git://git.savannah.nongnu.org/eliot.git
synced 2025-02-07 08:48:26 +01:00
Stats: simplify the flipping code using the decorator pattern
This commit is contained in:
parent
3f2568db5d
commit
fa55eab82e
2 changed files with 70 additions and 21 deletions
|
@ -49,9 +49,58 @@ const QColor StatsWidget::PassBrush(210, 210, 210);
|
||||||
const QColor StatsWidget::InvalidBrush(255, 0, 0);
|
const QColor StatsWidget::InvalidBrush(255, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flipped version of a model (using the decorator pattern)
|
||||||
|
* This implementation does not super tree-models.
|
||||||
|
*/
|
||||||
|
class FlippedModel : public QAbstractItemModel
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FlippedModel(QAbstractItemModel *iRefModel) : m_refModel(iRefModel) {}
|
||||||
|
|
||||||
|
virtual int columnCount(const QModelIndex &) const
|
||||||
|
{
|
||||||
|
// Ignore the parent
|
||||||
|
return m_refModel->rowCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int rowCount(const QModelIndex &) const
|
||||||
|
{
|
||||||
|
// Ignore the parent
|
||||||
|
return m_refModel->columnCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual QModelIndex index(int row, int column, const QModelIndex &) const
|
||||||
|
{
|
||||||
|
return createIndex(row, column, (void*)NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual QModelIndex parent(const QModelIndex &) const
|
||||||
|
{
|
||||||
|
// Ignore the given index
|
||||||
|
return QModelIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
|
||||||
|
{
|
||||||
|
return m_refModel->data(m_refModel->index(index.column(), index.row()), role);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const
|
||||||
|
{
|
||||||
|
Qt::Orientation newOrient =
|
||||||
|
orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal;
|
||||||
|
return m_refModel->headerData(section, newOrient, role);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// Wrapped model
|
||||||
|
QAbstractItemModel *m_refModel;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
StatsWidget::StatsWidget(QWidget *parent, const PublicGame *iGame)
|
StatsWidget::StatsWidget(QWidget *parent, const PublicGame *iGame)
|
||||||
: QWidget(parent), m_game(iGame), m_autoResizeColumns(true),
|
: QWidget(parent), m_game(iGame), m_autoResizeColumns(true)
|
||||||
m_isHorizontal(false)
|
|
||||||
{
|
{
|
||||||
// Layout
|
// Layout
|
||||||
setLayout(new QVBoxLayout);
|
setLayout(new QVBoxLayout);
|
||||||
|
@ -67,6 +116,8 @@ StatsWidget::StatsWidget(QWidget *parent, const PublicGame *iGame)
|
||||||
layout()->addWidget(m_table);
|
layout()->addWidget(m_table);
|
||||||
|
|
||||||
m_model = new QStandardItemModel();
|
m_model = new QStandardItemModel();
|
||||||
|
m_flippedModel = new FlippedModel(m_model);
|
||||||
|
|
||||||
m_table->setModel(m_model);
|
m_table->setModel(m_model);
|
||||||
|
|
||||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||||
|
@ -249,36 +300,30 @@ void StatsWidget::refresh()
|
||||||
|
|
||||||
QModelIndex StatsWidget::getIndex(int row, int col) const
|
QModelIndex StatsWidget::getIndex(int row, int col) const
|
||||||
{
|
{
|
||||||
if (m_isHorizontal)
|
return m_model->index(col, row);
|
||||||
return m_model->index(row, col);
|
|
||||||
else
|
|
||||||
return m_model->index(col, row);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void StatsWidget::setSectionHidden(int index, bool iHide)
|
void StatsWidget::setSectionHidden(int index, bool iHide)
|
||||||
{
|
{
|
||||||
if (m_isHorizontal)
|
m_table->setRowHidden(index, iHide);
|
||||||
m_table->setColumnHidden(index, iHide);
|
|
||||||
else
|
|
||||||
m_table->setRowHidden(index, iHide);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void StatsWidget::setModelSize(int rowCount, int colCount)
|
void StatsWidget::setModelSize(int rowCount, int colCount)
|
||||||
{
|
{
|
||||||
m_model->setRowCount(m_isHorizontal ? rowCount : colCount);
|
m_model->setRowCount(colCount);
|
||||||
m_model->setColumnCount(m_isHorizontal ? colCount : rowCount);
|
m_model->setColumnCount(rowCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void StatsWidget::setModelHeader(int index, const QString &iText, bool iPlayerNames)
|
void StatsWidget::setModelHeader(int index, const QString &iText, bool iPlayerNames)
|
||||||
{
|
{
|
||||||
Qt::Orientation orientation;
|
Qt::Orientation orientation;
|
||||||
if ((m_isHorizontal && iPlayerNames) || (!m_isHorizontal && !iPlayerNames))
|
if (iPlayerNames)
|
||||||
orientation = Qt::Vertical;
|
|
||||||
else
|
|
||||||
orientation = Qt::Horizontal;
|
orientation = Qt::Horizontal;
|
||||||
|
else
|
||||||
|
orientation = Qt::Vertical;
|
||||||
m_model->setHeaderData(index, orientation, iText);
|
m_model->setHeaderData(index, orientation, iText);
|
||||||
m_model->setHeaderData(index, orientation, Qt::AlignCenter, Qt::TextAlignmentRole);
|
m_model->setHeaderData(index, orientation, Qt::AlignCenter, Qt::TextAlignmentRole);
|
||||||
}
|
}
|
||||||
|
@ -309,7 +354,7 @@ void StatsWidget::setModelTurnData(const QModelIndex &iIndex,
|
||||||
score >= iGameTurn.getMove().getScore());
|
score >= iGameTurn.getMove().getScore());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the background c constolor
|
// Set the background color
|
||||||
if (iTurn.getSoloPoints() != 0)
|
if (iTurn.getSoloPoints() != 0)
|
||||||
m_model->setData(iIndex, SoloBrush, Qt::BackgroundRole);
|
m_model->setData(iIndex, SoloBrush, Qt::BackgroundRole);
|
||||||
else if (iTurn.getPenaltyPoints() != 0)
|
else if (iTurn.getPenaltyPoints() != 0)
|
||||||
|
@ -411,7 +456,11 @@ void StatsWidget::lockSizesChanged(bool checked)
|
||||||
|
|
||||||
void StatsWidget::flipTable()
|
void StatsWidget::flipTable()
|
||||||
{
|
{
|
||||||
m_isHorizontal = !m_isHorizontal;
|
bool flipped = m_table->model() != m_model;
|
||||||
|
if (flipped)
|
||||||
|
m_table->setModel(m_model);
|
||||||
|
else
|
||||||
|
m_table->setModel(m_flippedModel);
|
||||||
refresh();
|
refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ class PublicGame;
|
||||||
class Player;
|
class Player;
|
||||||
class TurnData;
|
class TurnData;
|
||||||
class QTableView;
|
class QTableView;
|
||||||
|
class QAbstractItemModel;
|
||||||
class QStandardItemModel;
|
class QStandardItemModel;
|
||||||
class QVariant;
|
class QVariant;
|
||||||
class QModelIndex;
|
class QModelIndex;
|
||||||
|
@ -57,16 +58,15 @@ private:
|
||||||
/// Model of the game
|
/// Model of the game
|
||||||
QStandardItemModel *m_model;
|
QStandardItemModel *m_model;
|
||||||
|
|
||||||
|
/// Proxy model (decorator), used to flip the view
|
||||||
|
QAbstractItemModel *m_flippedModel;
|
||||||
|
|
||||||
/// Table to display the model data
|
/// Table to display the model data
|
||||||
QTableView *m_table;
|
QTableView *m_table;
|
||||||
|
|
||||||
/// Indicate whether the columns should be resized automatically
|
/// Indicate whether the columns should be resized automatically
|
||||||
bool m_autoResizeColumns;
|
bool m_autoResizeColumns;
|
||||||
|
|
||||||
/// Orientation of the table (horizontal means the players are displayed as rows)
|
|
||||||
bool m_isHorizontal;
|
|
||||||
|
|
||||||
|
|
||||||
static const QColor WarningBrush;
|
static const QColor WarningBrush;
|
||||||
static const QColor PenaltyBrush;
|
static const QColor PenaltyBrush;
|
||||||
static const QColor SoloBrush;
|
static const QColor SoloBrush;
|
||||||
|
|
Loading…
Add table
Reference in a new issue