-
Notifications
You must be signed in to change notification settings - Fork 7
/
export-base.cpp
251 lines (215 loc) · 5.75 KB
/
export-base.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "export-base.h"
#include <cstdlib>
#include <QDebug>
#include <QFileDialog>
#include <QHash>
#include <QMessageBox>
#include "branchitem.h"
#include "file.h"
#include "linkablemapobj.h"
#include "misc.h"
#include "mainwindow.h"
#include "warningdialog.h"
#include "xsltproc.h"
#include "vymprocess.h"
extern Main *mainWindow;
//extern QDir vymBaseDir;
//extern QString flagsPath;
//extern QString vymName;
//extern QString vymVersion;
//extern QString vymHome;
extern Settings settings;
extern QDir lastExportDir;
ExportBase::ExportBase()
{
init();
}
ExportBase::ExportBase(VymModel *m)
{
model = m;
init();
}
ExportBase::~ExportBase()
{
// Cleanup tmpdir: No longer required, part of general tmp dir of vym instance now
// Remember current directory
lastExportDir = QDir(dirPath);
}
void ExportBase::init()
{
indentPerDepth = " ";
exportName = "unnamed";
lastCommand = "";
cancelFlag = false;
success = false;
defaultDirPath = lastExportDir.absolutePath();
dirPath = defaultDirPath;
}
void ExportBase::setupTmpDir()
{
bool ok;
tmpDir.setPath (
makeTmpDir(ok,
model->tmpDirPath(),
QString("export-%2").arg(exportName)));
if (!tmpDir.exists() || !ok)
QMessageBox::critical(
0,
QObject::tr( "Error" ),
QObject::tr("Couldn't access temporary directory\n"));
}
void ExportBase::setDirPath (const QString &s)
{
if (!s.isEmpty())
dirPath = s;
// Otherwise lastExportDir is used, which defaults to current dir
}
QString ExportBase::getDirPath()
{
return dirPath;
}
void ExportBase::setFilePath (const QString &s)
{
if(!s.isEmpty())
{
filePath = s;
if (!filePath.startsWith("/"))
// Absolute path
filePath = lastExportDir.absolutePath() + "/" + filePath;
}
}
QString ExportBase::getFilePath ()
{
if (!filePath.isEmpty())
return filePath;
else
return dirPath + "/" + model->getMapName() + extension;
}
QString ExportBase::getMapName ()
{
QString fn = basename(filePath);
return fn.left(fn.lastIndexOf("."));
}
void ExportBase::setModel(VymModel *m)
{
model = m;
}
void ExportBase::setWindowTitle (const QString &s)
{
caption = s;
}
void ExportBase::setName (const QString &s)
{
exportName = s;
}
QString ExportBase::getName ()
{
return exportName;
}
void ExportBase::addFilter(const QString &s)
{
filter = s;
}
void ExportBase::setListTasks(bool b)
{
listTasks = b;
}
bool ExportBase::execDialog()
{
QString fn=QFileDialog::getSaveFileName(
NULL,
caption,
dirPath,
filter,
NULL,
QFileDialog::DontConfirmOverwrite);
if (!fn.isEmpty() )
{
if (QFile (fn).exists() )
{
WarningDialog dia;
dia.showCancelButton (true);
dia.setCaption(QObject::tr("Warning: Overwriting file"));
dia.setText(QObject::tr("Exporting to %1 will overwrite the existing file:\n%2").arg(exportName).arg(fn));
dia.setShowAgainName("/exports/overwrite/" + exportName);
if (!(dia.exec() == QDialog::Accepted))
{
cancelFlag = true;
return false;
}
}
dirPath = fn.left(fn.lastIndexOf ("/"));
filePath = fn;
return true;
}
return false;
}
bool ExportBase::canceled()
{
return cancelFlag;
}
void ExportBase::setLastCommand( const QString &s)
{
lastCommand = s;
}
void ExportBase::completeExport(QMap <QString, QString> args)
{
QString command;
QMapIterator <QString, QString> i(args);
if (args.isEmpty())
{
// Add at least filepath as argument. exportName is added anyway
command = QString("vym.currentMap().exportMap(\"%1\",\"%2\")").arg(exportName).arg(filePath);
settings.setLocalValue ( model->getFilePath(), "/export/last/destination", filePath);
} else
{
QStringList list;
i.toBack();
while (i.hasPrevious() )
{
i.previous();
list << "\"" + i.value() + "\"";
settings.setLocalValue ( model->getFilePath(), "/export/" + exportName.toLower() + "/" + i.key(), i.value() );
}
command = QString("vym.currentMap().exportMap(\"%1\",%2)").arg(exportName).arg(list.join(","));
}
settings.setLocalValue ( model->getFilePath(), "/export/last/command", command);
settings.setLocalValue ( model->getFilePath(), "/export/last/description", exportName);
settings.setLocalValue ( model->getFilePath(), "/export/last/destination", destination);
// Trigger saving of export command if it has changed
if (model && (lastCommand != command) ) model->setChanged();
if (success)
mainWindow->statusMessage(QString("Exported as %1 to %2").arg(exportName).arg(destination));
else
mainWindow->statusMessage(QString("Failed to export as %1 to %2").arg(exportName).arg(destination));
}
void ExportBase::completeExport()
{
QMap <QString, QString> args;
completeExport (args);
}
QString ExportBase::getSectionString(TreeItem *start)
{
// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
QString r;
TreeItem *ti = start;
int depth=ti->depth();
while (depth>0)
{
r = QString("%1").arg(1 + ti->num(),0,10) + "." + r;
ti = ti->parent();
depth = ti->depth();
}
if (r.isEmpty())
return r;
else
return r + " ";
}
QString ExportBase::indent (const int &n, bool useBullet)
{
QString s;
for (int i=0; i<n; i++) s += indentPerDepth;
if (useBullet && s.length() >= 2 && bulletPoints.count() > n)
s.replace( s.length() - 2, 1, bulletPoints.at(n) );
return s;
}