The Battle for Wesnoth  1.17.0-dev
uninstall_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 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 
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::dialogs
27 {
28 
29 REGISTER_DIALOG(addon_uninstall_list)
30 
32 {
33  set_restore(true);
34 
35  listbox& list = find_widget<listbox>(&window, "addons_list", false);
36  window.keyboard_capture(&list);
37 
38  this->selections_.clear();
39 
40  for(const auto & entry : titles_map_)
41  {
42  const std::string& id = entry.first;
43  const std::string& title = entry.second;
44 
45  this->ids_.push_back(id);
46  this->selections_[id] = false;
47 
48  std::map<std::string, string_map> data;
49  string_map column;
50 
51  column["label"] = title;
52  data.emplace("name", column);
53 
54  list.add_row(data);
55  }
56 }
57 
59 {
60  const listbox& list = find_widget<listbox>(&window, "addons_list", false);
61  const unsigned rows = list.get_item_count();
62 
63  assert(rows == this->ids_.size() && rows == this->titles_map_.size());
64 
65  if(!rows || get_retval() != retval::OK) {
66  return;
67  }
68 
69  for(unsigned k = 0; k < rows; ++k) {
70  grid const* g = list.get_row_grid(k);
71  const toggle_button& checkbox
72  = find_widget<const toggle_button>(g, "checkbox", false);
73  this->selections_[this->ids_[k]] = checkbox.get_value_bool();
74  }
75 }
76 
77 std::vector<std::string> addon_uninstall_list::selected_addons() const
78 {
79  std::vector<std::string> retv;
80 
81  for(const auto & entry : selections_)
82  {
83  if(entry.second) {
84  retv.push_back(entry.first);
85  }
86  }
87 
88  return retv;
89 }
90 
91 } // namespace dialogs
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
The listbox class.
Definition: listbox.hpp:42
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:126
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:132
std::map< std::string, std::string > titles_map_
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:214
double g
Definition: astarsearch.cpp:63
std::map< std::string, t_string > string_map
Definition: widget.hpp:25
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:67
Dialog with a checkbox list for choosing installed add-ons to remove.
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:238
int get_retval() const
Returns the cached window exit code.
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:64
Class for a toggle button.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.