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