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