The Battle for Wesnoth  1.15.1+dev
uninstall_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Iris Morelle <shadowm2006@gmail.com>
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 
16 
18 #include "gui/widgets/grid.hpp"
19 #include "gui/widgets/listbox.hpp"
20 #include "gui/widgets/settings.hpp"
22 #include "gui/widgets/window.hpp"
23 
24 #include <algorithm>
25 
26 namespace gui2
27 {
28 namespace dialogs
29 {
30 
31 /*WIKI
32  * @page = GUIWindowDefinitionWML
33  * @order = 2_addon_uninstall_list
34  *
35  * == Add-on uninstall list ==
36  *
37  * Dialog with a checkbox list for choosing installed add-ons to remove.
38  *
39  * @begin{table}{dialog_widgets}
40  *
41  * addons_list & & listbox & m &
42  * A listbox containing add-on selection entries. $
43  *
44  * -checkbox & & toggle_button & m &
45  * A toggle button allowing the user to mark/unmark the add-on. $
46  *
47  * -name & & styled_widget & o &
48  * The name of the add-on. $
49  *
50  * @end{table}
51  */
52 
53 REGISTER_DIALOG(addon_uninstall_list)
54 
56 {
57  set_restore(true);
58 
59  listbox& list = find_widget<listbox>(&window, "addons_list", false);
60  window.keyboard_capture(&list);
61 
62  this->selections_.clear();
63 
64  for(const auto & entry : titles_map_)
65  {
66  const std::string& id = entry.first;
67  const std::string& title = entry.second;
68 
69  this->ids_.push_back(id);
70  this->selections_[id] = false;
71 
72  std::map<std::string, string_map> data;
73  string_map column;
74 
75  column["label"] = title;
76  data.emplace("name", column);
77 
78  list.add_row(data);
79  }
80 }
81 
83 {
84  const listbox& list = find_widget<listbox>(&window, "addons_list", false);
85  const unsigned rows = list.get_item_count();
86 
87  assert(rows == this->ids_.size() && rows == this->titles_map_.size());
88 
89  if(!rows || get_retval() != retval::OK) {
90  return;
91  }
92 
93  for(unsigned k = 0; k < rows; ++k) {
94  grid const* g = list.get_row_grid(k);
95  const toggle_button& checkbox
96  = find_widget<const toggle_button>(g, "checkbox", false);
97  this->selections_[this->ids_[k]] = checkbox.get_value_bool();
98  }
99 }
100 
101 std::vector<std::string> addon_uninstall_list::selected_addons() const
102 {
103  std::vector<std::string> retv;
104 
105  for(const auto & entry : selections_)
106  {
107  if(entry.second) {
108  retv.push_back(entry.first);
109  }
110  }
111 
112  return retv;
113 }
114 
115 } // namespace dialogs
116 } // namespace gui2
This file contains the window object, this object is a top level container which has the event manage...
std::vector< std::string > selected_addons() const
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
Base container class.
Definition: grid.hpp:30
std::vector< std::string > ids_
This file contains the settings handling of the widget library.
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:125
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:131
std::map< std::string, std::string > titles_map_
Various uncategorised dialogs.
double g
Definition: astarsearch.cpp:64
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:66
std::map< std::string, bool > selections_
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:237
Dialog was closed with the OK button.
Definition: retval.hpp:34
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
Class for a toggle button.
virtual void post_show(window &window) override
Inherited from modal_dialog.