File indexing completed on 2024-07-14 07:35:38

0001 /*
0002     SPDX-FileCopyrightText: 2003 Russell Steffen <rsteffen@bayarea.net>
0003     SPDX-FileCopyrightText: 2003 Stephan Zehetner <s.zehetner@nevox.org>
0004     SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com>
0005     SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se>
0006     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@gmail.com>
0007     SPDX-FileCopyrightText: 2013 Alexander Schuch <aschuch247@gmail.com>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include "standingswidget.h"
0013 
0014 #include <QHeaderView>
0015 #include <QTableWidget>
0016 #include <QVBoxLayout>
0017 
0018 #include <KLocalizedString>
0019 
0020 
0021 StandingsWidget::StandingsWidget(QWidget *parent) :
0022     QWidget(parent)
0023 {
0024     setupTable();
0025 }
0026 
0027 
0028 /**
0029  * @note Use "delegating constructor" once C++11 is required by KDE.
0030  */
0031 
0032 StandingsWidget::StandingsWidget(QWidget *parent, const QList<Player *> &players) :
0033     // StandingsWidget(parent)
0034     QWidget(parent)
0035 {
0036     setupTable();
0037     update(players);
0038 }
0039 
0040 
0041 StandingsWidget::~StandingsWidget()
0042 {
0043 }
0044 
0045 
0046 QSize
0047 StandingsWidget::sizeHint() const
0048 {
0049     int w = m_standingsTable->verticalHeader()->width();
0050     int h = m_standingsTable->horizontalHeader()->height();
0051 
0052     for (int col = 0; col < m_standingsTable->columnCount(); ++col) {
0053         w += m_standingsTable->columnWidth(col);
0054     }
0055 
0056     for (int row = 0; row < m_standingsTable->rowCount(); ++row) {
0057         h += m_standingsTable->rowHeight(row);
0058     }
0059 
0060     /**
0061      * @todo The size calculated here does not yet prevent scrollbars to be
0062      * shown for the table. Figure out the offsets needed to be added and remove
0063      * the hard-coded numbers below!
0064      */
0065 
0066     return QSize(w, h) + QSize(m_standingsTable->columnCount() - 1, 40);
0067 }
0068 
0069 
0070 void
0071 StandingsWidget::update(const QList<Player *> &players )
0072 {
0073     m_standingsTable->setRowCount(players.count());
0074     m_standingsTable->clearContents();
0075     m_standingsTable->setSortingEnabled(false);
0076 
0077     int row = 0;
0078     QTableWidgetItem *item;
0079 
0080     for (Player *curPlayer : players) {
0081         item = new QTableWidgetItem();
0082         item->setData(Qt::DisplayRole, curPlayer->name());
0083         item->setData(Qt::DecorationRole, curPlayer->color());
0084         item->setFlags(Qt::ItemIsEnabled);
0085         m_standingsTable->setItem(row, 0, item);
0086 
0087         item = new QTableWidgetItem();
0088         item->setData(Qt::DisplayRole, curPlayer->shipsBuilt());
0089         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0090         item->setFlags(Qt::ItemIsEnabled);
0091         m_standingsTable->setItem(row, 1, item);
0092 
0093         item = new QTableWidgetItem();
0094         item->setData(Qt::DisplayRole, curPlayer->planetsConquered());
0095         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0096         item->setFlags(Qt::ItemIsEnabled);
0097         m_standingsTable->setItem(row, 2, item);
0098 
0099         item = new QTableWidgetItem();
0100         item->setData(Qt::DisplayRole, curPlayer->fleetsLaunched());
0101         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0102         item->setFlags(Qt::ItemIsEnabled);
0103         m_standingsTable->setItem(row, 3, item);
0104 
0105         item = new QTableWidgetItem();
0106         item->setData(Qt::DisplayRole, curPlayer->enemyFleetsDestroyed());
0107         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0108         item->setFlags(Qt::ItemIsEnabled);
0109         m_standingsTable->setItem(row, 4, item);
0110 
0111         item = new QTableWidgetItem();
0112         item->setData(Qt::DisplayRole, curPlayer->enemyShipsDestroyed());
0113         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0114         item->setFlags(Qt::ItemIsEnabled);
0115         m_standingsTable->setItem(row, 5, item);
0116 
0117         item = new QTableWidgetItem();
0118         item->setData(Qt::DisplayRole, curPlayer->turnProduction());
0119         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0120         item->setFlags(Qt::ItemIsEnabled);
0121         m_standingsTable->setItem(row, 6, item);
0122 
0123         item = new QTableWidgetItem();
0124         item->setData(Qt::DisplayRole, curPlayer->turnShips());
0125         item->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
0126         item->setFlags(Qt::ItemIsEnabled);
0127         m_standingsTable->setItem(row, 7, item);
0128 
0129         ++row;
0130     }
0131 
0132     m_standingsTable->setSortingEnabled(true);
0133     m_standingsTable->resizeColumnsToContents();
0134 }
0135 
0136 
0137 void
0138 StandingsWidget::setupTable()
0139 {
0140     setObjectName(QStringLiteral("widget-standings"));
0141 
0142     QVBoxLayout *main = new QVBoxLayout(this);
0143 
0144     m_standingsTable = new QTableWidget();
0145     m_standingsTable->setColumnCount(8);
0146     m_standingsTable->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
0147     m_standingsTable->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
0148     m_standingsTable->setSelectionMode(QAbstractItemView::NoSelection);
0149 
0150     QStringList headerLabels;
0151     headerLabels
0152         << i18nc("The player name", "Player")
0153         << i18n("Ships\nBuilt")
0154         << i18n("Planets\nConquered")
0155         << i18n("Fleets\nLaunched")
0156         << i18n("Fleets\nDestroyed")
0157         << i18n("Ships\nDestroyed")
0158         << i18n("Current\nProduction")
0159         << i18n("Current\nFleet Size");
0160 
0161     m_standingsTable->setHorizontalHeaderLabels(headerLabels);
0162     m_standingsTable->verticalHeader()->hide();
0163 
0164     main->addWidget(m_standingsTable);
0165 }