2012-02-16 21:59:01 +01: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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <QtGui/QTableWidget>
|
|
|
|
#include <QtGui/QPushButton>
|
|
|
|
#include <QtGui/QHeaderView>
|
|
|
|
#include <QtGui/QComboBox>
|
|
|
|
#include <QtGui/QSpinBox>
|
|
|
|
#include <QtGui/QKeyEvent>
|
2012-02-17 22:18:20 +01:00
|
|
|
#include <QtGui/QMenu>
|
|
|
|
#include <QtGui/QAction>
|
2012-02-16 21:59:01 +01:00
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
|
|
|
#include "players_table_helper.h"
|
2012-02-17 22:18:20 +01:00
|
|
|
#include "custom_popup.h"
|
2012-03-18 02:45:15 +01:00
|
|
|
#include "misc_helpers.h"
|
2012-02-16 21:59:01 +01:00
|
|
|
#include "qtcommon.h"
|
|
|
|
|
|
|
|
|
|
|
|
INIT_LOGGER(qt, PlayersTableHelper);
|
|
|
|
|
|
|
|
|
|
|
|
const char * PlayersTableHelper::kHUMAN = _("Human");
|
|
|
|
const char * PlayersTableHelper::kAI = _("Computer");
|
|
|
|
|
2012-02-17 23:38:08 +01:00
|
|
|
// Global function, used to put PlayerDef in a QSet
|
|
|
|
uint qHash(const PlayerDef &key)
|
|
|
|
{
|
|
|
|
return qHash(key.name) ^ qHash(key.type) ^ qHash(key.level);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-27 20:08:42 +02:00
|
|
|
PlayerDef::PlayerDef(QString iName, QString iType, QString iLevel, bool iIsDefault)
|
|
|
|
: name(iName), type(iType), level(iLevel), isDefault(iIsDefault)
|
2012-02-17 23:38:08 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlayerDef::operator==(const PlayerDef &iOther) const
|
|
|
|
{
|
2012-04-27 20:08:42 +02:00
|
|
|
// Ignore the "isDefault" flag
|
2012-02-17 23:38:08 +01:00
|
|
|
return name == iOther.name
|
|
|
|
&& type == iOther.type
|
|
|
|
&& level == iOther.level;
|
|
|
|
}
|
|
|
|
|
2012-02-16 21:59:01 +01:00
|
|
|
|
|
|
|
PlayersTableHelper::PlayersTableHelper(QObject *parent,
|
|
|
|
QTableWidget *tablePlayers,
|
|
|
|
QPushButton *addButton,
|
2012-04-27 20:08:42 +02:00
|
|
|
QPushButton *removeButton,
|
|
|
|
bool showDefaultColumn)
|
2012-02-16 21:59:01 +01:00
|
|
|
: QObject(parent), m_tablePlayers(tablePlayers),
|
2012-04-27 20:08:42 +02:00
|
|
|
m_buttonAdd(addButton), m_buttonRemove(removeButton),
|
|
|
|
m_showDefaultColumn(showDefaultColumn)
|
2012-02-16 21:59:01 +01:00
|
|
|
{
|
|
|
|
// Initialize the table headers
|
2012-04-27 20:08:42 +02:00
|
|
|
tablePlayers->setColumnCount(m_showDefaultColumn ? 4 : 3);
|
2012-02-16 21:59:01 +01:00
|
|
|
tablePlayers->setHorizontalHeaderItem(0, new QTableWidgetItem(_("Name")));
|
|
|
|
tablePlayers->setHorizontalHeaderItem(1, new QTableWidgetItem(_("Type")));
|
|
|
|
tablePlayers->setHorizontalHeaderItem(2, new QTableWidgetItem(_("Level")));
|
2012-04-27 20:08:42 +02:00
|
|
|
tablePlayers->setHorizontalHeaderItem(3, new QTableWidgetItem(_("Default")));
|
2012-02-16 21:59:01 +01:00
|
|
|
QHeaderView *header = tablePlayers->horizontalHeader();
|
|
|
|
header->setHighlightSections(false);
|
|
|
|
header->setStretchLastSection(true);
|
|
|
|
header->setDefaultAlignment(Qt::AlignLeft);
|
|
|
|
header->resizeSection(0, 200);
|
|
|
|
header->resizeSection(1, 100);
|
|
|
|
header->resizeSection(2, 50);
|
2012-04-27 20:08:42 +02:00
|
|
|
header->resizeSection(3, 70);
|
2012-02-16 21:59:01 +01:00
|
|
|
tablePlayers->verticalHeader()->setVisible(false);
|
|
|
|
|
|
|
|
// Set other table properties
|
|
|
|
tablePlayers->setAlternatingRowColors(true);
|
|
|
|
tablePlayers->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
tablePlayers->setShowGrid(false);
|
|
|
|
|
|
|
|
if (m_buttonAdd)
|
|
|
|
{
|
|
|
|
QObject::connect(m_buttonAdd, SIGNAL(clicked()),
|
|
|
|
this, SLOT(addRow()));
|
|
|
|
}
|
|
|
|
if (m_buttonRemove)
|
|
|
|
{
|
|
|
|
QObject::connect(tablePlayers, SIGNAL(itemSelectionChanged()),
|
|
|
|
this, SLOT(enableRemoveButton()));
|
|
|
|
QObject::connect(m_buttonRemove, SIGNAL(clicked()),
|
|
|
|
this, SLOT(removeSelectedRows()));
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayersTypeDelegate *typeDelegate = new PlayersTypeDelegate(this);
|
|
|
|
m_tablePlayers->setItemDelegateForColumn(1, typeDelegate);
|
|
|
|
PlayersLevelDelegate *levelDelegate = new PlayersLevelDelegate(this);
|
|
|
|
m_tablePlayers->setItemDelegateForColumn(2, levelDelegate);
|
|
|
|
|
2012-02-17 22:18:20 +01:00
|
|
|
// Add a context menu for the results
|
|
|
|
CustomPopup *popup = new CustomPopup(m_tablePlayers);
|
|
|
|
QObject::connect(popup, SIGNAL(popupCreated(QMenu&, const QPoint&)),
|
|
|
|
this, SLOT(populateMenu(QMenu&, const QPoint&)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::populateMenu(QMenu &iMenu, const QPoint &iPoint)
|
|
|
|
{
|
|
|
|
const QModelIndex &index = m_tablePlayers->indexAt(iPoint);
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Q_FOREACH(QAction *action, m_popupActions)
|
|
|
|
{
|
|
|
|
iMenu.addAction(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::addPopupAction(QAction *iAction)
|
|
|
|
{
|
|
|
|
m_popupActions.push_back(iAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-17 23:45:08 +01:00
|
|
|
void PlayersTableHelper::addPopupRemoveAction()
|
2012-02-17 22:18:20 +01:00
|
|
|
{
|
|
|
|
QAction *removeAction = new QAction(_q("Remove selected player(s)"), this);
|
|
|
|
removeAction->setStatusTip(_q("Remove the selected player(s) from the list"));
|
|
|
|
removeAction->setShortcut(Qt::Key_Delete);
|
|
|
|
QObject::connect(removeAction, SIGNAL(triggered()),
|
|
|
|
this, SLOT(removeSelectedRows()));
|
2012-02-17 23:45:08 +01:00
|
|
|
addPopupAction(removeAction);
|
|
|
|
|
|
|
|
// Install a custom event filter, to remove the selection when the
|
|
|
|
// "Delete" key is pressed
|
2012-03-18 02:45:15 +01:00
|
|
|
KeyEventFilter *filter = new KeyEventFilter(this, Qt::Key_Delete);
|
2012-02-17 23:45:08 +01:00
|
|
|
m_tablePlayers->installEventFilter(filter);
|
2012-03-19 23:14:34 +01:00
|
|
|
QObject::connect(filter, SIGNAL(keyPressed(int, int)),
|
2012-02-17 23:45:08 +01:00
|
|
|
this, SLOT(removeSelectedRows()));
|
2012-02-16 21:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::enableRemoveButton()
|
|
|
|
{
|
|
|
|
m_buttonRemove->setEnabled(!m_tablePlayers->selectedItems().isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::removeSelectedRows()
|
|
|
|
{
|
|
|
|
bool changed = false;
|
|
|
|
QItemSelectionModel *selModel = m_tablePlayers->selectionModel();
|
|
|
|
for (int i = m_tablePlayers->rowCount() - 1; i >= 0; --i)
|
|
|
|
{
|
|
|
|
if (selModel->isRowSelected(i, QModelIndex()))
|
|
|
|
{
|
|
|
|
changed = true;
|
|
|
|
m_tablePlayers->removeRow(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed)
|
|
|
|
emit rowCountChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::addRow()
|
|
|
|
{
|
2012-04-27 20:08:42 +02:00
|
|
|
addPlayer(PlayerDef(_q("New player"), _q(kHUMAN), "", false),
|
2012-02-18 00:36:36 +01:00
|
|
|
true, true);
|
2012-02-16 21:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-17 23:14:04 +01:00
|
|
|
QList<PlayerDef> PlayersTableHelper::getPlayers(bool onlySelected) const
|
2012-02-16 21:59:01 +01:00
|
|
|
{
|
|
|
|
QList<PlayerDef> playersList;
|
|
|
|
QItemSelectionModel *selModel = m_tablePlayers->selectionModel();
|
|
|
|
for (int i = 0; i < m_tablePlayers->rowCount(); ++i)
|
|
|
|
{
|
|
|
|
if (onlySelected && !selModel->isRowSelected(i, QModelIndex()))
|
|
|
|
continue;
|
2012-04-27 20:08:42 +02:00
|
|
|
const QVariant & data = m_tablePlayers->model()->data(m_tablePlayers->model()->index(i, 3));
|
2012-02-17 23:38:08 +01:00
|
|
|
PlayerDef playerDef(m_tablePlayers->item(i, 0)->text(),
|
|
|
|
m_tablePlayers->item(i, 1)->text(),
|
2012-04-27 20:08:42 +02:00
|
|
|
m_tablePlayers->item(i, 2)->text(),
|
|
|
|
data.toBool());
|
2012-02-16 21:59:01 +01:00
|
|
|
playersList.push_back(playerDef);
|
|
|
|
}
|
|
|
|
return playersList;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int PlayersTableHelper::getRowCount() const
|
|
|
|
{
|
|
|
|
return m_tablePlayers->rowCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::addPlayers(const QList<PlayerDef> &iList)
|
|
|
|
{
|
2012-02-17 23:38:08 +01:00
|
|
|
// Only add players which are not already there
|
|
|
|
QSet<PlayerDef> tmpSet = getPlayers(false).toSet();
|
2012-02-16 21:59:01 +01:00
|
|
|
Q_FOREACH(const PlayerDef &player, iList)
|
|
|
|
{
|
2012-02-17 23:38:08 +01:00
|
|
|
if (!tmpSet.contains(player))
|
|
|
|
{
|
|
|
|
addRow(player);
|
|
|
|
tmpSet.insert(player);
|
|
|
|
}
|
2012-02-16 21:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-18 00:11:13 +01:00
|
|
|
void PlayersTableHelper::addPlayer(const PlayerDef &iPlayer,
|
|
|
|
bool selectAndEdit,
|
|
|
|
bool renameIfDuplicate)
|
2012-02-17 23:38:08 +01:00
|
|
|
{
|
2012-02-18 00:11:13 +01:00
|
|
|
QSet<PlayerDef> tmpSet = getPlayers(false).toSet();
|
|
|
|
PlayerDef def = iPlayer;
|
|
|
|
// Generate a unique name if needed
|
|
|
|
if (renameIfDuplicate)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
while (tmpSet.contains(def))
|
|
|
|
{
|
|
|
|
def.name = QString("%1 (%2)").arg(iPlayer.name).arg(i);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (tmpSet.contains(def))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add the player
|
|
|
|
addRow(def);
|
2012-02-17 23:50:14 +01:00
|
|
|
|
|
|
|
if (selectAndEdit)
|
|
|
|
{
|
|
|
|
int row = m_tablePlayers->rowCount() - 1;
|
|
|
|
// Give focus to the newly created cell containing the player name
|
|
|
|
m_tablePlayers->setCurrentCell(row, 0,
|
|
|
|
QItemSelectionModel::ClearAndSelect |
|
|
|
|
QItemSelectionModel::Current |
|
|
|
|
QItemSelectionModel::Rows);
|
|
|
|
// Edit the name
|
|
|
|
m_tablePlayers->editItem(m_tablePlayers->item(row, 0));
|
|
|
|
}
|
2012-02-17 23:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTableHelper::addRow(const PlayerDef &iDef)
|
2012-02-16 21:59:01 +01:00
|
|
|
{
|
|
|
|
const int row = m_tablePlayers->rowCount();
|
|
|
|
m_tablePlayers->setRowCount(row + 1);
|
|
|
|
m_tablePlayers->setRowHeight(row, 24);
|
2012-02-17 23:38:08 +01:00
|
|
|
m_tablePlayers->setItem(row, 0, new QTableWidgetItem(iDef.name));
|
|
|
|
m_tablePlayers->setItem(row, 1, new QTableWidgetItem(iDef.type));
|
|
|
|
m_tablePlayers->setItem(row, 2, new QTableWidgetItem(iDef.level));
|
2012-04-27 20:08:42 +02:00
|
|
|
QTableWidgetItem *item = new QTableWidgetItem;
|
|
|
|
item->setData(Qt::DisplayRole, iDef.isDefault);
|
|
|
|
m_tablePlayers->setItem(row, 3, item);
|
2012-02-16 21:59:01 +01:00
|
|
|
emit rowCountChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-17 23:14:04 +01:00
|
|
|
QList<PlayerDef> PlayersTableHelper::getFavPlayers()
|
2012-02-16 21:59:01 +01:00
|
|
|
{
|
2012-02-17 23:10:55 +01:00
|
|
|
QList<PlayerDef> playersList;
|
2012-02-16 21:59:01 +01:00
|
|
|
QSettings qs;
|
|
|
|
int size = qs.beginReadArray("FavPlayers");
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
qs.setArrayIndex(i);
|
2012-02-17 23:38:08 +01:00
|
|
|
PlayerDef playerDef(qs.value("name").toString(),
|
|
|
|
qs.value("type").toString(),
|
2012-04-27 20:08:42 +02:00
|
|
|
qs.value("level").toString(),
|
|
|
|
qs.value("default").toBool());
|
2012-02-17 23:10:55 +01:00
|
|
|
playersList.push_back(playerDef);
|
2012-02-16 21:59:01 +01:00
|
|
|
}
|
|
|
|
qs.endArray();
|
2012-02-17 23:10:55 +01:00
|
|
|
return playersList;
|
2012-02-16 21:59:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-17 23:10:55 +01:00
|
|
|
void PlayersTableHelper::saveFavPlayers(const QList<PlayerDef> &iFavPlayers)
|
2012-02-16 21:59:01 +01:00
|
|
|
{
|
|
|
|
QSettings qs;
|
|
|
|
qs.beginWriteArray("FavPlayers");
|
2012-02-17 23:10:55 +01:00
|
|
|
for (int i = 0; i < iFavPlayers.size(); ++i)
|
2012-02-16 21:59:01 +01:00
|
|
|
{
|
|
|
|
qs.setArrayIndex(i);
|
2012-02-17 23:10:55 +01:00
|
|
|
qs.setValue("name", iFavPlayers.at(i).name);
|
|
|
|
qs.setValue("type", iFavPlayers.at(i).type);
|
|
|
|
qs.setValue("level", iFavPlayers.at(i).level);
|
2012-04-27 20:08:42 +02:00
|
|
|
qs.setValue("default", iFavPlayers.at(i).isDefault);
|
2012-02-16 21:59:01 +01:00
|
|
|
}
|
|
|
|
qs.endArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PlayersTypeDelegate::PlayersTypeDelegate(QObject *parent)
|
|
|
|
: QItemDelegate(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QWidget *PlayersTypeDelegate::createEditor(QWidget *parent,
|
|
|
|
const QStyleOptionViewItem &,
|
|
|
|
const QModelIndex &) const
|
|
|
|
{
|
|
|
|
QComboBox *editor = new QComboBox(parent);
|
|
|
|
editor->addItem(_q(PlayersTableHelper::kHUMAN));
|
|
|
|
editor->addItem(_q(PlayersTableHelper::kAI));
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTypeDelegate::setEditorData(QWidget *editor,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QComboBox *combo = static_cast<QComboBox*>(editor);
|
|
|
|
QString text = index.model()->data(index, Qt::DisplayRole).toString();
|
|
|
|
combo->setCurrentIndex(combo->findText(text));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTypeDelegate::setModelData(QWidget *editor,
|
|
|
|
QAbstractItemModel *model,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QComboBox *combo = static_cast<QComboBox*>(editor);
|
|
|
|
model->setData(index, combo->currentText());
|
|
|
|
// Adapt the level to the chosen type of player
|
|
|
|
QModelIndex levelIndex = model->index(index.row(), 2);
|
|
|
|
if (combo->currentText() == _q(PlayersTableHelper::kHUMAN))
|
|
|
|
model->setData(levelIndex, QVariant());
|
|
|
|
else
|
|
|
|
model->setData(levelIndex, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersTypeDelegate::updateEditorGeometry(QWidget *editor,
|
|
|
|
const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &) const
|
|
|
|
{
|
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PlayersLevelDelegate::PlayersLevelDelegate(QObject *parent)
|
|
|
|
: QItemDelegate(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QWidget *PlayersLevelDelegate::createEditor(QWidget *parent,
|
|
|
|
const QStyleOptionViewItem &,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
// Allow changing the level only for computer players, i.e.
|
|
|
|
// if there is a level defined
|
2012-02-17 22:18:20 +01:00
|
|
|
const QAbstractItemModel *model = index.model();
|
|
|
|
QString type = model->data(model->index(index.row(), 1), Qt::DisplayRole).toString();
|
|
|
|
if (type != _q(PlayersTableHelper::kAI))
|
2012-02-16 21:59:01 +01:00
|
|
|
return NULL;
|
|
|
|
QSpinBox *editor = new QSpinBox(parent);
|
|
|
|
editor->setMinimum(0);
|
|
|
|
editor->setMaximum(100);
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersLevelDelegate::setEditorData(QWidget *editor,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
|
|
|
int value = index.model()->data(index, Qt::DisplayRole).toInt();
|
|
|
|
spinBox->setValue(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersLevelDelegate::setModelData(QWidget *editor,
|
|
|
|
QAbstractItemModel *model,
|
|
|
|
const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
|
|
|
|
model->setData(index, spinBox->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayersLevelDelegate::updateEditorGeometry(QWidget *editor,
|
|
|
|
const QStyleOptionViewItem &option,
|
|
|
|
const QModelIndex &) const
|
|
|
|
{
|
|
|
|
editor->setGeometry(option.rect);
|
|
|
|
}
|
|
|
|
|