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 lastExportDir;
22 ExportBase::ExportBase() { init(); }
24 ExportBase::ExportBase(VymModel *m)
30 ExportBase::~ExportBase()
32 // Cleanup tmpdir: No longer required, part of general tmp dir of vym
35 // Remember current directory
36 lastExportDir = QDir(dirPath);
39 void ExportBase::init()
42 exportName = "unnamed";
46 defaultDirPath = lastExportDir.absolutePath();
47 dirPath = defaultDirPath;
50 void ExportBase::setupTmpDir()
53 tmpDir.setPath(makeTmpDir(ok, model->tmpDirPath(),
54 QString("export-%2").arg(exportName)));
55 if (!tmpDir.exists() || !ok)
56 QMessageBox::critical(
57 0, QObject::tr("Error"),
58 QObject::tr("Couldn't access temporary directory\n"));
61 void ExportBase::setDirPath(const QString &s)
65 // Otherwise lastExportDir is used, which defaults to current dir
68 QString ExportBase::getDirPath() { return dirPath; }
70 void ExportBase::setFilePath(const QString &s)
74 if (!filePath.startsWith("/"))
76 filePath = lastExportDir.absolutePath() + "/" + filePath;
80 QString ExportBase::getFilePath()
82 if (!filePath.isEmpty())
85 return dirPath + "/" + model->getMapName() + extension;
88 QString ExportBase::getMapName()
90 QString fn = basename(filePath);
91 return fn.left(fn.lastIndexOf("."));
94 void ExportBase::setModel(VymModel *m) { model = m; }
96 void ExportBase::setWindowTitle(const QString &s) { caption = s; }
98 void ExportBase::setName(const QString &s) { exportName = s; }
100 QString ExportBase::getName() { return exportName; }
102 void ExportBase::addFilter(const QString &s) { filter = s; }
104 void ExportBase::setListTasks(bool b) { listTasks = b; }
106 bool ExportBase::execDialog()
109 QFileDialog::getSaveFileName(nullptr, caption, filePath, filter, nullptr,
110 QFileDialog::DontConfirmOverwrite);
113 if (QFile(fn).exists()) {
115 dia.showCancelButton(true);
116 dia.setCaption(QObject::tr("Warning: Overwriting file"));
119 "Exporting to %1 will overwrite the existing file:\n%2")
122 dia.setShowAgainName("/exports/overwrite/" + exportName);
123 if (!(dia.exec() == QDialog::Accepted)) {
128 dirPath = fn.left(fn.lastIndexOf("/"));
135 bool ExportBase::canceled() { return cancelFlag; }
137 void ExportBase::setLastCommand(const QString &s) { lastCommand = s; }
139 void ExportBase::setResult(const Result &r)
144 void ExportBase::completeExport(QStringList args)
148 if (args.isEmpty()) {
149 // Add at least filepath as argument. exportName is added anyway
150 command = QString("vym.currentMap().exportMap(\"%1\",\"%2\")")
155 // Only add exportName as default, rest of arguments need to be passed
156 // (Cloud exports ahve no filename...)
157 command = QString("vym.currentMap().exportMap(\"%1\"")
160 foreach (QString arg, args)
161 command += QString(", \"%1\"").arg(arg);
166 model->setExportLastCommand(command);
167 model->setExportLastDestination(displayedDestination);
168 model->setExportLastDescription(exportName);
170 // Trigger saving of export command if it has changed
171 if (model && (lastCommand != command))
176 mainWindow->statusMessage(
177 QString("Exported as %1 to %2").arg(exportName).arg(displayedDestination));
180 mainWindow->statusMessage(QString("Failed to export as %1 to %2")
182 .arg(displayedDestination));
187 qWarning() << "Export base: undefined export result for " << exportName;
191 void ExportBase::completeExport()
193 completeExport(QStringList());
196 QString ExportBase::getSectionString(TreeItem *start)
198 // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
200 TreeItem *ti = start;
201 int depth = ti->depth();
203 r = QString("%1").arg(1 + ti->num(), 0, 10) + "." + r;
213 QString ExportBase::indent(const int &n, bool useBullet)
216 for (int i = 0; i < n; i++)
218 if (useBullet && s.length() >= 2 && bulletPoints.count() > n)
219 s.replace(s.length() - 2, 1, bulletPoints.at(n));