The Battle for Wesnoth  1.17.10+dev
screenshot_notification.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2022
3  by Iris Morelle <shadowm2006@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 "desktop/clipboard.hpp"
21 #include "desktop/open.hpp"
22 #include "filesystem.hpp"
23 #include "gettext.hpp"
26 #include "gui/dialogs/message.hpp"
27 #include "gui/widgets/button.hpp"
28 #include "gui/widgets/label.hpp"
29 #include "gui/widgets/text_box.hpp"
30 #include "gui/widgets/window.hpp"
31 #include "picture.hpp"
32 
33 #include <boost/filesystem/path.hpp>
34 
35 #include <functional>
36 #include <stdexcept>
37 
38 namespace gui2::dialogs
39 {
40 
41 REGISTER_DIALOG(screenshot_notification)
42 
44  : modal_dialog(window_id())
45  , path_(path)
46  , screenshots_dir_path_(filesystem::get_screenshot_dir())
47  , screenshot_(screenshot)
48 {
49 }
50 
52 {
53  window.set_enter_disabled(true);
54 
55  text_box& path_box = find_widget<text_box>(&window, "path", false);
57  path_box.set_selection(0, path_box.text().find_last_of('.')); // TODO: do this cleaner!
58  window.keyboard_capture(&path_box);
60  std::placeholders::_3, std::placeholders::_5));
61 
62  find_widget<label>(&window, "filesize", false).set_label(font::unicode_em_dash);
63 
64  button& copy_b = find_widget<button>(&window, "copy", false);
66  copy_b, std::bind(&desktop::clipboard::copy_to_clipboard, std::ref(path_), false));
67  copy_b.set_active(false);
68 
70  copy_b.set_tooltip(_("Clipboard support not found, contact your packager"));
71  }
72 
73  button& open_b = find_widget<button>(&window, "open", false);
75  open_b, std::bind(&desktop::open_object, std::ref(path_)));
76  open_b.set_active(false);
77 
78  button& bdir_b = find_widget<button>(&window, "browse_dir", false);
80  bdir_b,
81  std::bind(&desktop::open_object,
82  std::ref(screenshots_dir_path_)));
83 
84  button& save_b = find_widget<button>(&window, "save", false);
86 }
87 
89 {
90  text_box& path_box = find_widget<text_box>(get_window(), "path", false);
91  std::string filename = path_box.get_value();
93  path /= filename;
94 
95  path_ = path.string();
96 
97  image::save_result res = image::save_image(screenshot_, path.string());
99  gui2::show_error_message(_("Unsupported image format.\n\n"
100  "Try to save the screenshot as PNG instead."));
101  } else if(res == image::save_result::save_failed) {
103  translation::dsgettext("wesnoth", "Screenshot creation failed.\n\n"
104  "Make sure there is enough space on the drive holding Wesnoth’s player resource files and that file permissions are set up correctly."));
105  } else if(res != image::save_result::success) {
106  throw std::logic_error("Unexpected error while trying to save a screenshot");
107  } else {
108  path_box.set_active(false);
109  find_widget<button>(get_window(), "open", false).set_active(true);
110  find_widget<button>(get_window(), "save", false).set_active(false);
111 
113  find_widget<button>(get_window(), "copy", false).set_active(true);
114  }
115 
116  const int filesize = filesystem::file_size(path_);
117  const std::string sizetext = utils::si_string(filesize, true, _("unit_byte^B"));
118  find_widget<label>(get_window(), "filesize", false).set_label(sizetext);
119  }
120 }
121 
122 void screenshot_notification::keypress_callback(bool& handled, SDL_Keycode key)
123 {
124  if(key == SDLK_RETURN || key == SDLK_KP_ENTER) {
125  save_screenshot();
126  handled = true;
127  }
128 }
129 
130 } // namespace dialogs
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
void set_selection(std::size_t start, int length)
Sets or clears the text selection.
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:55
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
save_result
Definition: picture.hpp:338
std::string get_value() const
This file contains the window object, this object is a top level container which has the event manage...
std::string get_screenshot_dir()
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: picture.cpp:1092
STL namespace.
static std::string _(const char *str)
Definition: gettext.hpp:93
Notification dialog used after saving a game or map screenshot to display information about it for th...
std::string dsgettext(const char *domainname, const char *msgid)
Definition: gettext.cpp:436
Class for a single line text area.
Definition: text_box.hpp:141
virtual void set_label(const t_string &label)
Desktop environment interaction functions.
void set_tooltip(const t_string &tooltip)
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
std::string path
Definition: game_config.cpp:39
window * get_window()
Returns a pointer to the dialog&#39;s window.
bool open_object([[maybe_unused]] const std::string &path_or_url)
Definition: open.cpp:47
std::string path_
The display function.
Declarations for File-IO.
virtual void set_active(const bool active) override
See styled_widget::set_active.
std::string base_name(const std::string &file, const bool remove_extension)
Returns the base filename of a file, with directory name stripped.
int file_size(const std::string &fname)
Returns the size of a file, or -1 if the file doesn&#39;t exist.
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
void copy_to_clipboard(const std::string &text, const bool)
Copies text to the clipboard.
Definition: clipboard.cpp:34
const std::string & text() const
const std::string unicode_em_dash
Definition: constants.cpp:44
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Abstract base class for all modal dialogs.
Simple push button.
Definition: button.hpp:36
void connect_signal_pre_key_press(dispatcher &dispatcher, const signal_keyboard &signal)
Connects the signal for &#39;snooping&#39; on the keypress.
Definition: dispatcher.cpp:174
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:204
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
virtual void set_active(const bool active) override
See styled_widget::set_active.
void keypress_callback(bool &handled, const SDL_Keycode key)
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:286