1 #include "export-ascii.h"
3 #include "mainwindow.h"
6 extern QString vymName;
7 extern Main *mainWindow;
9 ExportASCII::ExportASCII()
12 filter = "TXT (*.txt);;All (* *.*)";
13 caption = vymName + " -" + QObject::tr("Export as ASCII");
16 void ExportASCII::doExport()
19 if (!file.open(QIODevice::WriteOnly)) {
20 QMessageBox::critical(
21 0, QObject::tr("Critical Export Error"),
22 QObject::tr("Could not export as ASCII to %1").arg(filePath));
23 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
29 // Main loop over all branches
34 BranchItem *cur = NULL;
35 BranchItem *prev = NULL;
41 model->nextBranch(cur, prev);
43 if (cur->getType() == TreeItem::Branch ||
44 cur->getType() == TreeItem::MapCenter) {
45 if (!cur->hasHiddenExportParent()) {
46 // qDebug() << "ExportASCII::
47 // "<<curIndent.toStdString()<<cur->getHeadingPlain().toStdString();
49 // Insert newline after previous list
50 //if (cur->depth() < lastDepth)
55 for (i = 1; i < cur->depth() - 1; i++)
56 curIndent += indentPerDepth;
59 switch (cur->depth()) {
62 // Add extra line breaks for 2nd, 3rd, ... MapCenter
63 ensureEmptyLines(out, 2);
64 out += underline(cur->getHeadingPlain(), QString("="));
66 // Empty line below "====" of MapCenters
67 ensureEmptyLines(out, 1);
68 dashIndent = ""; // No indention for notes in MapCenter
72 out += (underline(getSectionString(cur) +
73 cur->getHeadingPlain(),
75 // Empty line below "----" of MainBranches
76 ensureEmptyLines(out, 1);
77 dashIndent = ""; // No indention for notes in MainBranch
81 out += (curIndent + "* " + cur->getHeadingPlain());
86 out += (curIndent + "- " + cur->getHeadingPlain());
91 // If there is a task, save it for potential later display
92 if (listTasks && cur->getTask()) {
93 tasks.append(QString("[%1]: %2")
94 .arg(cur->getTask()->getStatusString())
95 .arg(cur->getHeadingPlain()));
98 // If necessary, write URL
99 if (!cur->getURL().isEmpty()) {
101 out += (curIndent + dashIndent + cur->getURL()) + "\n";
104 // If necessary, write vymlink
105 if (!cur->getVymLink().isEmpty()) {
107 out += (curIndent + dashIndent + cur->getVymLink()) +
111 // If necessary, write note
112 if (!cur->isNoteEmpty()) {
113 // Add at least one empty line before note
114 ensureEmptyLines(out, 1);
116 // Add note and empty line after note
117 out += cur->getNoteASCII(curIndent + dashIndent, 80);
119 ensureEmptyLines(out, 1);
121 lastDepth = cur->depth();
124 model->nextBranch(cur, prev);
128 out += "\n\nTasks\n-----\n\n";
130 foreach (QString t, tasks) {
131 out += " - " + t + "\n";
135 QTextStream ts(&file);
136 ts.setCodec("UTF-8");
140 QClipboard *clipboard = QGuiApplication::clipboard();
141 clipboard->setText(out);
143 QString listTasksString = listTasks ? "true" : "false";
145 displayedDestination = filePath;
147 result = ExportBase::Success;
151 args << listTasksString;
153 completeExport(args);
156 QString ExportASCII::underline(const QString &text, const QString &line)
158 QString r = text + "\n";
159 for (int j = 0; j < text.length(); j++)
164 QString ExportASCII::ensureEmptyLines(QString &text, int n)
166 // Ensure at least n empty lines at the end of text
168 // First count trailing line breaks
170 int i = text.count() - 1;
171 while (i > -1 && text.at(i) == "\n") {
184 QString ExportASCII::ensureNewLine(QString &text)
186 // Add one line break, if not already there yet e.g. from empty line
187 if (!text.endsWith("\n"))