]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-ascii.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / export-ascii.cpp
1 #include "export-ascii.h"
2
3 #include "mainwindow.h"
4 #include <QMessageBox>
5
6 extern QString vymName;
7 extern Main *mainWindow;
8
9 ExportASCII::ExportASCII()
10 {
11     exportName = "ASCII";
12     filter = "TXT (*.txt);;All (* *.*)";
13     caption = vymName + " -" + QObject::tr("Export as ASCII");
14 }
15
16 void ExportASCII::doExport()
17 {
18     QFile file(filePath);
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.")));
24         return;
25     }
26
27     QString out;
28
29     // Main loop over all branches
30     QString s;
31     QString curIndent;
32     QString dashIndent;
33     int i;
34     BranchItem *cur = NULL;
35     BranchItem *prev = NULL;
36
37     int lastDepth = 0;
38
39     QStringList tasks;
40
41     model->nextBranch(cur, prev);
42     while (cur) {
43         if (cur->getType() == TreeItem::Branch ||
44             cur->getType() == TreeItem::MapCenter) {
45             if (!cur->hasHiddenExportParent()) {
46                 // qDebug() << "ExportASCII::
47                 // "<<curIndent.toStdString()<<cur->getHeadingPlain().toStdString();
48
49                 // Insert newline after previous list
50                 //if (cur->depth() < lastDepth)
51                 //    out += "\n";
52
53                 // Make indentstring
54                 curIndent = "";
55                 for (i = 1; i < cur->depth() - 1; i++)
56                     curIndent += indentPerDepth;
57
58                 dashIndent = "";
59                 switch (cur->depth()) {
60                 case 0:
61                     if (!out.isEmpty())
62                         // Add extra line breaks for 2nd, 3rd, ... MapCenter
63                         ensureEmptyLines(out, 2);
64                     out += underline(cur->getHeadingPlain(), QString("="));
65
66                     // Empty line below "====" of MapCenters
67                     ensureEmptyLines(out, 1);
68                     dashIndent = "";    // No indention for notes in MapCenter
69                     break;
70                 case 1:
71                     ensureNewLine(out);
72                     out += (underline(getSectionString(cur) +
73                         cur->getHeadingPlain(),
74                         QString("-")));
75                     // Empty line below "----" of MainBranches
76                     ensureEmptyLines(out, 1);
77                     dashIndent = "";    // No indention for notes in MainBranch
78                     break;
79                 case 2:
80                     ensureNewLine(out);
81                     out += (curIndent + "* " + cur->getHeadingPlain());
82                     dashIndent = "  ";
83                     break;
84                 default:
85                     ensureNewLine(out);
86                     out += (curIndent + "- " + cur->getHeadingPlain());
87                     dashIndent = "  ";
88                     break;
89                 }
90
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()));
96                 }
97
98                 // If necessary, write URL
99                 if (!cur->getURL().isEmpty()) {
100                     ensureNewLine(out);
101                     out += (curIndent + dashIndent + cur->getURL()) + "\n";
102                 }
103
104                 // If necessary, write vymlink
105                 if (!cur->getVymLink().isEmpty()) {
106                     ensureNewLine(out);
107                     out += (curIndent + dashIndent + cur->getVymLink()) +
108                            " (vym mindmap)\n";
109                 }
110
111                 // If necessary, write note
112                 if (!cur->isNoteEmpty()) {
113                     // Add at least one empty line before note
114                     ensureEmptyLines(out, 1);
115
116                     // Add note and empty line after note
117                     out += cur->getNoteASCII(curIndent + dashIndent, 80);
118
119                     ensureEmptyLines(out, 1);
120                 }
121                 lastDepth = cur->depth();
122             }
123         }
124         model->nextBranch(cur, prev);
125     }
126
127     if (listTasks) {
128         out += "\n\nTasks\n-----\n\n";
129
130         foreach (QString t, tasks) {
131             out += " - " + t + "\n";
132         }
133     }
134
135     QTextStream ts(&file);
136     ts.setCodec("UTF-8");
137     ts << out;
138     file.close();
139
140     QClipboard *clipboard = QGuiApplication::clipboard();
141     clipboard->setText(out);
142
143     QString listTasksString = listTasks ? "true" : "false";
144
145     displayedDestination = filePath;
146
147     result = ExportBase::Success;
148
149     QStringList args;
150     args << filePath;
151     args << listTasksString;
152
153     completeExport(args);
154 }
155
156 QString ExportASCII::underline(const QString &text, const QString &line)
157 {
158     QString r = text + "\n";
159     for (int j = 0; j < text.length(); j++)
160         r += line;
161     return r;
162 }
163
164 QString ExportASCII::ensureEmptyLines(QString &text, int n)
165 {
166     // Ensure at least n empty lines at the end of text
167
168     // First count trailing line breaks
169     int j = 0;
170     int i = text.count() - 1;
171     while (i > -1 && text.at(i) == "\n")  {
172         i--;
173         j++;
174     }
175
176     while (j < n + 1) {
177         text = text + "\n";
178         j++;
179     }
180
181     return text;
182 }
183
184 QString ExportASCII::ensureNewLine(QString &text)
185 {
186     // Add one line break, if not already there yet e.g. from empty line
187     if (!text.endsWith("\n"))
188         text += "\n";
189     return text;
190 }