File indexing completed on 2024-04-28 05:38:03

0001 /*************************************************************************
0002  *     Copyright (C) 2011 by Joseph Boudou                               *
0003  *      Copyright (C) 2014 by Renaud Guezennec                            *
0004  *                                                                       *
0005  *     https://rolisteam.org/                                         *
0006  *                                                                       *
0007  *   Rolisteam is free software; you can redistribute it and/or modify   *
0008  *   it under the terms of the GNU General Public License as published   *
0009  *   by the Free Software Foundation; either version 2 of the License,   *
0010  *   or (at your option) any later version.                              *
0011  *                                                                       *
0012  *   This program is distributed in the hope that it will be useful,     *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0015  *   GNU General Public License for more details.                        *
0016  *                                                                       *
0017  *   You should have received a copy of the GNU General Public License   *
0018  *   along with this program; if not, write to the                       *
0019  *   Free Software Foundation, Inc.,                                     *
0020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0021  *************************************************************************/
0022 
0023 #include <QDebug>
0024 #include <QMenu>
0025 #include <QVBoxLayout>
0026 
0027 #include "modelviews/userlistview.h"
0028 #include "playerspanel.h"
0029 
0030 #include "controller/playercontroller.h"
0031 #include "data/character.h"
0032 #include "data/person.h"
0033 #include "data/player.h"
0034 #include "delegates/delegate.h"
0035 #include "dialogs/persondialog.h"
0036 #include "model/playermodel.h"
0037 #include "ui_playerspanel.h"
0038 
0039 /********************
0040  * PlayersPanel *
0041  ********************/
0042 
0043 PlayersPanel::PlayersPanel(PlayerController* ctrl, QWidget* parent)
0044     : QDockWidget(parent), m_ui(new Ui::PlayersPanel), m_ctrl(ctrl)
0045 {
0046     m_ui->setupUi(this);
0047     m_ui->m_playerView->setPlayerController(ctrl);
0048 
0049     m_ui->m_addBtn->setDefaultAction(m_ui->m_addLocalCharacter);
0050     m_ui->m_addLocalCharacter->setIcon(QIcon::fromTheme("add_round"));
0051 
0052     m_ui->m_removeBtn->setDefaultAction(m_ui->m_removeLocalCharacter);
0053     m_ui->m_removeLocalCharacter->setIcon(QIcon::fromTheme("delete"));
0054 
0055     setAllowedAreas(Qt::AllDockWidgetAreas);
0056     setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable);
0057     setWindowTitle(tr("Player List"));
0058     setObjectName("PlayersPanel");
0059 
0060     setConnection();
0061 }
0062 
0063 PlayersPanel::~PlayersPanel() {}
0064 
0065 void PlayersPanel::setConnection()
0066 {
0067     if(!m_ctrl)
0068         return;
0069     connect(m_ui->m_playerView, &UserListView::runDiceForCharacter, this, &PlayersPanel::runDiceForCharacter);
0070 
0071     m_ui->m_playerView->setModel(m_ctrl->model());
0072     m_selectionModel= m_ui->m_playerView->selectionModel();
0073     m_ui->m_playerView->setHeaderHidden(true);
0074 
0075     // Actions
0076     connect(m_selectionModel, &QItemSelectionModel::currentChanged, this, &PlayersPanel::selectAnotherPerson);
0077     connect(m_ui->m_addLocalCharacter, &QAction::triggered, m_ctrl, &PlayerController::addLocalCharacter);
0078     connect(m_ui->m_removeLocalCharacter, &QAction::triggered, this,
0079             [this]() { m_ctrl->removeLocalCharacter(m_ui->m_playerView->currentIndex()); });
0080 
0081     selectAnotherPerson(m_ui->m_playerView->currentIndex(), {});
0082 }
0083 
0084 void PlayersPanel::selectAnotherPerson(const QModelIndex& current, const QModelIndex& previous)
0085 {
0086     Q_UNUSED(previous)
0087     m_ui->m_removeLocalCharacter->setEnabled(current.isValid() && current.parent().isValid()
0088                                              && current.data(PlayerModel::LocalRole).toBool());
0089 }