Implement recursive tree project structure and other smaller fixes
This commit is contained in:
1 parent
516d030c3c
commit
b832e641b9
48 files changed
+1666
-462
No files matched your search
@@ -52,6 +52,7 @@ HEADERS += \
|
||||
src/editor/widgets/qlineeditwithmenu.h \
|
||||
src/editor/widgets/qscrollareawithmenu.h \
|
||||
src/editor/widgets/roomeditwidget.h \
|
||||
src/editor/widgets/resourcemenuadapter.h \
|
||||
src/editor/widgets/zoomablescrollarea.h \
|
||||
src/engine/gameengine.h \
|
||||
src/engine/glgamewindow.h \
|
||||
@@ -60,12 +61,14 @@ HEADERS += \
|
||||
src/engine/vulkangamewindow.h \
|
||||
src/futurecpp.h \
|
||||
src/projectcontainer.h \
|
||||
src/resourcefolder.h \
|
||||
src/editor/imagehelpers.h \
|
||||
src/editor/jshighlighter.h \
|
||||
src/editor/mainwindow.h \
|
||||
src/editor/dialogs/addeventdialog.h \
|
||||
src/editor/dialogs/backgroundpropertiesdialog.h \
|
||||
src/editor/dialogs/codeeditordialog.h \
|
||||
src/editor/dialogs/projectchangesapplier.h \
|
||||
src/editor/dialogs/createspritedialog.h \
|
||||
src/editor/dialogs/deletemomentsdialog.h \
|
||||
src/editor/dialogs/editspritedialog.h \
|
||||
@@ -131,6 +134,7 @@ SOURCES += \
|
||||
src/editor/widgets/qlineeditwithmenu.cpp \
|
||||
src/editor/widgets/qscrollareawithmenu.cpp \
|
||||
src/editor/widgets/roomeditwidget.cpp \
|
||||
src/editor/widgets/resourcemenuadapter.cpp \
|
||||
src/editor/widgets/zoomablescrollarea.cpp \
|
||||
src/engine/gameengine.cpp \
|
||||
src/engine/glgamewindow.cpp \
|
||||
|
||||
@@ -73,7 +73,7 @@ void ExecuteCodeDialog::accept()
|
||||
m_action.appliesTo = AppliesToSelf{};
|
||||
else if (m_radioButtonOther->isChecked())
|
||||
m_action.appliesTo = AppliesToOther{};
|
||||
else if (m_radioButtonObject->isChecked())
|
||||
else if (m_radioButtonObject->isChecked() && !m_objectSelector->objectName().isEmpty())
|
||||
m_action.appliesTo = AppliesToObject{ .objectName = m_objectSelector->objectName() };
|
||||
else
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QMenu>
|
||||
|
||||
#include "models/projecttreemodel.h"
|
||||
#include "widgets/resourcemenuadapter.h"
|
||||
#include "mainwindow.h"
|
||||
#include "editorguiutils.h"
|
||||
#include "editorsettings.h"
|
||||
@@ -58,27 +59,14 @@ AddEventDialog::AddEventDialog(ProjectTreeModel &projectModel, MainWindow &mainW
|
||||
|
||||
{
|
||||
auto menu = new QMenu;
|
||||
connect(menu, &QMenu::aboutToShow, menu, [this,menu](){
|
||||
menu->clear();
|
||||
for (const Object &object : m_projectModel.project()->objects)
|
||||
{
|
||||
QIcon icon;
|
||||
if (!object.spriteName.isEmpty())
|
||||
{
|
||||
const auto &sprites = m_projectModel.project()->sprites;
|
||||
const auto iter = std::find_if(std::cbegin(sprites), std::cend(sprites),
|
||||
[&](const Sprite &sprite){ return sprite.name == object.spriteName; });
|
||||
if (iter != std::cend(sprites))
|
||||
{
|
||||
if (!iter->pixmaps.empty())
|
||||
icon = iter->pixmaps.front();
|
||||
}
|
||||
m_collisionMenuAdapter = std::make_unique<ResourceMenuAdapter>(
|
||||
m_projectModel, m_projectModel.rootFor<Object>(), *menu,
|
||||
[this](const QModelIndex &index) {
|
||||
if (const Object *object = m_projectModel.get<Object>(index)) {
|
||||
m_eventType = object->name;
|
||||
accept();
|
||||
}
|
||||
menu->addAction(icon, object.name, this, [this,&object](){
|
||||
m_eventType = object.name; accept();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
m_ui->pushButtonCollision->setMenu(menu);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Ui { class AddEventDialog; }
|
||||
|
||||
class ProjectTreeModel;
|
||||
class MainWindow;
|
||||
class ResourceMenuAdapter;
|
||||
|
||||
class AddEventDialog : public QDialog
|
||||
{
|
||||
@@ -35,4 +36,5 @@ private:
|
||||
const std::unique_ptr<Ui::AddEventDialog> m_ui;
|
||||
|
||||
std::optional<std::variant<Object::EventType, QString>> m_eventType;
|
||||
std::unique_ptr<ResourceMenuAdapter> m_collisionMenuAdapter;
|
||||
};
|
||||
@@ -61,24 +61,34 @@ void BackgroundPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool BackgroundPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_background.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Background>(m_background, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Background failed!"), tr("Renaming Background failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_projectModel.setBackgroundPixmap(m_background, std::move(m_pixmap)))
|
||||
if (!m_projectModel.setBackgroundPixmap(m_background, QPixmap{m_pixmap}))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Setting Background pixmap failed!"), tr("Setting Background pixmap failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_background.tileset = m_ui->checkBoxTileset->isChecked();
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void BackgroundPropertiesDialog::reject()
|
||||
@@ -154,6 +164,7 @@ void BackgroundPropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,14 @@
|
||||
#include <QString>
|
||||
|
||||
#include <memory>
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
namespace Ui { class BackgroundPropertiesDialog; }
|
||||
struct Background;
|
||||
class ProjectTreeModel;
|
||||
class MainWindow;
|
||||
|
||||
class BackgroundPropertiesDialog : public QDialog
|
||||
class BackgroundPropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -19,6 +20,7 @@ public:
|
||||
~BackgroundPropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -111,10 +111,18 @@ void CodeEditorDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
void CodeEditorDialog::markChangesApplied()
|
||||
{
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
}
|
||||
|
||||
QAction *CodeEditorDialog::addToolbarWidget(QWidget *widget)
|
||||
{
|
||||
return m_ui->toolBar->addWidget(widget);
|
||||
|
||||
@@ -29,6 +29,7 @@ protected slots:
|
||||
|
||||
protected:
|
||||
bool m_unsavedChanges{};
|
||||
void markChangesApplied();
|
||||
|
||||
QAction *addToolbarWidget(QWidget *widget);
|
||||
|
||||
|
||||
@@ -69,12 +69,19 @@ void FontPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool FontPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_font.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Font>(m_font, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Font failed!"), tr("Renaming Font failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +91,10 @@ void FontPropertiesDialog::accept()
|
||||
.to = m_ui->spinBoxCharRangeTo->value(),
|
||||
};
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void FontPropertiesDialog::reject()
|
||||
@@ -147,6 +157,7 @@ void FontPropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
#include <QDialog>
|
||||
|
||||
#include <memory>
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
namespace Ui { class FontPropertiesDialog; }
|
||||
struct Font;
|
||||
class ProjectTreeModel;
|
||||
class MainWindow;
|
||||
|
||||
class FontPropertiesDialog : public QDialog
|
||||
class FontPropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -18,6 +19,7 @@ public:
|
||||
~FontPropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "addeventdialog.h"
|
||||
#include "editorguiutils.h"
|
||||
#include "editorsettings.h"
|
||||
#include "widgets/resourcemenuadapter.h"
|
||||
|
||||
ObjectPropertiesDialog::ObjectPropertiesDialog(Object &object, ProjectTreeModel &projectModel, MainWindow &mainWindow) :
|
||||
QDialog{&mainWindow},
|
||||
@@ -105,10 +106,18 @@ ObjectPropertiesDialog::ObjectPropertiesDialog(Object &object, ProjectTreeModel
|
||||
connect(m_ui->checkBoxPersistent, &QCheckBox::toggled,
|
||||
this, &ObjectPropertiesDialog::changed);
|
||||
|
||||
connect(m_menuSprites, &QMenu::aboutToShow,
|
||||
this, &ObjectPropertiesDialog::spritesMenuAboutToShow);
|
||||
connect(m_menuMaskSprites, &QMenu::aboutToShow,
|
||||
this, &ObjectPropertiesDialog::maskSpritesMenuAboutToShow);
|
||||
m_spriteMenuAdapter = std::make_unique<ResourceMenuAdapter>(
|
||||
m_projectModel, m_projectModel.rootFor<Sprite>(), *m_menuSprites,
|
||||
[this](const QModelIndex &index) {
|
||||
if (const Sprite *sprite = m_projectModel.get<Sprite>(index)) setSprite(*sprite);
|
||||
},
|
||||
[this](QMenu &menu) { menu.addAction(tr("<no sprite>"), this, &ObjectPropertiesDialog::clearSprite); });
|
||||
m_maskMenuAdapter = std::make_unique<ResourceMenuAdapter>(
|
||||
m_projectModel, m_projectModel.rootFor<Sprite>(), *m_menuMaskSprites,
|
||||
[this](const QModelIndex &index) {
|
||||
if (const Sprite *sprite = m_projectModel.get<Sprite>(index)) setMaskSprite(*sprite);
|
||||
},
|
||||
[this](QMenu &menu) { menu.addAction(tr("<same as sprite>"), this, &ObjectPropertiesDialog::clearMaskSprite); });
|
||||
|
||||
connect(m_ui->widgetParent, &ObjectSelectorWidget::changed,
|
||||
this, &ObjectPropertiesDialog::changed);
|
||||
@@ -137,19 +146,26 @@ void ObjectPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool ObjectPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_object.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Object>(m_object, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Object failed!"), tr("Renaming Object failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_projectModel.setObjectSpriteName(m_object, std::move(m_spriteName)))
|
||||
if (!m_projectModel.setObjectSpriteName(m_object, QString{m_spriteName}))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Setting Object Sprite failed!"), tr("Setting Object Sprite failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_object.visible = m_ui->checkBoxVisible->isChecked();
|
||||
@@ -158,10 +174,13 @@ void ObjectPropertiesDialog::accept()
|
||||
m_object.persistent = m_ui->checkBoxPersistent->isChecked();
|
||||
m_object.parentName = m_ui->widgetParent->objectName();
|
||||
m_object.maskSpriteName = m_maskSpriteName;
|
||||
m_object.events = std::move(m_events);
|
||||
m_object.collisionEvents = std::move(m_collisionEvents);
|
||||
m_object.events = m_events;
|
||||
m_object.collisionEvents = m_collisionEvents;
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void ObjectPropertiesDialog::reject()
|
||||
@@ -275,12 +294,29 @@ void ObjectPropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectPropertiesDialog::objectNameChanged(const Object &object, const QString &oldName)
|
||||
{
|
||||
const auto renameTargets = [&object, &oldName](ActionsContainer &actions) {
|
||||
for (Action &action : actions)
|
||||
std::visit([&](auto &value) {
|
||||
if (auto *target = std::get_if<AppliesToObject>(&value.appliesTo);
|
||||
target && target->objectName == oldName)
|
||||
target->objectName = object.name;
|
||||
}, action);
|
||||
};
|
||||
for (auto &[event, actions] : m_events) renameTargets(actions);
|
||||
for (auto &[event, actions] : m_collisionEvents) renameTargets(actions);
|
||||
if (m_collisionEvents.contains(oldName))
|
||||
{
|
||||
m_ui->actionsWidget->setActionsContainer(nullptr);
|
||||
m_eventsModel->changeEvent(oldName, object.name);
|
||||
}
|
||||
|
||||
if (&object == &m_object)
|
||||
{
|
||||
{
|
||||
@@ -323,6 +359,24 @@ void ObjectPropertiesDialog::objectAboutToBeRemoved(const Object &object)
|
||||
if (QSignalBlocker blocker{m_ui->widgetParent};
|
||||
!m_ui->widgetParent->objectName().isEmpty() && m_ui->widgetParent->objectName() == object.name)
|
||||
m_ui->widgetParent->clearObject();
|
||||
|
||||
const auto clearTargets = [&object](ActionsContainer &actions) {
|
||||
for (Action &action : actions)
|
||||
std::visit([&object](auto &value) {
|
||||
if (auto *target = std::get_if<AppliesToObject>(&value.appliesTo);
|
||||
target && target->objectName == object.name)
|
||||
target->objectName.clear();
|
||||
}, action);
|
||||
};
|
||||
for (auto &[event, actions] : m_events)
|
||||
clearTargets(actions);
|
||||
for (auto &[event, actions] : m_collisionEvents)
|
||||
clearTargets(actions);
|
||||
if (m_collisionEvents.contains(object.name))
|
||||
{
|
||||
m_ui->actionsWidget->setActionsContainer(nullptr);
|
||||
m_eventsModel->removeEvent(object.name);
|
||||
}
|
||||
}
|
||||
|
||||
void ObjectPropertiesDialog::spriteAboutToBeRemoved(const Sprite &sprite)
|
||||
@@ -358,28 +412,6 @@ void ObjectPropertiesDialog::spritePixmapsChanged(const Sprite &sprite)
|
||||
updateSpritePreview(sprite.pixmaps);
|
||||
}
|
||||
|
||||
void ObjectPropertiesDialog::spritesMenuAboutToShow()
|
||||
{
|
||||
m_menuSprites->clear();
|
||||
m_menuSprites->addAction(tr("<no sprite>"), this, &ObjectPropertiesDialog::clearSprite);
|
||||
for (const auto &sprite : m_projectModel.project()->sprites)
|
||||
m_menuSprites->addAction(sprite.pixmaps.empty() ? QPixmap{} : sprite.pixmaps.front(),
|
||||
sprite.name,
|
||||
this,
|
||||
[&sprite,this](){ setSprite(sprite); });
|
||||
}
|
||||
|
||||
void ObjectPropertiesDialog::maskSpritesMenuAboutToShow()
|
||||
{
|
||||
m_menuMaskSprites->clear();
|
||||
m_menuMaskSprites->addAction(tr("<same as sprite>"), this, &ObjectPropertiesDialog::clearMaskSprite);
|
||||
for (const auto &sprite : m_projectModel.project()->sprites)
|
||||
m_menuMaskSprites->addAction(sprite.pixmaps.empty() ? QPixmap{} : sprite.pixmaps.front(),
|
||||
sprite.name,
|
||||
this,
|
||||
[&sprite,this](){ setMaskSprite(sprite); });
|
||||
}
|
||||
|
||||
void ObjectPropertiesDialog::currentEventChanged(const QModelIndex &index)
|
||||
{
|
||||
if (index.isValid())
|
||||
@@ -424,6 +456,9 @@ void ObjectPropertiesDialog::clearSprite()
|
||||
|
||||
void ObjectPropertiesDialog::setSprite(const Sprite &sprite)
|
||||
{
|
||||
if (m_spriteName == sprite.name)
|
||||
return;
|
||||
|
||||
m_spriteName = sprite.name;
|
||||
updateSpritePreview(sprite.pixmaps);
|
||||
m_ui->lineEditSprite->setText(sprite.name);
|
||||
@@ -479,5 +514,5 @@ void ObjectPropertiesDialog::updateSpritePreview()
|
||||
|
||||
void ObjectPropertiesDialog::updateSpritePreview(const std::vector<QPixmap> &pixmaps)
|
||||
{
|
||||
m_ui->labelSpritePreview->setPixmap(pixmaps.empty() ? QPixmap{} : pixmaps.front().scaled(32, m_ui->labelSpritePreview->height(), Qt::KeepAspectRatio));
|
||||
m_ui->labelSpritePreview->setPixmap(pixmaps.empty() ? QPixmap{} : pixmaps.front().scaled(32, 32, Qt::KeepAspectRatio));
|
||||
}
|
||||
@@ -6,14 +6,16 @@
|
||||
#include <map>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
class QMenu;
|
||||
namespace Ui { class ObjectPropertiesDialog; }
|
||||
class ProjectTreeModel;
|
||||
class ObjectEventsModel;
|
||||
class MainWindow;
|
||||
class ResourceMenuAdapter;
|
||||
|
||||
class ObjectPropertiesDialog : public QDialog
|
||||
class ObjectPropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -22,6 +24,7 @@ public:
|
||||
~ObjectPropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
@@ -41,8 +44,6 @@ private slots:
|
||||
void spriteAboutToBeRemoved(const Sprite &sprite);
|
||||
void spritePixmapsChanged(const Sprite &sprite);
|
||||
|
||||
void spritesMenuAboutToShow();
|
||||
void maskSpritesMenuAboutToShow();
|
||||
void currentEventChanged(const QModelIndex &index);
|
||||
void eventsContextMenuRequested(const QPoint &pos);
|
||||
void rowsInserted(const QModelIndex &parent, int first);
|
||||
@@ -73,6 +74,8 @@ private:
|
||||
|
||||
QMenu * const m_menuSprites;
|
||||
QMenu * const m_menuMaskSprites;
|
||||
std::unique_ptr<ResourceMenuAdapter> m_spriteMenuAdapter;
|
||||
std::unique_ptr<ResourceMenuAdapter> m_maskMenuAdapter;
|
||||
|
||||
QString m_spriteName;
|
||||
QString m_maskSpriteName;
|
||||
|
||||
@@ -34,12 +34,6 @@
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLineEdit" name="lineEditName">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>The name of the object</string>
|
||||
</property>
|
||||
@@ -58,13 +52,13 @@
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>0</height>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>16777215</height>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "models/projecttreemodel.h"
|
||||
#include "widgets/resourcemenuadapter.h"
|
||||
#include "models/pathpointsmodel.h"
|
||||
#include "mainwindow.h"
|
||||
|
||||
@@ -71,8 +72,10 @@ PathPropertiesDialog::PathPropertiesDialog(Path &path, ProjectTreeModel &project
|
||||
m_ui->toolBar->addWidget(toolButton);
|
||||
}
|
||||
|
||||
connect(m_menuRooms, &QMenu::aboutToShow,
|
||||
this, &PathPropertiesDialog::roomsMenuAboutToShow);
|
||||
m_roomMenuAdapter = std::make_unique<ResourceMenuAdapter>(
|
||||
m_projectModel, m_projectModel.rootFor<Room>(), *m_menuRooms,
|
||||
[](const QModelIndex &) {},
|
||||
[this](QMenu &menu) { menu.addAction(tr("No Room")); });
|
||||
|
||||
m_ui->treeView->setModel(m_pointsModel.get());
|
||||
|
||||
@@ -218,17 +221,22 @@ void PathPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool PathPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_path.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Path>(m_path, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Path failed!"), tr("Renaming Path failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_path.points = m_points;
|
||||
|
||||
if (m_ui->radioButtonStraight->isChecked())
|
||||
m_path.type = Path::Type::Straight;
|
||||
else if (m_ui->radioButtonSmooth->isChecked())
|
||||
@@ -236,16 +244,20 @@ void PathPropertiesDialog::accept()
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("No kind selected!"), tr("No kind selected!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_path.points = m_points;
|
||||
m_path.closed = m_ui->checkBoxClosed->isChecked();
|
||||
m_path.precision = m_ui->spinBoxPrecision->value();
|
||||
m_path.snapX = m_spinBoxSnapX->value();
|
||||
m_path.snapY = m_spinBoxSnapY->value();
|
||||
m_path.gridEnabled = m_ui->actionGridEnabled->isChecked();
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void PathPropertiesDialog::reject()
|
||||
@@ -428,6 +440,7 @@ void PathPropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
@@ -445,18 +458,6 @@ void PathPropertiesDialog::pathNameChanged(const Path &path)
|
||||
updateTitle();
|
||||
}
|
||||
|
||||
void PathPropertiesDialog::roomsMenuAboutToShow()
|
||||
{
|
||||
m_menuRooms->clear();
|
||||
qDebug() << "called" << tr("No Room");
|
||||
m_menuRooms->addAction(tr("No Room"));
|
||||
for (const auto &room : m_projectModel.project()->rooms)
|
||||
{
|
||||
qDebug() << "called" << room.name;
|
||||
m_menuRooms->addAction(room.name);
|
||||
}
|
||||
}
|
||||
|
||||
void PathPropertiesDialog::updateTitle()
|
||||
{
|
||||
setWindowTitle(tr("Path Properties: %0%1")
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <optional>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
class QSpinBox;
|
||||
class QLabel;
|
||||
@@ -14,8 +15,9 @@ namespace Ui { class PathPropertiesDialog; }
|
||||
class ProjectTreeModel;
|
||||
class PathPointsModel;
|
||||
class MainWindow;
|
||||
class ResourceMenuAdapter;
|
||||
|
||||
class PathPropertiesDialog : public QDialog
|
||||
class PathPropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -24,6 +26,7 @@ public:
|
||||
~PathPropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
@@ -55,7 +58,6 @@ private slots:
|
||||
|
||||
void pathNameChanged(const Path &path);
|
||||
|
||||
void roomsMenuAboutToShow();
|
||||
|
||||
private:
|
||||
void updateTitle();
|
||||
@@ -78,6 +80,7 @@ private:
|
||||
QSpinBox * const m_spinBoxSnapY;
|
||||
|
||||
QMenu * const m_menuRooms;
|
||||
std::unique_ptr<ResourceMenuAdapter> m_roomMenuAdapter;
|
||||
|
||||
QLabel * const m_labelX;
|
||||
QLabel * const m_labelY;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
class ProjectChangesApplier
|
||||
{
|
||||
public:
|
||||
virtual ~ProjectChangesApplier() = default;
|
||||
virtual bool applyChanges() = 0;
|
||||
};
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "models/projecttreemodel.h"
|
||||
#include "widgets/resourcemenuadapter.h"
|
||||
#include "roomscene.h"
|
||||
#include "editorguiutils.h"
|
||||
#include "genericcodeeditordialog.h"
|
||||
@@ -31,6 +32,8 @@ RoomPropertiesDialog::RoomPropertiesDialog(Room &room, ProjectTreeModel &project
|
||||
m_labelY{new QLabel{tr("y: %0").arg(0)}}
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
m_ui->splitter->setStretchFactor(0, 0);
|
||||
m_ui->splitter->setStretchFactor(1, 1);
|
||||
|
||||
updateTitle();
|
||||
|
||||
@@ -188,8 +191,11 @@ RoomPropertiesDialog::RoomPropertiesDialog(Room &room, ProjectTreeModel &project
|
||||
connect(m_ui->roomEditWidget, &RoomEditWidget::changed,
|
||||
this, &RoomPropertiesDialog::changed);
|
||||
|
||||
connect(m_menuObjects, &QMenu::aboutToShow,
|
||||
this, &RoomPropertiesDialog::objectsMenuAboutToShow);
|
||||
m_objectMenuAdapter = std::make_unique<ResourceMenuAdapter>(
|
||||
m_projectModel, m_projectModel.rootFor<Object>(), *m_menuObjects,
|
||||
[this](const QModelIndex &index) {
|
||||
if (const Object *object = m_projectModel.get<Object>(index)) setObject(*object);
|
||||
});
|
||||
|
||||
if (!m_projectModel.project()->objects.empty())
|
||||
setObject(m_projectModel.project()->objects.back());
|
||||
@@ -209,12 +215,19 @@ void RoomPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool RoomPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_room.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Room>(m_room, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Room failed!"), tr("Renaming Room failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,7 +244,10 @@ void RoomPropertiesDialog::accept()
|
||||
|
||||
m_room.objects = m_objects;
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void RoomPropertiesDialog::reject()
|
||||
@@ -315,6 +331,7 @@ void RoomPropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
@@ -401,26 +418,6 @@ void RoomPropertiesDialog::objectSpriteNameChanged(const Object &object)
|
||||
m_ui->labelObjectPreview->setPixmap(std::move(pixmap));
|
||||
}
|
||||
|
||||
void RoomPropertiesDialog::objectsMenuAboutToShow()
|
||||
{
|
||||
m_menuObjects->clear();
|
||||
for (const auto &object : m_projectModel.project()->objects)
|
||||
{
|
||||
QIcon icon;
|
||||
if (!object.spriteName.isEmpty())
|
||||
{
|
||||
const auto iter = std::find_if(std::cbegin(m_projectModel.project()->sprites), std::cend(m_projectModel.project()->sprites),
|
||||
[&object](const auto &sprite){ return object.spriteName == sprite.name; });
|
||||
if (iter == std::cend(m_projectModel.project()->sprites))
|
||||
qWarning() << "invalid sprite" << object.spriteName;
|
||||
else if (!iter->pixmaps.empty() && !iter->pixmaps.front().isNull())
|
||||
icon = iter->pixmaps.front();
|
||||
}
|
||||
m_menuObjects->addAction(icon, object.name, this,
|
||||
[&object,this](){ setObject(object); });
|
||||
}
|
||||
}
|
||||
|
||||
void RoomPropertiesDialog::cursorMoved(const QPoint &point)
|
||||
{
|
||||
m_labelX->setText(tr("X: %0").arg(point.x()));
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
class QSpinBox;
|
||||
class QLabel;
|
||||
@@ -16,8 +17,9 @@ struct Object;
|
||||
class ProjectTreeModel;
|
||||
class RoomScene;
|
||||
class MainWindow;
|
||||
class ResourceMenuAdapter;
|
||||
|
||||
class RoomPropertiesDialog : public QDialog
|
||||
class RoomPropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -26,6 +28,7 @@ public:
|
||||
~RoomPropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
@@ -46,7 +49,6 @@ private slots:
|
||||
void objectAboutToBeRemoved(const Object &object);
|
||||
void objectSpriteNameChanged(const Object &object);
|
||||
|
||||
void objectsMenuAboutToShow();
|
||||
|
||||
void cursorMoved(const QPoint &point);
|
||||
|
||||
@@ -73,6 +75,7 @@ private:
|
||||
QSpinBox * const m_spinBoxSnapY;
|
||||
|
||||
QMenu * const m_menuObjects;
|
||||
std::unique_ptr<ResourceMenuAdapter> m_objectMenuAdapter;
|
||||
|
||||
QLabel * const m_labelX;
|
||||
QLabel * const m_labelY;
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMessageBox>
|
||||
#include <QDebug>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "models/projecttreemodel.h"
|
||||
@@ -43,18 +42,26 @@ void ScriptPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool ScriptPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_script.name != m_lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Script>(m_script, m_lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Script failed!"), tr("Renaming Script failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_script.script = script();
|
||||
|
||||
CodeEditorDialog::accept();
|
||||
markChangesApplied();
|
||||
return true;
|
||||
}
|
||||
|
||||
void ScriptPropertiesDialog::scriptNameChanged(const Script &script)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "codeeditordialog.h"
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
class QLineEdit;
|
||||
struct Script;
|
||||
class ProjectTreeModel;
|
||||
class MainWindow;
|
||||
|
||||
class ScriptPropertiesDialog : public CodeEditorDialog
|
||||
class ScriptPropertiesDialog : public CodeEditorDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -13,6 +14,7 @@ public:
|
||||
ScriptPropertiesDialog(Script &script, ProjectTreeModel &projectModel, MainWindow &mainWindow);
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
|
||||
private slots:
|
||||
void scriptNameChanged(const Script &script);
|
||||
|
||||
@@ -101,17 +101,22 @@ void SoundPropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool SoundPropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_sound.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Sound>(m_sound, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Sound failed!"), tr("Renaming Sound failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_sound.path = std::move(m_path);
|
||||
|
||||
if (m_ui->radioButtonNormal->isChecked())
|
||||
m_sound.type = Sound::Type::Sound;
|
||||
else if (m_ui->radioButtonMusic->isChecked())
|
||||
@@ -120,14 +125,15 @@ void SoundPropertiesDialog::accept()
|
||||
m_ui->radioButtonMultimedia->isChecked())
|
||||
{
|
||||
QMessageBox::critical(this, tr("Not implemented"), tr("This kind of sound is not yet supported!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("No kind selected!"), tr("No kind selected!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_sound.path = m_path;
|
||||
m_sound.effects.chorus = m_ui->checkBoxChorus->isChecked();
|
||||
m_sound.effects.flanger = m_ui->checkBoxFlanger->isChecked();
|
||||
m_sound.effects.gargle = m_ui->checkBoxGargle->isChecked();
|
||||
@@ -136,7 +142,10 @@ void SoundPropertiesDialog::accept()
|
||||
m_sound.volume = m_ui->horizontalSliderVolume->value();
|
||||
m_sound.pan = m_ui->horizontalSliderPan->value();
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void SoundPropertiesDialog::reject()
|
||||
@@ -226,6 +235,7 @@ void SoundPropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
#include <QSoundEffect>
|
||||
|
||||
#include <memory>
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
namespace Ui { class SoundPropertiesDialog; }
|
||||
struct Sound;
|
||||
class ProjectTreeModel;
|
||||
class MainWindow;
|
||||
|
||||
class SoundPropertiesDialog : public QDialog
|
||||
class SoundPropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -20,6 +21,7 @@ public:
|
||||
~SoundPropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -76,19 +76,26 @@ void SpritePropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool SpritePropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_sprite.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<Sprite>(m_sprite, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Sprite failed!"), tr("Renaming Sprite failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!m_projectModel.setSpritePixmaps(m_sprite, std::move(m_pixmaps)))
|
||||
if (!m_projectModel.setSpritePixmaps(m_sprite, std::vector<QPixmap>{m_pixmaps}))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Setting Sprite pixmaps failed!"), tr("Setting Sprite pixmaps failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
m_sprite.origin.x = m_ui->spinBoxOriginX->value();
|
||||
@@ -96,7 +103,10 @@ void SpritePropertiesDialog::accept()
|
||||
m_sprite.preciseCollisionChecking = m_ui->checkBoxPreciseCollisionChecking->isChecked();
|
||||
m_sprite.separateCollisionMasks = m_ui->checkBoxSeparateCollisionMasks->isChecked();
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpritePropertiesDialog::reject()
|
||||
@@ -170,9 +180,7 @@ void SpritePropertiesDialog::editSprite()
|
||||
void SpritePropertiesDialog::centerOrigin()
|
||||
{
|
||||
QPoint center;
|
||||
if (m_pixmaps.empty() || m_pixmaps.front().isNull())
|
||||
qDebug() << "unexpected empty pixmaps";
|
||||
else
|
||||
if (!m_pixmaps.empty() && !m_pixmaps.front().isNull())
|
||||
center = {
|
||||
m_pixmaps.front().width() / 2,
|
||||
m_pixmaps.front().height() / 2
|
||||
@@ -193,6 +201,7 @@ void SpritePropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,14 @@
|
||||
#include <QString>
|
||||
|
||||
#include <memory>
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
namespace Ui { class SpritePropertiesDialog; }
|
||||
struct Sprite;
|
||||
class ProjectTreeModel;
|
||||
class MainWindow;
|
||||
|
||||
class SpritePropertiesDialog : public QDialog
|
||||
class SpritePropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -19,6 +20,7 @@ public:
|
||||
~SpritePropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
|
||||
@@ -34,6 +34,10 @@ TimeLinePropertiesDialog::TimeLinePropertiesDialog(TimeLine &timeLine, ProjectTr
|
||||
|
||||
connect(&m_projectModel, &ProjectTreeModel::timeLineNameChanged,
|
||||
this, &TimeLinePropertiesDialog::timeLineNameChanged);
|
||||
connect(&m_projectModel, &ProjectTreeModel::objectAboutToBeRemoved,
|
||||
this, &TimeLinePropertiesDialog::objectAboutToBeRemoved);
|
||||
connect(&m_projectModel, &ProjectTreeModel::objectNameChanged,
|
||||
this, &TimeLinePropertiesDialog::objectNameChanged);
|
||||
|
||||
connect(m_momentsModel.get(), &QAbstractItemModel::modelReset,
|
||||
this, &TimeLinePropertiesDialog::changed);
|
||||
@@ -81,6 +85,28 @@ TimeLinePropertiesDialog::TimeLinePropertiesDialog(TimeLine &timeLine, ProjectTr
|
||||
|
||||
TimeLinePropertiesDialog::~TimeLinePropertiesDialog() = default;
|
||||
|
||||
void TimeLinePropertiesDialog::objectAboutToBeRemoved(const Object &object)
|
||||
{
|
||||
for (auto &[moment, actions] : m_moments)
|
||||
for (Action &action : actions)
|
||||
std::visit([&object](auto &value) {
|
||||
if (auto *target = std::get_if<AppliesToObject>(&value.appliesTo);
|
||||
target && target->objectName == object.name)
|
||||
target->objectName.clear();
|
||||
}, action);
|
||||
}
|
||||
|
||||
void TimeLinePropertiesDialog::objectNameChanged(const Object &object, const QString &oldName)
|
||||
{
|
||||
for (auto &[moment, actions] : m_moments)
|
||||
for (Action &action : actions)
|
||||
std::visit([&](auto &value) {
|
||||
if (auto *target = std::get_if<AppliesToObject>(&value.appliesTo);
|
||||
target && target->objectName == oldName)
|
||||
target->objectName = object.name;
|
||||
}, action);
|
||||
}
|
||||
|
||||
void TimeLinePropertiesDialog::accept()
|
||||
{
|
||||
if (!m_unsavedChanges)
|
||||
@@ -89,18 +115,28 @@ void TimeLinePropertiesDialog::accept()
|
||||
return;
|
||||
}
|
||||
|
||||
if (applyChanges()) QDialog::accept();
|
||||
}
|
||||
|
||||
bool TimeLinePropertiesDialog::applyChanges()
|
||||
{
|
||||
if (!m_unsavedChanges) return true;
|
||||
|
||||
if (m_timeLine.name != m_ui->lineEditName->text())
|
||||
{
|
||||
if (!m_projectModel.rename<TimeLine>(m_timeLine, m_ui->lineEditName->text()))
|
||||
{
|
||||
QMessageBox::critical(this, tr("Renaming Time Line failed!"), tr("Renaming Time Line failed!"));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_timeLine.moments = m_moments;
|
||||
|
||||
QDialog::accept();
|
||||
m_unsavedChanges = false;
|
||||
setWindowModified(false);
|
||||
updateTitle();
|
||||
return true;
|
||||
}
|
||||
|
||||
void TimeLinePropertiesDialog::reject()
|
||||
@@ -138,6 +174,7 @@ void TimeLinePropertiesDialog::changed()
|
||||
if (!m_unsavedChanges)
|
||||
{
|
||||
m_unsavedChanges = true;
|
||||
setWindowModified(true);
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
@@ -252,8 +289,6 @@ void TimeLinePropertiesDialog::showInformation()
|
||||
|
||||
void TimeLinePropertiesDialog::currentMomentChanged(const QModelIndex &index)
|
||||
{
|
||||
qDebug() << index.isValid() << index.row();
|
||||
|
||||
if (index.isValid())
|
||||
{
|
||||
if (auto moment = m_momentsModel->getMoment(index))
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
#include <memory>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
#include "projectchangesapplier.h"
|
||||
|
||||
namespace Ui { class TimeLinePropertiesDialog; }
|
||||
class ProjectTreeModel;
|
||||
class TimelineMomentsModel;
|
||||
class MainWindow;
|
||||
|
||||
class TimeLinePropertiesDialog : public QDialog
|
||||
class TimeLinePropertiesDialog : public QDialog, public ProjectChangesApplier
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -20,12 +21,15 @@ public:
|
||||
~TimeLinePropertiesDialog();
|
||||
|
||||
void accept() override;
|
||||
bool applyChanges() override;
|
||||
void reject() override;
|
||||
|
||||
private slots:
|
||||
void changed();
|
||||
|
||||
void timeLineNameChanged(const TimeLine &timeLine);
|
||||
void objectAboutToBeRemoved(const Object &object);
|
||||
void objectNameChanged(const Object &object, const QString &oldName);
|
||||
|
||||
void addMoment();
|
||||
void changeMoment();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "triggersdialog.h"
|
||||
#include "ui_triggersdialog.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "userdefinedconstantsdialog.h"
|
||||
#include "ui_userdefinedconstantsdialog.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "projectcontainer.h"
|
||||
|
||||
+140
-31
@@ -10,6 +10,11 @@
|
||||
#include <QFileDialog>
|
||||
#include <QInputDialog>
|
||||
#include <QDebug>
|
||||
#include <QPersistentModelIndex>
|
||||
#include <QPointer>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "editorsettings.h"
|
||||
#include "projectserialization.h"
|
||||
@@ -24,6 +29,7 @@
|
||||
#include "dialogs/timelinepropertiesdialog.h"
|
||||
#include "dialogs/objectpropertiesdialog.h"
|
||||
#include "dialogs/roompropertiesdialog.h"
|
||||
#include "dialogs/projectchangesapplier.h"
|
||||
#include "dialogs/objectinformationdialog.h"
|
||||
#include "dialogs/transparentbackgroundsettingsdialog.h"
|
||||
#include "dialogs/gameinformationdialog.h"
|
||||
@@ -56,16 +62,21 @@ MainWindow::MainWindow(const QString &filePath, EditorSettings &settings, QWidge
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
||||
m_ui->splitter->setSizes({
|
||||
m_ui->splitter->width() / 2,
|
||||
m_ui->splitter->width() / 2
|
||||
});
|
||||
//m_ui->splitter->setSizes({
|
||||
// m_ui->splitter->width() / 2,
|
||||
// m_ui->splitter->width() / 2
|
||||
//});
|
||||
m_ui->splitter->setStretchFactor(0, 0);
|
||||
m_ui->splitter->setStretchFactor(1, 1);
|
||||
|
||||
m_ui->actionNew->setShortcut(QKeySequence::New);
|
||||
m_ui->actionOpen->setShortcut(QKeySequence::Open);
|
||||
m_ui->actionSave->setShortcut(QKeySequence::Save);
|
||||
m_ui->actionSaveAs->setShortcut(QKeySequence::SaveAs);
|
||||
m_ui->actionExit->setShortcut(QKeySequence::Quit);
|
||||
m_ui->actionDelete->setShortcut(QKeySequence::Delete);
|
||||
m_ui->actionDelete->setShortcutContext(Qt::WidgetShortcut);
|
||||
m_ui->treeView->addAction(m_ui->actionDelete);
|
||||
m_ui->actionFindResource->setShortcut(QKeySequence::Find);
|
||||
m_ui->actionPrevious->setShortcut(QKeySequence::PreviousChild);
|
||||
m_ui->actionNext->setShortcut(QKeySequence::NextChild);
|
||||
@@ -158,6 +169,8 @@ MainWindow::MainWindow(const QString &filePath, EditorSettings &settings, QWidge
|
||||
this, &MainWindow::rowsAboutToBeRemoved);
|
||||
connect(m_projectTreeModel.get(), &QAbstractTableModel::rowsRemoved,
|
||||
this, &MainWindow::changed);
|
||||
connect(m_projectTreeModel.get(), &QAbstractItemModel::rowsMoved,
|
||||
this, &MainWindow::changed);
|
||||
connect(m_projectTreeModel.get(), &ProjectTreeModel::modelAboutToBeReset,
|
||||
this, &MainWindow::modelAboutToBeReset);
|
||||
connect(m_projectTreeModel.get(), &QAbstractTableModel::dataChanged,
|
||||
@@ -327,6 +340,8 @@ MainWindow::~MainWindow() = default;
|
||||
|
||||
void MainWindow::contextMenuRequested(const QPoint &pos)
|
||||
{
|
||||
if (const QModelIndex clicked = m_ui->treeView->indexAt(pos); clicked.isValid())
|
||||
m_ui->treeView->setCurrentIndex(clicked);
|
||||
QMenu menu{this};
|
||||
menu.addAction(m_ui->actionCreate);
|
||||
menu.addAction(m_ui->actionDuplicate);
|
||||
@@ -370,6 +385,16 @@ void MainWindow::selectionChanged(const QModelIndex &index)
|
||||
{
|
||||
switch (const auto nodeType = m_projectTreeModel->nodeType(index))
|
||||
{
|
||||
case ProjectTreeModel::NodeType::Folder:
|
||||
m_ui->actionCreate->setEnabled(true);
|
||||
m_ui->actionCreate->setText(tr("&Create Resource"));
|
||||
m_ui->actionCreateGroup->setEnabled(true);
|
||||
m_ui->actionSortByName->setEnabled(true);
|
||||
m_ui->actionDuplicate->setEnabled(false);
|
||||
m_ui->actionDelete->setEnabled(true);
|
||||
m_ui->actionRename->setEnabled(true);
|
||||
m_ui->actionProperties->setEnabled(false);
|
||||
break;
|
||||
case ProjectTreeModel::NodeType::Root:
|
||||
if (index == m_projectTreeModel->rootFor<Sprite>() ||
|
||||
index == m_projectTreeModel->rootFor<Sound>() ||
|
||||
@@ -663,6 +688,10 @@ void MainWindow::create()
|
||||
using NodeType = ProjectTreeModel::NodeType;
|
||||
case NodeType::Root:
|
||||
break;
|
||||
case NodeType::Folder:
|
||||
if (!m_projectTreeModel->insertRows(m_projectTreeModel->rowCount(index), 1, index))
|
||||
QMessageBox::warning(this, tr("Creating failed!"), tr("Creating failed!"));
|
||||
break;
|
||||
case NodeType::Sprite:
|
||||
case NodeType::Sound:
|
||||
case NodeType::Background:
|
||||
@@ -686,12 +715,39 @@ void MainWindow::duplicate()
|
||||
|
||||
void MainWindow::createGroup()
|
||||
{
|
||||
QMessageBox::warning(this, tr("Not yet implemented"), tr("Not yet implemented"));
|
||||
auto parent = m_ui->treeView->currentIndex();
|
||||
if (!parent.isValid()) return;
|
||||
int row = -1;
|
||||
if (m_projectTreeModel->nodeType(parent) != ProjectTreeModel::NodeType::Folder &&
|
||||
m_projectTreeModel->nodeType(parent) != ProjectTreeModel::NodeType::Root)
|
||||
{
|
||||
row = parent.row();
|
||||
parent = parent.parent();
|
||||
}
|
||||
const auto folder = m_projectTreeModel->createFolder(parent, {}, row);
|
||||
if (!folder.isValid())
|
||||
QMessageBox::warning(this, tr("Creating failed!"), tr("Could not create a group here."));
|
||||
else
|
||||
m_ui->treeView->edit(folder);
|
||||
}
|
||||
|
||||
void MainWindow::sortByName()
|
||||
{
|
||||
QMessageBox::warning(this, tr("Not yet implemented"), tr("Not yet implemented"));
|
||||
const QModelIndex parent = m_ui->treeView->currentIndex();
|
||||
if (!parent.isValid()) return;
|
||||
if (m_projectTreeModel->nodeType(parent) != ProjectTreeModel::NodeType::Root &&
|
||||
m_projectTreeModel->nodeType(parent) != ProjectTreeModel::NodeType::Folder)
|
||||
return;
|
||||
std::vector<QPersistentModelIndex> order;
|
||||
for (int row = 0; row < m_projectTreeModel->rowCount(parent); ++row)
|
||||
order.emplace_back(m_projectTreeModel->index(row, 0, parent));
|
||||
std::stable_sort(order.begin(), order.end(), [this](const auto &a, const auto &b) {
|
||||
return QString::localeAwareCompare(m_projectTreeModel->data(a, Qt::DisplayRole).toString(),
|
||||
m_projectTreeModel->data(b, Qt::DisplayRole).toString()) < 0;
|
||||
});
|
||||
for (int row = 0; row < int(order.size()); ++row)
|
||||
if (order[row].row() != row)
|
||||
m_projectTreeModel->moveRows(parent, order[row].row(), 1, parent, row);
|
||||
}
|
||||
|
||||
void MainWindow::delete_()
|
||||
@@ -705,6 +761,7 @@ void MainWindow::delete_()
|
||||
using NodeType = ProjectTreeModel::NodeType;
|
||||
case NodeType::Root:
|
||||
return;
|
||||
case NodeType::Folder:
|
||||
case NodeType::Sprite:
|
||||
case NodeType::Sound:
|
||||
case NodeType::Background:
|
||||
@@ -769,7 +826,15 @@ void MainWindow::transparentBackgroundSettings()
|
||||
template<typename T>
|
||||
void MainWindow::createFor()
|
||||
{
|
||||
if (!m_projectTreeModel->insertRows(m_project.containerFor<T>().size(), 1, m_projectTreeModel->rootFor<T>()))
|
||||
QModelIndex parent = m_projectTreeModel->rootFor<T>();
|
||||
const auto current = m_ui->treeView->currentIndex();
|
||||
if (current.isValid() && m_projectTreeModel->nodeType(current) == ProjectTreeModel::NodeType::Folder)
|
||||
{
|
||||
auto category = current;
|
||||
while (category.parent().isValid()) category = category.parent();
|
||||
if (category == parent) parent = current;
|
||||
}
|
||||
if (!m_projectTreeModel->insertRows(m_projectTreeModel->rowCount(parent), 1, parent))
|
||||
QMessageBox::warning(this, tr("Creating failed!"), tr("Creating failed!"));
|
||||
}
|
||||
|
||||
@@ -820,6 +885,9 @@ void MainWindow::showIncludedFiles()
|
||||
|
||||
void MainWindow::runGame()
|
||||
{
|
||||
if (!applyOpenPropertyWindows())
|
||||
return;
|
||||
|
||||
if (m_project.rooms.empty())
|
||||
{
|
||||
QMessageBox::information(this, tr("No room to play"),
|
||||
@@ -834,6 +902,41 @@ void MainWindow::runGame()
|
||||
setEnabled(true);
|
||||
}
|
||||
|
||||
bool MainWindow::applyOpenPropertyWindows()
|
||||
{
|
||||
std::vector<QPointer<QMdiSubWindow>> windows;
|
||||
const auto collect = [&windows](const auto &propertyWindows) {
|
||||
for (const auto &entry : propertyWindows)
|
||||
windows.emplace_back(entry.second);
|
||||
};
|
||||
collect(m_spritePropertiesWindows);
|
||||
collect(m_soundPropertiesWindows);
|
||||
collect(m_backgroundPropertiesWindows);
|
||||
collect(m_pathPropertiesWindows);
|
||||
collect(m_scriptPropertiesWindows);
|
||||
collect(m_fontPropertiesWindows);
|
||||
collect(m_timeLinePropertiesWindows);
|
||||
collect(m_objectPropertiesWindows);
|
||||
collect(m_roomPropertiesWindows);
|
||||
|
||||
for (const auto &window : windows)
|
||||
{
|
||||
if (!window) continue;
|
||||
QPointer<QDialog> dialog = qobject_cast<QDialog *>(window->widget());
|
||||
if (!dialog) continue;
|
||||
auto *applier = dynamic_cast<ProjectChangesApplier *>(dialog.data());
|
||||
Q_ASSERT(applier);
|
||||
const bool hadChanges = dialog->isWindowModified();
|
||||
if (!applier->applyChanges())
|
||||
{
|
||||
m_ui->mdiArea->setActiveSubWindow(window);
|
||||
return false;
|
||||
}
|
||||
if (hadChanges) changed();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void MainWindow::debugGame()
|
||||
{
|
||||
QMessageBox::warning(this, tr("Not yet implemented"), tr("Not yet implemented"));
|
||||
@@ -857,15 +960,15 @@ void MainWindow::rowsInserted(const QModelIndex &parent, int first, int last)
|
||||
|
||||
void MainWindow::rowsAboutToBeRemoved(const QModelIndex &parent, int first, int last)
|
||||
{
|
||||
if (rowsAboutToBeRemovedFor<Sprite>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Sound>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Background>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Path>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Script>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Font>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<TimeLine>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Object>(parent, first, last)) return;
|
||||
if (rowsAboutToBeRemovedFor<Room>(parent, first, last)) return;
|
||||
rowsAboutToBeRemovedFor<Sprite>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Sound>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Background>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Path>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Script>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Font>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<TimeLine>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Object>(parent, first, last);
|
||||
rowsAboutToBeRemovedFor<Room>(parent, first, last);
|
||||
}
|
||||
|
||||
void MainWindow::modelAboutToBeReset()
|
||||
@@ -884,7 +987,6 @@ void MainWindow::modelAboutToBeReset()
|
||||
void MainWindow::setupStylesMenu()
|
||||
{
|
||||
auto currentStyle = QApplication::style();
|
||||
qDebug() << currentStyle;
|
||||
|
||||
m_actionGroupStyles = new QActionGroup{this};
|
||||
m_actionGroupStyles->setExclusive(true);
|
||||
@@ -946,6 +1048,12 @@ void MainWindow::loadFile(const QString &path)
|
||||
|
||||
QDataStream dataStream{&file};
|
||||
dataStream >> project;
|
||||
if (dataStream.status() != QDataStream::Ok)
|
||||
{
|
||||
QMessageBox::warning(this, tr("Could not load game!"),
|
||||
tr("This project has an unsupported or damaged format."));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
m_ui->mdiArea->closeAllSubWindows();
|
||||
@@ -1088,25 +1196,26 @@ bool MainWindow::activatedFor(const QModelIndex &index)
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool MainWindow::rowsAboutToBeRemovedFor(const QModelIndex &parent, int first, int last)
|
||||
void MainWindow::rowsAboutToBeRemovedFor(const QModelIndex &parent, int first, int last)
|
||||
{
|
||||
if (parent != m_projectTreeModel->rootFor<T>())
|
||||
return false;
|
||||
|
||||
for (int row = first; row <= last; row++)
|
||||
{
|
||||
if (const auto entry = m_projectTreeModel->get<T>(m_projectTreeModel->index(row, 0, parent)))
|
||||
const auto closeDescendants = [this](const auto &self, const QModelIndex &item) -> void {
|
||||
if (m_projectTreeModel->nodeType(item) == ProjectTreeModel::nodeTypeFor<T>())
|
||||
{
|
||||
auto &propertyWindows = propertyWindowsFor<T>();
|
||||
if (const auto iter = propertyWindows.find(entry); iter != std::end(propertyWindows))
|
||||
if (auto *entry = m_projectTreeModel->get<T>(item))
|
||||
{
|
||||
delete iter->second;
|
||||
propertyWindows.erase(iter);
|
||||
auto &windows = propertyWindowsFor<T>();
|
||||
if (auto it = windows.find(entry); it != windows.end())
|
||||
{
|
||||
delete it->second;
|
||||
windows.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
for (int row = 0; row < m_projectTreeModel->rowCount(item); ++row)
|
||||
self(self, m_projectTreeModel->index(row, 0, item));
|
||||
};
|
||||
for (int row = first; row <= last; ++row)
|
||||
closeDescendants(closeDescendants, m_projectTreeModel->index(row, 0, parent));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
||||
@@ -88,6 +88,7 @@ private:
|
||||
void updateTitle();
|
||||
void updateVisibilities();
|
||||
void updateRecentFiles();
|
||||
bool applyOpenPropertyWindows();
|
||||
|
||||
QMdiSubWindow *addSubWindow(QDialog *dialog);
|
||||
|
||||
@@ -98,7 +99,7 @@ private:
|
||||
bool activatedFor(const QModelIndex &index);
|
||||
|
||||
template<typename T>
|
||||
bool rowsAboutToBeRemovedFor(const QModelIndex &parent, int first, int last);
|
||||
void rowsAboutToBeRemovedFor(const QModelIndex &parent, int first, int last);
|
||||
|
||||
template<typename T>
|
||||
void modelAboutToBeResetFor();
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="dragDropMode">
|
||||
<enum>QAbstractItemView::DragDrop</enum>
|
||||
<enum>QAbstractItemView::InternalMove</enum>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
|
||||
@@ -148,7 +148,6 @@ QMimeData *ActionsContainerModel::mimeData(const QModelIndexList &indexes) const
|
||||
|
||||
bool ActionsContainerModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
qDebug() << data->formats();
|
||||
// if the drop is on an item, reject drop
|
||||
if (parent.isValid() && row == -1 && column == -1)
|
||||
{
|
||||
@@ -161,7 +160,6 @@ bool ActionsContainerModel::canDropMimeData(const QMimeData *data, Qt::DropActio
|
||||
|
||||
bool ActionsContainerModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
qDebug() << data->formats();
|
||||
// check if the action is supported
|
||||
if (!data || !(action == Qt::CopyAction || action == Qt::MoveAction))
|
||||
{
|
||||
@@ -254,8 +252,6 @@ void ActionsContainerModel::insertAction(int row, Action action)
|
||||
|
||||
bool ActionsContainerModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
qDebug() << row << count << parent.isValid();
|
||||
|
||||
if (!m_actionsContainer)
|
||||
return false;
|
||||
if (parent.isValid())
|
||||
@@ -287,8 +283,6 @@ bool ActionsContainerModel::removeRows(int row, int count, const QModelIndex &pa
|
||||
|
||||
bool ActionsContainerModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
|
||||
{
|
||||
qDebug() << sourceRow << count << destinationChild;
|
||||
|
||||
if (!m_actionsContainer)
|
||||
return false;
|
||||
if (sourceParent.isValid())
|
||||
|
||||
@@ -239,10 +239,6 @@ bool ObjectEventsModel::removeRows(int row, int count, const QModelIndex &parent
|
||||
}
|
||||
else if (std::size_t(row) < m_events.size() + m_collisionEvents.size())
|
||||
{
|
||||
qDebug() << row - m_events.size();
|
||||
qDebug() << count;
|
||||
qDebug() << m_collisionEvents.size();
|
||||
|
||||
const auto begin = std::next(std::begin(m_collisionEvents), row - m_events.size());
|
||||
const auto end = std::next(begin, count);
|
||||
m_collisionEvents.erase(begin, end);
|
||||
@@ -515,9 +511,14 @@ bool ObjectEventsModel::removeEvent(Object::EventType eventType)
|
||||
|
||||
bool ObjectEventsModel::removeEvent(const QString &object)
|
||||
{
|
||||
Q_UNUSED(object);
|
||||
qCritical() << "not implemented";
|
||||
return false;
|
||||
const auto iter = m_collisionEvents.find(object);
|
||||
if (iter == m_collisionEvents.end())
|
||||
return false;
|
||||
const int row = int(m_events.size() + std::distance(m_collisionEvents.begin(), iter));
|
||||
beginRemoveRows({}, row, row);
|
||||
m_collisionEvents.erase(iter);
|
||||
endRemoveRows();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto ObjectEventsModel::getEvent(const QModelIndex &index) -> get_event_result_t
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <QDebug>
|
||||
#include <QPixmap>
|
||||
#include <QIcon>
|
||||
#include <QMimeData>
|
||||
#include <QPersistentModelIndex>
|
||||
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
@@ -11,6 +13,17 @@
|
||||
#include "projectcontainer.h"
|
||||
|
||||
namespace {
|
||||
constexpr auto ResourceMimeType = "application/x-qtgamemaker-resource-node";
|
||||
|
||||
class ResourceMimeData : public QMimeData
|
||||
{
|
||||
public:
|
||||
explicit ResourceMimeData(const QModelIndex &index) : source{index} {
|
||||
setData(ResourceMimeType, QByteArray{1, 'x'});
|
||||
}
|
||||
QPersistentModelIndex source;
|
||||
};
|
||||
|
||||
enum {
|
||||
RowSprites,
|
||||
RowSounds,
|
||||
@@ -26,6 +39,95 @@ enum {
|
||||
RowExtensionPackages,
|
||||
NumberOfRows
|
||||
};
|
||||
|
||||
template<typename Callback>
|
||||
void visitActions(ActionsContainer &actions, Callback &&callback)
|
||||
{
|
||||
for (Action &action : actions)
|
||||
std::visit([&](auto &value) { callback(value.appliesTo); }, action);
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
void visitProjectActions(ProjectContainer &project, Callback &&callback)
|
||||
{
|
||||
for (Object &object : project.objects)
|
||||
{
|
||||
for (auto &[event, actions] : object.events)
|
||||
visitActions(actions, callback);
|
||||
for (auto &[event, actions] : object.collisionEvents)
|
||||
visitActions(actions, callback);
|
||||
}
|
||||
for (TimeLine &timeline : project.timeLines)
|
||||
for (auto &[moment, actions] : timeline.moments)
|
||||
visitActions(actions, callback);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool findNode(ResourceFolder<T> &folder, const void *pointer,
|
||||
ResourceFolder<T> *&owner, ResourceNode<T> *&node)
|
||||
{
|
||||
for (const auto &child : folder.children.template get<0>())
|
||||
{
|
||||
if (child.get() == pointer)
|
||||
{
|
||||
owner = &folder;
|
||||
node = child.get();
|
||||
return true;
|
||||
}
|
||||
if (auto *nested = child->folder(); nested && findNode(*nested, pointer, owner, node))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool findFolderNode(ResourceFolder<T> &folder, const ResourceFolder<T> *target,
|
||||
ResourceFolder<T> *&owner, ResourceNode<T> *&node)
|
||||
{
|
||||
for (const auto &child : folder.children.template get<0>())
|
||||
{
|
||||
if (child->folder() == target)
|
||||
{
|
||||
owner = &folder;
|
||||
node = child.get();
|
||||
return true;
|
||||
}
|
||||
if (auto *nested = child->folder(); nested && findFolderNode(*nested, target, owner, node))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::pair<ResourceFolder<T> *, ResourceNode<T> *>
|
||||
ProjectTreeModel::locationFor(const QModelIndex &index) const
|
||||
{
|
||||
if (!m_project || !index.isValid() || index.internalId() == std::to_underlying(NodeType::Root))
|
||||
return {nullptr, nullptr};
|
||||
ResourceFolder<T> *owner{};
|
||||
ResourceNode<T> *node{};
|
||||
findNode(const_cast<ResourceFolder<T> &>(m_project->containerFor<T>().root),
|
||||
index.internalPointer(), owner, node);
|
||||
return {owner, node};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
ResourceFolder<T> *ProjectTreeModel::folderFor(const QModelIndex &index) const
|
||||
{
|
||||
if (!m_project) return nullptr;
|
||||
if (index == rootFor<T>())
|
||||
return &const_cast<ResourceFolder<T> &>(m_project->containerFor<T>().root);
|
||||
auto [owner, node] = locationFor<T>(index);
|
||||
return node ? node->folder() : nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QModelIndex ProjectTreeModel::indexForResource(const T &resource) const
|
||||
{
|
||||
if (!m_project) return {};
|
||||
auto [owner, node] = const_cast<ResourceCollection<T> &>(m_project->containerFor<T>()).findNode(&resource);
|
||||
return owner && node ? createIndex(owner->rowOf(node), 0, node) : QModelIndex{};
|
||||
}
|
||||
|
||||
ProjectTreeModel::ProjectTreeModel(QObject *parent) :
|
||||
@@ -48,22 +150,17 @@ QVariant ProjectTreeModel::dataFor(const QModelIndex &index, int role) const
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto &container = m_project->containerFor<T>();
|
||||
if (std::size_t(index.row()) >= container.size())
|
||||
{
|
||||
qWarning() << "index out of bounds" << index.row();
|
||||
return {};
|
||||
}
|
||||
|
||||
auto iter = std::next(std::cbegin(container), index.row());
|
||||
auto [owner, node] = locationFor<T>(index);
|
||||
if (!owner || !node || !node->resource()) return {};
|
||||
const T &resource = *node->resource();
|
||||
|
||||
switch (role)
|
||||
{
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
return iter->name;
|
||||
return resource.name;
|
||||
case Qt::DecorationRole:
|
||||
return iconFor<T>(*iter);
|
||||
return iconFor<T>(resource);
|
||||
|
||||
default:
|
||||
return {};
|
||||
@@ -171,34 +268,13 @@ bool ProjectTreeModel::setDataFor(const QModelIndex &index, const QVariant &valu
|
||||
return {};
|
||||
}
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
if (index.row() < 0 || std::size_t(index.row()) >= container.size())
|
||||
{
|
||||
qWarning() << "unexpected row" << index.row();
|
||||
return false;
|
||||
}
|
||||
auto iter = std::next(std::begin(container), index.row());
|
||||
auto [owner, node] = locationFor<T>(index);
|
||||
if (!owner || !node || !node->resource()) return false;
|
||||
switch (role)
|
||||
{
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
if (auto name = value.toString(); name != iter->name)
|
||||
{
|
||||
if (std::any_of(std::cbegin(container), std::cend(container),
|
||||
[&name](const auto &entry){ return entry.name == name; }))
|
||||
{
|
||||
qWarning() << "duplicate name" << name;
|
||||
emit errorOccured(tr("A Sprite with the name \"%0\" is already existing").arg(name));
|
||||
return false;
|
||||
}
|
||||
onBeforeRename<T>(*iter, name);
|
||||
auto oldName = std::move(iter->name);
|
||||
iter->name = std::move(name);
|
||||
emitNameChanged<T>(*iter, oldName);
|
||||
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
|
||||
onAfterRename<T>(*iter, std::move(oldName));
|
||||
}
|
||||
return true;
|
||||
return rename<T>(*node->resource(), value.toString());
|
||||
default:
|
||||
qWarning() << "unexpected role" << role;
|
||||
return false;
|
||||
@@ -208,7 +284,8 @@ bool ProjectTreeModel::setDataFor(const QModelIndex &index, const QVariant &valu
|
||||
template<typename T>
|
||||
std::optional<bool> ProjectTreeModel::insertRowsFor(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
if (parent != rootFor<T>())
|
||||
auto *folder = folderFor<T>(parent);
|
||||
if (!folder)
|
||||
return std::nullopt;
|
||||
|
||||
if (!m_project)
|
||||
@@ -218,15 +295,20 @@ std::optional<bool> ProjectTreeModel::insertRowsFor(int row, int count, const QM
|
||||
}
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
if (row < 0 || std::size_t(row) > container.size())
|
||||
if (row < 0 || std::size_t(row) > folder->children.size() || count <= 0)
|
||||
{
|
||||
qWarning() << "unexpected row" << row;
|
||||
return false;
|
||||
}
|
||||
|
||||
beginInsertRows(parent, row, row + count - 1);
|
||||
for (size_t i = 0; i < std::size_t(count); i++)
|
||||
container.emplace(std::next(std::begin(container), row + i), T { .name = getFreeNameFor<T>(container) });
|
||||
auto &sequence = folder->children.template get<0>();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
sequence.insert(std::next(sequence.begin(), row + i),
|
||||
std::make_unique<ResourceNode<T>>(T{.name = getFreeNameFor<T>(*folder)}));
|
||||
container.invalidate();
|
||||
}
|
||||
endInsertRows();
|
||||
|
||||
return true;
|
||||
@@ -235,7 +317,8 @@ std::optional<bool> ProjectTreeModel::insertRowsFor(int row, int count, const QM
|
||||
template<typename T>
|
||||
std::optional<bool> ProjectTreeModel::removeRowsFor(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
if (parent != rootFor<T>())
|
||||
auto *folder = folderFor<T>(parent);
|
||||
if (!folder)
|
||||
return std::nullopt;
|
||||
|
||||
if (!m_project)
|
||||
@@ -246,29 +329,38 @@ std::optional<bool> ProjectTreeModel::removeRowsFor(int row, int count, const QM
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
|
||||
if (row < 0 || std::size_t(row) >= container.size())
|
||||
if (row < 0 || std::size_t(row) >= folder->children.size())
|
||||
{
|
||||
qWarning() << "unexpected row" << row;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (count < 0 || std::size_t(count) > container.size() - row)
|
||||
if (count <= 0 || std::size_t(count) > folder->children.size() - row)
|
||||
{
|
||||
qWarning() << "unexpected row+count" << count << row;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto begin = std::next(std::begin(container), row);
|
||||
auto &sequence = folder->children.template get<0>();
|
||||
auto begin = std::next(sequence.begin(), row);
|
||||
auto end = std::next(begin, count);
|
||||
|
||||
for (auto iter = begin; iter != end; iter++)
|
||||
onBeforeRemove<T>(*iter);
|
||||
|
||||
for (auto iter = begin; iter != end; iter++)
|
||||
emitAboutToBeRemoved(*iter);
|
||||
const auto notifyDescendants = [this](const auto &self, ResourceNode<T> &node) -> void {
|
||||
if (T *resource = node.resource())
|
||||
{
|
||||
onBeforeRemove<T>(*resource);
|
||||
emitAboutToBeRemoved(*resource);
|
||||
}
|
||||
else if (auto *nested = node.folder())
|
||||
for (const auto &child : nested->children.template get<0>())
|
||||
self(self, *child);
|
||||
};
|
||||
for (auto iter = begin; iter != end; ++iter)
|
||||
notifyDescendants(notifyDescendants, **iter);
|
||||
|
||||
beginRemoveRows(parent, row, row + count - 1);
|
||||
container.erase(begin, end);
|
||||
sequence.erase(begin, end);
|
||||
container.invalidate();
|
||||
endRemoveRows();
|
||||
|
||||
return true;
|
||||
@@ -288,7 +380,7 @@ template<> void ProjectTreeModel::onBeforeRemove<Sprite>(const Sprite &sprite)
|
||||
auto oldSpriteName = std::move(iter->spriteName);
|
||||
iter->spriteName.clear();
|
||||
|
||||
const auto index = this->index(std::distance(std::begin(m_project->objects), iter), 0, rootFor<Object>());
|
||||
const auto index = indexForResource(*iter);
|
||||
emit dataChanged(index, index, {Qt::DecorationRole});
|
||||
|
||||
emit objectSpriteNameChanged(*iter, std::move(oldSpriteName));
|
||||
@@ -303,6 +395,12 @@ template<> void ProjectTreeModel::onBeforeRemove<Sprite>(const Sprite &sprite)
|
||||
|
||||
template<> void ProjectTreeModel::onBeforeRemove<Object>(const Object &object)
|
||||
{
|
||||
visitProjectActions(*m_project, [&object](AppliesTo &appliesTo) {
|
||||
if (auto *target = std::get_if<AppliesToObject>(&appliesTo);
|
||||
target && target->objectName == object.name)
|
||||
target->objectName.clear();
|
||||
});
|
||||
|
||||
for (Object &obj : m_project->objects)
|
||||
{
|
||||
if (!obj.parentName.isEmpty() && obj.parentName == object.name)
|
||||
@@ -341,8 +439,8 @@ template<> void ProjectTreeModel::onAfterRename<Sprite>(const Sprite &sprite, co
|
||||
|
||||
object.spriteName = sprite.name;
|
||||
|
||||
// const auto index = this->index(std::distance(std::begin(m_project->objects), iter), 0, rootFor<Object>());
|
||||
// emit dataChanged(index, index, {Qt::DecorationRole});
|
||||
const auto index = indexForResource(object);
|
||||
emit dataChanged(index, index, {Qt::DecorationRole});
|
||||
|
||||
emit objectSpriteNameChanged(object, std::move(oldSpriteName));
|
||||
}
|
||||
@@ -356,6 +454,12 @@ template<> void ProjectTreeModel::onAfterRename<Sprite>(const Sprite &sprite, co
|
||||
|
||||
template<> void ProjectTreeModel::onBeforeRename<Object>(const Object &object, const QString &newName)
|
||||
{
|
||||
visitProjectActions(*m_project, [&object, &newName](AppliesTo &appliesTo) {
|
||||
if (auto *target = std::get_if<AppliesToObject>(&appliesTo);
|
||||
target && target->objectName == object.name)
|
||||
target->objectName = newName;
|
||||
});
|
||||
|
||||
for (Object &obj : m_project->objects)
|
||||
{
|
||||
if (!obj.parentName.isEmpty() && obj.parentName == object.name)
|
||||
@@ -376,20 +480,20 @@ template<> void ProjectTreeModel::onBeforeRename<Object>(const Object &object, c
|
||||
if (obj.objectName != object.name)
|
||||
continue;
|
||||
|
||||
obj.objectName = object.name;
|
||||
obj.objectName = newName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QString ProjectTreeModel::getFreeNameFor(const std::list<T> &container)
|
||||
QString ProjectTreeModel::getFreeNameFor(const ResourceFolder<T> &folder) const
|
||||
{
|
||||
QString name;
|
||||
for (int i = 0; ; i++)
|
||||
{
|
||||
name = nameTempateFor<T>().arg(i);
|
||||
if (std::none_of(std::cbegin(container), std::cend(container),
|
||||
[&name](const T &entry){ return entry.name == name; }))
|
||||
if (!m_project->hasResourceName(name) &&
|
||||
folder.children.template get<1>().find(name) == folder.children.template get<1>().end())
|
||||
break;
|
||||
}
|
||||
return name;
|
||||
@@ -434,58 +538,47 @@ template<> QString ProjectTreeModel::nameTempateFor<Room>() { return QStringLite
|
||||
|
||||
QModelIndex ProjectTreeModel::index(int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if (row < 0 || column != 0 || !m_project) return {};
|
||||
if (!parent.isValid())
|
||||
return createIndex(row, column, std::to_underlying(NodeType::Root));
|
||||
else if (!parent.parent().isValid())
|
||||
{
|
||||
switch (nodeType(parent))
|
||||
{
|
||||
case NodeType::Root:
|
||||
switch (parent.row())
|
||||
{
|
||||
case RowSprites: return createIndex(row, column, std::to_underlying(NodeType::Sprite));
|
||||
case RowSounds: return createIndex(row, column, std::to_underlying(NodeType::Sound));
|
||||
case RowBackgrounds: return createIndex(row, column, std::to_underlying(NodeType::Background));
|
||||
case RowPaths: return createIndex(row, column, std::to_underlying(NodeType::Path));
|
||||
case RowScripts: return createIndex(row, column, std::to_underlying(NodeType::Script));
|
||||
case RowFonts: return createIndex(row, column, std::to_underlying(NodeType::Font));
|
||||
case RowTimeLines: return createIndex(row, column, std::to_underlying(NodeType::TimeLine));
|
||||
case RowObjects: return createIndex(row, column, std::to_underlying(NodeType::Object));
|
||||
case RowRooms: return createIndex(row, column, std::to_underlying(NodeType::Room));
|
||||
default:
|
||||
qWarning() << "unexpected parent row" << parent.row();
|
||||
return {};
|
||||
}
|
||||
default:
|
||||
qWarning() << "unexpected parent type" << parent.internalId();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qWarning() << "unexpected parent";
|
||||
return {};
|
||||
}
|
||||
return row < NumberOfRows ? createIndex(row, column, std::to_underlying(NodeType::Root)) : QModelIndex{};
|
||||
#define RESOURCE_INDEX(T) if (auto *folder = folderFor<T>(parent)) { \
|
||||
auto *node = folder->at(row); return node ? createIndex(row, column, node) : QModelIndex{}; }
|
||||
RESOURCE_INDEX(Sprite)
|
||||
RESOURCE_INDEX(Sound)
|
||||
RESOURCE_INDEX(Background)
|
||||
RESOURCE_INDEX(Path)
|
||||
RESOURCE_INDEX(Script)
|
||||
RESOURCE_INDEX(Font)
|
||||
RESOURCE_INDEX(TimeLine)
|
||||
RESOURCE_INDEX(Object)
|
||||
RESOURCE_INDEX(Room)
|
||||
#undef RESOURCE_INDEX
|
||||
return {};
|
||||
}
|
||||
|
||||
QModelIndex ProjectTreeModel::parent(const QModelIndex &child) const
|
||||
{
|
||||
switch (nodeType(child))
|
||||
{
|
||||
case NodeType::Root: return {};
|
||||
case NodeType::Sprite: return rootFor<Sprite>();
|
||||
case NodeType::Sound: return rootFor<Sound>();
|
||||
case NodeType::Background: return rootFor<Background>();
|
||||
case NodeType::Path: return rootFor<Path>();
|
||||
case NodeType::Script: return rootFor<Script>();
|
||||
case NodeType::Font: return rootFor<Font>();
|
||||
case NodeType::TimeLine: return rootFor<TimeLine>();
|
||||
case NodeType::Object: return rootFor<Object>();
|
||||
case NodeType::Room: return rootFor<Room>();
|
||||
default:
|
||||
qWarning() << "unexpected child type" << child.internalId();
|
||||
return {};
|
||||
}
|
||||
if (!child.isValid() || child.internalId() == std::to_underlying(NodeType::Root)) return {};
|
||||
#define RESOURCE_PARENT(T) { \
|
||||
auto [owner, node] = locationFor<T>(child); \
|
||||
if (owner && node) { \
|
||||
auto &root = const_cast<ResourceFolder<T> &>(m_project->containerFor<T>().root); \
|
||||
if (owner == &root) return rootFor<T>(); \
|
||||
ResourceFolder<T> *grandOwner{}; ResourceNode<T> *folderNode{}; \
|
||||
findFolderNode(root, owner, grandOwner, folderNode); \
|
||||
return grandOwner && folderNode ? createIndex(grandOwner->rowOf(folderNode), 0, folderNode) : QModelIndex{}; \
|
||||
} }
|
||||
RESOURCE_PARENT(Sprite)
|
||||
RESOURCE_PARENT(Sound)
|
||||
RESOURCE_PARENT(Background)
|
||||
RESOURCE_PARENT(Path)
|
||||
RESOURCE_PARENT(Script)
|
||||
RESOURCE_PARENT(Font)
|
||||
RESOURCE_PARENT(TimeLine)
|
||||
RESOURCE_PARENT(Object)
|
||||
RESOURCE_PARENT(Room)
|
||||
#undef RESOURCE_PARENT
|
||||
return {};
|
||||
}
|
||||
|
||||
int ProjectTreeModel::rowCount(const QModelIndex &parent) const
|
||||
@@ -498,15 +591,15 @@ int ProjectTreeModel::rowCount(const QModelIndex &parent) const
|
||||
case NodeType::Root:
|
||||
switch (parent.row())
|
||||
{
|
||||
case RowSprites: return m_project ? m_project->containerFor<Sprite>().size() : 0;
|
||||
case RowSounds: return m_project ? m_project->containerFor<Sound>().size() : 0;
|
||||
case RowBackgrounds: return m_project ? m_project->containerFor<Background>().size() : 0;
|
||||
case RowPaths: return m_project ? m_project->containerFor<Path>().size() : 0;
|
||||
case RowScripts: return m_project ? m_project->containerFor<Script>().size() : 0;
|
||||
case RowFonts: return m_project ? m_project->containerFor<Font>().size() : 0;
|
||||
case RowTimeLines: return m_project ? m_project->containerFor<TimeLine>().size() : 0;
|
||||
case RowObjects: return m_project ? m_project->containerFor<Object>().size() : 0;
|
||||
case RowRooms: return m_project ? m_project->containerFor<Room>().size() : 0;
|
||||
case RowSprites: return m_project ? m_project->containerFor<Sprite>().root.children.size() : 0;
|
||||
case RowSounds: return m_project ? m_project->containerFor<Sound>().root.children.size() : 0;
|
||||
case RowBackgrounds: return m_project ? m_project->containerFor<Background>().root.children.size() : 0;
|
||||
case RowPaths: return m_project ? m_project->containerFor<Path>().root.children.size() : 0;
|
||||
case RowScripts: return m_project ? m_project->containerFor<Script>().root.children.size() : 0;
|
||||
case RowFonts: return m_project ? m_project->containerFor<Font>().root.children.size() : 0;
|
||||
case RowTimeLines: return m_project ? m_project->containerFor<TimeLine>().root.children.size() : 0;
|
||||
case RowObjects: return m_project ? m_project->containerFor<Object>().root.children.size() : 0;
|
||||
case RowRooms: return m_project ? m_project->containerFor<Room>().root.children.size() : 0;
|
||||
case RowGameInformation:
|
||||
case RowGlobalGameSettings:
|
||||
case RowExtensionPackages:
|
||||
@@ -517,6 +610,19 @@ int ProjectTreeModel::rowCount(const QModelIndex &parent) const
|
||||
}
|
||||
|
||||
return {};
|
||||
case NodeType::Folder:
|
||||
#define FOLDER_ROWS(T) if (auto *folder = folderFor<T>(parent)) return int(folder->children.size());
|
||||
FOLDER_ROWS(Sprite)
|
||||
FOLDER_ROWS(Sound)
|
||||
FOLDER_ROWS(Background)
|
||||
FOLDER_ROWS(Path)
|
||||
FOLDER_ROWS(Script)
|
||||
FOLDER_ROWS(Font)
|
||||
FOLDER_ROWS(TimeLine)
|
||||
FOLDER_ROWS(Object)
|
||||
FOLDER_ROWS(Room)
|
||||
#undef FOLDER_ROWS
|
||||
return 0;
|
||||
case NodeType::Sprite:
|
||||
case NodeType::Sound:
|
||||
case NodeType::Background:
|
||||
@@ -562,6 +668,7 @@ int ProjectTreeModel::columnCount(const QModelIndex &parent) const
|
||||
}
|
||||
|
||||
return {};
|
||||
case NodeType::Folder:
|
||||
case NodeType::Sprite:
|
||||
case NodeType::Sound:
|
||||
case NodeType::Background:
|
||||
@@ -588,6 +695,26 @@ QVariant ProjectTreeModel::data(const QModelIndex &index, int role) const
|
||||
|
||||
switch (nodeType(index))
|
||||
{
|
||||
case NodeType::Folder:
|
||||
{
|
||||
#define FOLDER_DATA(T) { auto [owner, node] = locationFor<T>(index); \
|
||||
if (owner && node && node->folder()) { \
|
||||
if (role == Qt::DisplayRole || role == Qt::EditRole) return node->name(); \
|
||||
if (role == Qt::DecorationRole) return QIcon{":/qtgameengine/icons/folder.png"}; \
|
||||
return {}; \
|
||||
} }
|
||||
FOLDER_DATA(Sprite)
|
||||
FOLDER_DATA(Sound)
|
||||
FOLDER_DATA(Background)
|
||||
FOLDER_DATA(Path)
|
||||
FOLDER_DATA(Script)
|
||||
FOLDER_DATA(Font)
|
||||
FOLDER_DATA(TimeLine)
|
||||
FOLDER_DATA(Object)
|
||||
FOLDER_DATA(Room)
|
||||
#undef FOLDER_DATA
|
||||
return {};
|
||||
}
|
||||
case NodeType::Root:
|
||||
switch (role)
|
||||
{
|
||||
@@ -651,11 +778,17 @@ QVariant ProjectTreeModel::data(const QModelIndex &index, int role) const
|
||||
|
||||
Qt::ItemFlags ProjectTreeModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid()) return Qt::NoItemFlags;
|
||||
auto flags = QAbstractItemModel::flags(index);
|
||||
|
||||
switch (nodeType(index))
|
||||
{
|
||||
case NodeType::Root:
|
||||
if (index.row() <= RowRooms)
|
||||
flags |= Qt::ItemIsDropEnabled;
|
||||
break;
|
||||
case NodeType::Folder:
|
||||
flags |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
|
||||
break;
|
||||
case NodeType::Sprite:
|
||||
case NodeType::Sound:
|
||||
@@ -666,7 +799,7 @@ Qt::ItemFlags ProjectTreeModel::flags(const QModelIndex &index) const
|
||||
case NodeType::TimeLine:
|
||||
case NodeType::Object:
|
||||
case NodeType::Room:
|
||||
flags |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
|
||||
flags |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
|
||||
}
|
||||
|
||||
return flags;
|
||||
@@ -674,13 +807,95 @@ Qt::ItemFlags ProjectTreeModel::flags(const QModelIndex &index) const
|
||||
|
||||
Qt::DropActions ProjectTreeModel::supportedDropActions() const
|
||||
{
|
||||
return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
|
||||
return Qt::MoveAction;
|
||||
}
|
||||
|
||||
QStringList ProjectTreeModel::mimeTypes() const
|
||||
{
|
||||
return {QString::fromLatin1(ResourceMimeType)};
|
||||
}
|
||||
|
||||
QMimeData *ProjectTreeModel::mimeData(const QModelIndexList &indexes) const
|
||||
{
|
||||
for (const QModelIndex &index : indexes)
|
||||
if (index.isValid() && nodeType(index) != NodeType::Root)
|
||||
return new ResourceMimeData(index);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool ProjectTreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action,
|
||||
int row, int column, const QModelIndex &parent) const
|
||||
{
|
||||
if (action != Qt::MoveAction || column > 0 || !data) return false;
|
||||
const auto *resourceData = dynamic_cast<const ResourceMimeData *>(data);
|
||||
if (!resourceData || !resourceData->source.isValid() || resourceData->source.model() != this) return false;
|
||||
if (nodeType(parent) != NodeType::Root && nodeType(parent) != NodeType::Folder) return false;
|
||||
const QModelIndex source = resourceData->source;
|
||||
const auto categoryRoot = [](QModelIndex index) {
|
||||
while (index.isValid() && index.parent().isValid())
|
||||
index = index.parent();
|
||||
return index;
|
||||
};
|
||||
if (!parent.isValid() || categoryRoot(source) != categoryRoot(parent)) return false;
|
||||
if (row < -1 || row > rowCount(parent)) return false;
|
||||
for (QModelIndex ancestor = parent; ancestor.isValid(); ancestor = ancestor.parent())
|
||||
if (ancestor == source) return false;
|
||||
const int destinationRow = row < 0 ? rowCount(parent) : row;
|
||||
if (source.parent() == parent &&
|
||||
(destinationRow == source.row() || destinationRow == source.row() + 1)) return false;
|
||||
if (source.parent() != parent)
|
||||
{
|
||||
const QString sourceName = source.data(Qt::DisplayRole).toString();
|
||||
for (int child = 0; child < rowCount(parent); ++child)
|
||||
if (this->data(index(child, 0, parent), Qt::DisplayRole).toString() == sourceName)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ProjectTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action,
|
||||
int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
if (!canDropMimeData(data, action, row, column, parent)) return false;
|
||||
const auto *resourceData = static_cast<const ResourceMimeData *>(data);
|
||||
const QModelIndex source = resourceData->source;
|
||||
return moveRows(source.parent(), source.row(), 1, parent,
|
||||
row < 0 ? rowCount(parent) : row);
|
||||
}
|
||||
|
||||
bool ProjectTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
switch (const auto nodeType = this->nodeType(index))
|
||||
{
|
||||
case NodeType::Folder:
|
||||
{
|
||||
if (role != Qt::EditRole && role != Qt::DisplayRole) return false;
|
||||
const QString name = value.toString().trimmed();
|
||||
if (name.isEmpty()) return false;
|
||||
#define RENAME_FOLDER(T) { auto [owner, node] = locationFor<T>(index); \
|
||||
if (owner && node && node->folder()) { \
|
||||
if (node->name() == name) return true; \
|
||||
auto &hashed = owner->children.template get<1>(); \
|
||||
if (hashed.find(name) != hashed.end()) return false; \
|
||||
auto &sequence = owner->children.template get<0>(); \
|
||||
auto it = std::find_if(sequence.begin(), sequence.end(), \
|
||||
[node](const auto &child) { return child.get() == node; }); \
|
||||
const bool okay = sequence.modify(it, [&name](auto &child) { child->folder()->name = name; }); \
|
||||
if (okay) emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); \
|
||||
return okay; \
|
||||
} }
|
||||
RENAME_FOLDER(Sprite)
|
||||
RENAME_FOLDER(Sound)
|
||||
RENAME_FOLDER(Background)
|
||||
RENAME_FOLDER(Path)
|
||||
RENAME_FOLDER(Script)
|
||||
RENAME_FOLDER(Font)
|
||||
RENAME_FOLDER(TimeLine)
|
||||
RENAME_FOLDER(Object)
|
||||
RENAME_FOLDER(Room)
|
||||
#undef RENAME_FOLDER
|
||||
return false;
|
||||
}
|
||||
case NodeType::Sprite: return setDataFor<Sprite>(index, value, role);
|
||||
case NodeType::Sound: return setDataFor<Sound>(index, value, role);
|
||||
case NodeType::Background: return setDataFor<Background>(index, value, role);
|
||||
@@ -714,10 +929,83 @@ bool ProjectTreeModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||
|
||||
bool ProjectTreeModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
|
||||
{
|
||||
qDebug() << "called" << sourceParent << sourceRow << count << destinationParent << destinationChild;
|
||||
if (count <= 0 || sourceRow < 0 || destinationChild < 0) return false;
|
||||
#define MOVE_RESOURCES(T) { \
|
||||
auto *source = folderFor<T>(sourceParent); \
|
||||
auto *destination = folderFor<T>(destinationParent); \
|
||||
if (source && destination) { \
|
||||
auto &from = source->children.template get<0>(); \
|
||||
auto &to = destination->children.template get<0>(); \
|
||||
if (std::size_t(sourceRow + count) > from.size() || std::size_t(destinationChild) > to.size()) return false; \
|
||||
if (source == destination && destinationChild >= sourceRow && destinationChild <= sourceRow + count) return false; \
|
||||
for (int i = 0; i < count; ++i) { \
|
||||
auto *moving = source->at(sourceRow + i); \
|
||||
if (source != destination && destination->children.template get<1>().find(moving->name()) != destination->children.template get<1>().end()) return false; \
|
||||
if (auto *nested = moving->folder()) { \
|
||||
const auto contains = [&](const auto &self, const ResourceFolder<T> *folder) -> bool { \
|
||||
if (folder == destination) return true; \
|
||||
for (const auto &child : folder->children.template get<0>()) \
|
||||
if (const auto *childFolder = child->folder(); childFolder && self(self, childFolder)) return true; \
|
||||
return false; \
|
||||
}; \
|
||||
if (contains(contains, nested)) return false; \
|
||||
} \
|
||||
} \
|
||||
if (!beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destinationParent, destinationChild)) return false; \
|
||||
auto first = std::next(from.begin(), sourceRow); \
|
||||
auto last = std::next(first, count); \
|
||||
to.splice(std::next(to.begin(), destinationChild), from, first, last); \
|
||||
m_project->containerFor<T>().invalidate(); \
|
||||
endMoveRows(); \
|
||||
return true; \
|
||||
} }
|
||||
MOVE_RESOURCES(Sprite)
|
||||
MOVE_RESOURCES(Sound)
|
||||
MOVE_RESOURCES(Background)
|
||||
MOVE_RESOURCES(Path)
|
||||
MOVE_RESOURCES(Script)
|
||||
MOVE_RESOURCES(Font)
|
||||
MOVE_RESOURCES(TimeLine)
|
||||
MOVE_RESOURCES(Object)
|
||||
MOVE_RESOURCES(Room)
|
||||
#undef MOVE_RESOURCES
|
||||
return false;
|
||||
}
|
||||
|
||||
QModelIndex ProjectTreeModel::createFolder(const QModelIndex &parent, const QString &requestedName, int requestedRow)
|
||||
{
|
||||
QString name = requestedName.trimmed();
|
||||
#define CREATE_FOLDER(T) if (auto *folder = folderFor<T>(parent)) { \
|
||||
auto &sequence = folder->children.template get<0>(); \
|
||||
if (name.isEmpty()) { \
|
||||
for (int i = 0;; ++i) { \
|
||||
name = tr("group%1").arg(i); \
|
||||
if (folder->children.template get<1>().find(name) == folder->children.template get<1>().end()) break; \
|
||||
} \
|
||||
} \
|
||||
if (folder->children.template get<1>().find(name) != folder->children.template get<1>().end()) return {}; \
|
||||
const int row = requestedRow < 0 ? int(sequence.size()) : requestedRow; \
|
||||
if (row < 0 || std::size_t(row) > sequence.size()) return {}; \
|
||||
beginInsertRows(parent, row, row); \
|
||||
auto node = std::make_unique<ResourceNode<T>>(std::make_unique<ResourceFolder<T>>(name)); \
|
||||
auto *pointer = node.get(); \
|
||||
sequence.insert(std::next(sequence.begin(), row), std::move(node)); \
|
||||
endInsertRows(); \
|
||||
return createIndex(row, 0, pointer); \
|
||||
}
|
||||
CREATE_FOLDER(Sprite)
|
||||
CREATE_FOLDER(Sound)
|
||||
CREATE_FOLDER(Background)
|
||||
CREATE_FOLDER(Path)
|
||||
CREATE_FOLDER(Script)
|
||||
CREATE_FOLDER(Font)
|
||||
CREATE_FOLDER(TimeLine)
|
||||
CREATE_FOLDER(Object)
|
||||
CREATE_FOLDER(Room)
|
||||
#undef CREATE_FOLDER
|
||||
return {};
|
||||
}
|
||||
|
||||
bool ProjectTreeModel::removeRows(int row, int count, const QModelIndex &parent)
|
||||
{
|
||||
if (auto result = removeRowsFor<Sprite>(row, count, parent)) return *result;
|
||||
@@ -743,10 +1031,22 @@ void ProjectTreeModel::setProject(ProjectContainer *project)
|
||||
|
||||
ProjectTreeModel::NodeType ProjectTreeModel::nodeType(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid())
|
||||
qWarning() << "invalid index";
|
||||
|
||||
return NodeType(index.internalId());
|
||||
if (!index.isValid()) return NodeType::Root;
|
||||
if (index.internalId() == std::to_underlying(NodeType::Root)) return NodeType::Root;
|
||||
#define RESOURCE_TYPE(T) { auto [owner, node] = locationFor<T>(index); \
|
||||
if (owner && node) return node->isFolder() ? NodeType::Folder : nodeTypeFor<T>(); }
|
||||
RESOURCE_TYPE(Sprite)
|
||||
RESOURCE_TYPE(Sound)
|
||||
RESOURCE_TYPE(Background)
|
||||
RESOURCE_TYPE(Path)
|
||||
RESOURCE_TYPE(Script)
|
||||
RESOURCE_TYPE(Font)
|
||||
RESOURCE_TYPE(TimeLine)
|
||||
RESOURCE_TYPE(Object)
|
||||
RESOURCE_TYPE(Room)
|
||||
#undef RESOURCE_TYPE
|
||||
qWarning() << "index is not in this project" << index;
|
||||
return NodeType::Root;
|
||||
}
|
||||
|
||||
template<> QModelIndex ProjectTreeModel::rootFor<Sprite>() const
|
||||
@@ -824,14 +1124,8 @@ T *ProjectTreeModel::get(const QModelIndex &index)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
if (index.row() < 0 || std::size_t(index.row()) >= container.size())
|
||||
{
|
||||
qWarning() << "invalid row" << index.row();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &*std::next(std::begin(container), index.row());
|
||||
auto [owner, node] = locationFor<T>(index);
|
||||
return owner && node ? node->resource() : nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -849,14 +1143,8 @@ const T *ProjectTreeModel::get(const QModelIndex &index) const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto &container = m_project->containerFor<T>();
|
||||
if (index.row() < 0 || std::size_t(index.row()) >= container.size())
|
||||
{
|
||||
qWarning() << "invalid row" << index.row();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return &*std::next(std::cbegin(container), index.row());
|
||||
auto [owner, node] = locationFor<T>(index);
|
||||
return owner && node ? node->resource() : nullptr;
|
||||
}
|
||||
|
||||
template Sprite *ProjectTreeModel::get<Sprite>(const QModelIndex &index);
|
||||
@@ -889,11 +1177,9 @@ T *ProjectTreeModel::create()
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
|
||||
beginInsertRows(rootFor<T>(), container.size(), container.size());
|
||||
container.emplace_back(T { .name = getFreeNameFor<T>(container) });
|
||||
endInsertRows();
|
||||
|
||||
return &container.back();
|
||||
const QModelIndex root = rootFor<T>();
|
||||
if (!insertRows(int(container.root.children.size()), 1, root)) return nullptr;
|
||||
return get<T>(index(int(container.root.children.size()) - 1, 0, root));
|
||||
}
|
||||
|
||||
template Sprite *ProjectTreeModel::create<Sprite>();
|
||||
@@ -916,25 +1202,14 @@ bool ProjectTreeModel::remove(const T &entry)
|
||||
}
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
const auto iter = std::find_if(std::begin(container), std::end(container),
|
||||
[&entry](const auto &otherEntry){ return &entry == &otherEntry; });
|
||||
if (iter == std::cend(container))
|
||||
auto [owner, node] = container.findNode(&entry);
|
||||
if (!owner || !node)
|
||||
{
|
||||
qWarning() << "entry not from this project!";
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto row = std::distance(std::begin(container), iter);
|
||||
|
||||
onBeforeRemove<T>(*iter);
|
||||
|
||||
emitAboutToBeRemoved<T>(*iter);
|
||||
|
||||
beginRemoveRows(rootFor<T>(), row, row);
|
||||
container.erase(iter);
|
||||
endRemoveRows();
|
||||
|
||||
return true;
|
||||
return removeRows(owner->rowOf(node), 1, indexForResource(entry).parent());
|
||||
}
|
||||
|
||||
template bool ProjectTreeModel::remove<Sprite>(const Sprite &entry);
|
||||
@@ -957,34 +1232,39 @@ bool ProjectTreeModel::rename(const T &entry, const QString &newName)
|
||||
}
|
||||
|
||||
auto &container = m_project->containerFor<T>();
|
||||
const auto iter = std::find_if(std::begin(container), std::end(container),
|
||||
[&entry](const auto &otherEntry){ return &entry == &otherEntry; });
|
||||
if (iter == std::cend(container))
|
||||
auto [owner, node] = container.findNode(&entry);
|
||||
if (!owner || !node || !node->resource())
|
||||
{
|
||||
qWarning() << "entry not from this project!";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (iter->name == newName)
|
||||
if (node->resource()->name == newName)
|
||||
return true;
|
||||
|
||||
if (std::any_of(std::cbegin(container), std::cend(container),
|
||||
[&newName](const auto &entry){ return entry.name == newName; }))
|
||||
if (newName.trimmed().isEmpty()) return false;
|
||||
|
||||
if (m_project->hasResourceName(newName) ||
|
||||
owner->children.template get<1>().find(newName) != owner->children.template get<1>().end())
|
||||
{
|
||||
qWarning() << "duplicate name" << newName;
|
||||
return false;
|
||||
}
|
||||
|
||||
onBeforeRename<T>(*iter, newName);
|
||||
onBeforeRename<T>(*node->resource(), newName);
|
||||
|
||||
auto oldName = std::move(iter->name);
|
||||
iter->name = std::move(newName);
|
||||
const QString oldName = node->resource()->name;
|
||||
auto &sequence = owner->children.template get<0>();
|
||||
auto nodeIter = std::find_if(sequence.begin(), sequence.end(),
|
||||
[node](const auto &child) { return child.get() == node; });
|
||||
if (!sequence.modify(nodeIter, [&newName](auto &child) { child->resource()->name = newName; }))
|
||||
return false;
|
||||
container.invalidate();
|
||||
|
||||
emitNameChanged<T>(*iter, oldName);
|
||||
const auto index = this->index(std::distance(std::begin(container), iter), 0, rootFor<T>());
|
||||
emitNameChanged<T>(*node->resource(), oldName);
|
||||
const auto index = indexForResource(*node->resource());
|
||||
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
|
||||
|
||||
onAfterRename<T>(*iter, std::move(oldName));
|
||||
onAfterRename<T>(*node->resource(), oldName);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1019,7 +1299,7 @@ bool ProjectTreeModel::setSpritePixmaps(const Sprite &sprite, std::vector<QPixma
|
||||
|
||||
iter->pixmaps = std::move(pixmaps);
|
||||
|
||||
const auto index = this->index(std::distance(std::begin(container), iter), 0, rootFor<Sprite>());
|
||||
const auto index = indexForResource(*iter);
|
||||
emit dataChanged(index, index, { Qt::DecorationRole });
|
||||
|
||||
emit spritePixmapsChanged(*iter);
|
||||
@@ -1030,7 +1310,7 @@ bool ProjectTreeModel::setSpritePixmaps(const Sprite &sprite, std::vector<QPixma
|
||||
if (iter->spriteName != sprite.name)
|
||||
continue;
|
||||
|
||||
const auto index = this->index(std::distance(std::cbegin(m_project->objects), iter), 0, rootFor<Object>());
|
||||
const auto index = indexForResource(*iter);
|
||||
emit dataChanged(index, index, {Qt::DecorationRole});
|
||||
}
|
||||
|
||||
@@ -1056,7 +1336,7 @@ bool ProjectTreeModel::setBackgroundPixmap(const Background &background, QPixmap
|
||||
|
||||
iter->pixmap = std::move(pixmap);
|
||||
|
||||
const auto index = this->index(std::distance(std::begin(container), iter), 0, rootFor<Background>());
|
||||
const auto index = indexForResource(*iter);
|
||||
emit dataChanged(index, index, { Qt::DecorationRole });
|
||||
|
||||
emit backgroundPixmapChanged(*iter);
|
||||
@@ -1087,7 +1367,7 @@ bool ProjectTreeModel::setObjectSpriteName(const Object &object, QString &&sprit
|
||||
auto oldSpriteName = std::move(iter->spriteName);
|
||||
iter->spriteName = std::move(spriteName);
|
||||
|
||||
const auto index = this->index(std::distance(std::begin(container), iter), 0, rootFor<Object>());
|
||||
const auto index = indexForResource(*iter);
|
||||
emit dataChanged(index, index, { Qt::DecorationRole });
|
||||
|
||||
emit objectSpriteNameChanged(object, std::move(oldSpriteName));
|
||||
|
||||
@@ -14,6 +14,9 @@ struct Font;
|
||||
struct TimeLine;
|
||||
struct Object;
|
||||
struct Room;
|
||||
template<typename T> struct ResourceFolder;
|
||||
template<typename T> struct ResourceNode;
|
||||
template<typename T> class ResourceCollection;
|
||||
|
||||
class ProjectTreeModel : public QAbstractItemModel
|
||||
{
|
||||
@@ -31,18 +34,22 @@ public:
|
||||
TimeLine,
|
||||
Object,
|
||||
Room,
|
||||
Folder,
|
||||
};
|
||||
|
||||
explicit ProjectTreeModel(QObject *parent = nullptr);
|
||||
explicit ProjectTreeModel(ProjectContainer &project, QObject *parent = nullptr);
|
||||
|
||||
private:
|
||||
template<typename T> ResourceFolder<T> *folderFor(const QModelIndex &index) const;
|
||||
template<typename T> std::pair<ResourceFolder<T> *, ResourceNode<T> *> locationFor(const QModelIndex &index) const;
|
||||
template<typename T> QModelIndex indexForResource(const T &resource) const;
|
||||
template<typename T> QVariant dataFor(const QModelIndex &index, int role) const;
|
||||
template<typename T> QVariant iconFor(const T &entry) const;
|
||||
template<typename T> bool setDataFor(const QModelIndex &index, const QVariant &value, int role);
|
||||
template<typename T> std::optional<bool> insertRowsFor(int row, int count, const QModelIndex &parent);
|
||||
template<typename T> std::optional<bool> removeRowsFor(int row, int count, const QModelIndex &parent);
|
||||
template<typename T> static QString getFreeNameFor(const std::list<T> &container);
|
||||
template<typename T> QString getFreeNameFor(const ResourceFolder<T> &folder) const;
|
||||
|
||||
template<typename T> void emitCreated(const T &entry);
|
||||
template<typename T> void onBeforeRemove(const T &entry);
|
||||
@@ -60,6 +67,12 @@ public:
|
||||
QVariant data(const QModelIndex &index, int role) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
Qt::DropActions supportedDropActions() const override;
|
||||
QStringList mimeTypes() const override;
|
||||
QMimeData *mimeData(const QModelIndexList &indexes) const override;
|
||||
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
|
||||
int column, const QModelIndex &parent) const override;
|
||||
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row,
|
||||
int column, const QModelIndex &parent) override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
bool insertRows(int row, int count, const QModelIndex &parent) override;
|
||||
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
|
||||
@@ -82,6 +95,8 @@ public:
|
||||
|
||||
template<typename T> T *create();
|
||||
|
||||
QModelIndex createFolder(const QModelIndex &parent, const QString &name, int row = -1);
|
||||
|
||||
template<typename T> bool remove(const T &entry);
|
||||
|
||||
template<typename T> bool rename(const T &entry, const QString &newName);
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <QMouseEvent>
|
||||
#include <QApplication>
|
||||
#include <QDrag>
|
||||
#include <QDebug>
|
||||
#include <QIODevice>
|
||||
#include <QMimeData>
|
||||
|
||||
@@ -42,5 +41,5 @@ void ActionDragWidget::mouseMoveEvent(QMouseEvent *event)
|
||||
drag->setMimeData(mimeData);
|
||||
|
||||
Qt::DropAction dropAction = drag->exec(Qt::CopyAction);
|
||||
qDebug() << dropAction;
|
||||
Q_UNUSED(dropAction)
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "actionscontainerwidget.h"
|
||||
#include "ui_actionscontainerwidget.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
#include <QMenu>
|
||||
#include <QKeySequence>
|
||||
|
||||
@@ -1,44 +1,70 @@
|
||||
#include "draggabletreeview.h"
|
||||
|
||||
#include <QDrag>
|
||||
#include <QMimeData>
|
||||
#include <QPersistentModelIndex>
|
||||
#include <QItemSelectionModel>
|
||||
|
||||
#include <vector>
|
||||
|
||||
void DraggableTreeView::startDrag(Qt::DropActions supportedActions)
|
||||
{
|
||||
if (!(supportedActions & Qt::MoveAction) || !model())
|
||||
return;
|
||||
|
||||
QModelIndexList draggable;
|
||||
for (const QModelIndex &index : selectedIndexes())
|
||||
if (index.flags() & Qt::ItemIsDragEnabled)
|
||||
draggable.push_back(index);
|
||||
if (draggable.isEmpty())
|
||||
return;
|
||||
const QPersistentModelIndex moved{draggable.front()};
|
||||
|
||||
QMimeData *data = model()->mimeData(draggable);
|
||||
if (!data)
|
||||
return;
|
||||
|
||||
// dropMimeData() moves the node with beginMoveRows()/endMoveRows(). The
|
||||
// QAbstractItemView implementation of startDrag() removes the selected
|
||||
// source row after QDrag::exec() returns MoveAction, which would delete
|
||||
// the resource that has already been moved into its destination folder.
|
||||
QDrag drag(this);
|
||||
drag.setMimeData(data);
|
||||
if (executeDrag(drag) == Qt::MoveAction && moved.isValid())
|
||||
{
|
||||
std::vector<QModelIndex> ancestors;
|
||||
for (QModelIndex ancestor = moved.parent(); ancestor.isValid(); ancestor = ancestor.parent())
|
||||
ancestors.push_back(ancestor);
|
||||
for (auto it = ancestors.rbegin(); it != ancestors.rend(); ++it)
|
||||
expand(*it);
|
||||
selectionModel()->setCurrentIndex(moved, QItemSelectionModel::ClearAndSelect);
|
||||
scrollTo(moved, QAbstractItemView::PositionAtCenter);
|
||||
setFocus(Qt::OtherFocusReason);
|
||||
}
|
||||
}
|
||||
|
||||
Qt::DropAction DraggableTreeView::executeDrag(QDrag &drag)
|
||||
{
|
||||
return drag.exec(Qt::MoveAction, Qt::MoveAction);
|
||||
}
|
||||
|
||||
void DraggableTreeView::dragMoveEvent(QDragMoveEvent *event)
|
||||
{
|
||||
setDropIndicatorShown(true);
|
||||
//qDebug() << QStringList{"OnItem","AboveItem","BelowItem","OnViewport"}[dropIndicatorPosition()];
|
||||
QTreeView::dragMoveEvent(event);
|
||||
}
|
||||
|
||||
void DraggableTreeView::dropEvent(QDropEvent *event)
|
||||
{
|
||||
bool dropOK = false;
|
||||
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
|
||||
|
||||
switch (dropIndicator)
|
||||
{
|
||||
case QAbstractItemView::AboveItem:
|
||||
dropOK = true;
|
||||
break;
|
||||
case QAbstractItemView::BelowItem:
|
||||
dropOK = true;
|
||||
break;
|
||||
case QAbstractItemView::OnItem:
|
||||
dropOK = false;
|
||||
break;
|
||||
case QAbstractItemView::OnViewport:
|
||||
dropOK = false;
|
||||
break;
|
||||
}
|
||||
if(dropOK)
|
||||
{
|
||||
// Here, you need to manage yourself the case of dropping an item
|
||||
}
|
||||
setDropIndicatorShown(false); // hide the drop indicator once the drop is done
|
||||
QTreeView::dropEvent(event);
|
||||
}
|
||||
|
||||
void DraggableTreeView::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
DropIndicatorPosition position = dropIndicatorPosition();
|
||||
setDropIndicatorShown( position == QAbstractItemView::BelowItem || position == QAbstractItemView::AboveItem );
|
||||
setDropIndicatorShown(position == QAbstractItemView::BelowItem ||
|
||||
position == QAbstractItemView::AboveItem ||
|
||||
position == QAbstractItemView::OnItem);
|
||||
|
||||
QTreeView::paintEvent(event);
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <QTreeView>
|
||||
|
||||
class QDrag;
|
||||
|
||||
class DraggableTreeView : public QTreeView
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -9,7 +11,11 @@ class DraggableTreeView : public QTreeView
|
||||
public:
|
||||
using QTreeView::QTreeView;
|
||||
|
||||
void startDrag(Qt::DropActions supportedActions) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
|
||||
protected:
|
||||
virtual Qt::DropAction executeDrag(QDrag &drag);
|
||||
};
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <QPainter>
|
||||
#include <QMouseEvent>
|
||||
#include <QQueue>
|
||||
#include <QDebug>
|
||||
|
||||
DrawingCanvasWidget::DrawingCanvasWidget(QWidget *parent) :
|
||||
QWidget{parent}
|
||||
@@ -14,7 +13,6 @@ DrawingCanvasWidget::DrawingCanvasWidget(QWidget *parent) :
|
||||
|
||||
void DrawingCanvasWidget::setMode(Mode mode)
|
||||
{
|
||||
qDebug() << mode;
|
||||
if (m_mode != mode)
|
||||
emit modeChanged(m_mode = mode);
|
||||
// TODO cursor
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#include "ui_objectselectorwidget.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QDebug>
|
||||
|
||||
#include "models/projecttreemodel.h"
|
||||
#include "projectcontainer.h"
|
||||
#include "widgets/resourcemenuadapter.h"
|
||||
|
||||
ObjectSelectorWidget::ObjectSelectorWidget(QWidget *parent) :
|
||||
QWidget{parent},
|
||||
@@ -17,14 +17,51 @@ ObjectSelectorWidget::ObjectSelectorWidget(QWidget *parent) :
|
||||
m_ui->lineEdit->setMenu(m_menuObjects);
|
||||
m_ui->toolButton->setMenu(m_menuObjects);
|
||||
|
||||
connect(m_menuObjects, &QMenu::aboutToShow,
|
||||
this, &ObjectSelectorWidget::menuAboutToShow);
|
||||
}
|
||||
|
||||
ObjectSelectorWidget::~ObjectSelectorWidget() = default;
|
||||
|
||||
void ObjectSelectorWidget::setProjectModel(ProjectTreeModel *projectModel)
|
||||
{
|
||||
m_menuAdapter.reset();
|
||||
if (m_projectModel)
|
||||
disconnect(m_projectModel, nullptr, this, nullptr);
|
||||
m_projectModel = projectModel;
|
||||
if (!m_projectModel)
|
||||
{
|
||||
clearObject();
|
||||
return;
|
||||
}
|
||||
connect(m_projectModel, &ProjectTreeModel::objectAboutToBeRemoved, this,
|
||||
[this](const Object &object) {
|
||||
if (m_objectName == object.name)
|
||||
clearObject();
|
||||
});
|
||||
connect(m_projectModel, &ProjectTreeModel::objectNameChanged, this,
|
||||
[this](const Object &object, const QString &oldName) {
|
||||
if (m_objectName == oldName)
|
||||
setObject(object.name);
|
||||
});
|
||||
connect(m_projectModel, &QAbstractItemModel::modelAboutToBeReset,
|
||||
this, &ObjectSelectorWidget::clearObject);
|
||||
m_menuAdapter = std::make_unique<ResourceMenuAdapter>(
|
||||
*m_projectModel, m_projectModel->rootFor<Object>(), *m_menuObjects,
|
||||
[this](const QModelIndex &index) {
|
||||
if (const Object *object = m_projectModel->get<Object>(index)) setObject(object->name);
|
||||
},
|
||||
[this](QMenu &menu) {
|
||||
if (m_showClearObjectAction)
|
||||
menu.addAction(m_emptySelectionText, this, &ObjectSelectorWidget::clearObject);
|
||||
});
|
||||
}
|
||||
|
||||
void ObjectSelectorWidget::setObject(const QString &objectName)
|
||||
{
|
||||
if (objectName.isEmpty())
|
||||
{
|
||||
clearObject();
|
||||
return;
|
||||
}
|
||||
if (m_objectName == objectName)
|
||||
return;
|
||||
|
||||
@@ -49,35 +86,3 @@ void ObjectSelectorWidget::setEmptySelectionText(const QString &emptySelectionTe
|
||||
if (m_objectName.isEmpty())
|
||||
m_ui->lineEdit->setText(m_emptySelectionText);
|
||||
}
|
||||
|
||||
void ObjectSelectorWidget::menuAboutToShow()
|
||||
{
|
||||
if (!m_projectModel)
|
||||
{
|
||||
qCritical() << "no project model!";
|
||||
return;
|
||||
}
|
||||
|
||||
m_menuObjects->clear();
|
||||
|
||||
if (m_showClearObjectAction)
|
||||
m_menuObjects->addAction(m_emptySelectionText, this, &ObjectSelectorWidget::clearObject);
|
||||
|
||||
for (const Object &object : m_projectModel->project()->objects)
|
||||
{
|
||||
QIcon icon;
|
||||
if (!object.spriteName.isEmpty())
|
||||
{
|
||||
const auto &sprites = m_projectModel->project()->sprites;
|
||||
const auto iter = std::find_if(std::cbegin(sprites), std::cend(sprites),
|
||||
[&](const Sprite &sprite){ return sprite.name == object.spriteName; });
|
||||
if (iter != std::cend(sprites))
|
||||
{
|
||||
if (!iter->pixmaps.empty())
|
||||
icon = iter->pixmaps.front();
|
||||
}
|
||||
}
|
||||
m_menuObjects->addAction(icon, object.name, this,
|
||||
[&object,this](){ setObject(object.name); });
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
namespace Ui { class ObjectSelectorWidget; }
|
||||
class QMenu;
|
||||
class ProjectTreeModel;
|
||||
class ResourceMenuAdapter;
|
||||
|
||||
class ObjectSelectorWidget : public QWidget
|
||||
{
|
||||
@@ -20,7 +21,7 @@ public:
|
||||
|
||||
ProjectTreeModel *projectModel() { return m_projectModel; }
|
||||
const ProjectTreeModel *projectModel() const { return m_projectModel; }
|
||||
void setProjectModel(ProjectTreeModel *projectModel) { m_projectModel = projectModel; }
|
||||
void setProjectModel(ProjectTreeModel *projectModel);
|
||||
|
||||
QString objectName() const { return m_objectName; }
|
||||
virtual void setObject(const QString &objectName);
|
||||
@@ -35,9 +36,6 @@ public:
|
||||
signals:
|
||||
void changed();
|
||||
|
||||
private slots:
|
||||
void menuAboutToShow();
|
||||
|
||||
private:
|
||||
const std::unique_ptr<Ui::ObjectSelectorWidget> m_ui;
|
||||
|
||||
@@ -49,4 +47,5 @@ private:
|
||||
bool m_showClearObjectAction;
|
||||
|
||||
QMenu * const m_menuObjects;
|
||||
std::unique_ptr<ResourceMenuAdapter> m_menuAdapter;
|
||||
};
|
||||
@@ -0,0 +1,236 @@
|
||||
#include "resourcemenuadapter.h"
|
||||
|
||||
#include <QAction>
|
||||
#include <QAbstractItemModel>
|
||||
#include <QMenu>
|
||||
#include <QPixmap>
|
||||
|
||||
#include <iterator>
|
||||
|
||||
#include "models/projecttreemodel.h"
|
||||
|
||||
namespace {
|
||||
QIcon iconFor(const ProjectTreeModel &model, const QModelIndex &index)
|
||||
{
|
||||
const QVariant value = model.data(index, Qt::DecorationRole);
|
||||
if (value.metaType() == QMetaType::fromType<QPixmap>())
|
||||
return QIcon{value.value<QPixmap>()};
|
||||
return value.value<QIcon>();
|
||||
}
|
||||
}
|
||||
|
||||
ResourceMenuAdapter::ResourceMenuAdapter(ProjectTreeModel &model, const QModelIndex &root,
|
||||
QMenu &menu, Selection selection, Prefix prefix) :
|
||||
m_model{model}, m_root{root}, m_rootRow{root.row()}, m_menu{menu},
|
||||
m_selection{std::move(selection)}, m_prefix{std::move(prefix)}
|
||||
{
|
||||
m_connections.push_back(QObject::connect(&model, &QAbstractItemModel::rowsInserted, &menu,
|
||||
[this](const QModelIndex &parent, int first, int last) {
|
||||
auto *entries = entriesFor(parent);
|
||||
auto *targetMenu = menuFor(parent);
|
||||
if (!entries || !targetMenu) return;
|
||||
for (int row = first; row <= last; ++row)
|
||||
insertEntry(m_model.index(row, 0, parent), *targetMenu, *entries, row);
|
||||
}));
|
||||
m_connections.push_back(QObject::connect(&model, &QAbstractItemModel::rowsAboutToBeRemoved, &menu,
|
||||
[this](const QModelIndex &parent, int first, int last) { removeEntries(parent, first, last); }));
|
||||
m_connections.push_back(QObject::connect(&model, &QAbstractItemModel::rowsAboutToBeMoved, &menu,
|
||||
[this](const QModelIndex &source, int first, int last, const QModelIndex &destination, int row) {
|
||||
moveEntriesAboutTo(source, first, last, destination, row);
|
||||
}));
|
||||
m_connections.push_back(QObject::connect(&model, &QAbstractItemModel::rowsMoved, &menu,
|
||||
[this](const QModelIndex &source, int first, int last, const QModelIndex &destination, int row) {
|
||||
moveEntries(source, first, last, destination, row);
|
||||
}));
|
||||
m_connections.push_back(QObject::connect(&model, &QAbstractItemModel::dataChanged, &menu,
|
||||
[this](const QModelIndex &first, const QModelIndex &last, const QList<int> &roles) {
|
||||
updateEntries(first, last, roles);
|
||||
}));
|
||||
m_connections.push_back(QObject::connect(&model, &QAbstractItemModel::modelReset, &menu,
|
||||
[this] { rebuild(); }));
|
||||
rebuild();
|
||||
}
|
||||
|
||||
ResourceMenuAdapter::~ResourceMenuAdapter()
|
||||
{
|
||||
for (const auto &connection : m_connections)
|
||||
QObject::disconnect(connection);
|
||||
for (auto &entry : m_entries)
|
||||
destroyEntry(m_menu, *entry);
|
||||
m_entries.clear();
|
||||
m_menu.clear();
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::rebuild()
|
||||
{
|
||||
for (auto &entry : m_entries)
|
||||
destroyEntry(m_menu, *entry);
|
||||
m_entries.clear();
|
||||
m_menu.clear();
|
||||
if (m_prefix) m_prefix(m_menu);
|
||||
if (!m_root.isValid()) m_root = m_model.index(m_rootRow, 0, {});
|
||||
if (m_root.isValid()) populate(m_root, m_menu, m_entries);
|
||||
}
|
||||
|
||||
ResourceMenuAdapter::Entry *ResourceMenuAdapter::findEntry(const QModelIndex &index)
|
||||
{
|
||||
const auto find = [&](const auto &self, auto &entries) -> Entry * {
|
||||
for (auto &entry : entries)
|
||||
{
|
||||
if (entry->index == index) return entry.get();
|
||||
if (auto *child = self(self, entry->children)) return child;
|
||||
}
|
||||
return nullptr;
|
||||
};
|
||||
return find(find, m_entries);
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<ResourceMenuAdapter::Entry>> *
|
||||
ResourceMenuAdapter::entriesFor(const QModelIndex &parent)
|
||||
{
|
||||
if (parent == m_root) return &m_entries;
|
||||
if (auto *entry = findEntry(parent); entry && entry->submenu) return &entry->children;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
QMenu *ResourceMenuAdapter::menuFor(const QModelIndex &parent)
|
||||
{
|
||||
if (parent == m_root) return &m_menu;
|
||||
if (auto *entry = findEntry(parent)) return entry->submenu;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::populate(const QModelIndex &parent, QMenu &menu,
|
||||
std::vector<std::unique_ptr<Entry>> &entries)
|
||||
{
|
||||
for (int row = 0; row < m_model.rowCount(parent); ++row)
|
||||
insertEntry(m_model.index(row, 0, parent), menu, entries, row);
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::insertEntry(const QModelIndex &index, QMenu &menu,
|
||||
std::vector<std::unique_ptr<Entry>> &entries, int row)
|
||||
{
|
||||
if (!index.isValid()) return;
|
||||
auto entry = std::make_unique<Entry>();
|
||||
entry->index = index;
|
||||
const QString name = m_model.data(index, Qt::DisplayRole).toString();
|
||||
const QIcon icon = iconFor(m_model, index);
|
||||
if (m_model.nodeType(index) == ProjectTreeModel::NodeType::Folder)
|
||||
{
|
||||
entry->submenu = new QMenu(name, &menu);
|
||||
entry->submenu->setIcon(icon);
|
||||
entry->action = entry->submenu->menuAction();
|
||||
populate(index, *entry->submenu, entry->children);
|
||||
}
|
||||
else
|
||||
{
|
||||
entry->action = new QAction(icon, name, &menu);
|
||||
Entry *selectedEntry = entry.get();
|
||||
QObject::connect(entry->action, &QAction::triggered, entry->action, [this, selectedEntry] {
|
||||
if (selectedEntry->index.isValid() && selectedEntry->index.model() == &m_model)
|
||||
m_selection(selectedEntry->index);
|
||||
});
|
||||
}
|
||||
const auto position = std::next(entries.begin(), row);
|
||||
menu.insertAction(position == entries.end() ? nullptr : (*position)->action, entry->action);
|
||||
entries.insert(position, std::move(entry));
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::destroyEntry(QMenu &menu, Entry &entry)
|
||||
{
|
||||
menu.removeAction(entry.action);
|
||||
if (entry.submenu) delete entry.submenu;
|
||||
else delete entry.action;
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::removeEntries(const QModelIndex &parent, int first, int last)
|
||||
{
|
||||
auto *entries = entriesFor(parent);
|
||||
auto *menu = menuFor(parent);
|
||||
if (!entries || !menu) return;
|
||||
for (int row = last; row >= first; --row)
|
||||
{
|
||||
destroyEntry(*menu, *entries->at(row));
|
||||
entries->erase(std::next(entries->begin(), row));
|
||||
}
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::updateEntries(const QModelIndex &topLeft, const QModelIndex &bottomRight,
|
||||
const QList<int> &roles)
|
||||
{
|
||||
const bool updateText = roles.empty() || roles.contains(Qt::DisplayRole);
|
||||
const bool updateIcon = roles.empty() || roles.contains(Qt::DecorationRole);
|
||||
if (!updateText && !updateIcon) return;
|
||||
for (int row = topLeft.row(); row <= bottomRight.row(); ++row)
|
||||
{
|
||||
const QModelIndex index = m_model.index(row, 0, topLeft.parent());
|
||||
auto *entry = findEntry(index);
|
||||
if (!entry) continue;
|
||||
if (updateText)
|
||||
{
|
||||
const QString name = m_model.data(index, Qt::DisplayRole).toString();
|
||||
if (entry->submenu) entry->submenu->setTitle(name);
|
||||
else entry->action->setText(name);
|
||||
}
|
||||
if (updateIcon)
|
||||
{
|
||||
const QIcon icon = iconFor(m_model, index);
|
||||
if (entry->submenu) entry->submenu->setIcon(icon);
|
||||
else entry->action->setIcon(icon);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::moveEntriesAboutTo(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
|
||||
const QModelIndex &, int)
|
||||
{
|
||||
auto *entries = entriesFor(sourceParent);
|
||||
auto *menu = menuFor(sourceParent);
|
||||
if (!entries || !menu) return;
|
||||
for (int row = sourceFirst; row <= sourceLast; ++row)
|
||||
{
|
||||
auto &entry = entries->at(row);
|
||||
menu->removeAction(entry->action);
|
||||
m_movingEntries.push_back(std::move(entry));
|
||||
}
|
||||
entries->erase(std::next(entries->begin(), sourceFirst), std::next(entries->begin(), sourceLast + 1));
|
||||
}
|
||||
|
||||
void ResourceMenuAdapter::moveEntries(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
|
||||
const QModelIndex &destinationParent, int destinationRow)
|
||||
{
|
||||
auto *entries = entriesFor(destinationParent);
|
||||
auto *menu = menuFor(destinationParent);
|
||||
if (!entries || !menu)
|
||||
{
|
||||
for (auto &entry : m_movingEntries)
|
||||
{
|
||||
if (entry->submenu) delete entry->submenu;
|
||||
else delete entry->action;
|
||||
}
|
||||
m_movingEntries.clear();
|
||||
return;
|
||||
}
|
||||
// Qt reports the destination row before removing the source rows.
|
||||
if (sourceParent == destinationParent && destinationRow > sourceFirst)
|
||||
destinationRow -= sourceLast - sourceFirst + 1;
|
||||
for (int row = sourceFirst; row <= sourceLast; ++row)
|
||||
{
|
||||
if (m_movingEntries.empty())
|
||||
{
|
||||
insertEntry(m_model.index(destinationRow, 0, destinationParent), *menu, *entries,
|
||||
destinationRow);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto entry = std::move(m_movingEntries.front());
|
||||
m_movingEntries.erase(m_movingEntries.begin());
|
||||
if (entry->submenu) entry->submenu->setParent(menu);
|
||||
else entry->action->setParent(menu);
|
||||
const auto position = std::next(entries->begin(), destinationRow);
|
||||
menu->insertAction(position == entries->end() ? nullptr : (*position)->action, entry->action);
|
||||
entries->insert(position, std::move(entry));
|
||||
}
|
||||
++destinationRow;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <QMetaObject>
|
||||
#include <QPersistentModelIndex>
|
||||
#include <QList>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class ProjectTreeModel;
|
||||
class QMenu;
|
||||
class QAction;
|
||||
|
||||
class ResourceMenuAdapter
|
||||
{
|
||||
public:
|
||||
using Selection = std::function<void(const QModelIndex &)>;
|
||||
using Prefix = std::function<void(QMenu &)>;
|
||||
|
||||
ResourceMenuAdapter(ProjectTreeModel &model, const QModelIndex &root, QMenu &menu,
|
||||
Selection selection, Prefix prefix = {});
|
||||
~ResourceMenuAdapter();
|
||||
|
||||
void rebuild();
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
QPersistentModelIndex index;
|
||||
QAction *action{};
|
||||
QMenu *submenu{};
|
||||
std::vector<std::unique_ptr<Entry>> children;
|
||||
};
|
||||
|
||||
Entry *findEntry(const QModelIndex &index);
|
||||
std::vector<std::unique_ptr<Entry>> *entriesFor(const QModelIndex &parent);
|
||||
QMenu *menuFor(const QModelIndex &parent);
|
||||
void populate(const QModelIndex &parent, QMenu &menu, std::vector<std::unique_ptr<Entry>> &entries);
|
||||
void insertEntry(const QModelIndex &index, QMenu &menu,
|
||||
std::vector<std::unique_ptr<Entry>> &entries, int row);
|
||||
void removeEntries(const QModelIndex &parent, int first, int last);
|
||||
void updateEntries(const QModelIndex &topLeft, const QModelIndex &bottomRight,
|
||||
const QList<int> &roles);
|
||||
void moveEntriesAboutTo(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
|
||||
const QModelIndex &destinationParent, int destinationRow);
|
||||
void moveEntries(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
|
||||
const QModelIndex &destinationParent, int destinationRow);
|
||||
static void destroyEntry(QMenu &menu, Entry &entry);
|
||||
|
||||
ProjectTreeModel &m_model;
|
||||
QPersistentModelIndex m_root;
|
||||
int m_rootRow{};
|
||||
QMenu &m_menu;
|
||||
Selection m_selection;
|
||||
Prefix m_prefix;
|
||||
std::vector<QMetaObject::Connection> m_connections;
|
||||
std::vector<std::unique_ptr<Entry>> m_entries;
|
||||
std::vector<std::unique_ptr<Entry>> m_movingEntries;
|
||||
};
|
||||
+25
-18
@@ -1,20 +1,27 @@
|
||||
#include "projectcontainer.h"
|
||||
|
||||
template<> std::list<Sprite> &ProjectContainer::containerFor() { return sprites; }
|
||||
template<> const std::list<Sprite> &ProjectContainer::containerFor() const { return sprites; }
|
||||
template<> std::list<Sound> &ProjectContainer::containerFor() { return sounds; }
|
||||
template<> const std::list<Sound> &ProjectContainer::containerFor() const { return sounds; }
|
||||
template<> std::list<Background> &ProjectContainer::containerFor() { return backgrounds; }
|
||||
template<> const std::list<Background> &ProjectContainer::containerFor() const { return backgrounds; }
|
||||
template<> std::list<Path> &ProjectContainer::containerFor() { return paths; }
|
||||
template<> const std::list<Path> &ProjectContainer::containerFor() const { return paths; }
|
||||
template<> std::list<Script> &ProjectContainer::containerFor() { return scripts; }
|
||||
template<> const std::list<Script> &ProjectContainer::containerFor() const { return scripts; }
|
||||
template<> std::list<Font> &ProjectContainer::containerFor() { return fonts; }
|
||||
template<> const std::list<Font> &ProjectContainer::containerFor() const { return fonts; }
|
||||
template<> std::list<TimeLine> &ProjectContainer::containerFor() { return timeLines; }
|
||||
template<> const std::list<TimeLine> &ProjectContainer::containerFor() const { return timeLines; }
|
||||
template<> std::list<Object> &ProjectContainer::containerFor() { return objects; }
|
||||
template<> const std::list<Object> &ProjectContainer::containerFor() const { return objects; }
|
||||
template<> std::list<Room> &ProjectContainer::containerFor() { return rooms; }
|
||||
template<> const std::list<Room> &ProjectContainer::containerFor() const { return rooms; }
|
||||
template<> ResourceCollection<Sprite> &ProjectContainer::containerFor() { return sprites; }
|
||||
template<> const ResourceCollection<Sprite> &ProjectContainer::containerFor() const { return sprites; }
|
||||
template<> ResourceCollection<Sound> &ProjectContainer::containerFor() { return sounds; }
|
||||
template<> const ResourceCollection<Sound> &ProjectContainer::containerFor() const { return sounds; }
|
||||
template<> ResourceCollection<Background> &ProjectContainer::containerFor() { return backgrounds; }
|
||||
template<> const ResourceCollection<Background> &ProjectContainer::containerFor() const { return backgrounds; }
|
||||
template<> ResourceCollection<Path> &ProjectContainer::containerFor() { return paths; }
|
||||
template<> const ResourceCollection<Path> &ProjectContainer::containerFor() const { return paths; }
|
||||
template<> ResourceCollection<Script> &ProjectContainer::containerFor() { return scripts; }
|
||||
template<> const ResourceCollection<Script> &ProjectContainer::containerFor() const { return scripts; }
|
||||
template<> ResourceCollection<Font> &ProjectContainer::containerFor() { return fonts; }
|
||||
template<> const ResourceCollection<Font> &ProjectContainer::containerFor() const { return fonts; }
|
||||
template<> ResourceCollection<TimeLine> &ProjectContainer::containerFor() { return timeLines; }
|
||||
template<> const ResourceCollection<TimeLine> &ProjectContainer::containerFor() const { return timeLines; }
|
||||
template<> ResourceCollection<Object> &ProjectContainer::containerFor() { return objects; }
|
||||
template<> const ResourceCollection<Object> &ProjectContainer::containerFor() const { return objects; }
|
||||
template<> ResourceCollection<Room> &ProjectContainer::containerFor() { return rooms; }
|
||||
template<> const ResourceCollection<Room> &ProjectContainer::containerFor() const { return rooms; }
|
||||
|
||||
bool ProjectContainer::hasResourceName(const QString &name) const
|
||||
{
|
||||
return sprites.find(name) || sounds.find(name) || backgrounds.find(name) ||
|
||||
paths.find(name) || scripts.find(name) || fonts.find(name) ||
|
||||
timeLines.find(name) || objects.find(name) || rooms.find(name);
|
||||
}
|
||||
+14
-11
@@ -11,6 +11,8 @@
|
||||
#include <QFont>
|
||||
#include <QPoint>
|
||||
|
||||
#include "resourcefolder.h"
|
||||
|
||||
struct GlobalGameSettings
|
||||
{
|
||||
// Graphics settings
|
||||
@@ -356,18 +358,19 @@ struct ProjectContainer
|
||||
{
|
||||
GlobalGameSettings globalGameSettings;
|
||||
|
||||
std::list<Sprite> sprites;
|
||||
std::list<Sound> sounds;
|
||||
std::list<Background> backgrounds;
|
||||
std::list<Path> paths;
|
||||
std::list<Script> scripts;
|
||||
std::list<Font> fonts;
|
||||
std::list<TimeLine> timeLines;
|
||||
std::list<Object> objects;
|
||||
std::list<Room> rooms;
|
||||
ResourceCollection<Sprite> sprites;
|
||||
ResourceCollection<Sound> sounds;
|
||||
ResourceCollection<Background> backgrounds;
|
||||
ResourceCollection<Path> paths;
|
||||
ResourceCollection<Script> scripts;
|
||||
ResourceCollection<Font> fonts;
|
||||
ResourceCollection<TimeLine> timeLines;
|
||||
ResourceCollection<Object> objects;
|
||||
ResourceCollection<Room> rooms;
|
||||
|
||||
std::list<IncludedFile> includedFiles;
|
||||
|
||||
template<typename T> std::list<T> &containerFor();
|
||||
template<typename T> const std::list<T> &containerFor() const;
|
||||
template<typename T> ResourceCollection<T> &containerFor();
|
||||
template<typename T> const ResourceCollection<T> &containerFor() const;
|
||||
bool hasResourceName(const QString &name) const;
|
||||
};
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#include "stdserialization.h"
|
||||
|
||||
#include <QSet>
|
||||
|
||||
QDataStream &operator<<(QDataStream &ds, const GlobalGameSettings &globalGameSettings)
|
||||
{
|
||||
ds << globalGameSettings.startInFullscreenMode
|
||||
@@ -516,9 +518,78 @@ QDataStream &operator>>(QDataStream &ds, IncludedFile &includedFile)
|
||||
return ds;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QDataStream &writeFolder(QDataStream &ds, const ResourceFolder<T> &folder)
|
||||
{
|
||||
ds << quint32(folder.children.size());
|
||||
for (const auto &node : folder.children.template get<0>())
|
||||
{
|
||||
ds << node->isFolder();
|
||||
if (const auto *nested = node->folder())
|
||||
{
|
||||
ds << nested->name;
|
||||
writeFolder(ds, *nested);
|
||||
}
|
||||
else
|
||||
ds << *node->resource();
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QDataStream &readFolder(QDataStream &ds, ResourceFolder<T> &folder)
|
||||
{
|
||||
quint32 count{};
|
||||
ds >> count;
|
||||
if (count > 1000000) {
|
||||
ds.setStatus(QDataStream::ReadCorruptData);
|
||||
return ds;
|
||||
}
|
||||
for (quint32 i = 0; i < count && ds.status() == QDataStream::Ok; ++i)
|
||||
{
|
||||
bool isFolder{};
|
||||
ds >> isFolder;
|
||||
if (isFolder)
|
||||
{
|
||||
QString name;
|
||||
ds >> name;
|
||||
auto nested = std::make_unique<ResourceFolder<T>>(name);
|
||||
readFolder(ds, *nested);
|
||||
if (!folder.children.template get<0>().push_back(
|
||||
std::make_unique<ResourceNode<T>>(std::move(nested))).second)
|
||||
ds.setStatus(QDataStream::ReadCorruptData);
|
||||
}
|
||||
else
|
||||
{
|
||||
T resource;
|
||||
ds >> resource;
|
||||
if (!folder.children.template get<0>().push_back(
|
||||
std::make_unique<ResourceNode<T>>(std::move(resource))).second)
|
||||
ds.setStatus(QDataStream::ReadCorruptData);
|
||||
}
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QDataStream &operator<<(QDataStream &ds, const ResourceCollection<T> &resources)
|
||||
{
|
||||
return writeFolder(ds, resources.root);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
QDataStream &operator>>(QDataStream &ds, ResourceCollection<T> &resources)
|
||||
{
|
||||
resources.clear();
|
||||
readFolder(ds, resources.root);
|
||||
resources.invalidate();
|
||||
return ds;
|
||||
}
|
||||
|
||||
QDataStream &operator<<(QDataStream &ds, const ProjectContainer &project)
|
||||
{
|
||||
ds << project.globalGameSettings
|
||||
ds << quint32{0x5154474d} << quint32{2}
|
||||
<< project.globalGameSettings
|
||||
<< project.sprites
|
||||
<< project.sounds
|
||||
<< project.backgrounds
|
||||
@@ -534,6 +605,13 @@ QDataStream &operator<<(QDataStream &ds, const ProjectContainer &project)
|
||||
|
||||
QDataStream &operator>>(QDataStream &ds, ProjectContainer &project)
|
||||
{
|
||||
quint32 magic{}, version{};
|
||||
ds >> magic >> version;
|
||||
if (ds.status() != QDataStream::Ok || magic != 0x5154474d || version != 2)
|
||||
{
|
||||
ds.setStatus(QDataStream::ReadCorruptData);
|
||||
return ds;
|
||||
}
|
||||
ds >> project.globalGameSettings
|
||||
>> project.sprites
|
||||
>> project.sounds
|
||||
@@ -545,5 +623,20 @@ QDataStream &operator>>(QDataStream &ds, ProjectContainer &project)
|
||||
>> project.objects
|
||||
>> project.rooms
|
||||
>> project.includedFiles;
|
||||
if (ds.status() != QDataStream::Ok) return ds;
|
||||
QSet<QString> names;
|
||||
const auto checkNames = [&names](const auto &resources) {
|
||||
for (const auto &resource : resources) {
|
||||
if (resource.name.isEmpty() || names.contains(resource.name)) return false;
|
||||
names.insert(resource.name);
|
||||
}
|
||||
return true;
|
||||
};
|
||||
if (!checkNames(project.sprites) || !checkNames(project.sounds) ||
|
||||
!checkNames(project.backgrounds) || !checkNames(project.paths) ||
|
||||
!checkNames(project.scripts) || !checkNames(project.fonts) ||
|
||||
!checkNames(project.timeLines) || !checkNames(project.objects) ||
|
||||
!checkNames(project.rooms))
|
||||
ds.setStatus(QDataStream::ReadCorruptData);
|
||||
return ds;
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QHash>
|
||||
#include <boost/multi_index_container.hpp>
|
||||
#include <boost/multi_index/hashed_index.hpp>
|
||||
#include <boost/multi_index/sequenced_index.hpp>
|
||||
#include <boost/multi_index/key.hpp>
|
||||
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
struct ResourceQStringHash {
|
||||
std::size_t operator()(const QString &name) const { return qHash(name); }
|
||||
};
|
||||
|
||||
template<typename T> struct ResourceFolder;
|
||||
|
||||
template<typename T>
|
||||
struct ResourceNode
|
||||
{
|
||||
std::variant<T, std::unique_ptr<ResourceFolder<T>>> value;
|
||||
|
||||
explicit ResourceNode(T resource) : value{std::move(resource)} {}
|
||||
explicit ResourceNode(std::unique_ptr<ResourceFolder<T>> folder) : value{std::move(folder)} {}
|
||||
|
||||
bool isFolder() const { return std::holds_alternative<std::unique_ptr<ResourceFolder<T>>>(value); }
|
||||
QString name() const {
|
||||
return isFolder() ? std::get<std::unique_ptr<ResourceFolder<T>>>(value)->name
|
||||
: std::get<T>(value).name;
|
||||
}
|
||||
T *resource() { return std::get_if<T>(&value); }
|
||||
const T *resource() const { return std::get_if<T>(&value); }
|
||||
ResourceFolder<T> *folder() {
|
||||
return isFolder() ? std::get<std::unique_ptr<ResourceFolder<T>>>(value).get() : nullptr;
|
||||
}
|
||||
const ResourceFolder<T> *folder() const {
|
||||
return isFolder() ? std::get<std::unique_ptr<ResourceFolder<T>>>(value).get() : nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ResourceNodeName
|
||||
{
|
||||
using result_type = QString;
|
||||
QString operator()(const std::unique_ptr<ResourceNode<T>> &node) const { return node->name(); }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ResourceFolder
|
||||
{
|
||||
using Node = ResourceNode<T>;
|
||||
using Children = boost::multi_index::multi_index_container<
|
||||
std::unique_ptr<Node>,
|
||||
boost::multi_index::indexed_by<
|
||||
boost::multi_index::sequenced<>,
|
||||
boost::multi_index::hashed_unique<ResourceNodeName<T>, ResourceQStringHash>>>;
|
||||
|
||||
QString name;
|
||||
Children children;
|
||||
|
||||
explicit ResourceFolder(QString name = {}) : name{std::move(name)} {}
|
||||
|
||||
Node *at(std::size_t row) const {
|
||||
if (row >= children.size()) return nullptr;
|
||||
return std::next(children.template get<0>().begin(), row)->get();
|
||||
}
|
||||
int rowOf(const Node *node) const {
|
||||
int row = 0;
|
||||
for (const auto &child : children.template get<0>()) {
|
||||
if (child.get() == node) return row;
|
||||
++row;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
// The folder tree owns every resource. The flat view keeps legacy editor and
|
||||
// engine traversal working while callers are migrated to folder-aware APIs.
|
||||
template<typename T>
|
||||
class ResourceCollection
|
||||
{
|
||||
using Node = ResourceNode<T>;
|
||||
using Folder = ResourceFolder<T>;
|
||||
mutable std::list<T*> m_flat;
|
||||
mutable QHash<QString, T*> m_byName;
|
||||
mutable bool m_dirty{true};
|
||||
|
||||
static void collect(Folder &folder, std::list<T*> &flat) {
|
||||
for (const auto &node : folder.children.template get<0>()) {
|
||||
if (auto *resource = node->resource()) flat.push_back(resource);
|
||||
else collect(*node->folder(), flat);
|
||||
}
|
||||
}
|
||||
void ensureFlat() const {
|
||||
if (!m_dirty) return;
|
||||
m_flat.clear();
|
||||
m_byName.clear();
|
||||
collect(const_cast<Folder &>(root), m_flat);
|
||||
for (T *resource : m_flat) m_byName.insert(resource->name, resource);
|
||||
m_dirty = false;
|
||||
}
|
||||
static bool findNodeIn(Folder &folder, const T *resource, Folder *&owner, Node *&node) {
|
||||
for (const auto &child : folder.children.template get<0>()) {
|
||||
if (child->resource() == resource) { owner = &folder; node = child.get(); return true; }
|
||||
if (auto *nested = child->folder(); nested && findNodeIn(*nested, resource, owner, node)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public:
|
||||
template<bool IsConst>
|
||||
class FlatIterator
|
||||
{
|
||||
using Base = typename std::list<T*>::const_iterator;
|
||||
Base m_position;
|
||||
public:
|
||||
using iterator_category = std::bidirectional_iterator_tag;
|
||||
using value_type = T;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using reference = std::conditional_t<IsConst, const T &, T &>;
|
||||
using pointer = std::conditional_t<IsConst, const T *, T *>;
|
||||
|
||||
FlatIterator() = default;
|
||||
explicit FlatIterator(Base position) : m_position{position} {}
|
||||
reference operator*() const { return **m_position; }
|
||||
pointer operator->() const { return *m_position; }
|
||||
FlatIterator &operator++() { ++m_position; return *this; }
|
||||
FlatIterator operator++(int) { auto copy = *this; ++*this; return copy; }
|
||||
FlatIterator &operator--() { --m_position; return *this; }
|
||||
FlatIterator operator--(int) { auto copy = *this; --*this; return copy; }
|
||||
template<bool Other>
|
||||
bool operator==(const FlatIterator<Other> &other) const { return m_position == other.base(); }
|
||||
Base base() const { return m_position; }
|
||||
};
|
||||
using iterator = FlatIterator<false>;
|
||||
using const_iterator = FlatIterator<true>;
|
||||
Folder root;
|
||||
|
||||
ResourceCollection() = default;
|
||||
ResourceCollection(ResourceCollection &&other) noexcept : root{std::move(other.root)}, m_dirty{true} {}
|
||||
ResourceCollection &operator=(ResourceCollection &&other) noexcept {
|
||||
root = std::move(other.root); m_flat.clear(); m_byName.clear(); m_dirty = true; return *this;
|
||||
}
|
||||
ResourceCollection(const ResourceCollection &) = delete;
|
||||
ResourceCollection &operator=(const ResourceCollection &) = delete;
|
||||
|
||||
iterator begin() { ensureFlat(); return iterator{m_flat.cbegin()}; }
|
||||
iterator end() { ensureFlat(); return iterator{m_flat.cend()}; }
|
||||
const_iterator begin() const { ensureFlat(); return const_iterator{m_flat.cbegin()}; }
|
||||
const_iterator end() const { ensureFlat(); return const_iterator{m_flat.cend()}; }
|
||||
const_iterator cbegin() const { return begin(); }
|
||||
const_iterator cend() const { return end(); }
|
||||
std::size_t size() const { ensureFlat(); return m_flat.size(); }
|
||||
bool empty() const { return size() == 0; }
|
||||
T &front() { return *begin(); }
|
||||
const T &front() const { return *begin(); }
|
||||
T &back() { auto it = end(); return *--it; }
|
||||
const T &back() const { auto it = end(); return *--it; }
|
||||
|
||||
void invalidate() { m_dirty = true; }
|
||||
std::pair<Folder*, Node*> findNode(const T *resource) {
|
||||
Folder *owner{}; Node *node{};
|
||||
findNodeIn(root, resource, owner, node);
|
||||
return {owner, node};
|
||||
}
|
||||
T *find(const QString &name) {
|
||||
ensureFlat();
|
||||
return m_byName.value(name, nullptr);
|
||||
}
|
||||
const T *find(const QString &name) const {
|
||||
ensureFlat();
|
||||
return m_byName.value(name, nullptr);
|
||||
}
|
||||
|
||||
iterator emplace(iterator position, T resource) {
|
||||
T *before = position == end() ? nullptr : &*position;
|
||||
auto &sequence = root.children.template get<0>();
|
||||
auto at = sequence.end();
|
||||
if (before) {
|
||||
for (auto it = sequence.begin(); it != sequence.end(); ++it)
|
||||
if ((*it)->resource() == before) { at = it; break; }
|
||||
}
|
||||
auto node = std::make_unique<Node>(std::move(resource));
|
||||
T *pointer = node->resource();
|
||||
sequence.insert(at, std::move(node));
|
||||
invalidate();
|
||||
for (auto it = begin(); it != end(); ++it) if (&*it == pointer) return it;
|
||||
return end();
|
||||
}
|
||||
void emplace_back(T resource) { emplace(end(), std::move(resource)); }
|
||||
iterator erase(iterator position) {
|
||||
if (position == end()) return end();
|
||||
T *pointer = &*position;
|
||||
auto next = std::next(position);
|
||||
T *nextPointer = next == end() ? nullptr : &*next;
|
||||
auto [owner, node] = findNode(pointer);
|
||||
if (owner && node) {
|
||||
auto &sequence = owner->children.template get<0>();
|
||||
auto it = std::find_if(sequence.begin(), sequence.end(),
|
||||
[node](const auto &child) { return child.get() == node; });
|
||||
sequence.erase(it);
|
||||
invalidate();
|
||||
}
|
||||
if (nextPointer)
|
||||
for (auto it = begin(); it != end(); ++it) if (&*it == nextPointer) return it;
|
||||
return end();
|
||||
}
|
||||
iterator erase(iterator first, iterator last) {
|
||||
std::vector<T*> removed;
|
||||
for (auto it = first; it != last; ++it) removed.push_back(&*it);
|
||||
T *nextPointer = last == end() ? nullptr : &*last;
|
||||
for (T *pointer : removed) {
|
||||
auto [owner, node] = findNode(pointer);
|
||||
if (!owner || !node) continue;
|
||||
auto &sequence = owner->children.template get<0>();
|
||||
auto it = std::find_if(sequence.begin(), sequence.end(),
|
||||
[node](const auto &child) { return child.get() == node; });
|
||||
sequence.erase(it);
|
||||
}
|
||||
invalidate();
|
||||
if (nextPointer)
|
||||
for (auto it = begin(); it != end(); ++it) if (&*it == nextPointer) return it;
|
||||
return end();
|
||||
}
|
||||
void clear() { root.children.clear(); invalidate(); }
|
||||
};
|
||||
Reference in new issue
Block a user