The Battle for Wesnoth  1.17.0-dev
addon_list.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2021
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. */
123  void set_install_status_visibility(visibility visibility)
124  {
125  install_status_visibility_ = visibility;
126  }
127 
128  /** Determines if install/uninstall buttons are shown for each widget. */
129  void set_install_buttons_visibility(visibility visibility)
130  {
131  install_buttons_visibility_ = visibility;
132  }
133 
134  /** Adds the internal listbox to the keyboard event chain. */
135  void add_list_to_keyboard_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, preferences::SORT_ORDER)> callback) {
139  get_listbox().set_callback_order_change(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 widget* build() const override;
221 
222 private:
225 };
226 }
227 }
Base class of a resolution, contains the common keys for a resolution.
visibility install_status_visibility_
Definition: addon_list.hpp:163
virtual widget * build() const =0
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
Definition: addon_list.hpp:42
preferences::SORT_ORDER SORT_ORDER
Definition: listbox.cpp:42
logger & info()
Definition: log.cpp:89
ADDON_STATUS
Defines various add-on installation statuses.
Definition: state.hpp:22
Base class for all widgets.
Definition: widget.hpp:49
void set_addon_shown(boost::dynamic_bitset<> &shown)
Filters which add-ons are visible.
Definition: addon_list.hpp:109
addon_op_func_t delete_function_
Definition: addon_list.hpp:171
void set_install_status_visibility(visibility visibility)
Determines if install status of each widget is shown.
Definition: addon_list.hpp:123
void set_callback_order_change(std::function< void(unsigned, preferences::SORT_ORDER)> callback)
Sets up a callback that will be called when the player changes the sorting order. ...
Definition: addon_list.hpp:138
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: addon_list.hpp:155
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: addon_list.hpp:143
Generic file dialog.
Definition: field-fwd.hpp:23
The listbox class.
Definition: listbox.hpp:43
visibility install_buttons_visibility_
Definition: addon_list.hpp:164
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 connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:187
std::vector< const addon_info * > addon_vector_
Definition: addon_list.hpp:161
virtual bool get_active() const override
See styled_widget::get_active.
Definition: addon_list.hpp:149
widget::visibility install_buttons_visibility_
Definition: addon_list.hpp:224
void set_self_active(const bool) override
See container_base::set_self_active.
Definition: addon_list.hpp:192
addon_op_func_t uninstall_function_
Definition: addon_list.hpp:167
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_update_function(addon_op_func_t function)
Sets the function to call when the player clicks the update button.
Definition: addon_list.hpp:91
visibility
Visibility settings done by the user.
Definition: widget.hpp:59
A generic container base class.
addon_op_func_t publish_function_
Definition: addon_list.hpp:170
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
addon_op_func_t install_function_
Definition: addon_list.hpp:166
void set_install_buttons_visibility(visibility visibility)
Determines if install/uninstall buttons are shown for each widget.
Definition: addon_list.hpp:129
point resolution()
Definition: general.cpp:393
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
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
Stores additional status information about add-ons.
Definition: state.hpp:46
std::function< void(const addon_info &)> addon_op_func_t
Definition: addon_list.hpp:69
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::map< std::string, addon_info > addons_list
Definition: info.hpp:28
std::shared_ptr< builder_grid > builder_grid_ptr
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
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
addon_op_func_t update_function_
Definition: addon_list.hpp:168