From ea75ad215193c97405a87d8188959b4108435dd4 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 9 Jan 2022 23:05:01 +0100 Subject: [PATCH] Fixed rename errors in dialogs, implemented lots more --- dialogs/backgroundpropertiesdialog.cpp | 98 ++++++++++++-- dialogs/backgroundpropertiesdialog.h | 12 +- dialogs/backgroundpropertiesdialog.ui | 115 ++++++++-------- dialogs/createspritedialog.cpp | 9 +- dialogs/extensionpackagesdialog.cpp | 8 ++ dialogs/extensionpackagesdialog.ui | 2 +- dialogs/fontpropertiesdialog.cpp | 46 ++++++- dialogs/fontpropertiesdialog.h | 7 +- dialogs/fontpropertiesdialog.ui | 74 ++++------ dialogs/globalgamesettingsdialog.cpp | 8 ++ dialogs/pathpropertiesdialog.cpp | 61 ++++++++- dialogs/pathpropertiesdialog.h | 16 ++- dialogs/pathpropertiesdialog.ui | 60 +++++++++ dialogs/scriptpropertiesdialog.cpp | 39 +++++- dialogs/scriptpropertiesdialog.h | 12 +- dialogs/soundpropertiesdialog.cpp | 48 ++++++- dialogs/soundpropertiesdialog.h | 8 +- dialogs/soundpropertiesdialog.ui | 71 ++++------ dialogs/spritepropertiesdialog.cpp | 63 +++++++-- dialogs/spritepropertiesdialog.h | 7 +- dialogs/spritepropertiesdialog.ui | 71 ++++------ mainwindow.cpp | 12 +- projectcontainer.cpp | 2 + projectcontainer.h | 1 + projecttreemodel.cpp | 180 +++++++++++++++++++++++++ projecttreemodel.h | 14 ++ 26 files changed, 792 insertions(+), 252 deletions(-) diff --git a/dialogs/backgroundpropertiesdialog.cpp b/dialogs/backgroundpropertiesdialog.cpp index 599eeba..78ce2a8 100644 --- a/dialogs/backgroundpropertiesdialog.cpp +++ b/dialogs/backgroundpropertiesdialog.cpp @@ -6,22 +6,32 @@ #include #include "projectcontainer.h" +#include "projecttreemodel.h" #include "imageeditordialog.h" -BackgroundPropertiesDialog::BackgroundPropertiesDialog(Background &background, QWidget *parent) : +BackgroundPropertiesDialog::BackgroundPropertiesDialog(Background &background, ProjectTreeModel &projectModel, QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()}, - m_background{background} + m_background{background}, + m_projectModel{projectModel} { m_ui->setupUi(this); - setWindowTitle(tr("Background Properties: %0").arg(m_background.name)); + updateTitle(); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); m_ui->lineEditName->setText(m_background.name); - m_ui->labelWidth->setText(tr("Width: %0").arg(m_background.pixmap.width())); - m_ui->labelHeight->setText(tr("Height: %0").arg(m_background.pixmap.height())); + updateSpriteInfo(); + m_ui->checkBoxTileset->setChecked(m_background.tileset); m_ui->labelPreview->setPixmap(m_background.pixmap); + connect(&m_projectModel, &ProjectTreeModel::backgroundNameChanged, + this, &BackgroundPropertiesDialog::backgroundNameChanged); + connect(m_ui->pushButtonLoad, &QAbstractButton::pressed, this, &BackgroundPropertiesDialog::loadBackground); connect(m_ui->pushButtonSave, &QAbstractButton::pressed, @@ -41,11 +51,16 @@ void BackgroundPropertiesDialog::accept() { if (m_background.name != m_ui->lineEditName->text()) { - QMessageBox::critical(this, tr("Not implemented"), tr("Changing the name is not yet implemented!")); - return; + if (!m_projectModel.renameBackground(m_background, m_ui->lineEditName->text())) + { + QMessageBox::critical(this, tr("Renaming Background failed!"), tr("Renaming Background failed!")); + return; + } } - // TODO + if (m_newPixmap) + m_background.pixmap = std::move(*m_newPixmap); + m_background.tileset = m_ui->checkBoxTileset->isChecked(); QDialog::accept(); } @@ -82,12 +97,45 @@ void BackgroundPropertiesDialog::reject() void BackgroundPropertiesDialog::loadBackground() { - QFileDialog::getOpenFileName(this, tr("Open a Background Image...")); + const auto path = QFileDialog::getOpenFileName(this, tr("Open a Background Image..."), {}, tr("BMP Files (*.bmp), PNG Files (*png)")); + if (path.isEmpty()) + return; + + QPixmap pixmap; + if (!pixmap.load(path)) + { + QMessageBox::warning(this, tr("Could not load sprite!"), tr("Could not load sprite!")); + return; + } + + m_ui->labelPreview->setPixmap(pixmap); + + m_newPixmap = std::move(pixmap); + m_unsavedChanges = true; + + updateTitle(); + updateSpriteInfo(); } void BackgroundPropertiesDialog::saveBackground() { - QFileDialog::getSaveFileName(this, tr("Save a Background Image..."), m_background.name + ".png", tr("PNG Files (*.png)")); + const auto &pixmap = m_newPixmap ? *m_newPixmap : m_background.pixmap; + + if (pixmap.isNull()) + { + QMessageBox::warning(this, tr("No background available to save!"), tr("No background available to save!")); + return; + } + + const auto path = QFileDialog::getSaveFileName(this, tr("Save a Background Image..."), m_background.name + ".png", tr("PNG Files (*.png)")); + if (path.isEmpty()) + return; + + if (!pixmap.save(path)) + { + QMessageBox::warning(this, tr("Could not save Background!"), tr("Could not save Background!")); + return; + } } void BackgroundPropertiesDialog::editBackground() @@ -99,7 +147,35 @@ void BackgroundPropertiesDialog::changed() { if (!m_unsavedChanges) { - setWindowTitle(tr("Background Properties: %0*").arg(m_background.name)); m_unsavedChanges = true; + updateTitle(); } } + +void BackgroundPropertiesDialog::backgroundNameChanged(const Background &background) +{ + if (&background != &m_background) + return; + + { + QSignalBlocker blocker{m_ui->lineEditName}; + m_ui->lineEditName->setText(background.name); + } + + updateTitle(); +} + +void BackgroundPropertiesDialog::updateTitle() +{ + setWindowTitle(tr("Background Properties: %0%1") + .arg(m_background.name) + .arg(m_unsavedChanges ? tr("*") : QString{}) + ); +} + +void BackgroundPropertiesDialog::updateSpriteInfo() +{ + const auto &pixmap = m_newPixmap ? *m_newPixmap : m_background.pixmap; + m_ui->labelWidth->setText(tr("Width: %0").arg(pixmap.width())); + m_ui->labelHeight->setText(tr("Height: %0").arg(pixmap.height())); +} diff --git a/dialogs/backgroundpropertiesdialog.h b/dialogs/backgroundpropertiesdialog.h index 670e7ab..a349ea5 100644 --- a/dialogs/backgroundpropertiesdialog.h +++ b/dialogs/backgroundpropertiesdialog.h @@ -4,16 +4,18 @@ #include #include +#include namespace Ui { class BackgroundPropertiesDialog; } struct Background; +class ProjectTreeModel; class BackgroundPropertiesDialog : public QDialog { Q_OBJECT public: - explicit BackgroundPropertiesDialog(Background &background, QWidget *parent = nullptr); + explicit BackgroundPropertiesDialog(Background &background, ProjectTreeModel &projectModel, QWidget *parent = nullptr); ~BackgroundPropertiesDialog(); void accept() override; @@ -26,10 +28,18 @@ private slots: void changed(); + void backgroundNameChanged(const Background &background); + private: + void updateTitle(); + void updateSpriteInfo(); + const std::unique_ptr m_ui; Background &m_background; + ProjectTreeModel &m_projectModel; bool m_unsavedChanges{}; + + std::optional m_newPixmap; }; diff --git a/dialogs/backgroundpropertiesdialog.ui b/dialogs/backgroundpropertiesdialog.ui index f119c4a..9bd2054 100644 --- a/dialogs/backgroundpropertiesdialog.ui +++ b/dialogs/backgroundpropertiesdialog.ui @@ -6,8 +6,8 @@ 0 0 - 573 - 221 + 597 + 295 @@ -44,6 +44,19 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -95,6 +108,19 @@ + + + + Qt::Vertical + + + + 20 + 40 + + + + @@ -103,51 +129,14 @@ - - - - - Qt::Horizontal - - - QSizePolicy::Minimum - - - - 40 - 20 - - - - - - - - &OK - - - - :/qtgameengine/icons/ok.png:/qtgameengine/icons/ok.png - - - - - - - Qt::Horizontal - - - QSizePolicy::Minimum - - - - 40 - 20 - - - - - + + + QDialogButtonBox::Ok + + + true + + @@ -161,8 +150,8 @@ 0 0 - 371 - 201 + 396 + 275 @@ -178,18 +167,34 @@ - pushButtonOk - pressed() + buttonBox + accepted() BackgroundPropertiesDialog accept() - 124 - 197 + 96 + 272 - 286 - 110 + 298 + 147 + + + + + buttonBox + rejected() + BackgroundPropertiesDialog + reject() + + + 96 + 272 + + + 298 + 147 diff --git a/dialogs/createspritedialog.cpp b/dialogs/createspritedialog.cpp index f694e67..d49520e 100644 --- a/dialogs/createspritedialog.cpp +++ b/dialogs/createspritedialog.cpp @@ -1,6 +1,8 @@ #include "createspritedialog.h" #include "ui_createspritedialog.h" +#include + CreateSpriteDialog::CreateSpriteDialog(QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()} @@ -8,9 +10,14 @@ CreateSpriteDialog::CreateSpriteDialog(QWidget *parent) : m_ui->setupUi(this); setWindowFlags(windowFlags() - & ~Qt::Dialog + & ~Qt::Dialog | Qt::Window | Qt::WindowCloseButtonHint); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); } CreateSpriteDialog::CreateSpriteDialog(const QSize &size, QWidget *parent) : diff --git a/dialogs/extensionpackagesdialog.cpp b/dialogs/extensionpackagesdialog.cpp index 769d5e4..eb31f4d 100644 --- a/dialogs/extensionpackagesdialog.cpp +++ b/dialogs/extensionpackagesdialog.cpp @@ -6,12 +6,20 @@ ExtensionPackagesDialog::ExtensionPackagesDialog(QWidget *parent) : m_ui{std::make_unique()} { m_ui->setupUi(this); + setWindowFlags(windowFlags() & ~Qt::Dialog | Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); + m_ui->buttonBox->addButton(tr("Install"), QDialogButtonBox::ActionRole) + ->setIcon(QIcon{":/qtgameengine/icons/extension-packages-file.png"}); } ExtensionPackagesDialog::~ExtensionPackagesDialog() = default; diff --git a/dialogs/extensionpackagesdialog.ui b/dialogs/extensionpackagesdialog.ui index ae35576..c3da11f 100644 --- a/dialogs/extensionpackagesdialog.ui +++ b/dialogs/extensionpackagesdialog.ui @@ -168,7 +168,7 @@ - QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok + QDialogButtonBox::Help|QDialogButtonBox::Ok diff --git a/dialogs/fontpropertiesdialog.cpp b/dialogs/fontpropertiesdialog.cpp index 4d51d19..c2de8e0 100644 --- a/dialogs/fontpropertiesdialog.cpp +++ b/dialogs/fontpropertiesdialog.cpp @@ -5,15 +5,22 @@ #include #include "projectcontainer.h" +#include "projecttreemodel.h" -FontPropertiesDialog::FontPropertiesDialog(Font &font, QWidget *parent) : +FontPropertiesDialog::FontPropertiesDialog(Font &font, ProjectTreeModel &projectModel, QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()}, - m_font{font} + m_font{font}, + m_projectModel{projectModel} { m_ui->setupUi(this); - setWindowTitle(tr("Font Properties: %0").arg(m_font.name)); + updateTitle(); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); m_ui->lineEditName->setText(m_font.name); m_ui->fontComboBox->setCurrentFont(m_font.font); @@ -25,6 +32,9 @@ FontPropertiesDialog::FontPropertiesDialog(Font &font, QWidget *parent) : m_ui->labelPreview->setFont(currentFont()); + connect(&m_projectModel, &ProjectTreeModel::fontNameChanged, + this, &FontPropertiesDialog::fontNameChanged); + connect(m_ui->pushButtonNormal, &QAbstractButton::pressed, this, &FontPropertiesDialog::normalRange); connect(m_ui->pushButtonDigits, &QAbstractButton::pressed, @@ -56,8 +66,11 @@ void FontPropertiesDialog::accept() { if (m_font.name != m_ui->lineEditName->text()) { - QMessageBox::critical(this, tr("Not implemented"), tr("Changing the name is not yet implemented!")); - return; + if (!m_projectModel.renameFont(m_font, m_ui->lineEditName->text())) + { + QMessageBox::critical(this, tr("Renaming Font failed!"), tr("Renaming Font failed!")); + return; + } } m_font.font = currentFont(); @@ -128,11 +141,32 @@ void FontPropertiesDialog::changed() m_ui->labelPreview->setFont(currentFont()); if (!m_unsavedChanges) { - setWindowTitle(tr("Font Properties: %0*").arg(m_font.name)); m_unsavedChanges = true; + updateTitle(); } } +void FontPropertiesDialog::fontNameChanged(const Font &font) +{ + if (&font != &m_font) + return; + + { + QSignalBlocker blocker{m_ui->lineEditName}; + m_ui->lineEditName->setText(font.name); + } + + updateTitle(); +} + +void FontPropertiesDialog::updateTitle() +{ + setWindowTitle(tr("Font Properties: %0%1") + .arg(m_font.name) + .arg(m_unsavedChanges ? tr("*") : QString{}) + ); +} + QFont FontPropertiesDialog::currentFont() const { QFont font; diff --git a/dialogs/fontpropertiesdialog.h b/dialogs/fontpropertiesdialog.h index 2943e9a..35a24b7 100644 --- a/dialogs/fontpropertiesdialog.h +++ b/dialogs/fontpropertiesdialog.h @@ -6,13 +6,14 @@ namespace Ui { class FontPropertiesDialog; } struct Font; +class ProjectTreeModel; class FontPropertiesDialog : public QDialog { Q_OBJECT public: - explicit FontPropertiesDialog(Font &font, QWidget *parent = nullptr); + explicit FontPropertiesDialog(Font &font, ProjectTreeModel &projectModel, QWidget *parent = nullptr); ~FontPropertiesDialog(); void accept() override; @@ -26,12 +27,16 @@ private slots: void changed(); + void fontNameChanged(const Font &font); + private: + void updateTitle(); QFont currentFont() const; const std::unique_ptr m_ui; Font &m_font; + ProjectTreeModel &m_projectModel; bool m_unsavedChanges{}; }; diff --git a/dialogs/fontpropertiesdialog.ui b/dialogs/fontpropertiesdialog.ui index 0a58548..ff0e779 100644 --- a/dialogs/fontpropertiesdialog.ui +++ b/dialogs/fontpropertiesdialog.ui @@ -6,7 +6,7 @@ 0 0 - 266 + 338 451 @@ -54,8 +54,7 @@ - - + @@ -193,45 +192,14 @@ - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - &OK - - - - :/qtgameengine/icons/ok.png:/qtgameengine/icons/ok.png - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - + + + QDialogButtonBox::Ok + + + true + + @@ -240,14 +208,30 @@ - pushButtonOk - pressed() + buttonBox + accepted() FontPropertiesDialog accept() 168 - 428 + 429 + + + 168 + 225 + + + + + buttonBox + rejected() + FontPropertiesDialog + reject() + + + 168 + 429 168 diff --git a/dialogs/globalgamesettingsdialog.cpp b/dialogs/globalgamesettingsdialog.cpp index 8e275db..bfa8504 100644 --- a/dialogs/globalgamesettingsdialog.cpp +++ b/dialogs/globalgamesettingsdialog.cpp @@ -1,17 +1,25 @@ #include "globalgamesettingsdialog.h" #include "ui_globalgamesettingsdialog.h" +#include + GlobalGameSettingsDialog::GlobalGameSettingsDialog(QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()} { m_ui->setupUi(this); + setWindowFlags(windowFlags() & ~Qt::Dialog | Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); } GlobalGameSettingsDialog::~GlobalGameSettingsDialog() = default; diff --git a/dialogs/pathpropertiesdialog.cpp b/dialogs/pathpropertiesdialog.cpp index fbf3f98..b7d9e27 100644 --- a/dialogs/pathpropertiesdialog.cpp +++ b/dialogs/pathpropertiesdialog.cpp @@ -5,18 +5,20 @@ #include #include "projectcontainer.h" +#include "projecttreemodel.h" -PathPropertiesDialog::PathPropertiesDialog(Path &path, QWidget *parent) : +PathPropertiesDialog::PathPropertiesDialog(Path &path, ProjectTreeModel &projectModel, QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()}, m_path{path}, + m_projectModel{projectModel}, m_labelX{new QLabel{tr("x: %0").arg(0)}}, m_labelY{new QLabel{tr("y: %0").arg(0)}}, m_labelArea{new QLabel{tr("Area: (%0,%1)->(%2,%3)").arg(0).arg(0).arg(0).arg(0)}} { m_ui->setupUi(this); - setWindowTitle(tr("Path Properties: %0").arg(m_path.name)); + updateTitle(); m_labelX->setFrameStyle(QFrame::Sunken); m_ui->statusbar->addWidget(m_labelX, 1); @@ -33,6 +35,16 @@ PathPropertiesDialog::PathPropertiesDialog(Path &path, QWidget *parent) : m_ui->lineEditName->setText(m_path.name); + connect(&m_projectModel, &ProjectTreeModel::pathNameChanged, + this, &PathPropertiesDialog::pathNameChanged); + + connect(m_ui->pushButtonAdd, &QAbstractButton::pressed, + this, &PathPropertiesDialog::add); + connect(m_ui->pushButtonInsert, &QAbstractButton::pressed, + this, &PathPropertiesDialog::insert); + connect(m_ui->pushButtonDelete, &QAbstractButton::pressed, + this, &PathPropertiesDialog::delete_); + connect(m_ui->lineEditName, &QLineEdit::textChanged, this, &PathPropertiesDialog::changed); } @@ -43,8 +55,11 @@ void PathPropertiesDialog::accept() { if (m_path.name != m_ui->lineEditName->text()) { - QMessageBox::critical(this, tr("Not implemented"), tr("Changing the name is not yet implemented!")); - return; + if (!m_projectModel.renamePath(m_path, m_ui->lineEditName->text())) + { + QMessageBox::critical(this, tr("Renaming Path failed!"), tr("Renaming Path failed!")); + return; + } } // TODO @@ -82,11 +97,47 @@ void PathPropertiesDialog::reject() } } +void PathPropertiesDialog::add() +{ + QMessageBox::warning(this, tr("Not yet implemented"), tr("Not yet implemented")); +} + +void PathPropertiesDialog::insert() +{ + QMessageBox::warning(this, tr("Not yet implemented"), tr("Not yet implemented")); +} + +void PathPropertiesDialog::delete_() +{ + QMessageBox::warning(this, tr("Not yet implemented"), tr("Not yet implemented")); +} + void PathPropertiesDialog::changed() { if (!m_unsavedChanges) { - setWindowTitle(tr("Path Properties: %0*").arg(m_path.name)); m_unsavedChanges = true; + updateTitle(); } } + +void PathPropertiesDialog::pathNameChanged(const Path &path) +{ + if (&path != &m_path) + return; + + { + QSignalBlocker blocker{m_ui->lineEditName}; + m_ui->lineEditName->setText(path.name); + } + + updateTitle(); +} + +void PathPropertiesDialog::updateTitle() +{ + setWindowTitle(tr("Path Properties: %0%1") + .arg(m_path.name) + .arg(m_unsavedChanges ? tr("*") : QString{}) + ); +} diff --git a/dialogs/pathpropertiesdialog.h b/dialogs/pathpropertiesdialog.h index bbe8897..9db12a3 100644 --- a/dialogs/pathpropertiesdialog.h +++ b/dialogs/pathpropertiesdialog.h @@ -7,29 +7,39 @@ class QLabel; namespace Ui { class PathPropertiesDialog; } struct Path; +class ProjectTreeModel; class PathPropertiesDialog : public QDialog { Q_OBJECT public: - explicit PathPropertiesDialog(Path &path, QWidget *parent = nullptr); + explicit PathPropertiesDialog(Path &path, ProjectTreeModel &projectModel, QWidget *parent = nullptr); ~PathPropertiesDialog(); void accept() override; void reject() override; private slots: + void add(); + void insert(); + void delete_(); + void changed(); + void pathNameChanged(const Path &path); + private: + void updateTitle(); + const std::unique_ptr m_ui; Path &m_path; + ProjectTreeModel &m_projectModel; + + bool m_unsavedChanges{}; QLabel * const m_labelX; QLabel * const m_labelY; QLabel * const m_labelArea; - - bool m_unsavedChanges{}; }; diff --git a/dialogs/pathpropertiesdialog.ui b/dialogs/pathpropertiesdialog.ui index 9cdc782..aff912a 100644 --- a/dialogs/pathpropertiesdialog.ui +++ b/dialogs/pathpropertiesdialog.ui @@ -87,6 +87,18 @@ + + 10 + + + 10 + + + 10 + + + 10 + @@ -104,6 +116,22 @@ + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 10 + + + + @@ -114,6 +142,22 @@ + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 10 + + + + @@ -190,6 +234,22 @@ + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 10 + + + + diff --git a/dialogs/scriptpropertiesdialog.cpp b/dialogs/scriptpropertiesdialog.cpp index 8efbc9c..b63baef 100644 --- a/dialogs/scriptpropertiesdialog.cpp +++ b/dialogs/scriptpropertiesdialog.cpp @@ -9,18 +9,20 @@ #include #include "projectcontainer.h" +#include "projecttreemodel.h" #include "jshighlighter.h" -ScriptPropertiesDialog::ScriptPropertiesDialog(Script &script, QWidget *parent) : +ScriptPropertiesDialog::ScriptPropertiesDialog(Script &script, ProjectTreeModel &projectModel, QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()}, m_script{script}, + m_projectModel{projectModel}, m_lineEditName{new QLineEdit{this}}, m_labelPosition{new QLabel{this}} { m_ui->setupUi(this); - setWindowTitle(tr("Script Properties: %0").arg(m_script.name)); + updateTitle(); { auto label = new QLabel{tr("Name:"), this}; @@ -48,6 +50,9 @@ ScriptPropertiesDialog::ScriptPropertiesDialog(Script &script, QWidget *parent) updatePosition(); + connect(&m_projectModel, &ProjectTreeModel::scriptNameChanged, + this, &ScriptPropertiesDialog::scriptNameChanged); + connect(m_ui->actionLoad, &QAction::triggered, this, &ScriptPropertiesDialog::load); connect(m_ui->actionSave, &QAction::triggered, @@ -71,8 +76,11 @@ void ScriptPropertiesDialog::accept() { if (m_script.name != m_lineEditName->text()) { - QMessageBox::critical(this, tr("Not implemented"), tr("Changing the name is not yet implemented!")); - return; + if (!m_projectModel.renameScript(m_script, m_lineEditName->text())) + { + QMessageBox::critical(this, tr("Renaming Script failed!"), tr("Renaming Script failed!")); + return; + } } m_script.script = m_ui->codeEdit->toPlainText(); @@ -114,8 +122,8 @@ void ScriptPropertiesDialog::changed() { if (!m_unsavedChanges) { - setWindowTitle(tr("Script Properties: %0*").arg(m_script.name)); m_unsavedChanges = true; + updateTitle(); } } @@ -156,3 +164,24 @@ void ScriptPropertiesDialog::updatePosition() m_labelPosition->setText(tr("%0/%1: %2").arg(lines).arg(m_ui->codeEdit->blockCount()).arg(position)); } + +void ScriptPropertiesDialog::scriptNameChanged(const Script &script) +{ + if (&script != &m_script) + return; + + { + QSignalBlocker blocker{m_lineEditName}; + m_lineEditName->setText(script.name); + } + + updateTitle(); +} + +void ScriptPropertiesDialog::updateTitle() +{ + setWindowTitle(tr("Script Properties: %0%1") + .arg(m_script.name) + .arg(m_unsavedChanges ? tr("*") : QString{}) + ); +} diff --git a/dialogs/scriptpropertiesdialog.h b/dialogs/scriptpropertiesdialog.h index 46c514a..4ed43ab 100644 --- a/dialogs/scriptpropertiesdialog.h +++ b/dialogs/scriptpropertiesdialog.h @@ -8,13 +8,14 @@ class QLineEdit; class QLabel; namespace Ui { class ScriptPropertiesDialog; } struct Script; +class ProjectTreeModel; class ScriptPropertiesDialog : public QDialog { Q_OBJECT public: - explicit ScriptPropertiesDialog(Script &script, QWidget *parent = nullptr); + explicit ScriptPropertiesDialog(Script &script, ProjectTreeModel &projectModel, QWidget *parent = nullptr); ~ScriptPropertiesDialog(); void accept() override; @@ -29,14 +30,19 @@ private slots: void updatePosition(); + void scriptNameChanged(const Script &script); + private: + void updateTitle(); + const std::unique_ptr m_ui; Script &m_script; + ProjectTreeModel &m_projectModel; + + bool m_unsavedChanges{}; QLineEdit * const m_lineEditName; QLabel * const m_labelPosition; - - bool m_unsavedChanges{}; }; diff --git a/dialogs/soundpropertiesdialog.cpp b/dialogs/soundpropertiesdialog.cpp index 5992254..32d5189 100644 --- a/dialogs/soundpropertiesdialog.cpp +++ b/dialogs/soundpropertiesdialog.cpp @@ -8,15 +8,22 @@ #include #include "projectcontainer.h" +#include "projecttreemodel.h" -SoundPropertiesDialog::SoundPropertiesDialog(Sound &sound, QWidget *parent) : +SoundPropertiesDialog::SoundPropertiesDialog(Sound &sound, ProjectTreeModel &projectModel, QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()}, - m_sound{sound} + m_sound{sound}, + m_projectModel{projectModel} { m_ui->setupUi(this); - setWindowTitle(tr("Sound Properties: %0").arg(m_sound.name)); + updateTitle(); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); m_ui->lineEditName->setText(m_sound.name); if (!m_sound.path.isEmpty()) @@ -35,6 +42,9 @@ SoundPropertiesDialog::SoundPropertiesDialog(Sound &sound, QWidget *parent) : m_ui->horizontalSliderPan->setValue(m_sound.pan); m_ui->checkBoxPreload->setChecked(m_sound.preload); + connect(&m_projectModel, &ProjectTreeModel::soundNameChanged, + this, &SoundPropertiesDialog::soundNameChanged); + connect(m_ui->pushButtonLoad, &QAbstractButton::pressed, this, &SoundPropertiesDialog::loadSound); connect(m_ui->pushButtonPlay, &QAbstractButton::pressed, @@ -80,12 +90,15 @@ void SoundPropertiesDialog::accept() { if (m_sound.name != m_ui->lineEditName->text()) { - QMessageBox::critical(this, tr("Not implemented"), tr("Changing the name is not yet implemented!")); - return; + if (!m_projectModel.renameSound(m_sound, m_ui->lineEditName->text())) + { + QMessageBox::critical(this, tr("Renaming Sound failed!"), tr("Renaming Sound failed!")); + return; + } } if (m_newPath) - m_sound.path = *m_newPath; + m_sound.path = std::move(*m_newPath); if (m_ui->radioButtonNormal->isChecked()) m_sound.type = Sound::Type::Sound; @@ -195,7 +208,28 @@ void SoundPropertiesDialog::changed() { if (!m_unsavedChanges) { - setWindowTitle(tr("Sound Properties: %0*").arg(m_sound.name)); m_unsavedChanges = true; + updateTitle(); } } + +void SoundPropertiesDialog::soundNameChanged(const Sound &sound) +{ + if (&sound != &m_sound) + return; + + { + QSignalBlocker blocker{m_ui->lineEditName}; + m_ui->lineEditName->setText(sound.name); + } + + updateTitle(); +} + +void SoundPropertiesDialog::updateTitle() +{ + setWindowTitle(tr("Sound Properties: %0%1") + .arg(m_sound.name) + .arg(m_unsavedChanges ? tr("*") : QString{}) + ); +} diff --git a/dialogs/soundpropertiesdialog.h b/dialogs/soundpropertiesdialog.h index ea61004..c361506 100644 --- a/dialogs/soundpropertiesdialog.h +++ b/dialogs/soundpropertiesdialog.h @@ -9,13 +9,14 @@ namespace Ui { class SoundPropertiesDialog; } struct Sound; +class ProjectTreeModel; class SoundPropertiesDialog : public QDialog { Q_OBJECT public: - explicit SoundPropertiesDialog(Sound &sound, QWidget *parent = nullptr); + explicit SoundPropertiesDialog(Sound &sound, ProjectTreeModel &projectModel, QWidget *parent = nullptr); ~SoundPropertiesDialog(); void accept() override; @@ -30,10 +31,15 @@ private slots: void changed(); + void soundNameChanged(const Sound &sound); + private: + void updateTitle(); + const std::unique_ptr m_ui; Sound &m_sound; + ProjectTreeModel &m_projectModel; bool m_unsavedChanges{}; diff --git a/dialogs/soundpropertiesdialog.ui b/dialogs/soundpropertiesdialog.ui index 28e7fe3..571defa 100644 --- a/dialogs/soundpropertiesdialog.ui +++ b/dialogs/soundpropertiesdialog.ui @@ -288,45 +288,14 @@ - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - &OK - - - - :/qtgameengine/icons/ok.png:/qtgameengine/icons/ok.png - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - + + + QDialogButtonBox::Ok + + + true + + @@ -335,18 +304,34 @@ - pushButtonOk - pressed() + buttonBox + accepted() SoundPropertiesDialog accept() 119 - 534 + 480 119 - 279 + 266 + + + + + buttonBox + rejected() + SoundPropertiesDialog + reject() + + + 119 + 512 + + + 119 + 266 diff --git a/dialogs/spritepropertiesdialog.cpp b/dialogs/spritepropertiesdialog.cpp index 323b42c..58f20d5 100644 --- a/dialogs/spritepropertiesdialog.cpp +++ b/dialogs/spritepropertiesdialog.cpp @@ -4,29 +4,38 @@ #include #include #include +#include #include "projectcontainer.h" +#include "projecttreemodel.h" #include "editspritedialog.h" -SpritePropertiesDialog::SpritePropertiesDialog(Sprite &sprite, QWidget *parent) : +SpritePropertiesDialog::SpritePropertiesDialog(Sprite &sprite, ProjectTreeModel &projectModel, QWidget *parent) : QDialog{parent}, m_ui{std::make_unique()}, - m_sprite{sprite} + m_sprite{sprite}, + m_projectModel{projectModel} { m_ui->setupUi(this); - setWindowTitle(tr("Sprite Properties: %0").arg(m_sprite.name)); + updateTitle(); + + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Ok)) + button->setIcon(QIcon{":/qtgameengine/icons/ok.png"}); + if (auto button = m_ui->buttonBox->button(QDialogButtonBox::Cancel)) + button->setIcon(QIcon{":/qtgameengine/icons/delete.png"}); m_ui->lineEditName->setText(m_sprite.name); - m_ui->labelWidth->setText(tr("Width: %0").arg(m_sprite.pixmaps.empty() ? tr("n/a") : QString::number(m_sprite.pixmaps.front().width()))); - m_ui->labelHeight->setText(tr("Height: %0").arg(m_sprite.pixmaps.empty() ? tr("n/a") : QString::number(m_sprite.pixmaps.front().height()))); - m_ui->labelSubimages->setText(tr("Number of subimages: %0").arg(m_sprite.pixmaps.size())); + updateSpriteInfo(); m_ui->spinBoxOriginX->setValue(m_sprite.origin.x); m_ui->spinBoxOriginY->setValue(m_sprite.origin.y); m_ui->checkBoxPreciseCollisionChecking->setChecked(m_sprite.preciseCollisionChecking); m_ui->checkBoxSeparateCollisionMasks->setChecked(m_sprite.separateCollisionMasks); m_ui->labelPreview->setPixmap(m_sprite.pixmaps.empty() ? QPixmap{} : m_sprite.pixmaps.front()); + connect(&m_projectModel, &ProjectTreeModel::spriteNameChanged, + this, &SpritePropertiesDialog::spriteNameChanged); + connect(m_ui->pushButtonLoad, &QAbstractButton::pressed, this, &SpritePropertiesDialog::loadSprite); connect(m_ui->pushButtonSave, &QAbstractButton::pressed, @@ -54,12 +63,15 @@ void SpritePropertiesDialog::accept() { if (m_sprite.name != m_ui->lineEditName->text()) { - QMessageBox::critical(this, tr("Not implemented"), tr("Changing the name is not yet implemented!")); - return; + if (!m_projectModel.renameSprite(m_sprite, m_ui->lineEditName->text())) + { + QMessageBox::critical(this, tr("Renaming Sprite failed!"), tr("Renaming Sprite failed!")); + return; + } } if (m_newPixmaps) - m_sprite.pixmaps = *m_newPixmaps; + m_sprite.pixmaps = std::move(*m_newPixmaps); m_sprite.origin.x = m_ui->spinBoxOriginX->value(); m_sprite.origin.y = m_ui->spinBoxOriginY->value(); m_sprite.preciseCollisionChecking = m_ui->checkBoxPreciseCollisionChecking->isChecked(); @@ -117,13 +129,14 @@ void SpritePropertiesDialog::loadSprite() m_unsavedChanges = true; updateTitle(); + updateSpriteInfo(); } void SpritePropertiesDialog::saveSprite() { const auto &pixmaps = m_newPixmaps ? *m_newPixmaps : m_sprite.pixmaps; - if (pixmaps.empty()) + if (pixmaps.empty() || pixmaps.front().isNull()) { QMessageBox::warning(this, tr("No sprites available to save!"), tr("No sprites available to save!")); return; @@ -135,7 +148,7 @@ void SpritePropertiesDialog::saveSprite() if (!pixmaps.front().save(path)) { - QMessageBox::warning(this, tr("Could not save sprite!"), tr("Could not save sprite!")); + QMessageBox::warning(this, tr("Could not save Sprite!"), tr("Could not save Sprite!")); return; } } @@ -147,14 +160,15 @@ void SpritePropertiesDialog::editSprite() void SpritePropertiesDialog::centerOrigin() { - if (m_sprite.pixmaps.empty()) + const auto &pixmaps = m_newPixmaps ? *m_newPixmaps : m_sprite.pixmaps; + if (pixmaps.empty() || pixmaps.front().isNull()) { qDebug() << "unexpected empty pixmaps"; return; } - m_ui->spinBoxOriginX->setValue(m_sprite.pixmaps.front().width() / 2); - m_ui->spinBoxOriginY->setValue(m_sprite.pixmaps.front().height() / 2); + m_ui->spinBoxOriginX->setValue(pixmaps.front().width() / 2); + m_ui->spinBoxOriginY->setValue(pixmaps.front().height() / 2); } void SpritePropertiesDialog::changed() @@ -166,6 +180,19 @@ void SpritePropertiesDialog::changed() } } +void SpritePropertiesDialog::spriteNameChanged(const Sprite &sprite) +{ + if (&sprite != &m_sprite) + return; + + { + QSignalBlocker blocker{m_ui->lineEditName}; + m_ui->lineEditName->setText(sprite.name); + } + + updateTitle(); +} + void SpritePropertiesDialog::updateTitle() { setWindowTitle(tr("Sprite Properties: %0%1") @@ -173,3 +200,11 @@ void SpritePropertiesDialog::updateTitle() .arg(m_unsavedChanges ? tr("*") : QString{}) ); } + +void SpritePropertiesDialog::updateSpriteInfo() +{ + const auto &pixmaps = m_newPixmaps ? *m_newPixmaps : m_sprite.pixmaps; + m_ui->labelWidth->setText(tr("Width: %0").arg(pixmaps.empty() ? tr("n/a") : QString::number(pixmaps.front().width()))); + m_ui->labelHeight->setText(tr("Height: %0").arg(pixmaps.empty() ? tr("n/a") : QString::number(pixmaps.front().height()))); + m_ui->labelSubimages->setText(tr("Number of subimages: %0").arg(pixmaps.size())); +} diff --git a/dialogs/spritepropertiesdialog.h b/dialogs/spritepropertiesdialog.h index e623bae..6379bcf 100644 --- a/dialogs/spritepropertiesdialog.h +++ b/dialogs/spritepropertiesdialog.h @@ -8,13 +8,14 @@ namespace Ui { class SpritePropertiesDialog; } struct Sprite; +class ProjectTreeModel; class SpritePropertiesDialog : public QDialog { Q_OBJECT public: - explicit SpritePropertiesDialog(Sprite &sprite, QWidget *parent = nullptr); + explicit SpritePropertiesDialog(Sprite &sprite, ProjectTreeModel &projectModel, QWidget *parent = nullptr); ~SpritePropertiesDialog(); void accept() override; @@ -28,12 +29,16 @@ private slots: void changed(); + void spriteNameChanged(const Sprite &sprite); + private: void updateTitle(); + void updateSpriteInfo(); const std::unique_ptr m_ui; Sprite &m_sprite; + ProjectTreeModel &m_projectModel; bool m_unsavedChanges{}; diff --git a/dialogs/spritepropertiesdialog.ui b/dialogs/spritepropertiesdialog.ui index 1684cbc..a270763 100644 --- a/dialogs/spritepropertiesdialog.ui +++ b/dialogs/spritepropertiesdialog.ui @@ -225,45 +225,14 @@ - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Ok - - - - :/qtgameengine/icons/ok.png:/qtgameengine/icons/ok.png - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - + + + QDialogButtonBox::Ok + + + true + + @@ -341,14 +310,30 @@ - pushButtonOk - pressed() + buttonBox + accepted() SpritePropertiesDialog accept() - 106 - 320 + 108 + 321 + + + 279 + 171 + + + + + buttonBox + rejected() + SpritePropertiesDialog + reject() + + + 108 + 321 279 diff --git a/mainwindow.cpp b/mainwindow.cpp index 83c00c2..9a0dd62 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -202,7 +202,7 @@ void MainWindow::doubleClicked(const QModelIndex &index) } else { - auto dialog = new SpritePropertiesDialog{*sprite}; + auto dialog = new SpritePropertiesDialog{*sprite, *m_projectTreeModel}; auto subwindow = m_ui->mdiArea->addSubWindow(dialog); auto action = m_ui->menuWindow->addAction(dialog->windowTitle()); action->setCheckable(true); @@ -247,7 +247,7 @@ void MainWindow::doubleClicked(const QModelIndex &index) } else { - auto dialog = new SoundPropertiesDialog{*sound}; + auto dialog = new SoundPropertiesDialog{*sound, *m_projectTreeModel}; auto subwindow = m_ui->mdiArea->addSubWindow(dialog); auto action = m_ui->menuWindow->addAction(dialog->windowTitle()); action->setCheckable(true); @@ -292,7 +292,7 @@ void MainWindow::doubleClicked(const QModelIndex &index) } else { - auto dialog = new BackgroundPropertiesDialog{*background}; + auto dialog = new BackgroundPropertiesDialog{*background, *m_projectTreeModel}; auto subwindow = m_ui->mdiArea->addSubWindow(dialog); auto action = m_ui->menuWindow->addAction(dialog->windowTitle()); action->setCheckable(true); @@ -337,7 +337,7 @@ void MainWindow::doubleClicked(const QModelIndex &index) } else { - auto dialog = new PathPropertiesDialog{*path}; + auto dialog = new PathPropertiesDialog{*path, *m_projectTreeModel}; auto subwindow = m_ui->mdiArea->addSubWindow(dialog); auto action = m_ui->menuWindow->addAction(dialog->windowTitle()); action->setCheckable(true); @@ -382,7 +382,7 @@ void MainWindow::doubleClicked(const QModelIndex &index) } else { - auto dialog = new ScriptPropertiesDialog{*script}; + auto dialog = new ScriptPropertiesDialog{*script, *m_projectTreeModel}; auto subwindow = m_ui->mdiArea->addSubWindow(dialog); auto action = m_ui->menuWindow->addAction(dialog->windowTitle()); action->setCheckable(true); @@ -427,7 +427,7 @@ void MainWindow::doubleClicked(const QModelIndex &index) } else { - auto dialog = new FontPropertiesDialog{*font}; + auto dialog = new FontPropertiesDialog{*font, *m_projectTreeModel}; auto subwindow = m_ui->mdiArea->addSubWindow(dialog); auto action = m_ui->menuWindow->addAction(dialog->windowTitle()); action->setCheckable(true); diff --git a/projectcontainer.cpp b/projectcontainer.cpp index 27703ae..d7141d3 100644 --- a/projectcontainer.cpp +++ b/projectcontainer.cpp @@ -73,6 +73,7 @@ QDataStream &operator<<(QDataStream &ds, const Background &background) { ds << background.name; ds << background.pixmap; + ds << background.tileset; return ds; } @@ -80,6 +81,7 @@ QDataStream &operator>>(QDataStream &ds, Background &background) { ds >> background.name; ds >> background.pixmap; + ds >> background.tileset; return ds; } diff --git a/projectcontainer.h b/projectcontainer.h index a17671b..efc90b2 100644 --- a/projectcontainer.h +++ b/projectcontainer.h @@ -43,6 +43,7 @@ struct Background { QString name; QPixmap pixmap; + bool tileset{}; }; struct Path diff --git a/projecttreemodel.cpp b/projecttreemodel.cpp index 3014cb5..e06ae82 100644 --- a/projecttreemodel.cpp +++ b/projecttreemodel.cpp @@ -465,6 +465,7 @@ bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, return false; } iter->name = std::move(name); + emit spriteNameChanged(*iter); emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); } return true; @@ -501,6 +502,7 @@ bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, return false; } iter->name = std::move(name); + emit soundNameChanged(*iter); emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); } return true; @@ -537,6 +539,7 @@ bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, return false; } iter->name = std::move(name); + emit backgroundNameChanged(*iter); emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); } return true; @@ -573,6 +576,7 @@ bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, return false; } iter->name = std::move(name); + emit pathNameChanged(*iter); emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); } return true; @@ -609,6 +613,7 @@ bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, return false; } iter->name = std::move(name); + emit scriptNameChanged(*iter); emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); } return true; @@ -645,6 +650,7 @@ bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, return false; } iter->name = std::move(name); + emit fontNameChanged(*iter); emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); } return true; @@ -1458,3 +1464,177 @@ const Font *ProjectTreeModel::getFont(const QModelIndex &index) const return &*std::next(std::cbegin(m_project->fonts), index.row()); } + +bool ProjectTreeModel::renameSprite(const Sprite &sprite, const QString &newName) +{ + const auto iter = std::find_if(std::begin(m_project->sprites), std::end(m_project->sprites), + [&sprite](const auto &entry){ return &entry == &sprite; }); + if (iter == std::cend(m_project->sprites)) + { + qWarning() << "sprite not from this project!"; + return false; + } + + if (iter->name == newName) + return true; + + if (std::any_of(std::cbegin(m_project->sprites), std::cend(m_project->sprites), + [&newName](const auto &entry){ return entry.name == newName; })) + { + qWarning() << "duplicate sprite name" << newName; + return false; + } + + iter->name = newName; + + emit spriteNameChanged(*iter); + const auto index = this->index(std::distance(std::begin(m_project->sprites), iter), 0, spritesRoot()); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + + return true; +} + +bool ProjectTreeModel::renameSound(const Sound &sound, const QString &newName) +{ + const auto iter = std::find_if(std::begin(m_project->sounds), std::end(m_project->sounds), + [&sound](const auto &entry){ return &entry == &sound; }); + if (iter == std::cend(m_project->sounds)) + { + qWarning() << "sound not from this project!"; + return false; + } + + if (iter->name == newName) + return true; + + if (std::any_of(std::cbegin(m_project->sounds), std::cend(m_project->sounds), + [&newName](const auto &entry){ return entry.name == newName; })) + { + qWarning() << "duplicate sound name" << newName; + return false; + } + + iter->name = newName; + + emit soundNameChanged(*iter); + const auto index = this->index(std::distance(std::begin(m_project->sounds), iter), 0, soundsRoot()); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + + return true; +} + +bool ProjectTreeModel::renameBackground(const Background &background, const QString &newName) +{ + const auto iter = std::find_if(std::begin(m_project->backgrounds), std::end(m_project->backgrounds), + [&background](const auto &entry){ return &entry == &background; }); + if (iter == std::cend(m_project->backgrounds)) + { + qWarning() << "background not from this project!"; + return false; + } + + if (iter->name == newName) + return true; + + if (std::any_of(std::cbegin(m_project->backgrounds), std::cend(m_project->backgrounds), + [&newName](const auto &entry){ return entry.name == newName; })) + { + qWarning() << "duplicate background name" << newName; + return false; + } + + iter->name = newName; + + emit backgroundNameChanged(*iter); + const auto index = this->index(std::distance(std::begin(m_project->backgrounds), iter), 0, backgroundsRoot()); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + + return true; +} + +bool ProjectTreeModel::renamePath(const Path &path, const QString &newName) +{ + const auto iter = std::find_if(std::begin(m_project->paths), std::end(m_project->paths), + [&path](const auto &entry){ return &entry == &path; }); + if (iter == std::cend(m_project->paths)) + { + qWarning() << "path not from this project!"; + return false; + } + + if (iter->name == newName) + return true; + + if (std::any_of(std::cbegin(m_project->paths), std::cend(m_project->paths), + [&newName](const auto &entry){ return entry.name == newName; })) + { + qWarning() << "duplicate path name" << newName; + return false; + } + + iter->name = newName; + + emit pathNameChanged(*iter); + const auto index = this->index(std::distance(std::begin(m_project->paths), iter), 0, pathsRoot()); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + + return true; +} + +bool ProjectTreeModel::renameScript(const Script &script, const QString &newName) +{ + const auto iter = std::find_if(std::begin(m_project->scripts), std::end(m_project->scripts), + [&script](const auto &entry){ return &entry == &script; }); + if (iter == std::cend(m_project->scripts)) + { + qWarning() << "script not from this project!"; + return false; + } + + if (iter->name == newName) + return true; + + if (std::any_of(std::cbegin(m_project->scripts), std::cend(m_project->scripts), + [&newName](const auto &entry){ return entry.name == newName; })) + { + qWarning() << "duplicate script name" << newName; + return false; + } + + iter->name = newName; + + emit scriptNameChanged(*iter); + const auto index = this->index(std::distance(std::begin(m_project->scripts), iter), 0, scriptsRoot()); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + + return true; +} + +bool ProjectTreeModel::renameFont(const Font &font, const QString &newName) +{ + const auto iter = std::find_if(std::begin(m_project->fonts), std::end(m_project->fonts), + [&font](const auto &entry){ return &entry == &font; }); + if (iter == std::cend(m_project->fonts)) + { + qWarning() << "font not from this project!"; + return false; + } + + if (iter->name == newName) + return true; + + if (std::any_of(std::cbegin(m_project->fonts), std::cend(m_project->fonts), + [&newName](const auto &entry){ return entry.name == newName; })) + { + qWarning() << "duplicate font name" << newName; + return false; + } + + iter->name = newName; + + emit fontNameChanged(*iter); + const auto index = this->index(std::distance(std::begin(m_project->fonts), iter), 0, fontsRoot()); + emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); + + return true; +} diff --git a/projecttreemodel.h b/projecttreemodel.h index e747a18..7f93e08 100644 --- a/projecttreemodel.h +++ b/projecttreemodel.h @@ -75,9 +75,23 @@ public: Font *getFont(const QModelIndex &index); const Font *getFont(const QModelIndex &index) const; + bool renameSprite(const Sprite &sprite, const QString &newName); + bool renameSound(const Sound &sound, const QString &newName); + bool renameBackground(const Background &background, const QString &newName); + bool renamePath(const Path &path, const QString &newName); + bool renameScript(const Script &script, const QString &newName); + bool renameFont(const Font &font, const QString &newName); + signals: void errorOccured(const QString &message); + void spriteNameChanged(const Sprite &sprite); + void soundNameChanged(const Sound &sound); + void backgroundNameChanged(const Background &background); + void pathNameChanged(const Path &path); + void scriptNameChanged(const Script &script); + void fontNameChanged(const Font &font); + private: ProjectContainer *m_project{}; };