|
| 1 | +#ifndef InjaLogDialog_h |
| 2 | +#define InjaLogDialog_h |
| 3 | +/* InterSpec: an application to analyze spectral gamma radiation data. |
| 4 | +
|
| 5 | + Copyright 2018 National Technology & Engineering Solutions of Sandia, LLC |
| 6 | + (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S. |
| 7 | + Government retains certain rights in this software. |
| 8 | + For questions contact William Johnson via email at [email protected], or |
| 9 | + alternative emails of [email protected]. |
| 10 | +
|
| 11 | + This library is free software; you can redistribute it and/or |
| 12 | + modify it under the terms of the GNU Lesser General Public |
| 13 | + License as published by the Free Software Foundation; either |
| 14 | + version 2.1 of the License, or (at your option) any later version. |
| 15 | +
|
| 16 | + This library is distributed in the hope that it will be useful, |
| 17 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | + Lesser General Public License for more details. |
| 20 | +
|
| 21 | + You should have received a copy of the GNU Lesser General Public |
| 22 | + License along with this library; if not, write to the Free Software |
| 23 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | + */ |
| 25 | + |
| 26 | +#include "InterSpec_config.h" |
| 27 | + |
| 28 | +#include <tuple> |
| 29 | +#include <vector> |
| 30 | +#include <string> |
| 31 | +#include <utility> |
| 32 | +#include <functional> |
| 33 | + |
| 34 | +#include <Wt/WString> |
| 35 | + |
| 36 | +#include <nlohmann/json.hpp> |
| 37 | + |
| 38 | +#include "external_libs/SpecUtils/3rdparty/inja/inja.hpp" |
| 39 | + |
| 40 | +#include "InterSpec/SimpleDialog.h" |
| 41 | + |
| 42 | + |
| 43 | +// Forward declarations |
| 44 | +namespace Wt |
| 45 | +{ |
| 46 | + class WText; |
| 47 | + class WComboBox; |
| 48 | + class WResource; |
| 49 | + class WPushButton; |
| 50 | + class WContainerWidget; |
| 51 | +}// namespace Wt |
| 52 | + |
| 53 | + |
| 54 | +/** A reusable dialog for displaying content rendered from inja templates. |
| 55 | +
|
| 56 | + This dialog allows displaying HTML content generated from JSON data using inja templates. |
| 57 | + It supports multiple template options (selected via a combo box), displays the content in |
| 58 | + an iframe for isolation, and provides download functionality. |
| 59 | +
|
| 60 | + Originally extracted from BatchGuiAnaWidget to be reusable across the application. |
| 61 | + */ |
| 62 | +class InjaLogDialog : public SimpleDialog |
| 63 | +{ |
| 64 | +public: |
| 65 | + /** Enum to specify the type of log content. */ |
| 66 | + enum class LogType |
| 67 | + { |
| 68 | + Html, ///< HTML content that will be displayed directly in iframe |
| 69 | + Text ///< Plain text content that will be HTML-escaped and wrapped for display |
| 70 | + }; |
| 71 | + |
| 72 | + /** HTML wrapper tags for text log templates. |
| 73 | + Use these to wrap plain text content in HTML for display, |
| 74 | + so this way they can be stripped off when downloading the text version. |
| 75 | + */ |
| 76 | + static const char * const sm_txt_log_pre_wrapper; |
| 77 | + static const char * const sm_txt_log_post_wrapper; |
| 78 | + |
| 79 | + /** Constructor for InjaLogDialog. |
| 80 | +
|
| 81 | + @param title The dialog title |
| 82 | + @param data The JSON data to be passed to inja templates |
| 83 | + @param template_options A vector of template options. Each entry contains a tuple: |
| 84 | + - get<0>: Display name for the template (shown in combo box) |
| 85 | + - get<1>: Filename suffix to append to spectrum filename for downloads (e.g., "_act_fit.html" or "_std_fit_log.txt") |
| 86 | + - get<2>: LogType enum indicating whether content is Html or Text |
| 87 | + - get<3>: Function that takes inja::Environment and JSON data, |
| 88 | + and returns rendered content string |
| 89 | + - For Html type: content must be a valid HTML page, as it will be displayed in an iframe |
| 90 | + - For Text type: content will be HTML-escaped and wrapped in <pre> tags for display |
| 91 | +
|
| 92 | + If template_options contains only one entry, no combo box is shown. |
| 93 | + If template_options is empty, an error is displayed. |
| 94 | + */ |
| 95 | + InjaLogDialog( const Wt::WString &title, |
| 96 | + const nlohmann::json &data, |
| 97 | + std::vector<std::tuple<Wt::WString, std::string, LogType, std::function<std::string(inja::Environment&, const nlohmann::json&)>>> template_options ); |
| 98 | + |
| 99 | + virtual ~InjaLogDialog(); |
| 100 | + |
| 101 | + const std::string ¤t_content() const; |
| 102 | + const std::string current_suggested_name() const; |
| 103 | + |
| 104 | + /** Show or hide the toolbar containing the template selector and download button. |
| 105 | + @param show If true, toolbar is shown; if false, toolbar is hidden |
| 106 | + */ |
| 107 | + void setToolbarVisible( const bool show ); |
| 108 | + |
| 109 | +protected: |
| 110 | + /** Update the displayed content using the currently selected template. */ |
| 111 | + void updateDisplay(); |
| 112 | + |
| 113 | + /** Handle template selection change from combo box. */ |
| 114 | + void handleTemplateChange(); |
| 115 | + |
| 116 | +private: |
| 117 | + Wt::WResource *m_download_resource; |
| 118 | + |
| 119 | + |
| 120 | + // Member variables |
| 121 | + nlohmann::json m_data; |
| 122 | + std::vector<std::tuple<Wt::WString, std::string, LogType, std::function<std::string(inja::Environment&, const nlohmann::json&)>>> m_template_options; |
| 123 | + |
| 124 | + Wt::WContainerWidget *m_toolbar; |
| 125 | + Wt::WComboBox *m_template_selector; |
| 126 | + Wt::WPushButton *m_download_button; |
| 127 | + Wt::WText *m_iframe_holder; |
| 128 | + Wt::WResource *m_current_resource; |
| 129 | + std::string m_current_content; |
| 130 | +};//class InjaLogDialog |
| 131 | + |
| 132 | + |
| 133 | +#endif // InjaLogDialog_h |
0 commit comments