The Battle for Wesnoth  1.17.21+dev
addon_list.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2023
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/manager.hpp"
19 #include "addon/state.hpp"
21 #include "gui/widgets/listbox.hpp"
22 #include "gui/widgets/widget.hpp"
23 
24 #include <boost/dynamic_bitset.hpp>
25 #include <functional>
26 #include <string>
27 #include <vector>
28 
29 namespace gui2
30 {
31 
32 namespace implementation
33 {
34  struct builder_addon_list;
35 }
36 
37 class addon_list : public container_base
38 {
40 
41 public:
42  using addon_sort_func = std::function<bool(const addon_info&, const addon_info&)>;
43 
44  explicit addon_list(const implementation::builder_addon_list& builder);
45 
46  /** Special retval for the toggle panels in the addons list */
47  static const int DEFAULT_ACTION_RETVAL = 200;
48 
49  const std::string display_title_full_shift(const addon_info& addon) const;
50 
51  /** Sets the add-ons to show. */
52  void set_addons(const addons_list& addons);
53 
54  /** Sets up a callback that will be called when the player selects an add-on. */
55  void set_modified_signal_handler(const std::function<void()>& callback)
56  {
57  connect_signal_notify_modified(get_listbox(), std::bind(callback));
58  }
59 
60  /** Returns the selected add-on. */
61  const addon_info* get_selected_addon() const;
62 
63  /** Returns the selected add-on id, for use with remote publish/delete ops. */
64  std::string get_remote_addon_id();
65 
66  /** Selects the add-on with the given ID. */
67  void select_addon(const std::string& id);
68 
69  using addon_op_func_t = std::function<void(const addon_info&)>;
70 
71  /**
72  * Helper to wrap the execution of any of the addon operation functions.
73  * It catches addons_client::user_exit exceptions and halts GUI2 event execution
74  * after calling the given function.
75  */
76  void addon_action_wrapper(addon_op_func_t& func, const addon_info& addon, bool& handled, bool& halt);
77 
78  /** Sets the function to call when the player clicks the install button. */
80  {
81  install_function_ = function;
82  }
83 
84  /** Sets the function to call when the player clicks the uninstall button. */
86  {
87  uninstall_function_ = function;
88  }
89 
90  /** Sets the function to call when the player clicks the update button. */
92  {
93  update_function_ = function;
94  }
95 
96  /** Sets the function to upload an addon to the addons server. */
98  {
99  publish_function_ = function;
100  }
101 
102  /** Sets the function to install an addon from the addons server. */
104  {
105  delete_function_ = function;
106  }
107 
108  /** Filters which add-ons are visible. 1 = visible, 0 = hidden. */
109  void set_addon_shown(boost::dynamic_bitset<>& shown)
110  {
111  get_listbox().set_row_shown(shown);
112  }
113 
114  void set_addon_order(addon_sort_func func);
115 
116  /**
117  * Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself.
118  * This function is here because the add-on list widget itself needs it.
119  */
120  static std::string colorize_addon_state_string(const std::string& str, ADDON_STATUS state, bool verbose = false);
121 
122  /** Determines if install status of each widget is shown. */
124  {
126  }
127 
128  /** Determines if install/uninstall buttons are shown for each widget. */
130  {
132  }
133 
134  /** Adds the internal listbox to the keyboard event chain. */
136 
137  /** Sets up a callback that will be called when the player changes the sorting order. */
138  void set_callback_order_change(std::function<void(unsigned, sort_order::type)> callback) {
140  }
141 
142  /** See @ref styled_widget::set_active. */
143  virtual void set_active(const bool) override
144  {
145  // DO NOTHING
146  }
147 
148  /** See @ref styled_widget::get_active. */
149  virtual bool get_active() const override
150  {
151  return true;
152  }
153 
154  /** See @ref styled_widget::get_state. */
155  virtual unsigned get_state() const override
156  {
157  return 0;
158  }
159 
160 private:
161  std::vector<const addon_info*> addon_vector_;
162 
165 
169 
172 
173  static std::string describe_status(const addon_tracking_info& info);
174 
175  /** Returns the underlying list box. */
176  listbox& get_listbox();
177 
178  void finalize_setup();
179 
180 public:
181  /** Choose the item at the top of the list (taking account of sort order). */
182  void select_first_addon();
183 
184  /** Static type getter that does not rely on the widget being constructed. */
185  static const std::string& type();
186 
187 private:
188  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
189  virtual const std::string& get_control_type() const override;
190 
191  /** See @ref container_base::set_self_active */
192  void set_self_active(const bool) override
193  {
194  // DO NOTHING
195  }
196 };
197 
199 {
200  explicit addon_list_definition(const config& cfg);
201 
203  {
204  explicit resolution(const config& cfg);
205 
207  };
208 };
209 
210 namespace implementation
211 {
212 
214 {
215 public:
216  explicit builder_addon_list(const config& cfg);
217 
219 
220  virtual std::unique_ptr<widget> build() const override;
221 
222 private:
225 };
226 }
227 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
addon_op_func_t uninstall_function_
Definition: addon_list.hpp:167
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:85
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:149
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
Definition: addon_list.hpp:42
void set_publish_function(addon_op_func_t function)
Sets the function to upload an addon to the addons server.
Definition: addon_list.hpp:97
void set_addon_shown(boost::dynamic_bitset<> &shown)
Filters which add-ons are visible.
Definition: addon_list.hpp:109
void add_list_to_keyboard_chain()
Adds the internal listbox to the keyboard event chain.
Definition: addon_list.cpp:372
void set_install_status_visibility(visibility visibility)
Determines if install status of each widget is shown.
Definition: addon_list.hpp:123
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:79
void set_delete_function(addon_op_func_t function)
Sets the function to install an addon from the addons server.
Definition: addon_list.hpp:103
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:137
void set_install_buttons_visibility(visibility visibility)
Determines if install/uninstall buttons are shown for each widget.
Definition: addon_list.hpp:129
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:315
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: addon_list.hpp:143
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:91
void set_self_active(const bool) override
See container_base::set_self_active.
Definition: addon_list.hpp:192
visibility install_buttons_visibility_
Definition: addon_list.hpp:164
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:55
listbox & get_listbox()
Returns the underlying list box.
Definition: addon_list.cpp:367
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:138
std::string get_remote_addon_id()
Returns the selected add-on id, for use with remote publish/delete ops.
Definition: addon_list.cpp:326
void set_addon_order(addon_sort_func func)
Definition: addon_list.cpp:393
const std::string display_title_full_shift(const addon_info &addon) const
Definition: addon_list.cpp:148
static const int DEFAULT_ACTION_RETVAL
Special retval for the toggle panels in the addons list.
Definition: addon_list.hpp:47
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:65
static std::string describe_status(const addon_tracking_info &info)
Definition: addon_list.cpp:98
std::vector< const addon_info * > addon_vector_
Definition: addon_list.hpp:161
addon_op_func_t update_function_
Definition: addon_list.hpp:168
void select_first_addon()
Choose the item at the top of the list (taking account of sort order).
Definition: addon_list.cpp:406
addon_op_func_t delete_function_
Definition: addon_list.hpp:171
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: addon_list.hpp:155
addon_list(const implementation::builder_addon_list &builder)
Definition: addon_list.cpp:53
addon_op_func_t publish_function_
Definition: addon_list.hpp:170
visibility install_status_visibility_
Definition: addon_list.hpp:163
void select_addon(const std::string &id)
Selects the add-on with the given ID.
Definition: addon_list.cpp:336
std::function< void(const addon_info &)> addon_op_func_t
Definition: addon_list.hpp:69
void set_addons(const addons_list &addons)
Sets the add-ons to show.
Definition: addon_list.cpp:158
addon_op_func_t install_function_
Definition: addon_list.hpp:166
A generic container base class.
The listbox class.
Definition: listbox.hpp:46
void set_row_shown(const unsigned row, const bool shown)
Makes a row visible or invisible.
Definition: listbox.cpp:139
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:297
visibility
Visibility settings done by the user.
Definition: widget.hpp:64
std::map< std::string, addon_info > addons_list
Definition: info.hpp:28
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:205
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:238
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:415
widget::visibility install_buttons_visibility_
Definition: addon_list.hpp:224
virtual std::unique_ptr< widget > build() const override
Definition: addon_list.cpp:466
virtual std::unique_ptr< widget > build() const=0
Base class of a resolution, contains the common keys for a resolution.