The Battle for Wesnoth  1.19.7+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"
22 #include "gui/widgets/button.hpp"
24 #include "gui/widgets/label.hpp"
26 #include "gui/widgets/window.hpp"
27 
28 
29 namespace gui2::dialogs {
30 
31 REGISTER_WINDOW(file_progress)
32 
33 const std::string& file_progress::window_id() const
34 {
35  static std::string wid = "file_progress";
36  return wid;
37 }
38 
39 file_progress::file_progress(const std::string& title, const std::string& message)
40  : modeless_dialog(file_progress::window_id())
41  , title_(title)
42  , message_(message)
43  , update_time_()
44 {
45  find_widget<label>("title").set_label(title_);
46  auto& label_widget = find_widget<label>("message");
47 
48  label_widget.set_use_markup(true);
49  label_widget.set_label(message_);
50 
51  find_widget<button>("placeholder").set_active(false);
52 
53  update_time_ = clock::now();
54 }
55 
56 void file_progress::update_progress(unsigned value)
57 {
58  auto now = clock::now();
59  auto elapsed = now - update_time_;
60 
61  // Update at most once per vsync.
62  if(elapsed < draw_manager::get_frame_length()) {
63  return;
64  }
65 
66  update_time_ = now;
67 
68  find_widget<progress_bar>("progress").set_percentage(value);
69 
70  queue_redraw();
71  events::draw();
72 }
73 
74 }
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:464
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.
std::chrono::milliseconds get_frame_length()
Returns the length of one display frame, in milliseconds.
void draw()
Trigger a draw cycle.
Definition: events.cpp:721