1 #include "export-base.h"
10 #include "branchitem.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
15 #include "vymprocess.h"
16 #include "warningdialog.h"
19 extern Main *mainWindow;
20 // extern QDir vymBaseDir;
21 // extern QString flagsPath;
22 // extern QString vymName;
23 // extern QString vymVersion;
24 // extern QString vymHome;
25 extern Settings settings;
26 extern QDir lastExportDir;
28 ExportBase::ExportBase() { init(); }
30 ExportBase::ExportBase(VymModel *m)
36 ExportBase::~ExportBase()
38 // Cleanup tmpdir: No longer required, part of general tmp dir of vym
41 // Remember current directory
42 lastExportDir = QDir(dirPath);
45 void ExportBase::init()
48 exportName = "unnamed";
52 defaultDirPath = lastExportDir.absolutePath();
53 dirPath = defaultDirPath;
56 void ExportBase::setupTmpDir()
59 tmpDir.setPath(makeTmpDir(ok, model->tmpDirPath(),
60 QString("export-%2").arg(exportName)));
61 if (!tmpDir.exists() || !ok)
62 QMessageBox::critical(
63 0, QObject::tr("Error"),
64 QObject::tr("Couldn't access temporary directory\n"));
67 void ExportBase::setDirPath(const QString &s)
71 // Otherwise lastExportDir is used, which defaults to current dir
74 QString ExportBase::getDirPath() { return dirPath; }
76 void ExportBase::setFilePath(const QString &s)
80 if (!filePath.startsWith("/"))
82 filePath = lastExportDir.absolutePath() + "/" + filePath;
86 QString ExportBase::getFilePath()
88 if (!filePath.isEmpty())
91 return dirPath + "/" + model->getMapName() + extension;
94 QString ExportBase::getMapName()
96 QString fn = basename(filePath);
97 return fn.left(fn.lastIndexOf("."));
100 void ExportBase::setModel(VymModel *m) { model = m; }
102 void ExportBase::setWindowTitle(const QString &s) { caption = s; }
104 void ExportBase::setName(const QString &s) { exportName = s; }
106 QString ExportBase::getName() { return exportName; }
108 void ExportBase::addFilter(const QString &s) { filter = s; }
110 void ExportBase::setListTasks(bool b) { listTasks = b; }
112 bool ExportBase::execDialog()
115 QFileDialog::getSaveFileName(nullptr, caption, filePath, filter, nullptr,
116 QFileDialog::DontConfirmOverwrite);
119 if (QFile(fn).exists()) {
121 dia.showCancelButton(true);
122 dia.setCaption(QObject::tr("Warning: Overwriting file"));
125 "Exporting to %1 will overwrite the existing file:\n%2")
128 dia.setShowAgainName("/exports/overwrite/" + exportName);
129 if (!(dia.exec() == QDialog::Accepted)) {
134 dirPath = fn.left(fn.lastIndexOf("/"));
141 bool ExportBase::canceled() { return cancelFlag; }
143 void ExportBase::setLastCommand(const QString &s) { lastCommand = s; }
145 void ExportBase::setResult(const Result &r)
150 void ExportBase::completeExport(QStringList args)
154 if (args.isEmpty()) {
155 // Add at least filepath as argument. exportName is added anyway
156 command = QString("vym.currentMap().exportMap(\"%1\",\"%2\")")
161 // Only add exportName as default, rest of arguments need to be passed
162 // (Cloud exports ahve no filename...)
163 command = QString("vym.currentMap().exportMap(\"%1\"")
166 foreach (QString arg, args)
167 command += QString(", \"%1\"").arg(arg);
172 settings.setLocalValue(model->getFilePath(), "/export/last/command",
174 settings.setLocalValue(model->getFilePath(), "/export/last/description",
176 settings.setLocalValue(model->getFilePath(), "/export/last/displayedDestination",
177 displayedDestination);
179 // Trigger saving of export command if it has changed
180 if (model && (lastCommand != command))
185 mainWindow->statusMessage(
186 QString("Exported as %1 to %2").arg(exportName).arg(displayedDestination));
189 mainWindow->statusMessage(QString("Failed to export as %1 to %2")
191 .arg(displayedDestination));
196 qWarning() << "Export base: undefined export result for " << exportName;
200 void ExportBase::completeExport()
202 completeExport(QStringList());
205 QString ExportBase::getSectionString(TreeItem *start)
207 // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
209 TreeItem *ti = start;
210 int depth = ti->depth();
212 r = QString("%1").arg(1 + ti->num(), 0, 10) + "." + r;
222 QString ExportBase::indent(const int &n, bool useBullet)
225 for (int i = 0; i < n; i++)
227 if (useBullet && s.length() >= 2 && bulletPoints.count() > n)
228 s.replace(s.length() - 2, 1, bulletPoints.at(n));