The Battle for Wesnoth
1.21.0-dev
Loading...
Searching...
No Matches
gui
dialogs
network_transmission.cpp
Go to the documentation of this file.
1
/*
2
Copyright (C) 2011 - 2025
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
18
#include "
gui/dialogs/network_transmission.hpp
"
19
20
#include "
gettext.hpp
"
21
#include "
gui/widgets/progress_bar.hpp
"
22
#include "
gui/widgets/label.hpp
"
23
#include "
gui/widgets/window.hpp
"
24
#include "
serialization/string_utils.hpp
"
25
26
namespace
gui2::dialogs
27
{
28
REGISTER_DIALOG
(network_transmission)
29
30
void
network_transmission
::
pump_monitor
::process()
31
{
32
if
(!window_) {
33
return
;
34
}
35
36
connection_->poll();
37
38
std::size_t completed = connection_->current();
39
std::size_t total = connection_->total();
40
41
if
(total) {
42
window_->find_widget<
progress_bar
>(
"progress"
)
43
.set_percentage(std::round((completed * 100.) / total));
44
45
auto
ss =
formatter
()
46
<<
utils::si_string
(completed,
true
,
_
(
"unit_byte^B"
)) <<
"/"
47
<<
utils::si_string
(total,
true
,
_
(
"unit_byte^B"
));
48
49
window_->find_widget<
label
>(
"numeric_progress"
)
50
.set_label(ss.str());
51
}
52
53
if
(connection_->finished()) {
54
window_->set_retval(
retval::OK
);
55
}
56
}
57
58
network_transmission::network_transmission
(
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
70
void
network_transmission::pre_show
()
71
{
72
// ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
73
if
(!
subtitle_
.empty()) {
74
label
& subtitle_label = find_widget<label>(
"subtitle"
);
75
subtitle_label.
set_label
(
subtitle_
);
76
subtitle_label.
set_use_markup
(
true
);
77
}
78
79
// NOTE: needed to avoid explicit calls to invalidate_layout()
80
// in network_transmission::pump_monitor::process()
81
find_widget<label>(
"numeric_progress"
).set_label(
" "
);
82
pump_monitor_
.
window_
=
this
;
83
}
84
85
void
network_transmission::post_show
()
86
{
87
// In case we close the dialog before transmission is complete
88
pump_monitor_
.
window_
=
nullptr
;
89
90
if
(
get_retval
() ==
retval::CANCEL
) {
91
connection_
->
cancel
();
92
}
93
}
94
95
}
// namespace dialogs
formatter
std::ostringstream wrapper.
Definition
formatter.hpp:40
gui2::dialogs::modal_dialog
Abstract base class for all modal dialogs.
Definition
modal_dialog.hpp:146
gui2::dialogs::modal_dialog::register_label
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.
Definition
modal_dialog.cpp:192
gui2::dialogs::network_transmission::connection_data
A wrapper of either a wesnothd_connection or a network_asio::connection.
Definition
network_transmission.hpp:36
gui2::dialogs::network_transmission::connection_data::cancel
virtual void cancel()=0
gui2::dialogs::network_transmission::pump_monitor
Definition
network_transmission.hpp:50
gui2::dialogs::network_transmission::pump_monitor::window_
window * window_
Definition
network_transmission.hpp:60
gui2::dialogs::network_transmission
Dialog that tracks network transmissions.
Definition
network_transmission.hpp:32
gui2::dialogs::network_transmission::subtitle_
std::string subtitle_
The subtitle for the dialog.
Definition
network_transmission.hpp:80
gui2::dialogs::network_transmission::network_transmission
network_transmission(connection_data &connection, const std::string &title, const std::string &subtitle)
Definition
network_transmission.cpp:58
gui2::dialogs::network_transmission::pump_monitor_
gui2::dialogs::network_transmission::pump_monitor pump_monitor_
gui2::dialogs::network_transmission::pre_show
virtual void pre_show() override
Actions to be taken before showing the window.
Definition
network_transmission.cpp:70
gui2::dialogs::network_transmission::connection_
connection_data * connection_
Definition
network_transmission.hpp:47
gui2::dialogs::network_transmission::post_show
virtual void post_show() override
Actions to be taken after the window has been shown.
Definition
network_transmission.cpp:85
gui2::label
Definition
label.hpp:32
gui2::progress_bar
Definition
progress_bar.hpp:32
gui2::styled_widget::set_label
virtual void set_label(const t_string &text)
Definition
styled_widget.cpp:309
gui2::styled_widget::set_use_markup
virtual void set_use_markup(bool use_markup)
Definition
styled_widget.cpp:330
gui2::window::get_retval
int get_retval()
Definition
window.hpp:394
gettext.hpp
_
static std::string _(const char *str)
Definition
gettext.hpp:100
label.hpp
window.hpp
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
Definition
modal_dialog.hpp:89
gui2::dialogs
Definition
achievements_dialog.cpp:35
gui2::OK
@ OK
Dialog was closed with the OK button.
Definition
retval.hpp:35
gui2::CANCEL
@ CANCEL
Dialog was closed with the CANCEL button.
Definition
retval.hpp:38
utils::si_string
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
Definition
string_utils.cpp:551
network_transmission.hpp
progress_bar.hpp
string_utils.hpp
Generated by
1.9.8