ManaPlus
playertablemodel.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  *
7  * This file is part of The ManaPlus Client.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
24 
25 #include "being/playerrelations.h"
26 
27 #include "gui/widgets/dropdown.h"
28 #include "gui/widgets/label.h"
29 
31 
32 #include "utils/delete2.h"
33 #include "utils/dtor.h"
34 
35 #include "debug.h"
36 
37 static const int COLUMNS_NR = 2; // name plus listbox
38 static const int NAME_COLUMN = 0;
39 static const unsigned int RELATION_CHOICE_COLUMN = 1;
40 
41 static const unsigned int ROW_HEIGHT = 12;
42 // The following column widths really shouldn't be hardcoded
43 // but should scale with the size of the widget... except
44 // that, right now, the widget doesn't exactly scale either.
45 static const unsigned int NAME_COLUMN_WIDTH = 230;
46 static const unsigned int RELATION_CHOICE_COLUMN_WIDTH = 80;
47 
48 #define WIDGET_AT(row, column) (((row) * COLUMNS_NR) + (column))
49 
51  Widget2(widget),
52  TableModel(),
53  mPlayers(nullptr),
54  mWidgets(),
55  mListModel(new PlayerRelationListModel)
56 {
58 }
59 
61 {
62  freeWidgets();
65 }
66 
68 {
69  if (mPlayers != nullptr)
70  return CAST_S32(mPlayers->size());
71  return 0;
72 }
73 
75 {
76  return COLUMNS_NR;
77 }
78 
80 {
81  return ROW_HEIGHT;
82 }
83 
84 int PlayerTableModel::getColumnWidth(const int index) const
85 {
86  if (index == NAME_COLUMN)
87  return NAME_COLUMN_WIDTH;
89 }
90 
92 {
94 
95  freeWidgets();
96  StringVect *const player_names = playerRelations.getPlayers();
97  delete mPlayers;
98  mPlayers = player_names;
99 
100  // set up widgets
101  for (unsigned int r = 0, fsz = CAST_U32(
102  player_names->size()); r < fsz; ++r)
103  {
104  const std::string name = (*player_names)[r];
105  Widget *const widget = new Label(this, name);
106  mWidgets.push_back(widget);
107 
108  DropDown *const choicebox = new DropDown(this,
109  mListModel,
110  false,
111  Modal_false,
112  nullptr,
113  std::string());
114  choicebox->setSelected(CAST_S32(
116  mWidgets.push_back(choicebox);
117  }
118 
120 }
121 
122 void PlayerTableModel::updateModelInRow(const int row) const
123 {
124  const DropDown *const choicebox = static_cast<DropDown *>(
126  if (choicebox == nullptr)
127  return;
129  static_cast<RelationT>(
130  choicebox->getSelected()));
131 }
132 
133 Widget *PlayerTableModel::getElementAt(int row, int column) const
134 {
135  return mWidgets[WIDGET_AT(row, column)];
136 }
137 
139 {
142  mWidgets.clear();
143 }
144 
145 std::string PlayerTableModel::getPlayerAt(const int index) const
146 {
147  if ((mPlayers == nullptr) || index < 0
148  || index >= CAST_S32(mPlayers->size()))
149  {
150  return std::string();
151  }
152  return (*mPlayers)[index];
153 }
TableModel
Definition: tablemodel.h:39
PlayerTableModel::getRows
int getRows() const
Definition: playertablemodel.cpp:67
PlayerTableModel::PlayerTableModel
PlayerTableModel(const Widget2 *const widget)
Definition: playertablemodel.cpp:50
PlayerTableModel::getPlayerAt
std::string getPlayerAt(const int index) const
Definition: playertablemodel.cpp:145
RELATION_CHOICE_COLUMN
static const unsigned int RELATION_CHOICE_COLUMN
Definition: playertablemodel.cpp:39
PlayerRelationsManager::setRelation
void setRelation(const std::string &name, const RelationT relation)
Definition: playerrelations.cpp:353
playertablemodel.h
Widget2
Definition: widget2.h:35
WIDGET_AT
#define WIDGET_AT(row, column)
Definition: playertablemodel.cpp:48
COLUMNS_NR
static const int COLUMNS_NR
Definition: playertablemodel.cpp:37
delete2.h
StringVect
std::vector< std::string > StringVect
Definition: stringvector.h:28
PlayerTableModel::mWidgets
std::vector< Widget * > mWidgets
Definition: playertablemodel.h:64
RelationT
Relation ::T RelationT
Definition: relation.h:38
CAST_U32
#define CAST_U32
Definition: cast.h:30
PlayerRelationsManager::getPlayers
StringVect * getPlayers() const
Definition: playerrelations.cpp:373
ROW_HEIGHT
static const unsigned int ROW_HEIGHT
Definition: playertablemodel.cpp:41
delete2
#define delete2(var)
Definition: delete2.h:24
PlayerTableModel::mListModel
PlayerRelationListModel * mListModel
Definition: playertablemodel.h:65
Modal_false
const bool Modal_false
Definition: modal.h:29
TableModel::signalAfterUpdate
virtual void signalAfterUpdate()
Definition: tablemodel.cpp:54
playerrelationlistmodel.h
NAME_COLUMN
static const int NAME_COLUMN
Definition: playertablemodel.cpp:38
nullptr
#define nullptr
Definition: localconsts.h:44
RELATION_CHOICE_COLUMN_WIDTH
static const unsigned int RELATION_CHOICE_COLUMN_WIDTH
Definition: playertablemodel.cpp:46
PlayerTableModel::getRowHeight
int getRowHeight() const A_CONST
Definition: playertablemodel.cpp:79
PlayerTableModel::updateModelInRow
void updateModelInRow(const int row) const
Definition: playertablemodel.cpp:122
DropDown::setSelected
void setSelected(int selected)
Definition: dropdown.cpp:513
PlayerTableModel::playerRelationsUpdated
void playerRelationsUpdated()
Definition: playertablemodel.cpp:91
label.h
playerRelations
PlayerRelationsManager playerRelations
Definition: playerrelations.cpp:661
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
dropdown.h
PlayerTableModel::mPlayers
StringVect * mPlayers
Definition: playertablemodel.h:63
NAME_COLUMN_WIDTH
static const unsigned int NAME_COLUMN_WIDTH
Definition: playertablemodel.cpp:45
Label
Definition: label.h:87
TableModel::signalBeforeUpdate
virtual void signalBeforeUpdate()
Definition: tablemodel.cpp:45
PlayerRelationsManager::getRelation
RelationT getRelation(const std::string &name) const
Definition: playerrelations.cpp:415
PlayerTableModel::freeWidgets
void freeWidgets()
Definition: playertablemodel.cpp:138
PlayerTableModel::getColumnWidth
int getColumnWidth(const int index) const A_CONST
Definition: playertablemodel.cpp:84
CAST_S32
#define CAST_S32
Definition: cast.h:29
PlayerTableModel::getColumns
int getColumns() const A_CONST
Definition: playertablemodel.cpp:74
new
#define new
Definition: debug_new.h:147
dtor.h
DropDown::getSelected
int getSelected() const
Definition: dropdown.cpp:508
PlayerTableModel::getElementAt
Widget * getElementAt(int row, int column) const
Definition: playertablemodel.cpp:133
PlayerTableModel::~PlayerTableModel
~PlayerTableModel()
Definition: playertablemodel.cpp:60
PlayerRelationListModel
Definition: playerrelationlistmodel.h:50
debug.h
DropDown
Definition: dropdown.h:50
playerrelations.h
Widget
Definition: widget.h:97