The Battle for Wesnoth  1.17.12+dev
file_progress.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2021 by Iris Morelle <shadowm@wesnoth.org>
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 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "draw_manager.hpp"
20 #include "events.hpp"
22 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/label.hpp"
26 #include "gui/widgets/settings.hpp"
27 #include "gui/widgets/window.hpp"
28 
29 #include <chrono>
30 
31 namespace gui2::dialogs {
32 
33 REGISTER_WINDOW(file_progress)
34 
35 const std::string& file_progress::window_id() const
36 {
37  static std::string wid = "file_progress";
38  return wid;
39 }
40 
41 file_progress::file_progress(const std::string& title, const std::string& message)
42  : modeless_dialog(file_progress::window_id())
43  , title_(title)
44  , message_(message)
45  , update_time_()
46 {
47  find_widget<label>(this, "title", false).set_label(title_);
48  auto& label_widget = find_widget<label>(this, "message", false);
49 
50  label_widget.set_use_markup(true);
51  label_widget.set_label(message_);
52 
53  find_widget<button>(this, "placeholder", false).set_active(false);
54 
55  update_time_ = clock::now();
56 }
57 
58 void file_progress::update_progress(unsigned value)
59 {
60  auto* window = get_window();
61 
62  if(!window) {
63  return;
64  }
65 
66  using std::chrono::duration_cast;
67  using std::chrono::milliseconds;
68  using namespace std::chrono_literals;
69 
70  auto now = clock::now();
71  auto elapsed = duration_cast<milliseconds>(now - update_time_);
72 
73  // Update at most once per vsync.
74  if(elapsed < milliseconds(draw_manager::get_frame_length())) {
75  return;
76  }
77 
78  update_time_ = now;
79 
80  find_widget<progress_bar>(window, "progress", false).set_percentage(value);
81 
83  events::draw();
84 }
85 
86 }
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
Main class to show messages to the user.
Definition: message.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
STL namespace.
file_progress(const std::string &title, const std::string &message)
virtual void set_label(const t_string &label)
This file contains the settings handling of the widget library.
std::chrono::time_point< clock > update_time_
int get_frame_length()
Returns the length of one display frame, in milliseconds.
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:442
window * get_window()
Get the parent window.
Definition: widget.cpp:118
virtual void set_active(const bool active) override
See styled_widget::set_active.
The popup class shows windows that are shown non-modal.
void update_progress(unsigned value)
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
void draw()
Trigger a draw cycle.
Definition: events.cpp:743