The Battle for Wesnoth  1.19.5+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"
22 #include "gui/widgets/label.hpp"
23 #include "gui/widgets/window.hpp"
25 
26 namespace gui2::dialogs
27 {
28 using namespace std::chrono_literals;
29 
30 REGISTER_DIALOG(network_transmission)
31 
33 {
34  if(!window_)
35  return;
36  connection_->poll();
37  if(connection_->finished()) {
38  window_->set_retval(retval::OK);
39  } else {
40  size_t completed, total;
41  completed = connection_->current();
42  total = connection_->total();
43  if(total) {
44  window_.ptr()->find_widget<progress_bar>("progress")
45  .set_percentage((completed * 100.) / total);
46 
47  std::stringstream ss;
48  ss << utils::si_string(completed, true, _("unit_byte^B")) << "/"
49  << utils::si_string(total, true, _("unit_byte^B"));
50 
51  window_.ptr()->find_widget<label>("numeric_progress")
52  .set_label(ss.str());
53  window_->invalidate_layout();
54  }
55  }
56 }
57 
59  connection_data& connection,
60  const std::string& title,
61  const std::string& subtitle)
62  : modal_dialog(window_id())
63  , connection_(&connection)
64  , pump_monitor_(connection_)
65  , subtitle_(subtitle)
66 {
67  register_label("title", true, title, false);
68 }
69 
71 {
72  // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
73  if(!subtitle_.empty()) {
74  label& subtitle_label
75  = find_widget<label>("subtitle");
76 
77  subtitle_label.set_label(subtitle_);
78  subtitle_label.set_use_markup(true);
79  }
80 
81  // NOTE: needed to avoid explicit calls to invalidate_layout()
82  // in network_transmission::pump_monitor::process()
83  find_widget<label>("numeric_progress").set_label(" ");
84  pump_monitor_.window_ = *this;
85 }
86 
88 {
89  pump_monitor_.window_ = utils::nullopt;
90 
91  if(get_retval() == retval::CANCEL) {
93  }
94 }
95 
96 } // 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.
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() override
Actions to be taken before showing the window.
virtual void post_show() override
Actions to be taken after the window has been shown.
virtual void set_label(const t_string &text)
virtual void set_use_markup(bool use_markup)
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...
REGISTER_DIALOG(editor_edit_unit)
@ 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:340
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.