Skip to content
Permalink
Browse files

Added option to repeat the last export on save

Also, unset the export file name when the map or tileset is saved under
a different name, since this likely means the previously used export
name should also be different (for now I tried to not be too smart about
this).

Closes #1610
  • Loading branch information...
bjorn committed Sep 4, 2019
1 parent b29d180 commit 749cd871c02c6d9fb48188c32b352dd7bc487c8a
@@ -637,6 +637,8 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
this, &MainWindow::closeDocument);
connect(mDocumentManager, &DocumentManager::reloadError,
this, &MainWindow::reloadError);
connect(mDocumentManager, &DocumentManager::documentSaved,
this, &MainWindow::documentSaved);

connect(mResetToDefaultLayout, &QAction::triggered, this, &MainWindow::resetToDefaultLayout);

@@ -995,42 +997,49 @@ bool MainWindow::confirmAllSave()
}

void MainWindow::export_()
{
if (!exportDocument(mDocument)) {
// fall back when no successful export happened
exportAs();
}
}

bool MainWindow::exportDocument(Document *document)
{
const QString exportFileName = mDocument->lastExportFileName();
if (exportFileName.isEmpty())
return false;

if (!exportFileName.isEmpty()) {
if (auto mapDocument = qobject_cast<MapDocument*>(mDocument)) {
if (MapFormat *exportFormat = mapDocument->exportFormat()) {
std::unique_ptr<Map> exportMap;
ExportHelper exportHelper;
const Map *map = exportHelper.prepareExportMap(mapDocument->map(), exportMap);
if (auto mapDocument = qobject_cast<MapDocument*>(mDocument)) {
if (MapFormat *exportFormat = mapDocument->exportFormat()) {
std::unique_ptr<Map> exportMap;
ExportHelper exportHelper;
const Map *map = exportHelper.prepareExportMap(mapDocument->map(), exportMap);

if (exportFormat->write(map, exportFileName, exportHelper.formatOptions())) {
mMapEditor->showMessage(tr("Exported to %1").arg(exportFileName), 3000);
return true;
}

if (exportFormat->write(map, exportFileName, exportHelper.formatOptions())) {
mMapEditor->showMessage(tr("Exported to %1").arg(exportFileName), 3000);
return;
}
QMessageBox::critical(this, tr("Error Exporting Map"),
exportFormat->errorString());
}
} else if (auto tilesetDocument = qobject_cast<TilesetDocument*>(mDocument)) {
if (TilesetFormat *exportFormat = tilesetDocument->exportFormat()) {
ExportHelper exportHelper;
const SharedTileset tileset = exportHelper.prepareExportTileset(tilesetDocument->tileset());

QMessageBox::critical(this, tr("Error Exporting Map"),
exportFormat->errorString());
if (exportFormat->write(*tileset, exportFileName, exportHelper.formatOptions())) {
mMapEditor->showMessage(tr("Exported to %1").arg(exportFileName), 3000);
return true;
}
} else if (auto tilesetDocument = qobject_cast<TilesetDocument*>(mDocument)) {
if (TilesetFormat *exportFormat = tilesetDocument->exportFormat()) {
ExportHelper exportHelper;
const SharedTileset tileset = exportHelper.prepareExportTileset(tilesetDocument->tileset());

if (exportFormat->write(*tileset, exportFileName, exportHelper.formatOptions())) {
mMapEditor->showMessage(tr("Exported to %1").arg(exportFileName), 3000);
return;
}

QMessageBox::critical(this, tr("Error Exporting Tileset"),
exportFormat->errorString());
}
QMessageBox::critical(this, tr("Error Exporting Tileset"),
exportFormat->errorString());
}
}

// fall back when no successful export happened
exportAs();
return false;
}

void MainWindow::exportAs()
@@ -1793,6 +1802,12 @@ void MainWindow::documentChanged(Document *document)
updateZoomable();
}

void MainWindow::documentSaved(Document *document)
{
if (Preferences::instance()->exportOnSave())
exportDocument(document);
}

void MainWindow::closeDocument(int index)
{
if (confirmSave(mDocumentManager->documents().at(index).data()))
@@ -114,6 +114,7 @@ class MainWindow : public QMainWindow
bool saveFileAs();
void saveAll();
void export_(); // 'export' is a reserved word
bool exportDocument(Document *document);
void exportAs();
void exportAsImage();
void reload();
@@ -154,6 +155,7 @@ class MainWindow : public QMainWindow
void openRecentFile();

void documentChanged(Document *document);
void documentSaved(Document *document);
void closeDocument(int index);

void reloadError(const QString &error);
@@ -136,7 +136,12 @@ bool MapDocument::save(const QString &fileName, QString *error)
}

undoStack()->setClean();
mMap->fileName = fileName;

if (mMap->fileName != fileName) {
mMap->fileName = fileName;
mMap->exportFileName.clear();
}

setFileName(fileName);
mLastSaved = QFileInfo(fileName).lastModified();

@@ -61,6 +61,7 @@ Preferences::Preferences()
(intValue("MapRenderOrder", Map::RightDown));
mDtdEnabled = boolValue("DtdEnabled");
mSafeSavingEnabled = boolValue("SafeSavingEnabled", true);
mExportOnSave = boolValue("ExportOnSave", false);
mReloadTilesetsOnChange = boolValue("ReloadTilesets", true);
mStampsDirectory = stringValue("StampsDirectory");
mTemplatesDirectory = stringValue("TemplatesDirectory");
@@ -408,6 +409,12 @@ void Preferences::setSafeSavingEnabled(bool enabled)
SaveFile::setSafeSavingEnabled(enabled);
}

void Preferences::setExportOnSave(bool enabled)
{
mExportOnSave = enabled;
mSettings->setValue(QLatin1String("Storage/ExportOnSave"), enabled);
}

void Preferences::setExportOption(Preferences::ExportOption option, bool value)
{
#if QT_VERSION >= 0x050700
@@ -95,6 +95,9 @@ class Preferences : public QObject
bool safeSavingEnabled() const;
void setSafeSavingEnabled(bool enabled);

bool exportOnSave() const;
void setExportOnSave(bool enabled);

enum ExportOption {
EmbedTilesets = 0x1,
DetachTemplateInstances = 0x2,
@@ -269,6 +272,7 @@ public slots:
Map::RenderOrder mMapRenderOrder;
bool mDtdEnabled;
bool mSafeSavingEnabled;
bool mExportOnSave;
ExportOptions mExportOptions;
QString mLanguage;
bool mReloadTilesetsOnChange;
@@ -398,6 +402,11 @@ inline bool Preferences::safeSavingEnabled() const
return mSafeSavingEnabled;
}

inline bool Preferences::exportOnSave() const
{
return mExportOnSave;
}

inline Preferences::ExportOptions Preferences::exportOptions() const
{
return mExportOptions;
@@ -86,6 +86,8 @@ PreferencesDialog::PreferencesDialog(QWidget *parent)
preferences, &Preferences::setOpenLastFilesOnStartup);
connect(mUi->safeSaving, &QCheckBox::toggled,
preferences, &Preferences::setSafeSavingEnabled);
connect(mUi->exportOnSave, &QCheckBox::toggled,
preferences, &Preferences::setExportOnSave);

connect(mUi->embedTilesets, &QCheckBox::toggled, preferences, [preferences] (bool value) {
preferences->setExportOption(Preferences::EmbedTilesets, value);
@@ -172,6 +174,7 @@ void PreferencesDialog::fromPreferences()
mUi->reloadTilesetImages->setChecked(prefs->reloadTilesetsOnChange());
mUi->openLastFiles->setChecked(prefs->openLastFilesOnStartup());
mUi->safeSaving->setChecked(prefs->safeSavingEnabled());
mUi->exportOnSave->setChecked(prefs->exportOnSave());

mUi->embedTilesets->setChecked(prefs->exportOption(Preferences::EmbedTilesets));
mUi->detachTemplateInstances->setChecked(prefs->exportOption(Preferences::DetachTemplateInstances));
@@ -57,6 +57,13 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="exportOnSave">
<property name="text">
<string>Repeat last export on save</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@@ -446,6 +453,7 @@
<tabstop>reloadTilesetImages</tabstop>
<tabstop>openLastFiles</tabstop>
<tabstop>safeSaving</tabstop>
<tabstop>exportOnSave</tabstop>
<tabstop>embedTilesets</tabstop>
<tabstop>detachTemplateInstances</tabstop>
<tabstop>resolveObjectTypesAndProperties</tabstop>
@@ -114,7 +114,11 @@ bool TilesetDocument::save(const QString &fileName, QString *error)

undoStack()->setClean();

mTileset->setFileName(fileName);
if (mTileset->fileName() != fileName) {
mTileset->setFileName(fileName);
mTileset->exportFileName.clear();
}

setFileName(fileName);

mLastSaved = QFileInfo(fileName).lastModified();

0 comments on commit 749cd87

Please sign in to comment.
You can’t perform that action at this time.