Ask user about unsaved changes when closing tileset editor window

This commit is contained in:
Marcus Huderle 2018-10-02 19:01:31 -05:00
parent da13b8ea5e
commit cf150f3ce9
4 changed files with 51 additions and 7 deletions

View file

@ -94,6 +94,7 @@ private slots:
void onAddNewMapToGroupClick(QAction* triggeredAction);
void onTilesetChanged(QString);
void currentMetatilesSelectionChanged();
void onTilesetEditorClosed();
void on_action_Export_Map_Image_triggered();

View file

@ -19,6 +19,7 @@ public:
explicit TilesetEditor(Project*, QString, QString, QWidget *parent = nullptr);
~TilesetEditor();
void setTilesets(QString, QString);
void init(Project*, QString, QString);
private slots:
void onHoveredMetatileChanged(uint16_t);
@ -46,6 +47,7 @@ private slots:
void on_actionImport_Secondary_Tiles_triggered();
private:
void closeEvent(QCloseEvent*);
void initMetatileSelector();
void initTileSelector();
void initSelectedTileItem();
@ -62,8 +64,9 @@ private:
int paletteId;
bool tileXFlip;
bool tileYFlip;
Tileset *primaryTileset;
Tileset *secondaryTileset;
bool hasUnsavedChanges;
Tileset *primaryTileset = nullptr;
Tileset *secondaryTileset = nullptr;
QGraphicsScene *metatilesScene = nullptr;
QGraphicsScene *tilesScene = nullptr;
QGraphicsScene *selectedTileScene = nullptr;
@ -72,6 +75,7 @@ private:
signals:
void tilesetsSaved(QString, QString);
void closed();
};
#endif // TILESETEDITOR_H

View file

@ -1246,11 +1246,21 @@ void MainWindow::on_actionTileset_Editor_triggered()
if (!this->tilesetEditor) {
this->tilesetEditor = new TilesetEditor(this->editor->project, this->editor->map->layout->tileset_primary_label, this->editor->map->layout->tileset_secondary_label, this);
connect(this->tilesetEditor, SIGNAL(tilesetsSaved(QString, QString)), this, SLOT(onTilesetsSaved(QString, QString)));
connect(this->tilesetEditor, SIGNAL(closed()), this, SLOT(onTilesetEditorClosed()));
}
if (!this->tilesetEditor->isVisible()) {
this->tilesetEditor->show();
} else if (this->tilesetEditor->isMinimized()) {
this->tilesetEditor->showNormal();
} else {
this->tilesetEditor->activateWindow();
}
}
void MainWindow::onTilesetEditorClosed() {
if (this->tilesetEditor) {
delete this->tilesetEditor;
this->tilesetEditor = nullptr;
}
}

View file

@ -9,15 +9,27 @@ TilesetEditor::TilesetEditor(Project *project, QString primaryTilesetLabel, QStr
QMainWindow(parent),
ui(new Ui::TilesetEditor)
{
this->init(project, primaryTilesetLabel, secondaryTilesetLabel);
}
TilesetEditor::~TilesetEditor()
{
delete ui;
}
void TilesetEditor::init(Project *project, QString primaryTilesetLabel, QString secondaryTilesetLabel) {
ui->setupUi(this);
this->project = project;
this->hasUnsavedChanges = false;
this->tileXFlip = ui->checkBox_xFlip->isChecked();
this->tileYFlip = ui->checkBox_yFlip->isChecked();
this->paletteId = ui->spinBox_paletteSelector->value();
Tileset *primaryTileset = project->getTileset(primaryTilesetLabel);
Tileset *secondaryTileset = project->getTileset(secondaryTilesetLabel);
if (this->primaryTileset) delete this->primaryTileset;
if (this->secondaryTileset) delete this->secondaryTileset;
this->primaryTileset = primaryTileset->copy();
this->secondaryTileset = secondaryTileset->copy();
@ -38,11 +50,6 @@ TilesetEditor::TilesetEditor(Project *project, QString primaryTilesetLabel, QStr
this->metatileSelector->select(0);
}
TilesetEditor::~TilesetEditor()
{
delete ui;
}
void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTilesetLabel) {
delete this->primaryTileset;
delete this->secondaryTileset;
@ -202,6 +209,7 @@ void TilesetEditor::onMetatileLayerTileChanged(int x, int y) {
this->metatileSelector->draw();
this->metatileLayersItem->draw();
this->hasUnsavedChanges = true;
}
void TilesetEditor::on_spinBox_paletteSelector_valueChanged(int paletteId)
@ -245,6 +253,7 @@ void TilesetEditor::on_actionSave_Tileset_triggered()
this->project->saveTilesets(this->primaryTileset, this->secondaryTileset);
emit this->tilesetsSaved(this->primaryTileset->name, this->secondaryTileset->name);
this->ui->statusbar->showMessage(QString("Saved primary and secondary Tilesets!"), 5000);
this->hasUnsavedChanges = false;
}
void TilesetEditor::on_actionImport_Primary_Tiles_triggered()
@ -319,4 +328,24 @@ void TilesetEditor::importTilesetTiles(Tileset *tileset, bool primary) {
this->project->loadTilesetTiles(tileset, image);
this->project->loadTilesetMetatiles(tileset);
this->refresh();
this->hasUnsavedChanges = true;
}
void TilesetEditor::closeEvent(QCloseEvent *event)
{
bool close = true;
if (this->hasUnsavedChanges) {
QMessageBox::StandardButton result = QMessageBox::question(this, "porymap",
"Discard unsaved Tileset changes?",
QMessageBox::No | QMessageBox::Yes,
QMessageBox::Yes);
close = result == QMessageBox::Yes;
}
if (close) {
event->accept();
emit closed();
} else {
event->ignore();
}
}