The Battle for Wesnoth  1.19.7+dev
screenshot_notification.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2024
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"
25 #include "gui/dialogs/message.hpp"
26 #include "gui/widgets/button.hpp"
27 #include "gui/widgets/label.hpp"
28 #include "gui/widgets/text_box.hpp"
29 #include "gui/widgets/window.hpp"
30 #include "picture.hpp"
31 
32 #include <boost/filesystem/path.hpp>
33 
34 #include <functional>
35 #include <stdexcept>
36 #include <utility>
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_(std::move(screenshot))
48 {
49 }
50 
52 {
53  set_enter_disabled(true);
54 
55  text_box& path_box = find_widget<text_box>("path");
57  path_box.set_selection(0, path_box.text().find_last_of('.')); // TODO: do this cleaner!
58  keyboard_capture(&path_box);
60  std::placeholders::_3, std::placeholders::_5));
61 
62  find_widget<label>("filesize").set_label(font::unicode_em_dash);
63 
64  button& copy_b = find_widget<button>("copy");
66  copy_b, std::bind(&desktop::clipboard::copy_to_clipboard, std::ref(path_)));
67  copy_b.set_active(false);
68 
69  button& open_b = find_widget<button>("open");
71  open_b, std::bind(&desktop::open_object, std::ref(path_)));
72  open_b.set_active(false);
73 
74  button& bdir_b = find_widget<button>("browse_dir");
76  bdir_b,
77  std::bind(&desktop::open_object,
78  std::ref(screenshots_dir_path_)));
79 
80  button& save_b = find_widget<button>("save");
82 }
83 
85 {
86  text_box& path_box = find_widget<text_box>("path");
87  std::string filename = path_box.get_value();
89  path /= filename;
90 
91  path_ = path.string();
92 
95  gui2::show_error_message(_("Unsupported image format.\n\n"
96  "Try to save the screenshot as PNG instead."));
97  } else if(res == image::save_result::save_failed) {
99  translation::dsgettext("wesnoth", "Screenshot creation failed.\n\n"
100  "Make sure there is enough space on the drive holding Wesnoth’s player resource files and that file permissions are set up correctly."));
101  } else if(res != image::save_result::success) {
102  throw std::logic_error("Unexpected error while trying to save a screenshot");
103  } else {
104  path_box.set_active(false);
105  find_widget<button>("open").set_active(true);
106  find_widget<button>("save").set_active(false);
107  find_widget<button>("copy").set_active(true);
108 
109  const int filesize = filesystem::file_size(path_);
110  const std::string sizetext = utils::si_string(filesize, true, _("unit_byte^B"));
111  find_widget<label>("filesize").set_label(sizetext);
112  }
113 }
114 
115 void screenshot_notification::keypress_callback(bool& handled, SDL_Keycode key)
116 {
117  if(key == SDLK_RETURN || key == SDLK_KP_ENTER) {
118  save_screenshot();
119  handled = true;
120  }
121 }
122 
123 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:64
Abstract base class for all modal dialogs.
void keypress_callback(bool &handled, const SDL_Keycode key)
virtual void pre_show() override
Actions to be taken before showing the window.
std::string path_
The display function.
std::string get_value() const
void set_selection(std::size_t start, int length)
Sets or clears the text selection.
const std::string & text() const
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.
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:322
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
Declarations for File-IO.
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
void copy_to_clipboard(const std::string &text)
Copies text to the clipboard.
Definition: clipboard.cpp:27
bool open_object([[maybe_unused]] const std::string &path_or_url)
Definition: open.cpp:46
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't exist.
std::string get_screenshot_dir()
const std::string unicode_em_dash
Definition: constants.cpp:44
std::string path
Definition: filesystem.cpp:91
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_pre_key_press(dispatcher &dispatcher, const signal_keyboard &signal)
Connects the signal for 'snooping' on the keypress.
Definition: dispatcher.cpp:172
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:201
save_result
Definition: picture.hpp:262
save_result save_image(const locator &i_locator, const std::string &filename)
Definition: picture.cpp:884
std::string dsgettext(const char *domainname, const char *msgid)
Definition: gettext.cpp:434
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
Desktop environment interaction functions.
std::string filename
Filename.