Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
template.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Martin Owens <doctormo@geek-2.com>
5 *
6 * Copyright (C) 2022 Authors
7 *
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11#ifndef INKSCAPE_EXTENSION_TEMPLATE_H__
12#define INKSCAPE_EXTENSION_TEMPLATE_H__
13
14#include <exception>
15#include <map>
16#include <string>
17#include <giomm/file.h>
18#include <glibmm/refptr.h>
19#include <glibmm/ustring.h>
20
21#include "extension/db.h"
22#include "extension/extension.h"
23#include "util/units.h"
24
25class SPDocument;
26class SPPage;
27
28namespace Inkscape {
29namespace Extension {
30
31using TemplateShow = int;
42
43class Template;
44class TemplatePreset;
45typedef std::map<std::string, std::string> TemplatePrefs;
46typedef std::vector<std::shared_ptr<TemplatePreset>> TemplatePresets;
47
49{
50public:
51 TemplatePreset(Template *mod, const Inkscape::XML::Node *repr, TemplatePrefs prefs = {}, int priority = 0);
53
54 std::string const &get_key() const { return _key; }
55 std::string const &get_icon() const { return _icon; }
56 std::string const &get_category() const { return _category; }
57 std::string const &get_name() const { return _name; }
58 std::string const &get_label() const { return _label; }
59 std::string const &get_description() const { return _desc; }
60 int get_sort_priority() const { return _priority; }
61 int get_visibility() const { return _visibility; }
62
64 // Not hidden and contains the requested mode.
65 return _visibility && (mode == TEMPLATE_ANY
66 || ((_visibility & (int)mode) == (int)mode));
67 }
68
69 std::unique_ptr<SPDocument> new_from_template(TemplatePrefs const &others = {});
70 void resize_to_template(SPDocument *doc, SPPage *page, const TemplatePrefs &others = {});
71 bool match_size(double width, double height, const TemplatePrefs &others = {});
72
73 std::string get_icon_path() const;
74
75private:
77
78protected:
79 std::string _key;
80 std::string _icon;
81 std::string _name;
82 std::string _label;
83 std::string _desc;
84 std::string _category;
87
88 // This is a set of preferences given to the extension
90
91 std::string _get_icon_path(std::string const &name) const;
92 bool setup_prefs(const TemplatePrefs &others = {});
93 void _add_prefs(const TemplatePrefs &prefs);
94};
95
96class Template : public Extension
97{
98public:
99 struct create_cancelled : public std::exception
100 {
101 ~create_cancelled() noexcept override = default;
102 const char *what() const noexcept override { return "Create was cancelled"; }
103 };
104
105 Template(Inkscape::XML::Node *in_repr, ImplementationHolder implementation, std::string *base_directory);
106 ~Template() override = default;
107
108 bool check() override;
109
110 std::unique_ptr<SPDocument> new_from_template();
112
113 std::string get_icon() const { return _icon; }
114 std::string get_description() const { return _desc; }
115 std::string get_category() const { return _category; }
116
117 bool can_resize() const { return _can_resize; }
118 int get_visibility() const { return _visibility; }
119
121
122 std::shared_ptr<TemplatePreset> get_preset(const std::string &key);
123 std::shared_ptr<TemplatePreset> get_preset(double width, double height);
124 static std::shared_ptr<TemplatePreset> get_any_preset(const std::string &key);
125 static std::shared_ptr<TemplatePreset> get_any_preset(double width, double height);
126
127 Glib::RefPtr<Gio::File> get_template_filename() const;
128 std::unique_ptr<SPDocument> get_template_document() const;
129
130protected:
131 friend class TemplatePreset;
132
133 static int parse_visibility(const std::string &value);
134private:
135 std::string _source;
136 std::string _icon;
137 std::string _desc;
138 std::string _category;
139
140 bool _can_resize = false; // Can this be used to resize existing pages?
142
144};
145
146} // namespace Extension
147} // namespace Inkscape
148#endif /* INKSCAPE_EXTENSION_TEMPLATE_H__ */
149
150/*
151 Local Variables:
152 mode:c++
153 c-file-style:"stroustrup"
154 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
155 indent-tabs-mode:nil
156 fill-column:99
157 End:
158*/
159// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
uint64_t page
Definition canvas.cpp:171
The object that is the basis for the Extension system.
Definition extension.h:133
Util::HybridPointer< Implementation::Implementation > ImplementationHolder
Definition extension.h:141
std::string const & get_category() const
Definition template.h:56
void _add_prefs(const TemplatePrefs &prefs)
Called by setup_prefs to save the given prefs into this extension.
Definition template.cpp:133
std::string _get_icon_path(std::string const &name) const
Definition template.cpp:87
std::string const & get_name() const
Definition template.h:57
std::string const & get_key() const
Definition template.h:54
bool setup_prefs(const TemplatePrefs &others={})
Setup the preferences and ask the user to fill in the remaineder.
Definition template.cpp:114
std::string const & get_description() const
Definition template.h:59
std::unique_ptr< SPDocument > new_from_template(TemplatePrefs const &others={})
Generate a new document from this preset.
Definition template.cpp:150
std::string const & get_label() const
Definition template.h:58
std::string get_icon_path() const
Definition template.cpp:80
void resize_to_template(SPDocument *doc, SPPage *page, const TemplatePrefs &others={})
Resize the given page to however the page format requires it to be.
Definition template.cpp:161
std::string const & get_icon() const
Definition template.h:55
bool match_size(double width, double height, const TemplatePrefs &others={})
Reverse match for templates, allowing page duplication and labeling.
Definition template.cpp:171
bool is_visible(TemplateShow mode)
Definition template.h:63
std::string get_icon() const
Definition template.h:113
~Template() override=default
static std::shared_ptr< TemplatePreset > get_any_preset(const std::string &key)
Return the template preset based on the key from any template class (static method).
Definition template.cpp:352
void resize_to_template(SPDocument *doc, SPPage *page)
Takes an existing page and resizes it to the required dimentions.
Definition template.cpp:295
Glib::RefPtr< Gio::File > get_template_filename() const
Get the template filename, or return the default template.
Definition template.cpp:384
std::string get_category() const
Definition template.h:115
std::string get_description() const
Definition template.h:114
bool check() override
Validate this extension.
Definition template.cpp:260
std::unique_ptr< SPDocument > new_from_template()
This function creates a document from a template.
Definition template.cpp:274
std::shared_ptr< TemplatePreset > get_preset(const std::string &key)
Return the template preset based on the key from this template class.
Definition template.cpp:326
static int parse_visibility(const std::string &value)
Parse the expected value for the visibility value, turn into enum.
Definition template.cpp:239
std::unique_ptr< SPDocument > get_template_document() const
Get the raw document svg for this template (pre-processing).
Definition template.cpp:407
TemplatePresets get_presets(TemplateShow visibility=TEMPLATE_ANY) const
Return a list of all template presets.
Definition template.cpp:309
Interface for refcounted XML nodes.
Definition node.h:80
Typed SVG document implementation.
Definition document.h:103
Inkscape::Extension::Extension: Frontend to certain, possibly pluggable, actions.
std::map< std::string, std::string > TemplatePrefs
Definition template.h:45
std::vector< std::shared_ptr< TemplatePreset > > TemplatePresets
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
int mode
~create_cancelled() noexcept override=default
const char * what() const noexcept override
Definition template.h:102
double height
double width
Glib::ustring name
Definition toolbars.cpp:55