fix multi event deletion crash, allow spinboxes to commit move history

This commit is contained in:
garakmon 2020-07-31 13:24:58 -04:00 committed by garak
parent 2598ca2277
commit 4c154501f1
6 changed files with 60 additions and 32 deletions

View file

@ -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<Event *> selectedEvents,
QUndoCommand *parent = nullptr);
~EventDelete();
@ -245,7 +244,7 @@ public:
private:
Map *map;
Event *event;
QList<Event *> selectedEvents; // allow multiple deletion of events
Editor *editor;
};

View file

@ -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

View file

@ -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:
};

View file

@ -50,7 +50,6 @@ void PaintMetatile::undo() {
}
bool PaintMetatile::mergeWith(const QUndoCommand *command) {
// does an up merge
const PaintMetatile *other = static_cast<const PaintMetatile *>(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<Event *> selectedEvents,
QUndoCommand *parent) : QUndoCommand(parent) {
//
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();
for (Event *event : selectedEvents) {
map->removeEvent(event);
if (editor->scene->items().contains(event->pixmapItem)) {
editor->scene->removeItem(event->pixmapItem);
}
editor->selected_events->removeOne(event->pixmapItem);
}
map->objectsChanged();
}
void EventDelete::undo() {
//
for (Event *event : selectedEvents) {
map->addEvent(event);
editor->project->loadEventPixmaps(map->getAllEvents());
editor->addMapEvent(event);
}
map->objectsChanged();

View file

@ -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<int>::of(&QSpinBox::valueChanged), [this, item, x](int value) {
int delta = value - item->event->x();
if (delta)
editor->map->editHistory.push(new EventMove(QList<Event *>() << 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<int>::of(&QSpinBox::valueChanged), [this, item, y](int value) {
int delta = value - item->event->y();
if (delta)
editor->map->editHistory.push(new EventMove(QList<Event *>() << 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<Event *> 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);
}

View file

@ -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;
}