From 0e9f18d8ebd2394a1ea0d51ac41c63761739deef Mon Sep 17 00:00:00 2001 From: "Ignacio R. Morelle" Date: Wed, 4 Jun 2014 23:20:52 -0400 Subject: [PATCH 1/2] gui2/tgamestate_inspector: Use write() function for printing WML objects The write() function from the WML parser is a dedicated WML serialization mechanism that is also used to write saved games and such, and thus it will output WML in the same format that the game would normally use. The config::debug() method we were using here before, instead, only implements a simplified strategy that will not produce valid multiline attribute values, for example, or include any textdomain directives applicable to the contents. If we are to have a way to copy the inspect window's contents, it should prove far more convenient for coders to see the generated WML in the same format as it would normally be saved to disk. --- changelog | 2 ++ src/gui/dialogs/gamestate_inspector.cpp | 27 +++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/changelog b/changelog index b86fc960bf0b..322edf75c29d 100644 --- a/changelog +++ b/changelog @@ -60,6 +60,8 @@ Version 1.13.0-dev: * Changed: A listbox can now update its size when rows are added. * Changed: Avoid listboxes to handle mouse clicks twice. * Fixed bug #22144: An assertion failure with empty labels in a listbox. + * The :inspect dialog now uses the same function as saved games to generate + WML in text form instead of a simplified version. * WML engine: * Added customizable recall costs for unit types and individual units, using the new recall_cost attribute in [unit_type] and [unit]. diff --git a/src/gui/dialogs/gamestate_inspector.cpp b/src/gui/dialogs/gamestate_inspector.cpp index 0f1711b91bea..0a96bb20ab29 100644 --- a/src/gui/dialogs/gamestate_inspector.cpp +++ b/src/gui/dialogs/gamestate_inspector.cpp @@ -26,6 +26,7 @@ #endif #include "gui/widgets/settings.hpp" #include "gui/widgets/window.hpp" +#include "serialization/parser.hpp" // for write() #include "utils/foreach.tpp" #include "../../gamestatus.hpp" @@ -37,6 +38,18 @@ #include #include +namespace +{ + +inline std::string config_to_string(const config& cfg) +{ + std::ostringstream s; + write(s, cfg); + return s.str(); +} + +} + namespace gui2 { @@ -70,7 +83,7 @@ static void inspect_ai(twindow& window, int side) NEW_find_widget( &window, "inspect", - false).set_label(ai_cfg.debug()); + false).set_label(config_to_string(ai_cfg.debug)); } */ @@ -243,7 +256,7 @@ class variable_mode_controller : public single_mode_controller FOREACH(const AUTO & c, vars.all_children_range()) { if(selected == i) { - model_.set_inspect_window_text(c.cfg.debug()); + model_.set_inspect_window_text(config_to_string(c.cfg)); return; } i++; @@ -317,7 +330,9 @@ class unit_mode_controller : public single_mode_controller if(selected == i) { config c_unit; u->write(c_unit); - model_.set_inspect_window_text(c_unit.debug()); + std::ostringstream cfg_str; + write(cfg_str, c_unit); + model_.set_inspect_window_text(cfg_str.str()); return; } i++; @@ -375,7 +390,7 @@ class team_mode_controller : public single_mode_controller : config(); c.clear_children("ai"); c.clear_children("village"); - model_.set_inspect_window_text(c.debug()); + model_.set_inspect_window_text(config_to_string(c)); return; } @@ -387,7 +402,7 @@ class team_mode_controller : public single_mode_controller if(selected == 2) { model_.set_inspect_window_text( - ai::manager::to_config(side_).debug()); + config_to_string(ai::manager::to_config(side_))); return; } @@ -427,7 +442,7 @@ class team_mode_controller : public single_mode_controller u.write(c_unit); c.add_child("unit", c_unit); } - model_.set_inspect_window_text(c.debug()); + model_.set_inspect_window_text(config_to_string(c)); return; } From eab3e6fb646fda8cc6101a3e568c86c2b17b707f Mon Sep 17 00:00:00 2001 From: "Ignacio R. Morelle" Date: Thu, 5 Jun 2014 00:25:47 -0400 Subject: [PATCH 2/2] gui2/tgamestate_inspector: Add a button to copy contents to clipboard This copies the contents of the main display area to clipboard. The button currently lacks a tooltip because the tooltip causes map labels to glitch through the dialog when displayed (noticeable in e.g. the test scenario). I'll file a bug for this issue later using a (commented-out) line introduced by this commit as a test case. --- changelog | 2 + .../default/window/gamestate_inspector.cfg | 51 ++++++++++++++----- src/gui/dialogs/gamestate_inspector.cpp | 25 +++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) diff --git a/changelog b/changelog index 322edf75c29d..f81b7ce9fe59 100644 --- a/changelog +++ b/changelog @@ -62,6 +62,8 @@ Version 1.13.0-dev: * Fixed bug #22144: An assertion failure with empty labels in a listbox. * The :inspect dialog now uses the same function as saved games to generate WML in text form instead of a simplified version. + * Added a button to copy the currently displayed content from the :inspect + dialog to clipboard. * WML engine: * Added customizable recall costs for unit types and individual units, using the new recall_cost attribute in [unit_type] and [unit]. diff --git a/data/gui/default/window/gamestate_inspector.cfg b/data/gui/default/window/gamestate_inspector.cfg index 44df78989578..c69a78549a70 100644 --- a/data/gui/default/window/gamestate_inspector.cfg +++ b/data/gui/default/window/gamestate_inspector.cfg @@ -204,29 +204,54 @@ [column] grow_factor = 6 - border = "all" - border_size = 5 vertical_alignment = "top" horizontal_alignment = "left" [grid] [row] [column] - # - # Emulate the listbox headers' top/bottom padding - # here relative to the scroll_label below. - # - border = "top,bottom" - border_size = 5 - horizontal_alignment = "left" - [label] - definition = "default" - label = _ "Contents" - [/label] + horizontal_grow = "true" + [grid] + [row] + [column] + # + # Emulate the listbox headers' top/bottom padding + # here relative to the scroll_label below. + # + border = "top,left,right" + border_size = 5 + horizontal_alignment = "left" + + [label] + definition = "default" + label = _ "Contents" + [/label] + [/column] + + [column] + border = "top,left,right" + border_size = 5 + horizontal_alignment = "right" + + [button] + id = "copy" + definition = "action_copy" + label = _ "clipboard^Copy" + # FIXME: tooltips cause weird interactions with map + # labels while running a GUI2 dialog, so let's + # not use a tooltip yet. + #tooltip = _ "Copy this report to clipboard" + [/button] + [/column] + [/row] + [/grid] [/column] [/row] [row] [column] + border = "left,bottom,right" + border_size = 5 horizontal_alignment = "left" + [scroll_label] id = "inspect" definition = "default" diff --git a/src/gui/dialogs/gamestate_inspector.cpp b/src/gui/dialogs/gamestate_inspector.cpp index 0a96bb20ab29..2834af1e6eef 100644 --- a/src/gui/dialogs/gamestate_inspector.cpp +++ b/src/gui/dialogs/gamestate_inspector.cpp @@ -26,6 +26,8 @@ #endif #include "gui/widgets/settings.hpp" #include "gui/widgets/window.hpp" + +#include "clipboard.hpp" #include "serialization/parser.hpp" // for write() #include "utils/foreach.tpp" @@ -72,6 +74,9 @@ namespace gui2 * inspect & & control & m & * The state of the variable or event. $ * + * copy & & button & m & + * A button to copy the state to clipboard. $ + * * @end{table} */ @@ -118,6 +123,7 @@ class tgamestate_inspector::model , stuff_types_list() , inspect() , inspector_name() + , copy_button() { name = cfg["name"].str(); } @@ -129,6 +135,7 @@ class tgamestate_inspector::model tlistbox* stuff_types_list; tcontrol* inspect; tcontrol* inspector_name; + tbutton* copy_button; void clear_stuff_list() @@ -572,6 +579,11 @@ class tgamestate_inspector::controller c->update_view_from_model(); // TODO: 'activate' } + void handle_copy_button_clicked() + { + copy_to_clipboard(model_.inspect->label(), false); + } + private: model& model_; @@ -605,6 +617,11 @@ class tgamestate_inspector::view controller_.handle_stuff_types_list_item_clicked(); } + void handle_copy_button_clicked(twindow& /*window*/) + { + controller_.handle_copy_button_clicked(); + } + void bind(twindow& window) { @@ -615,6 +632,8 @@ class tgamestate_inspector::view model_.inspect = find_widget(&window, "inspect", false, true); model_.inspector_name = &find_widget(&window, "inspector_name", false); + model_.copy_button + = &find_widget(&window, "copy", false); #ifdef GUI2_EXPERIMENTAL_LISTBOX connect_signal_notify_modified( @@ -642,6 +661,12 @@ class tgamestate_inspector::view &tgamestate_inspector::view:: handle_stuff_types_list_item_clicked>); #endif + + connect_signal_mouse_left_click( + *model_.copy_button, + boost::bind(&tgamestate_inspector::view::handle_copy_button_clicked, + this, + boost::ref(window))); } private: