]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-ascii.cpp
New upstream version 2.9.22
[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                     out += underline(cur->getHeadingPlain(), QString("="));
62                     out += "\n";
63                     break;
64                 case 1:
65                     out += "\n";
66                     out += (underline(getSectionString(cur) +
67                                           cur->getHeadingPlain(),
68                                       QString("-")));
69                     out += "\n";
70                     break;
71                 case 3:
72                     out += (curIndent + "- " + cur->getHeadingPlain());
73                     out += "\n";
74                     dashIndent = "  ";
75                     break;
76                 default:
77                     out += (curIndent + "- " + cur->getHeadingPlain());
78                     out += "\n";
79                     dashIndent = "  ";
80                     break;
81                 }
82
83                 // If there is a task, save it for potential later display
84                 if (listTasks && cur->getTask()) {
85                     tasks.append(QString("[%1]: %2")
86                                      .arg(cur->getTask()->getStatusString())
87                                      .arg(cur->getHeadingPlain()));
88                 }
89
90                 // If necessary, write URL
91                 if (!cur->getURL().isEmpty())
92                     out += (curIndent + dashIndent + cur->getURL()) + "\n";
93
94                 // If necessary, write vymlink
95                 if (!cur->getVymLink().isEmpty())
96                     out += (curIndent + dashIndent + cur->getVymLink()) +
97                            " (vym mindmap)\n";
98
99                 // If necessary, write note
100                 if (!cur->isNoteEmpty()) {
101                     // curIndent +="  | ";
102                     // Only indent for bullet points
103                     if (cur->depth() > 2)
104                         curIndent += "  ";
105                     out += '\n' + cur->getNoteASCII(curIndent, 80);
106                 }
107                 lastDepth = cur->depth();
108             }
109         }
110         model->nextBranch(cur, prev);
111     }
112
113     if (listTasks) {
114         out += "\n\nTasks\n-----\n\n";
115
116         foreach (QString t, tasks) {
117             out += " - " + t + "\n";
118         }
119     }
120
121     QTextStream ts(&file);
122     ts.setCodec("UTF-8");
123     ts << out;
124     file.close();
125
126     QClipboard *clipboard = QGuiApplication::clipboard();
127     clipboard->setText(out);
128
129     QString listTasksString = listTasks ? "true" : "false";
130
131     displayedDestination = filePath;
132
133     result = ExportBase::Success;
134
135     QStringList args;
136     args << filePath;
137     args << listTasksString;
138
139     completeExport(args);
140 }
141
142 QString ExportASCII::underline(const QString &text, const QString &line)
143 {
144     QString r = text + "\n";
145     for (int j = 0; j < text.length(); j++)
146         r += line;
147     return r;
148 }