The Battle for Wesnoth  1.19.5+dev
addon_list.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "addon/info.hpp"
18 #include "addon/state.hpp"
20 #include "gui/widgets/listbox.hpp"
21 #include "gui/widgets/widget.hpp"
22 
23 #include <functional>
24 #include <string>
25 #include <vector>
26 
27 namespace gui2
28 {
29 
30 namespace implementation
31 {
32  struct builder_addon_list;
33 }
34 
35 class addon_list : public container_base
36 {
38 
39 public:
40  using addon_sort_func = std::function<bool(const addon_info&, const addon_info&)>;
41 
42  explicit addon_list(const implementation::builder_addon_list& builder);
43 
44  /** Special retval for the toggle panels in the addons list */
45  static const int DEFAULT_ACTION_RETVAL = 200;
46 
47  const std::string display_title_full_shift(const addon_info& addon) const;
48 
49  /** Sets the add-ons to show. */
50  void set_addons(const addons_list& addons);
51 
52  /** Sets up a callback that will be called when the player selects an add-on. */
53  void set_modified_signal_handler(const std::function<void()>& callback)
54  {
55  connect_signal_notify_modified(get_listbox(), std::bind(callback));
56  }
57 
58  /** Returns the selected add-on. */
59  const addon_info* get_selected_addon() const;
60 
61  /** Returns the selected add-on id, for use with remote publish/delete ops. */
62  std::string get_remote_addon_id();
63 
64  /** Selects the add-on with the given ID. */
65  void select_addon(const std::string& id);
66 
67  using addon_op_func_t = std::function<void(const addon_info&)>;
68 
69  /**
70  * Helper to wrap the execution of any of the addon operation functions.
71  * It catches addons_client::user_exit exceptions and halts GUI2 event execution
72  * after calling the given function.
73  */
74  void addon_action_wrapper(addon_op_func_t& func, const addon_info& addon, bool& handled, bool& halt);
75 
76  /** Sets the function to call when the player clicks the install button. */
78  {
79  install_function_ = function;
80  }
81 
82  /** Sets the function to call when the player clicks the uninstall button. */
84  {
85  uninstall_function_ = function;
86  }
87 
88  /** Sets the function to call when the player clicks the update button. */
90  {
91  update_function_ = function;
92  }
93 
94  /** Sets the function to upload an addon to the addons server. */
96  {
97  publish_function_ = function;
98  }
99 
100  /** Sets the function to install an addon from the addons server. */
102  {
103  delete_function_ = function;
104  }
105 
106  /** Filters which add-ons are visible. 1 = visible, 0 = hidden. */
107  void set_addon_shown(boost::dynamic_bitset<>& shown)
108  {
109  get_listbox().set_row_shown(shown);
110  }
111 
112  void set_addon_order(addon_sort_func func);
113 
114  /**
115  * Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself.
116  * This function is here because the add-on list widget itself needs it.
117  */
118  static std::string colorize_addon_state_string(const std::string& str, ADDON_STATUS state, bool verbose = false);
119 
120  /** Determines if install status of each widget is shown. */
122  {
124  }
125 
126  /** Determines if install/uninstall buttons are shown for each widget. */
128  {
130  }
131 
132  /** Adds the internal listbox to the keyboard event chain. */
134 
135  /** Sets up a callback that will be called when the player changes the sorting order. */
136  void set_callback_order_change(std::function<void(unsigned, sort_order::type)> callback) {
138  }
139 
140  /** See @ref styled_widget::set_active. */
141  virtual void set_active(const bool) override
142  {
143  // DO NOTHING
144  }
145 
146  /** See @ref styled_widget::get_active. */
147  virtual bool get_active() const override
148  {
149  return true;
150  }
151 
152  /** See @ref styled_widget::get_state. */
153  virtual unsigned get_state() const override
154  {
155  return 0;
156  }
157 
158 private:
159  std::vector<const addon_info*> addon_vector_;
160 
163 
167 
170 
171  static std::string describe_status(const addon_tracking_info& info);
172 
173  /** Returns the underlying list box. */
174  listbox& get_listbox();
175 
176  void finalize_setup();
177 
178 public:
179  /** Choose the item at the top of the list (taking account of sort order). */
180  void select_first_addon();
181 
182  /** Static type getter that does not rely on the widget being constructed. */
183  static const std::string& type();
184 
185 private:
186  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
187  virtual const std::string& get_control_type() const override;
188 
189  /** See @ref container_base::set_self_active */
190  void set_self_active(const bool) override
191  {
192  // DO NOTHING
193  }
194 };
195 
197 {
198  explicit addon_list_definition(const config& cfg);
199 
201  {
202  explicit resolution(const config& cfg);
203 
205  };
206 };
207 
208 namespace implementation
209 {
210 
212 {
213 public:
214  explicit builder_addon_list(const config& cfg);
215 
217 
218  virtual std::unique_ptr<widget> build() const override;
219 
220 private:
223 };
224 }
225 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
addon_op_func_t uninstall_function_
Definition: addon_list.hpp:165
void set_uninstall_function(addon_op_func_t function)
Sets the function to call when the player clicks the uninstall button.
Definition: addon_list.hpp:83
virtual const std::string & get_control_type() const override
Inherited from styled_widget, implemented by REGISTER_WIDGET.
virtual bool get_active() const override
See styled_widget::get_active.
Definition: addon_list.hpp:147
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
Definition: addon_list.hpp:40
void set_publish_function(addon_op_func_t function)
Sets the function to upload an addon to the addons server.
Definition: addon_list.hpp:95
void set_addon_shown(boost::dynamic_bitset<> &shown)
Filters which add-ons are visible.
Definition: addon_list.hpp:107
void add_list_to_keyboard_chain()
Adds the internal listbox to the keyboard event chain.
Definition: addon_list.cpp:367
void set_install_status_visibility(visibility visibility)
Determines if install status of each widget is shown.
Definition: addon_list.hpp:121
void set_install_function(addon_op_func_t function)
Sets the function to call when the player clicks the install button.
Definition: addon_list.hpp:77
void set_delete_function(addon_op_func_t function)
Sets the function to install an addon from the addons server.
Definition: addon_list.hpp:101
void addon_action_wrapper(addon_op_func_t &func, const addon_info &addon, bool &handled, bool &halt)
Helper to wrap the execution of any of the addon operation functions.
Definition: addon_list.cpp:135
void set_install_buttons_visibility(visibility visibility)
Determines if install/uninstall buttons are shown for each widget.
Definition: addon_list.hpp:127
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
const addon_info * get_selected_addon() const
Returns the selected add-on.
Definition: addon_list.cpp:310
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: addon_list.hpp:141
void set_update_function(addon_op_func_t function)
Sets the function to call when the player clicks the update button.
Definition: addon_list.hpp:89
void set_self_active(const bool) override
See container_base::set_self_active.
Definition: addon_list.hpp:190
visibility install_buttons_visibility_
Definition: addon_list.hpp:162
void set_modified_signal_handler(const std::function< void()> &callback)
Sets up a callback that will be called when the player selects an add-on.
Definition: addon_list.hpp:53
listbox & get_listbox()
Returns the underlying list box.
Definition: addon_list.cpp:362
void set_callback_order_change(std::function< void(unsigned, sort_order::type)> callback)
Sets up a callback that will be called when the player changes the sorting order.
Definition: addon_list.hpp:136
std::string get_remote_addon_id()
Returns the selected add-on id, for use with remote publish/delete ops.
Definition: addon_list.cpp:321
void set_addon_order(addon_sort_func func)
Definition: addon_list.cpp:388
const std::string display_title_full_shift(const addon_info &addon) const
Definition: addon_list.cpp:146
static const int DEFAULT_ACTION_RETVAL
Special retval for the toggle panels in the addons list.
Definition: addon_list.hpp:45
static std::string colorize_addon_state_string(const std::string &str, ADDON_STATUS state, bool verbose=false)
Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself...
Definition: addon_list.cpp:63
static std::string describe_status(const addon_tracking_info &info)
Definition: addon_list.cpp:96
std::vector< const addon_info * > addon_vector_
Definition: addon_list.hpp:159
addon_op_func_t update_function_
Definition: addon_list.hpp:166
void select_first_addon()
Choose the item at the top of the list (taking account of sort order).
Definition: addon_list.cpp:401
addon_op_func_t delete_function_
Definition: addon_list.hpp:169
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: addon_list.hpp:153
addon_list(const implementation::builder_addon_list &builder)
Definition: addon_list.cpp:51
addon_op_func_t publish_function_
Definition: addon_list.hpp:168
visibility install_status_visibility_
Definition: addon_list.hpp:161
void select_addon(const std::string &id)
Selects the add-on with the given ID.
Definition: addon_list.cpp:331
std::function< void(const addon_info &)> addon_op_func_t
Definition: addon_list.hpp:67
void set_addons(const addons_list &addons)
Sets the add-ons to show.
Definition: addon_list.cpp:156
addon_op_func_t install_function_
Definition: addon_list.hpp:164
A generic container base class.
The listbox class.
Definition: listbox.hpp:43
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:135
void set_callback_order_change(std::function< void(unsigned, sort_order::type)> callback)
Registers a callback to be called when the active sorting option changes.
Definition: listbox.hpp:294
visibility
Visibility settings done by the user.
Definition: widget.hpp:65
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
Generic file dialog.
std::shared_ptr< builder_grid > builder_grid_ptr
Contains the implementation details for lexical_cast and shouldn't be used directly.
logger & info()
Definition: log.cpp:319
ADDON_STATUS
Defines various add-on installation statuses.
Definition: state.hpp:22
Stores additional status information about add-ons.
Definition: state.hpp:47
addon_list_definition(const config &cfg)
Definition: addon_list.cpp:410
widget::visibility install_buttons_visibility_
Definition: addon_list.hpp:222
virtual std::unique_ptr< widget > build() const override
Definition: addon_list.cpp:461
virtual std::unique_ptr< widget > build() const=0