1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
* Riku Leino, tsoots@gmail.com *
***************************************************************************/
#include "satemplate.h"
#include "satdialog.h"
#include "scpaths.h"
#include "scribuscore.h"
#include "scribusdoc.h"
#include "prefsfile.h"
#include "prefsmanager.h"
int saveastemplateplugin_getPluginAPIVersion()
{
return PLUGIN_API_VERSION;
}
ScPlugin* saveastemplateplugin_getPlugin()
{
SaveAsTemplatePlugin* plug = new SaveAsTemplatePlugin();
Q_CHECK_PTR(plug);
return plug;
}
void saveastemplateplugin_freePlugin(ScPlugin* plugin)
{
SaveAsTemplatePlugin* plug = dynamic_cast<SaveAsTemplatePlugin*>(plugin);
Q_ASSERT(plug);
delete plug;
}
SaveAsTemplatePlugin::SaveAsTemplatePlugin() : ScActionPlugin()
{
// Set action info in languageChange, so we only have to do
// it in one place.
languageChange();
}
SaveAsTemplatePlugin::~SaveAsTemplatePlugin() {};
void SaveAsTemplatePlugin::languageChange()
{
// Note that we leave the unused members unset. They'll be initialised
// with their default ctors during construction.
// Action name
m_actionInfo.name = "SaveAsDocumentTemplate";
// Action text for menu, including accel
m_actionInfo.text = tr("Save as &Template...");
// Shortcut
m_actionInfo.keySequence = "Ctrl+Alt+S";
// Menu
m_actionInfo.menu = "File";
m_actionInfo.menuAfterName = "SaveAs";
m_actionInfo.enabledOnStartup = true;
m_actionInfo.needsNumObjects = -1;
}
const QString SaveAsTemplatePlugin::fullTrName() const
{
return QObject::tr("Save As Template");
}
const ScActionPlugin::AboutData* SaveAsTemplatePlugin::getAboutData() const
{
AboutData* about = new AboutData;
Q_CHECK_PTR(about);
about->authors = QString::fromUtf8("Riku Leino <riku@scribus.info>");
about->shortDescription = tr("Save a document as a template");
about->description = tr("Save a document as a template. Good way to ease the "
"initial work for documents with a constant look");
// about->version
// about->releaseDate
// about->copyright
about->license = "GPL";
return about;
}
void SaveAsTemplatePlugin::deleteAboutData(const AboutData* about) const
{
Q_ASSERT(about);
delete about;
}
bool SaveAsTemplatePlugin::run(ScribusDoc* doc, QString target)
/*{
Q_ASSERT(target.isEmpty());
Sat = new MenuSAT();
Sat->RunSATPlug();
delete Sat;
Sat = 0;
return true;
}
*/
/* jghali's fix when the new file dialog is cancelled SaT is still active in the menu - PL */
{
m_Doc=doc;
if ( m_Doc )
{
Q_ASSERT(target.isEmpty());
Sat = new MenuSAT();
Sat->RunSATPlug(m_Doc);
delete Sat;
Sat = 0;
}
return true;
}
void MenuSAT::RunSATPlug(ScribusDoc* doc)
{
QDir templates(ScPaths::getApplicationDataDir());
if (!templates.exists("templates"))
{
templates.mkdir("templates");
}
QString currentPath(QDir::currentPath());
QString currentFile(doc->DocName);
bool hasName = doc->hasName;
bool isModified = doc->isModified();
QString userTemplatesDir = PrefsManager::instance()->appPrefs.documentTemplatesDir;
PrefsContext* dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
QString oldCollect = dirs->get("collect", ".");
QString templatesDir = ".";
if (userTemplatesDir.isEmpty())
templatesDir = ScPaths::getApplicationDataDir() + "templates";
else
{
if (userTemplatesDir.right(1) == "/")
userTemplatesDir = userTemplatesDir.left(userTemplatesDir.length() - 1);
templatesDir = userTemplatesDir;
}
dirs->set("collect", templatesDir);
if (doc->scMW()->Collect().isEmpty())
return;
if (oldCollect != ".")
dirs->set("collect", oldCollect);
QString docPath = doc->DocName;
QString docDir = docPath.left(docPath.lastIndexOf('/'));
QString docName = docPath.right(docPath.length() - docPath.lastIndexOf('/') - 1);
docName = docName.left(docName.lastIndexOf(".s"));
if (currentFile != doc->DocName)
{
SATDialog* satdia = new SATDialog(doc->scMW(),docName,
static_cast<int>(doc->pageWidth + 0.5),
static_cast<int>(doc->pageHeight + 0.5));
if (satdia->exec())
{
sat* s = new sat(doc, satdia, docPath.right(docPath.length() - docPath.lastIndexOf('/') - 1),docDir);
s->createImages();
s->createTmplXml();
delete s;
}
// Restore the state that was before ScMW->Collect()
doc->DocName = currentFile;
doc->hasName = hasName;
doc->setModified(isModified);
QString newCaption=currentFile;
if (isModified)
newCaption.append('*');
doc->scMW()->updateActiveWindowCaption(newCaption);
doc->scMW()->removeRecent(docPath);
QDir::setCurrent(currentPath);
delete satdia;
}
}
// --------------------- CLASS sat ------------------------------------------------//
sat::sat(ScribusDoc* doc, SATDialog* satdia, QString fileName, QString tmplDir)
{
lang = ScCore->getGuiLanguage();
m_Doc = doc;
dia = satdia;
dir = tmplDir;
if (dir.right(1) == "/")
dir = tmplDir.left(tmplDir.length() - 1);
file = fileName;
tmplXmlFile = findTemplateXml(dir);
}
void sat::createTmplXml()
{
QFile tmplXml(tmplXmlFile);
if (tmplXml.exists())
{
appendTmplXml();
return; // We don't want to overwrite the template.xml file
}
QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
xml += "<templates>\n";
xml += getTemplateTag();
xml += "</templates>\n";
if ( tmplXml.open( QIODevice::WriteOnly ) )
{
QTextStream stream(&tmplXml);
stream.setCodec("UTF-8");
stream << xml;
tmplXml.close();
}
}
void sat::createImages()
{
QString tnsmallName = dia->nameEdit->text() + "tn.png";
QString tnlargeName = dia->nameEdit->text() + ".png";
double pageh = m_Doc->pageHeight;
double pagew = m_Doc->pageWidth;
int pageSizeSmall = 0;
int pageSizeLarge = 0;
if (pageh > pagew)
{
pageSizeSmall = static_cast<int>(pageh / 10);
pageSizeLarge = static_cast<int>(pageh / 3);
}
else
{
pageSizeSmall = static_cast<int>(pagew / 10);
pageSizeLarge = static_cast<int>(pagew / 3);
}
QImage tnsmall = m_Doc->view()->PageToPixmap(0,pageSizeSmall);
QImage tnlarge = m_Doc->view()->PageToPixmap(0,pageSizeLarge);
tnsmall.save(dir+"/"+tnsmallName,"PNG",70);
tnlarge.save(dir+"/"+tnlargeName, "PNG", 70);
}
void sat::appendTmplXml()
{
QFile tmplXml(tmplXmlFile);
if (tmplXml.open(QIODevice::ReadOnly))
{
QTextStream stream(&tmplXml);
QString tmp = stream.readLine();
QString file = "";
while (!tmp.isNull())
{
file += tmp + "\n";
tmp = stream.readLine();
if (tmp.indexOf("</templates>") != -1)
file += getTemplateTag();
}
tmplXml.close();
if ( tmplXml.open( QIODevice::WriteOnly ) )
{
QTextStream stream2(&tmplXml);
stream2.setCodec("UTF-8");
stream2 << file;
tmplXml.close();
}
}
}
QString sat::getTemplateTag()
{
QString category = dia->catsCombo->currentText();
if (category.isEmpty())
category = QObject::tr("Own Templates");
else
category = dia->cats.key(category, category);
QDate now = QDate::currentDate();
QString cat = QString(category);
replaceIllegalChars(cat);
QString tag = "\t<template category=\""+cat+"\">\n";
QString name = QString(dia->nameEdit->text());
replaceIllegalChars(name);
tag += "\t\t<name>"+name+"</name>\n";
tag += "\t\t<file>"+file+"</file>\n";
tag += "\t\t<tnail>"+name+"tn.png"+"</tnail>\n";
tag += "\t\t<img>"+name+".png"+"</img>\n";
QString psize = QString(dia->psizeEdit->text());
replaceIllegalChars(psize);
tag += "\t\t<psize>"+psize+"</psize>\n";
QString colors = QString(dia->colorsEdit->text());
replaceIllegalChars(colors);
tag += "\t\t<color>"+colors+"</color>\n";
QString descr = QString(dia->descrEdit->toPlainText());
replaceIllegalChars(descr);
tag += "\t\t<descr>"+descr+"</descr>\n";
QString usage = QString(dia->usageEdit->toPlainText());
replaceIllegalChars(usage);
tag += "\t\t<usage>"+usage+"</usage>\n";
QString scribus_version = QString(VERSION);
replaceIllegalChars(scribus_version);
tag += "\t\t<scribus_version>" + scribus_version + "</scribus_version>\n";
QString date = QString(now.toString(Qt::ISODate));
replaceIllegalChars(date);
tag += "\t\t<date>" + date + "</date>\n";
QString author = QString(dia->authorEdit->text());
replaceIllegalChars(author);
tag += "\t\t<author>"+author+"</author>\n";
QString email = QString(dia->emailEdit->text());
replaceIllegalChars(email);
tag += "\t\t<email>"+email+"</email>\n";
tag += "\t</template>\n";
return tag;
}
void sat::replaceIllegalChars(QString& s)
{
s.replace("&", "&");
s.replace("<", "<");
s.replace(">", ">");
s.replace(""", "\"");
s.replace("'", "\'");
s.replace("&", "&");
s.replace("<", "<");
s.replace(">", ">");
s.replace("\"", """);
s.replace("\'", "'");
}
QString sat::findTemplateXml(QString dir)
{
QString tmp = dir + "/template." + lang + ".xml";
if (QFile(tmp).exists())
return tmp;
if (lang.length() > 2)
{
tmp = dir + "/template." + lang.left(2) + ".xml";
if (QFile(tmp).exists())
return tmp;
}
return dir + "/template.xml";
}
sat::~sat()
{
}
|