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