1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /** @file
3  * @brief New From Template - templates widget - implementation
4  */
5 /* Authors:
6  *   Jan Darowski <jan.darowski@gmail.com>, supervised by Krzysztof Kosiński
7  *
8  * Copyright (C) 2013 Authors
9  * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10  */
11 
12 #include "template-widget.h"
13 
14 #include <glibmm/miscutils.h>
15 #include <gtkmm/messagedialog.h>
16 
17 #include "desktop.h"
18 #include "document.h"
19 #include "document-undo.h"
20 #include "file.h"
21 #include "inkscape.h"
22 
23 #include "extension/implementation/implementation.h"
24 
25 #include "object/sp-namedview.h"
26 
27 namespace Inkscape {
28 namespace UI {
29 
30 
TemplateWidget()31 TemplateWidget::TemplateWidget()
32     : Gtk::Box(Gtk::ORIENTATION_VERTICAL)
33     , _more_info_button(_("More info"))
34     , _short_description_label(" ")
35     , _template_name_label(_("no template selected"))
36     , _effect_prefs(nullptr)
37     , _preview_box(Gtk::ORIENTATION_HORIZONTAL)
38 {
39     pack_start(_template_name_label, Gtk::PACK_SHRINK, 10);
40     pack_start(_preview_box, Gtk::PACK_SHRINK, 0);
41 
42     _preview_box.pack_start(_preview_image, Gtk::PACK_EXPAND_PADDING, 15);
43     _preview_box.pack_start(_preview_render, Gtk::PACK_EXPAND_PADDING, 10);
44 
45     _short_description_label.set_line_wrap(true);
46 
47     _more_info_button.set_halign(Gtk::ALIGN_END);
48     _more_info_button.set_valign(Gtk::ALIGN_CENTER);
49     pack_end(_more_info_button, Gtk::PACK_SHRINK);
50 
51     pack_end(_short_description_label, Gtk::PACK_SHRINK, 5);
52 
53     _more_info_button.signal_clicked().connect(
54     sigc::mem_fun(*this, &TemplateWidget::_displayTemplateDetails));
55     _more_info_button.set_sensitive(false);
56 }
57 
58 
create()59 void TemplateWidget::create()
60 {
61     if (_current_template.display_name == "")
62         return;
63 
64     if (_current_template.is_procedural){
65         SPDesktop *desktop = SP_ACTIVE_DESKTOP;
66         SPDesktop *desc = sp_file_new_default();
67         _current_template.tpl_effect->effect(desc);
68         DocumentUndo::clearUndo(desc->getDocument());
69         desc->getDocument()->setModifiedSinceSave(false);
70 
71 	// Apply cx,cy etc. from document
72 	sp_namedview_window_from_document( desc );
73 
74         if (desktop)
75             desktop->clearWaitingCursor();
76     }
77     else {
78         sp_file_new(_current_template.path);
79     }
80 }
81 
82 
display(TemplateLoadTab::TemplateData data)83 void TemplateWidget::display(TemplateLoadTab::TemplateData data)
84 {
85     clear();
86     _current_template = data;
87 
88     _template_name_label.set_text(_current_template.display_name);
89     _short_description_label.set_text(_current_template.short_description);
90 
91     if (data.preview_name != ""){
92         std::string imagePath = Glib::build_filename(Glib::path_get_dirname(_current_template.path),
93                                                      Glib::filename_from_utf8(_current_template.preview_name));
94         _preview_image.set(imagePath);
95         _preview_image.show();
96     }
97     else if (!data.is_procedural){
98         Glib::ustring gPath = data.path.c_str();
99         _preview_render.showImage(gPath);
100         _preview_render.show();
101     }
102 
103     if (data.is_procedural){
104         _effect_prefs = data.tpl_effect->get_imp()->prefs_effect(data.tpl_effect, SP_ACTIVE_DESKTOP, nullptr, nullptr);
105         pack_start(*_effect_prefs);
106     }
107     _more_info_button.set_sensitive(true);
108 }
109 
clear()110 void TemplateWidget::clear()
111 {
112     _template_name_label.set_text("");
113     _short_description_label.set_text("");
114     _preview_render.hide();
115     _preview_image.hide();
116     if (_effect_prefs != nullptr){
117         remove (*_effect_prefs);
118         _effect_prefs = nullptr;
119     }
120     _more_info_button.set_sensitive(false);
121 }
122 
_displayTemplateDetails()123 void TemplateWidget::_displayTemplateDetails()
124 {
125     Glib::ustring message = _current_template.display_name + "\n\n";
126 
127     if (!_current_template.author.empty()) {
128         message += _("Author");
129         message += ": ";
130         message += _current_template.author + " " + _current_template.creation_date + "\n\n";
131     }
132 
133     if (!_current_template.keywords.empty()){
134         message += _("Keywords");
135         message += ":";
136         for (const auto & keyword : _current_template.keywords) {
137             message += " ";
138             message += keyword;
139         }
140         message += "\n\n";
141     }
142 
143     if (!_current_template.path.empty()) {
144         message += _("Path");
145         message += ": ";
146         message += _current_template.path;
147         message += "\n\n";
148     }
149 
150     Gtk::MessageDialog dl(message, false, Gtk::MESSAGE_OTHER);
151     dl.run();
152 }
153 
154 }
155 }
156