From 4c154501f126c3a78dcb487a96a6643eb3c484fb Mon Sep 17 00:00:00 2001 From: garakmon Date: Fri, 31 Jul 2020 13:24:58 -0400 Subject: [PATCH] fix multi event deletion crash, allow spinboxes to commit move history --- include/core/editcommands.h | 7 +++---- include/core/event.h | 7 ------- include/ui/noscrollspinbox.h | 3 +++ src/core/editcommands.cpp | 36 ++++++++++++++++++++++-------------- src/mainwindow.cpp | 29 ++++++++++++++++++++++------- src/ui/noscrollspinbox.cpp | 10 ++++++++++ 6 files changed, 60 insertions(+), 32 deletions(-) diff --git a/include/core/editcommands.h b/include/core/editcommands.h index a83d6ac5..c57afa47 100644 --- a/include/core/editcommands.h +++ b/include/core/editcommands.h @@ -23,13 +23,12 @@ enum CommandId { ID_EventCreate, // - done ID_EventDelete, // - done ID_EventSetData, // - ? - // Tileset editor history commands // Region map editor history commands }; -/// TODO +/// class PaintMetatile : public QUndoCommand { public: PaintMetatile(Map *map, @@ -233,7 +232,7 @@ private: /// class EventDelete : public QUndoCommand { public: - EventDelete(Editor *editor, Map *map, Event *event, + EventDelete(Editor *editor, Map *map, QList selectedEvents, QUndoCommand *parent = nullptr); ~EventDelete(); @@ -245,7 +244,7 @@ public: private: Map *map; - Event *event; + QList selectedEvents; // allow multiple deletion of events Editor *editor; }; diff --git a/include/core/event.h b/include/core/event.h index f4c3466f..48d13253 100644 --- a/include/core/event.h +++ b/include/core/event.h @@ -99,15 +99,8 @@ public: bool hFlip = false; bool usingSprite; - void activate() { active = true; } - void deactivate() { active = false; } - bool isActive() { return active; } - DraggablePixmapItem *pixmapItem = nullptr; void setPixmapItem(DraggablePixmapItem *item) { pixmapItem = item; } - -private: - bool active = true; }; #endif // EVENT_H diff --git a/include/ui/noscrollspinbox.h b/include/ui/noscrollspinbox.h index d9361f6f..86f0a76f 100644 --- a/include/ui/noscrollspinbox.h +++ b/include/ui/noscrollspinbox.h @@ -10,6 +10,9 @@ class NoScrollSpinBox : public QSpinBox public: explicit NoScrollSpinBox(QWidget *parent = nullptr); void wheelEvent(QWheelEvent *event); + void focusOutEvent(QFocusEvent *event) override; + + unsigned getActionId(); private: }; diff --git a/src/core/editcommands.cpp b/src/core/editcommands.cpp index 563a38fc..6c014fbb 100644 --- a/src/core/editcommands.cpp +++ b/src/core/editcommands.cpp @@ -50,7 +50,6 @@ void PaintMetatile::undo() { } bool PaintMetatile::mergeWith(const QUndoCommand *command) { - // does an up merge const PaintMetatile *other = static_cast(command); if (this->map != other->map) @@ -312,6 +311,8 @@ bool EventMove::mergeWith(const QUndoCommand *command) { if (actionId != other->actionId) return false; + // TODO: check that same events as well? + this->deltaX += other->deltaX; this->deltaY += other->deltaY; @@ -360,7 +361,6 @@ void EventCreate::redo() { } void EventCreate::undo() { - // map->removeEvent(event); if (editor->scene->items().contains(event->pixmapItem)) { @@ -368,6 +368,8 @@ void EventCreate::undo() { } editor->selected_events->removeOne(event->pixmapItem); + editor->updateSelectedEvents(); + map->objectsChanged(); QUndoCommand::undo(); @@ -377,15 +379,18 @@ void EventCreate::undo() { ************************************************************************ ******************************************************************************/ -EventDelete::EventDelete(Editor *editor, Map *map, Event *event, +EventDelete::EventDelete(Editor *editor, Map *map, QList selectedEvents, QUndoCommand *parent) : QUndoCommand(parent) { - // - setText("Delete Event"); + if (selectedEvents.size() > 1) { + setText("Delete Events"); + } else { + setText("Delete Event"); + } this->editor = editor; this->map = map; - this->event = event; + this->selectedEvents = selectedEvents; } EventDelete::~EventDelete() {} @@ -393,22 +398,25 @@ EventDelete::~EventDelete() {} void EventDelete::redo() { QUndoCommand::redo(); - map->removeEvent(event); + for (Event *event : selectedEvents) { + map->removeEvent(event); - if (editor->scene->items().contains(event->pixmapItem)) { - editor->scene->removeItem(event->pixmapItem); + if (editor->scene->items().contains(event->pixmapItem)) { + editor->scene->removeItem(event->pixmapItem); + } + editor->selected_events->removeOne(event->pixmapItem); } - editor->selected_events->removeOne(event->pixmapItem); map->objectsChanged(); } void EventDelete::undo() { - // - map->addEvent(event); + for (Event *event : selectedEvents) { + map->addEvent(event); - editor->project->loadEventPixmaps(map->getAllEvents()); - editor->addMapEvent(event); + editor->project->loadEventPixmaps(map->getAllEvents()); + editor->addMapEvent(event); + } map->objectsChanged(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index faf63557..05846e2a 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -163,6 +163,11 @@ void MainWindow::initEditor() { undoView->setWindowTitle(tr("Edit History")); undoView->show(); undoView->setAttribute(Qt::WA_QuitOnClose, false); + + //ui->comboBox_History->setMinimumContentsLength(20); + //ui->comboBox_History->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength); + //ui->comboBox_History->setModel(undoView->model()); + //ui->comboBox_History->setView(undoView); } void MainWindow::initMiscHeapObjects() { @@ -1448,7 +1453,6 @@ void MainWindow::addNewEvent(QString event_type) if (editor && editor->project) { DraggablePixmapItem *object = editor->addNewEvent(event_type); if (object) { - updateObjects(); editor->selectMapEvent(object, false); } else { QMessageBox msgBox(this); @@ -1557,16 +1561,24 @@ void MainWindow::updateSelectedObjects() { EventPropertiesFrame *frame = new EventPropertiesFrame(item->event); // frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); - QSpinBox *x = frame->ui->spinBox_x; - QSpinBox *y = frame->ui->spinBox_y; - QSpinBox *z = frame->ui->spinBox_z; + NoScrollSpinBox *x = frame->ui->spinBox_x; + NoScrollSpinBox *y = frame->ui->spinBox_y; + NoScrollSpinBox *z = frame->ui->spinBox_z; x->setValue(item->event->x()); - connect(x, SIGNAL(valueChanged(QString)), item, SLOT(set_x(QString))); + connect(x, QOverload::of(&QSpinBox::valueChanged), [this, item, x](int value) { + int delta = value - item->event->x(); + if (delta) + editor->map->editHistory.push(new EventMove(QList() << item->event, delta, 0, x->getActionId())); + }); connect(item, SIGNAL(xChanged(int)), x, SLOT(setValue(int))); y->setValue(item->event->y()); - connect(y, SIGNAL(valueChanged(QString)), item, SLOT(set_y(QString))); + connect(y, QOverload::of(&QSpinBox::valueChanged), [this, item, y](int value) { + int delta = value - item->event->y(); + if (delta) + editor->map->editHistory.push(new EventMove(QList() << item->event, 0, delta, y->getActionId())); + }); connect(item, SIGNAL(yChanged(int)), y, SLOT(setValue(int))); z->setValue(item->event->elevation()); @@ -2102,6 +2114,7 @@ void MainWindow::on_toolButton_deleteObject_clicked() if (editor && editor->selected_events) { if (editor->selected_events->length()) { DraggablePixmapItem *next_selected_event = nullptr; + QList selectedEvents; for (DraggablePixmapItem *item : *editor->selected_events) { QString event_group = item->event->get("event_group_type"); if (event_group != "heal_event_group") { @@ -2125,12 +2138,14 @@ void MainWindow::on_toolButton_deleteObject_clicked() } } } - editor->map->editHistory.push(new EventDelete(editor, editor->map, item->event)); + item->event->setPixmapItem(item); + selectedEvents.append(item->event); } else { // don't allow deletion of heal locations logWarn(QString("Cannot delete event of type '%1'").arg(item->event->get("event_type"))); } } + editor->map->editHistory.push(new EventDelete(editor, editor->map, selectedEvents)); if (next_selected_event) { editor->selectMapEvent(next_selected_event); } diff --git a/src/ui/noscrollspinbox.cpp b/src/ui/noscrollspinbox.cpp index 026a145c..144b0e91 100644 --- a/src/ui/noscrollspinbox.cpp +++ b/src/ui/noscrollspinbox.cpp @@ -1,5 +1,7 @@ #include "noscrollspinbox.h" +unsigned actionId = 0xffff; + NoScrollSpinBox::NoScrollSpinBox(QWidget *parent) : QSpinBox(parent) { @@ -14,3 +16,11 @@ void NoScrollSpinBox::wheelEvent(QWheelEvent *event) QSpinBox::wheelEvent(event); } +void NoScrollSpinBox::focusOutEvent(QFocusEvent *event) { + actionId++; + QSpinBox::focusOutEvent(event); +} + +unsigned NoScrollSpinBox::getActionId() { + return actionId; +}