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