]> git.sven.stormbind.net Git - sven/vym.git/blob - src/export-ao.cpp
New upstream version 2.9.22
[sven/vym.git] / src / export-ao.cpp
1 #include <QMessageBox>
2
3 #include "export-ao.h"
4 #include "mainwindow.h"
5
6 extern QString vymName;
7 extern Main *mainWindow;
8 extern Settings settings;
9
10 ExportAO::ExportAO()
11 {
12     exportName = "AO";
13     filter = "TXT (*.txt);;All (* *.*)";
14     caption = vymName + " -" + QObject::tr("Export as AO report") + " " +
15               QObject::tr("(still experimental)");
16     indentPerDepth = "   ";
17     bulletPoints.clear();
18     for (int i = 0; i < 10; i++)
19         bulletPoints << "-";
20 }
21
22 void ExportAO::doExport()
23 {
24     QFile file(filePath);
25     if (!file.open(QIODevice::WriteOnly)) {
26         QMessageBox::critical(
27             0, QObject::tr("Critical Export Error"),
28             QObject::tr("Could not export as AO to %1").arg(filePath));
29         mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
30         return;
31     }
32
33     settings.setLocalValue(model->getFilePath(), "/export/last/command",
34                            "exportAO");
35     settings.setLocalValue(model->getFilePath(), "/export/last/description",
36                            "A&O report");
37
38     QString out;
39
40     // Main loop over all branches
41     QString s;
42     QString curIndent;
43     QString dashIndent;
44
45     int i;
46     BranchItem *cur = NULL;
47     BranchItem *prev = NULL;
48
49     model->nextBranch(cur, prev);
50     while (cur) {
51         QString line;
52         QString colString = "";
53         QString noColString;
54         QString statusString = "";
55         QColor col;
56
57         if (cur->getType() == TreeItem::Branch ||
58             cur->getType() == TreeItem::MapCenter) {
59             // Make indentstring
60             curIndent = indent(cur->depth() - 4, true);
61
62             if (!cur->hasHiddenExportParent()) {
63                 col = cur->getHeadingColor();
64                 if (col == QColor(255, 0, 0))
65                     colString = "[R] ";
66                 else if (col == QColor(217, 81, 0))
67                     colString = "[O] ";
68                 else if (col == QColor(0, 85, 0))
69                     colString = "[G] ";
70                 else if (cur->depth() == 4)
71                     colString = " *  ";
72                 else
73                     colString = "    ";
74
75                 noColString = QString(" ").repeated(colString.length());
76
77                 dashIndent = "";
78                 switch (cur->depth()) {
79                 case 0:
80                     break; // Mapcenter (Ignored)
81                 case 1:
82                     break; // Mainbranch "Archive" (Ignored)
83                 case 2:    // Title: "Current week number..."
84                     out += "\n";
85                     out += underline(cur->getHeadingPlain(), QString("="));
86                     out += "\n";
87                     break;
88                 case 3: // Headings: "Achievement", "Bonus", "Objective", ...
89                     out += "\n";
90                     out += underline(cur->getHeadingPlain(), "-");
91                     out += "\n";
92                     break;
93                 default: // depth 4 and higher are the items we need to know
94                     Task *task = cur->getTask();
95                     if (task) {
96                         // Task status overrides other flags
97                         switch (task->getStatus()) {
98                         case Task::NotStarted:
99                             statusString = "[NOT STARTED]";
100                             break;
101                         case Task::WIP:
102                             statusString = "[WIP]";
103                             break;
104                         case Task::Finished:
105                             statusString = "[DONE]";
106                             break;
107                         }
108                     }
109                     else {
110                         if (cur->hasActiveFlag("hook-green"))
111                             statusString = "[DONE]";
112                         else if (cur->hasActiveFlag("wip"))
113                             statusString = "[WIP]";
114                         else if (cur->hasActiveFlag("cross-red"))
115                             statusString = "[NOT STARTED]";
116                     }
117
118                     line += colString;
119                     line += curIndent;
120                     if (cur->depth() > 3)
121                         line += cur->getHeadingPlain();
122
123                     // Pad line width before status
124                     i = 80 - line.length() - statusString.length() - 1;
125                     for (int j = 0; j < i; j++)
126                         line += " ";
127                     line += " " + statusString + "\n";
128
129                     out += line;
130
131                     // If necessary, write URL
132                     if (!cur->getURL().isEmpty())
133                         out += noColString + indent(cur->depth() - 4, false) +
134                                cur->getURL() + "\n";
135
136                     // If necessary, write note
137                     if (!cur->isNoteEmpty()) {
138                         curIndent = noColString +
139                                     indent(cur->depth() - 4, false) + "| ";
140                         s = cur->getNoteASCII(curIndent, 80);
141                         out += s + "\n";
142                     }
143                     break;
144                 }
145             }
146         }
147         model->nextBranch(cur, prev);
148     }
149
150     QTextStream ts(&file);
151     ts.setCodec("UTF-8");
152     ts << out;
153     file.close();
154
155     QClipboard *clipboard = QGuiApplication::clipboard();
156     clipboard->setText(out);
157
158     displayedDestination = filePath;
159
160     result = ExportBase::Success;
161
162     completeExport();
163 }
164
165 QString ExportAO::underline(const QString &text, const QString &line)
166 {
167     QString r = text + "\n";
168     for (int j = 0; j < text.length(); j++)
169         r += line;
170     return r;
171 }