1
// --------------------------------------------------------------------
2
// Ipelet for handling template objects
3
// --------------------------------------------------------------------
6
This file is part of the extensible drawing editor Ipe.
7
Copyright (C) 1993-2004 Otfried Cheong
9
Ipe is free software; you can redistribute it and/or modify it
10
under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
14
As a special exception, you have permission to link Ipe with the
15
CGAL library and distribute executables, as long as you follow the
16
requirements of the Gnu General Public License in regard to all of
17
the software in the executable aside from CGAL.
19
Ipe is distributed in the hope that it will be useful, but WITHOUT
20
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22
License for more details.
24
You should have received a copy of the GNU General Public License
25
along with Ipe; if not, you can find it at
26
"http://www.gnu.org/copyleft/gpl.html", or write to the Free
27
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36
#include <qstringlist.h>
37
#include <qcombobox.h>
39
inline QString QIpe(const IpeString &str)
41
return QString::fromUtf8(str.CString());
44
// --------------------------------------------------------------------
46
class TemplateIpelet : public Ipelet {
48
virtual int IpelibVersion() const { return IPELIB_VERSION; }
49
virtual int NumFunctions() const { return 5; }
50
virtual const char *Label() const { return "Templates"; }
51
virtual const char *SubLabel(int function) const;
52
virtual void Run(int function, IpePage *page, IpeletHelper *helper);
54
void CreateTemplate(IpePage *page, IpeletHelper *helper, bool newSheet);
55
void UseTemplate(IpePage *page, IpeletHelper *helper, bool clone);
56
void InsertBackground(IpeletHelper *helper);
59
// --------------------------------------------------------------------
61
const char * const sublabel[] = {
62
"Create template (in new style sheet)",
63
"Create template (in top style sheet)",
65
"Clone template (for editing it)",
66
"Insert background on all pages"
69
const char *TemplateIpelet::SubLabel(int function) const
71
return sublabel[function];
74
// --------------------------------------------------------------------
76
void TemplateIpelet::CreateTemplate(IpePage *page, IpeletHelper *helper,
79
IpePage::iterator it = page->PrimarySelection();
80
if (it == page->end()) {
81
helper->Message("No object selected");
85
IpeDocument *doc = helper->EditDocument();
86
if (!newSheet && doc->StyleSheet()->IsStandard()) {
87
helper->MessageBox("I cannot add template to standard style sheet.",
93
if (!helper->GetString("Choose name for template:", name))
97
IpeStyleSheet *ns = new IpeStyleSheet(doc->Repository());
98
ns->SetCascade(doc->GetStyleSheet());
99
doc->SetStyleSheet(ns);
102
IpeStyleSheet *sheet = doc->GetStyleSheet();
104
doc->Repository()->MakeSymbol(IpeAttribute::ETemplate, name);
105
sheet->AddTemplate(tpl, it->Object()->Clone());
108
// --------------------------------------------------------------------
110
void TemplateIpelet::UseTemplate(IpePage *page, IpeletHelper *helper,
113
const IpeStyleSheet *sheet = helper->StyleSheet();
115
sheet->AllNames(IpeAttribute::ETemplate, seq);
117
helper->MessageBox("Your document's style sheet contains no templates",
122
for (IpeAttributeSeq::const_iterator it = seq.begin(); it != seq.end(); ++it)
123
sl.append(QIpe(sheet->Repository()->String(*it)));
125
DlgUseTemplate dlg(0, 0, true);
126
dlg.iTemplate->insertStringList(sl);
127
if (dlg.exec() == QDialog::Accepted) {
128
IpeAttribute tpl = seq[dlg.iTemplate->currentItem()];
129
IpeObject *obj = new IpeReference(sheet, tpl);
131
const IpeObject *obj = sheet->FindTemplate(tpl);
132
page->push_back(IpePgObject(IpePgObject::ESecondary,
133
helper->CurrentLayer(), obj->Clone()));
134
helper->Message("Inserted a copy of the template");
136
page->push_back(IpePgObject(IpePgObject::ESecondary,
137
helper->CurrentLayer(), obj));
138
helper->Message("Inserted reference to template");
143
// --------------------------------------------------------------------
145
void TemplateIpelet::InsertBackground(IpeletHelper *helper)
147
IpeDocument *doc = helper->EditDocument();
149
doc->Repository()->MakeSymbol(IpeAttribute::ETemplate, "background");
150
if (!doc->StyleSheet()->FindTemplate(bga)) {
151
helper->MessageBox("No template 'background' is defined in "
152
"the current style sheet",
157
if (helper->MessageBox("Do you wish to add a 'background' layer with "
158
"a reference to the 'background' template to "
159
"every page of your document?",
160
"Yes, please", "No, thank you", 0) != 0)
163
for (IpeDocument::iterator it = doc->begin(); it != doc->end(); ++it) {
165
if (page->FindLayer("background") >= 0)
168
IpeLayer bglayer("background");
169
bglayer.SetLocked(true);
170
page->AddLayer(bglayer, 0);
172
IpeObject *obj = new IpeReference(doc->StyleSheet(), bga);
173
page->push_back(IpePgObject(IpePgObject::ENone, 0, obj));
175
helper->Message("Background added to all pages");
178
// --------------------------------------------------------------------
180
void TemplateIpelet::Run(int function, IpePage *page, IpeletHelper *helper)
184
CreateTemplate(page, helper, true);
187
CreateTemplate(page, helper, false);
190
UseTemplate(page, helper, false);
193
UseTemplate(page, helper, true);
196
InsertBackground(helper);
202
// --------------------------------------------------------------------
204
IPELET_DECLARE Ipelet *NewIpelet()
206
return new TemplateIpelet;
209
// --------------------------------------------------------------------