ManaPlus
setup_relations.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 "actormanager.h"
27 
28 #include "being/localplayer.h"
29 
32 
33 #include "gui/widgets/button.h"
34 #include "gui/widgets/checkbox.h"
36 #include "gui/widgets/dropdown.h"
37 #include "gui/widgets/label.h"
39 #include "gui/widgets/scrollarea.h"
40 #include "gui/widgets/guitable.h"
41 
42 #include "utils/delete2.h"
43 
44 #include "debug.h"
45 
46 static const int COLUMNS_NR = 2; // name plus listbox
47 static const int NAME_COLUMN = 0;
48 static const unsigned int NAME_COLUMN_WIDTH = 230;
49 static const unsigned int RELATION_CHOICE_COLUMN = 1;
50 static const unsigned int RELATION_CHOICE_COLUMN_WIDTH = 80;
51 
52 static const std::string ACTION_DELETE("delete");
53 static const std::string ACTION_TABLE("table");
54 static const std::string ACTION_STRATEGY("strategy");
55 
56 static const char *const table_titles[COLUMNS_NR] =
57 {
58  // TRANSLATORS: relations table header
59  N_("Name"),
60  // TRANSLATORS: relations table header
61  N_("Relation")
62 };
63 
65  SetupTab(widget),
67  mPlayerTableTitleModel(new StaticTableModel(1, COLUMNS_NR)),
68  mPlayerTableModel(new PlayerTableModel(this)),
69  mPlayerTable(new GuiTable(this, mPlayerTableModel, Opaque_true)),
70  mPlayerTitleTable(new GuiTable(this, mPlayerTableTitleModel, Opaque_true)),
71  mPlayerScrollArea(new ScrollArea(this,
72  mPlayerTable, Opaque_true, std::string())),
73  // TRANSLATORS: relation dialog button
74  mDefaultTrading(new CheckBox(this, _("Allow trading"),
76  nullptr, std::string())),
77  // TRANSLATORS: relation dialog button
78  mDefaultWhisper(new CheckBox(this, _("Allow whispers"),
80  nullptr, std::string())),
81  // TRANSLATORS: relation dialog button
82  mDeleteButton(new Button(this, _("Delete"), ACTION_DELETE,
83  BUTTON_SKIN, this)),
84  mIgnoreActionChoicesModel(new IgnoreChoicesListModel),
85  mIgnoreActionChoicesBox(new DropDown(widget, mIgnoreActionChoicesModel,
86  false, Modal_false, nullptr, std::string()))
87 {
88  // TRANSLATORS: relation dialog name
89  setName(_("Relations"));
90 
92 
97  ThemeColorId::TABLE_BACKGROUND, 255U));
99 
100  for (int i = 0; i < COLUMNS_NR; i++)
101  {
102  mPlayerTableTitleModel->set(0, i, new Label(
103  this, gettext(table_titles[i])));
104  }
105 
107 
112 
113  // TRANSLATORS: relation dialog label
114  Label *const ignore_action_label = new Label(this, _("When ignoring:"));
115 
118 
119  int ignore_strategy_index = 0; // safe default
120 
121  if (playerRelations.getPlayerIgnoreStrategy() != nullptr)
122  {
123  ignore_strategy_index = playerRelations.getPlayerIgnoreStrategyIndex(
125  if (ignore_strategy_index < 0)
126  ignore_strategy_index = 0;
127  }
128  mIgnoreActionChoicesBox->setSelected(ignore_strategy_index);
130 
131  reset();
132 
133  // Do the layout
134  LayoutHelper h(this);
135  ContainerPlacer place = h.getPlacer(0, 0);
136 
137  place(0, 0, mPlayerTitleTable, 6, 1);
138  place(0, 1, mPlayerScrollArea, 6, 4).setPadding(2);
139  place(0, 5, mDeleteButton, 1, 1);
140  place(3, 5, ignore_action_label, 1, 1);
141  place(4, 5, mIgnoreActionChoicesBox, 2, 1).setPadding(2);
142  place(3, 6, mDefaultTrading, 3, 1);
143  place(3, 7, mDefaultWhisper, 3, 1);
144 
146 
147  setDimension(Rect(0, 0, 500, 350));
148 }
149 
151 {
154 }
155 
156 
158 {
159  // We now have to search through the list of ignore choices to find the
160  // current selection. We could use an index into the table of config
161  // options in playerRelations instead of strategies to sidestep this.
162  int selection = 0;
163  for (size_t i = 0, sz = playerRelations.getPlayerIgnoreStrategies()
164  ->size(); i < sz; ++ i)
165  {
168  {
169  selection = CAST_S32(i);
170  break;
171  }
172  }
174 }
175 
177 {
179 
180  const unsigned int old_default_relations = playerRelations.getDefault() &
182  playerRelations.setDefault(old_default_relations
185 
186  if (actorManager != nullptr)
188 
189  if (localPlayer != nullptr)
191 }
192 
194 {
195 }
196 
198 {
199  const std::string &eventId = event.getId();
200  if (eventId == ACTION_TABLE)
201  {
202  // temporarily eliminate ourselves: we are fully aware of this change,
203  // so there is no need for asynchronous updates. (In fact, thouse
204  // might destroy the widet that triggered them, which would be rather
205  // embarrassing.)
207 
208  const int row = mPlayerTable->getSelectedRow();
209  if (row >= 0)
211 
213  }
214  else if (eventId == ACTION_DELETE)
215  {
216  const int player_index = mPlayerTable->getSelectedRow();
217 
218  if (player_index < 0)
219  return;
220 
222  player_index));
223  }
224  else if (eventId == ACTION_STRATEGY)
225  {
226  const int sel = mIgnoreActionChoicesBox->getSelected();
227  if (sel < 0)
228  return;
229  PlayerIgnoreStrategy *const s =
231 
233  }
234 }
235 
236 void Setup_Relations::updatedPlayer(const std::string &name A_UNUSED)
237 {
243  if (localPlayer != nullptr)
245 }
246 
248 {
249  PlayerTableModel *const model = new PlayerTableModel(this);
250  mPlayerTable->setModel(model);
251  delete mPlayerTableModel;
252  mPlayerTableModel = model;
253  int ignore_strategy_index = 0; // safe default
254 
255  if (playerRelations.getPlayerIgnoreStrategy() != nullptr)
256  {
257  ignore_strategy_index = playerRelations.getPlayerIgnoreStrategyIndex(
259  if (ignore_strategy_index < 0)
260  ignore_strategy_index = 0;
261  }
262  mIgnoreActionChoicesBox->setSelected(ignore_strategy_index);
264  reset();
265 }
267 {
272 }
ActorManager * actorManager
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
void updatePlayerNames() const
void updateName()
Definition: being.cpp:3425
Definition: button.h:102
void setSelected(const bool selected)
Definition: checkbox.h:156
bool isSelected() const
Definition: checkbox.h:147
int getSelected() const
Definition: dropdown.cpp:509
void adjustHeight()
Definition: dropdown.cpp:470
void setSelected(int selected)
Definition: dropdown.cpp:514
void setSelectableGui(bool b)
Definition: guitable.h:163
void setModel(TableModel *const m)
Definition: guitable.cpp:85
int getSelectedRow() const
Definition: guitable.cpp:131
void setOpaque(Opaque opaque)
Definition: guitable.h:137
void setLinewiseSelection(bool linewise)
Definition: guitable.h:112
Definition: label.h:91
ContainerPlacer getPlacer(const int x, const int y)
void setCheckNameSetting(const bool checked)
Definition: localplayer.h:349
void setDefault(const unsigned int permissions)
void setPlayerIgnoreStrategy(PlayerIgnoreStrategy *const strategy)
void addListener(PlayerRelationsListener *const listener)
void removeListener(PlayerRelationsListener *const listener)
std::vector< PlayerIgnoreStrategy * > * getPlayerIgnoreStrategies()
unsigned int getDefault() const
int getPlayerIgnoreStrategyIndex(const std::string &shortname)
const PlayerIgnoreStrategy * getPlayerIgnoreStrategy() const
void removePlayer(const std::string &name)
void updateModelInRow(const int row) const
std::string getPlayerAt(const int index) const
Definition: rect.h:74
void setHorizontalScrollPolicy(const ScrollPolicy hPolicy)
void setName(const std::string &name)
Definition: setuptab.h:68
void updatedPlayer(const std::string &name)
ListModel * mIgnoreActionChoicesModel
CheckBox * mDefaultTrading
GuiTable * mPlayerTable
ScrollArea * mPlayerScrollArea
void action(const ActionEvent &event)
DropDown * mIgnoreActionChoicesBox
Button * mDeleteButton
CheckBox * mDefaultWhisper
Setup_Relations(const Widget2 *const widget)
PlayerTableModel * mPlayerTableModel
StaticTableModel * mPlayerTableTitleModel
GuiTable * mPlayerTitleTable
void set(const int row, const int column, Widget *const widget)
Definition: tablemodel.cpp:94
void fixColumnWidth(const int column, const int width)
Definition: tablemodel.cpp:131
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setBackgroundColor(const Color &color)
Definition: widget.h:425
void setActionEventId(const std::string &actionEventId)
Definition: widget.h:596
void setDimension(const Rect &dimension)
Definition: widget.cpp:169
void addActionListener(ActionListener *const actionListener)
Definition: widget.cpp:252
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define N_(s)
Definition: gettext.h:36
#define _(s)
Definition: gettext.h:35
#define nullptr
Definition: localconsts.h:45
#define A_UNUSED
Definition: localconsts.h:160
LocalPlayer * localPlayer
const bool Modal_false
Definition: modal.h:30
const bool Opaque_false
Definition: opaque.h:30
const bool Opaque_true
Definition: opaque.h:30
PlayerRelationsManager playerRelations
static RenderType getDefault() A_CONST
Definition: renderers.cpp:28
static const unsigned int NAME_COLUMN_WIDTH
static const std::string ACTION_STRATEGY("strategy")
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
static const std::string ACTION_TABLE("table")
static const std::string ACTION_DELETE("delete")
static const char *const table_titles[COLUMNS_NR]
static const unsigned int WHISPER
static const unsigned int TRADE