The Battle for Wesnoth  1.19.0-dev
network_transmission.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
3  by Sergey Popov <loonycyborg@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 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gettext.hpp"
23 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/window.hpp"
26 
27 namespace gui2::dialogs
28 {
29 using namespace std::chrono_literals;
30 
31 REGISTER_DIALOG(network_transmission)
32 
34 {
35  if(!window_)
36  return;
37  connection_->poll();
38  if(connection_->finished()) {
39  window_->set_retval(retval::OK);
40  } else {
41  size_t completed, total;
42  completed = connection_->current();
43  total = connection_->total();
44  if(total) {
45  find_widget<progress_bar>(window_.ptr(), "progress", false)
46  .set_percentage((completed * 100.) / total);
47 
48  std::stringstream ss;
49  ss << utils::si_string(completed, true, _("unit_byte^B")) << "/"
50  << utils::si_string(total, true, _("unit_byte^B"));
51 
52  find_widget<label>(window_.ptr(), "numeric_progress", false)
53  .set_label(ss.str());
54  window_->invalidate_layout();
55  }
56  }
57 }
58 
60  connection_data& connection,
61  const std::string& title,
62  const std::string& subtitle)
63  : modal_dialog(window_id())
64  , connection_(&connection)
65  , pump_monitor_(connection_)
66  , subtitle_(subtitle)
67 {
68  register_label("title", true, title, false);
69 }
70 
72 {
73  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
74  if(!subtitle_.empty()) {
75  label& subtitle_label
76  = find_widget<label>(&window, "subtitle", false);
77 
78  subtitle_label.set_label(subtitle_);
79  subtitle_label.set_use_markup(true);
80  }
81 
82  // NOTE: needed to avoid explicit calls to invalidate_layout()
83  // in network_transmission::pump_monitor::process()
84  find_widget<label>(&window, "numeric_progress", false).set_label(" ");
86 }
87 
89 {
90  pump_monitor_.window_ = std::nullopt;
91 
92  if(get_retval() == retval::CANCEL) {
94  }
95 }
96 
97 } // namespace dialogs
Abstract base class for all modal dialogs.
field_label * register_label(const std::string &id, const bool mandatory, const std::string &text, const bool use_markup=false)
Registers a new styled_widget as a label.
int get_retval() const
Returns the cached window exit code.
A wrapper of either a wesnothd_connection or a network_asio::connection.
Dialog that tracks network transmissions.
std::string subtitle_
The subtitle for the dialog.
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
network_transmission(connection_data &connection, const std::string &title, const std::string &subtitle)
gui2::dialogs::network_transmission::pump_monitor pump_monitor_
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
A label displays text that can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
virtual void set_label(const t_string &text)
virtual void set_use_markup(bool use_markup)
friend class window
Definition: widget.hpp:55
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
Handling of system events.
REGISTER_DIALOG(tod_new_schedule)
@ OK
Dialog was closed with the OK button.
Definition: retval.hpp:35
@ CANCEL
Dialog was closed with the CANCEL button.
Definition: retval.hpp:38
void process(int mousex, int mousey)
Definition: tooltips.cpp:278
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.