]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-impress.cpp
New upstream version 2.9.22
[sven/vym.git] / src / export-impress.cpp
1 #include <QMessageBox>
2
3 #include "export-impress.h"
4 #include "mainwindow.h"
5
6 extern QString vymName;
7 extern Main *mainWindow;
8
9 ExportOO::ExportOO()
10 {
11     exportName = "Impress";
12     filter = "LibreOffice Impress (*.odp);;All (* *.*)";
13     caption = vymName + " -" +
14               QObject::tr("Export as LibreOffice Impress presentation");
15     useSections = false;
16 }
17
18 ExportOO::~ExportOO() {}
19
20 QString ExportOO::buildList(TreeItem *current)
21 {
22     QString r;
23
24     uint i = 0;
25     BranchItem *bi = current->getFirstBranch();
26     if (bi) {
27         // Start list
28         r += "<text:list text:style-name=\"vym-list\">\n";
29         while (bi) {
30             if (!bi->hasHiddenExportParent()) {
31                 r += "<text:list-item><text:p >";
32                 r += quoteMeta(bi->getHeadingPlain());
33                 // If necessary, write note
34                 if (!bi->isNoteEmpty())
35                     r += "<text:line-break/>" + bi->getNoteASCII();
36                 r += "</text:p>";
37                 r += buildList(bi); // recursivly add deeper branches
38                 r += "</text:list-item>\n";
39             }
40             i++;
41             bi = current->getBranchNum(i);
42         }
43         r += "</text:list>\n";
44     }
45     return r;
46 }
47
48 void ExportOO::exportPresentation()
49 {
50     QString allPages;
51
52     BranchItem *firstMCO =
53         (BranchItem *)(model->getRootItem()->getFirstBranch());
54     if (!firstMCO) {
55         QMessageBox::critical(0, QObject::tr("Critical Export Error"),
56                               QObject::tr("No objects in map!"));
57         return;
58     }
59
60     // Insert new content
61     // FIXME add extra title in mapinfo for vym 1.13.x
62     content.replace("<!-- INSERT TITLE -->",
63                     quoteMeta(firstMCO->getHeadingPlain()));
64     content.replace("<!-- INSERT AUTHOR -->", quoteMeta(model->getAuthor()));
65
66     QString onePage;
67     QString list;
68
69     BranchItem *sectionBI;
70     int i = 0;
71     BranchItem *pagesBI;
72     int j = 0;
73
74     int mapcenters = model->getRootItem()->branchCount();
75
76     // useSections already has been set in setConfigFile
77     if (mapcenters > 1)
78         sectionBI = firstMCO;
79     else
80         sectionBI = firstMCO->getFirstBranch();
81
82     // Walk sections
83     while (sectionBI && !sectionBI->hasHiddenExportParent()) {
84         if (useSections) {
85             // Add page with section title
86             onePage = sectionTemplate;
87             onePage.replace("<!-- INSERT PAGE HEADING -->",
88                             quoteMeta(sectionBI->getHeadingPlain()));
89             allPages += onePage;
90             pagesBI = sectionBI->getFirstBranch();
91         }
92         else {
93             // i=-2; // only use inner loop to
94             // turn mainbranches into pages
95             // sectionBI=firstMCO;
96             pagesBI = sectionBI;
97         }
98
99         j = 0;
100         while (pagesBI && !pagesBI->hasHiddenExportParent()) {
101             // Add page with list of items
102             onePage = pageTemplate;
103             onePage.replace("<!-- INSERT PAGE HEADING -->",
104                             quoteMeta(pagesBI->getHeadingPlain()));
105             list = buildList(pagesBI);
106             onePage.replace("<!-- INSERT LIST -->", list);
107             allPages += onePage;
108             if (pagesBI != sectionBI) {
109                 j++;
110                 pagesBI = ((BranchItem *)pagesBI->parent())->getBranchNum(j);
111             }
112             else
113                 pagesBI = NULL; // We are already iterating over the sectionBIs
114         }
115         i++;
116         if (mapcenters > 1)
117             sectionBI = model->getRootItem()->getBranchNum(i);
118         else
119             sectionBI = firstMCO->getBranchNum(i);
120     }
121
122     content.replace("<!-- INSERT PAGES -->", allPages);
123
124     // Write modified content
125     QFile f(contentFile);
126     if (!f.open(QIODevice::WriteOnly)) {
127         QMessageBox::critical(
128             0, QObject::tr("Critical Export Error"),
129             QObject::tr("Could not write %1").arg(contentFile));
130         mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
131         return;
132     }
133
134     QTextStream t(&f);
135     t.setCodec("UTF-8");
136     t << content;
137     f.close();
138
139     // zip tmpdir to destination
140     zipDir(tmpDir, filePath);
141
142     displayedDestination = filePath;
143
144     result = ExportBase::Success;
145
146     QStringList args;
147     args << filePath;
148     args << configFile;
149     completeExport(args);
150 }
151
152 bool ExportOO::setConfigFile(const QString &cf)
153 {
154     configFile = cf;
155     int i = cf.lastIndexOf("/");
156     if (i >= 0)
157         configDir = cf.left(i);
158     SimpleSettings set;
159
160     if (!set.readSettings(configFile)) {
161         QMessageBox::critical(
162             0, QObject::tr("Critical Export Error"),
163             QObject::tr("Couldn't read settings from \"%1\"").arg(configFile));
164         return false;
165     }
166
167     // set paths
168     templateDir = configDir + "/" + set.value("Template");
169
170     setupTmpDir();
171
172     QDir d(templateDir);
173     if (!d.exists()) {
174         QMessageBox::critical(0, QObject::tr("Critical Export Error"),
175                               QObject::tr("Check \"%1\" in\n%2")
176                                   .arg("Template=" + set.value("Template"))
177                                   .arg(configFile));
178         return false;
179     }
180
181     contentTemplateFile = templateDir + "content-template.xml";
182     pageTemplateFile = templateDir + "page-template.xml";
183     sectionTemplateFile = templateDir + "section-template.xml";
184     contentFile = tmpDir.path() + "/content.xml";
185
186     if (set.value("useSections").contains("yes"))
187         useSections = true;
188
189     // Copy template to tmpdir
190     copyDir(templateDir, tmpDir);
191
192     // Read content-template
193     if (!loadStringFromDisk(contentTemplateFile, content)) {
194         QMessageBox::critical(
195             0, QObject::tr("Critical Export Error"),
196             QObject::tr("Could not read %1").arg(contentTemplateFile));
197         return false;
198     }
199
200     // Read page-template
201     if (!loadStringFromDisk(pageTemplateFile, pageTemplate)) {
202         QMessageBox::critical(
203             0, QObject::tr("Critical Export Error"),
204             QObject::tr("Could not read %1").arg(pageTemplateFile));
205         return false;
206     }
207
208     // Read section-template
209     if (useSections &&
210         !loadStringFromDisk(sectionTemplateFile, sectionTemplate)) {
211         QMessageBox::critical(
212             0, QObject::tr("Critical Export Error"),
213             QObject::tr("Could not read %1").arg(sectionTemplateFile));
214         return false;
215     }
216     return true;
217 }