The Battle for Wesnoth  1.17.21+dev
uninstall_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2023
3  by Iris Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
19 #include "gui/widgets/grid.hpp"
20 #include "gui/widgets/listbox.hpp"
21 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/window.hpp"
24 
25 #include <algorithm>
26 
27 namespace gui2::dialogs
28 {
29 
30 REGISTER_DIALOG(addon_uninstall_list)
31 
33 {
34  listbox& list = find_widget<listbox>(&window, "addons_list", false);
35  window.keyboard_capture(&list);
36 
37  this->selections_.clear();
38 
39  for(const auto & entry : titles_map_)
40  {
41  const std::string& id = entry.first;
42  const std::string& title = entry.second;
43 
44  this->ids_.push_back(id);
45  this->selections_[id] = false;
46 
48  widget_item column;
49 
50  column["label"] = title;
51  data.emplace("name", column);
52 
53  list.add_row(data);
54  }
55 }
56 
58 {
59  const listbox& list = find_widget<listbox>(&window, "addons_list", false);
60  const unsigned rows = list.get_item_count();
61 
62  assert(rows == this->ids_.size() && rows == this->titles_map_.size());
63 
64  if(!rows || get_retval() != retval::OK) {
65  return;
66  }
67 
68  for(unsigned k = 0; k < rows; ++k) {
69  grid const* g = list.get_row_grid(k);
70  const toggle_button& checkbox
71  = find_widget<const toggle_button>(g, "checkbox", false);
72  this->selections_[this->ids_[k]] = checkbox.get_value_bool();
73  }
74 }
75 
76 std::vector<std::string> addon_uninstall_list::selected_addons() const
77 {
78  std::vector<std::string> retv;
79 
80  for(const auto & entry : selections_)
81  {
82  if(entry.second) {
83  retv.push_back(entry.first);
84  }
85  }
86 
87  return retv;
88 }
89 
90 } // namespace dialogs
double g
Definition: astarsearch.cpp:65
Dialog with a checkbox list for choosing installed add-ons to remove.
std::map< std::string, bool > selections_
std::vector< std::string > selected_addons() const
std::vector< std::string > ids_
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
int get_retval() const
Returns the cached window exit code.
Base container class.
Definition: grid.hpp:32
The listbox class.
Definition: listbox.hpp:46
grid & add_row(const widget_item &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:62
const grid * get_row_grid(const unsigned row) const
Returns the grid of the wanted row.
Definition: listbox.cpp:233
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:127
Class for a toggle button.
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void keyboard_capture(widget *widget)
Definition: window.cpp:1130
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
std::string_view data
Definition: picture.cpp:199
This file contains the settings handling of the widget library.