Fix some memory leaks

This commit is contained in:
GriffinR
2025-02-07 19:27:54 -05:00
parent 01126a888a
commit c49470c47e
9 changed files with 67 additions and 81 deletions

View File

@@ -109,14 +109,15 @@ public:
void toggleBorderVisibility(bool visible, bool enableScriptCallback = true);
void updateCustomMapAttributes();
DraggablePixmapItem *addMapEvent(Event *event);
DraggablePixmapItem *addEventPixmapItem(Event *event);
void removeEventPixmapItem(Event *event);
bool eventLimitReached(Map *, Event::Type);
void selectMapEvent(DraggablePixmapItem *object, bool toggle = false);
DraggablePixmapItem *addNewEvent(Event::Type type);
void updateSelectedEvents();
void duplicateSelectedEvents();
void redrawObject(DraggablePixmapItem *item);
QList<DraggablePixmapItem *> getObjects();
void redrawEventPixmapItem(DraggablePixmapItem *item);
QList<DraggablePixmapItem *> getEventPixmapItems();
void updateCursorRectPos(int x, int y);
void setCursorRectVisible(bool visible);

View File

@@ -325,6 +325,7 @@ private:
QAction *undoAction = nullptr;
QAction *redoAction = nullptr;
QPointer<QUndoView> undoView = nullptr;
QAction *copyAction = nullptr;
QAction *pasteAction = nullptr;
@@ -353,6 +354,7 @@ private:
bool setProjectUI();
void clearProjectUI();
void openEditHistory();
void openNewMapDialog();
void openDuplicateMapDialog(const QString &mapName);
NewLayoutDialog* createNewLayoutDialog(const Layout *layoutToCopy = nullptr);

View File

@@ -327,9 +327,7 @@ void EventCreate::redo() {
QUndoCommand::redo();
map->addEvent(event);
editor->project->setEventPixmap(event);
editor->addMapEvent(event);
editor->addEventPixmapItem(event);
// select this event
editor->selected_events->clear();
@@ -338,12 +336,7 @@ void EventCreate::redo() {
void EventCreate::undo() {
map->removeEvent(event);
if (editor->scene->items().contains(event->getPixmapItem())) {
editor->scene->removeItem(event->getPixmapItem());
}
editor->selected_events->removeOne(event->getPixmapItem());
editor->removeEventPixmapItem(event);
editor->shouldReselectEvents();
QUndoCommand::undo();
@@ -378,11 +371,7 @@ void EventDelete::redo() {
for (Event *event : selectedEvents) {
map->removeEvent(event);
if (editor->scene->items().contains(event->getPixmapItem())) {
editor->scene->removeItem(event->getPixmapItem());
}
editor->selected_events->removeOne(event->getPixmapItem());
editor->removeEventPixmapItem(event);
}
editor->selected_events->clear();
@@ -394,8 +383,7 @@ void EventDelete::redo() {
void EventDelete::undo() {
for (Event *event : selectedEvents) {
map->addEvent(event);
editor->project->setEventPixmap(event);
editor->addMapEvent(event);
editor->addEventPixmapItem(event);
}
// select these events
@@ -436,8 +424,7 @@ void EventDuplicate::redo() {
for (Event *event : selectedEvents) {
map->addEvent(event);
editor->project->setEventPixmap(event);
editor->addMapEvent(event);
editor->addEventPixmapItem(event);
}
// select these events
@@ -451,11 +438,7 @@ void EventDuplicate::redo() {
void EventDuplicate::undo() {
for (Event *event : selectedEvents) {
map->removeEvent(event);
if (editor->scene->items().contains(event->getPixmapItem())) {
editor->scene->removeItem(event->getPixmapItem());
}
editor->selected_events->removeOne(event->getPixmapItem());
editor->removeEventPixmapItem(event);
}
editor->shouldReselectEvents();

View File

@@ -1392,7 +1392,7 @@ void Editor::mouseEvent_map(QGraphicsSceneMouseEvent *event, LayoutPixmapItem *i
QList<Event *> selectedEvents;
for (DraggablePixmapItem *pixmapItem : getObjects()) {
for (DraggablePixmapItem *pixmapItem : getEventPixmapItems()) {
selectedEvents.append(pixmapItem->event);
}
selection_origin = QPoint(pos.x(), pos.y());
@@ -1692,15 +1692,13 @@ void Editor::displayMovementPermissionSelector() {
void Editor::clearMapEvents() {
if (events_group) {
if (events_group->scene()) {
events_group->scene()->removeItem(events_group);
}
for (QGraphicsItem *child : events_group->childItems()) {
events_group->removeFromGroup(child);
delete child;
}
if (events_group->scene()) {
events_group->scene()->removeItem(events_group);
}
delete events_group;
events_group = nullptr;
}
@@ -1714,18 +1712,30 @@ void Editor::displayMapEvents() {
scene->addItem(events_group);
for (const auto &event : map->getEvents()) {
project->setEventPixmap(event);
addMapEvent(event);
addEventPixmapItem(event);
}
//objects_group->setFiltersChildEvents(false);
events_group->setHandlesChildEvents(false);
}
DraggablePixmapItem *Editor::addMapEvent(Event *event) {
DraggablePixmapItem *object = new DraggablePixmapItem(event, this);
this->redrawObject(object);
events_group->addToGroup(object);
return object;
DraggablePixmapItem *Editor::addEventPixmapItem(Event *event) {
this->project->setEventPixmap(event);
auto item = new DraggablePixmapItem(event, this);
redrawEventPixmapItem(item);
this->events_group->addToGroup(item);
return item;
}
void Editor::removeEventPixmapItem(Event *event) {
auto item = event->getPixmapItem();
if (!item) return;
this->events_group->removeFromGroup(item);
this->selected_events->removeOne(item);
event->setPixmapItem(nullptr);
delete item;
}
void Editor::clearMapConnections() {
@@ -1977,7 +1987,7 @@ Tileset* Editor::getCurrentMapPrimaryTileset()
return project->getTileset(tilesetLabel);
}
QList<DraggablePixmapItem *> Editor::getObjects() {
QList<DraggablePixmapItem *> Editor::getEventPixmapItems() {
QList<DraggablePixmapItem *> list;
for (QGraphicsItem *child : events_group->childItems()) {
list.append(static_cast<DraggablePixmapItem *>(child));
@@ -1985,7 +1995,7 @@ QList<DraggablePixmapItem *> Editor::getObjects() {
return list;
}
void Editor::redrawObject(DraggablePixmapItem *item) {
void Editor::redrawEventPixmapItem(DraggablePixmapItem *item) {
if (item && item->event && !item->event->getPixmap().isNull()) {
qreal opacity = item->event->getUsingSprite() ? 1.0 : 0.7;
item->setOpacity(opacity);
@@ -2040,8 +2050,8 @@ void Editor::shouldReselectEvents() {
}
void Editor::updateSelectedEvents() {
for (DraggablePixmapItem *item : getObjects()) {
redrawObject(item);
for (DraggablePixmapItem *item : getEventPixmapItems()) {
redrawEventPixmapItem(item);
}
emit objectsChanged();
@@ -2073,17 +2083,9 @@ void Editor::selectedEventIndexChanged(int index, Event::Group eventGroup) {
int event_offs = Event::getIndexOffset(eventGroup);
index = index - event_offs;
Event *event = this->map->getEvent(eventGroup, index);
DraggablePixmapItem *selectedEvent = nullptr;
for (QGraphicsItem *child : this->events_group->childItems()) {
DraggablePixmapItem *item = static_cast<DraggablePixmapItem *>(child);
if (item->event == event) {
selectedEvent = item;
break;
}
}
if (selectedEvent) {
this->selectMapEvent(selectedEvent);
if (event && event->getPixmapItem()) {
this->selectMapEvent(event->getPixmapItem());
} else {
updateSelectedEvents();
}

View File

@@ -96,6 +96,7 @@ MainWindow::~MainWindow()
saveGlobalConfigs();
delete label_MapRulerStatus;
delete undoView;
delete editor;
delete ui;
}
@@ -358,15 +359,15 @@ void MainWindow::initEditor() {
ui->menuEdit->addAction(undoAction);
ui->menuEdit->addAction(redoAction);
QUndoView *undoView = new QUndoView(&editor->editGroup);
undoView->setWindowTitle(tr("Edit History"));
undoView->setAttribute(Qt::WA_QuitOnClose, false);
this->undoView = new QUndoView(&editor->editGroup);
this->undoView->setWindowTitle(tr("Edit History"));
this->undoView->setAttribute(Qt::WA_QuitOnClose, false);
// Show the EditHistory dialog with Ctrl+E
QAction *showHistory = new QAction("Show Edit History...", this);
showHistory->setObjectName("action_ShowEditHistory");
showHistory->setShortcut(QKeySequence("Ctrl+E"));
connect(showHistory, &QAction::triggered, [this, undoView](){ openSubWindow(undoView); });
connect(showHistory, &QAction::triggered, this, &MainWindow::openEditHistory);
ui->menuEdit->addAction(showHistory);
@@ -391,6 +392,10 @@ void MainWindow::initEditor() {
});
}
void MainWindow::openEditHistory() {
openSubWindow(this->undoView);
}
void MainWindow::initMiscHeapObjects() {
ui->tabWidget_EventType->clear();
}
@@ -1003,13 +1008,12 @@ void MainWindow::openWarpMap(QString map_name, int event_id, Event::Group event_
int index = event_id - Event::getIndexOffset(event_group);
Event* event = editor->map->getEvent(event_group, index);
if (event) {
for (DraggablePixmapItem *item : editor->getObjects()) {
if (item->event == event) {
editor->selected_events->clear();
editor->selected_events->append(item);
editor->updateSelectedEvents();
return;
}
auto item = event->getPixmapItem();
if (item) {
editor->selected_events->clear();
editor->selected_events->append(item);
editor->updateSelectedEvents();
return;
}
}
// Can still warp to this map, but can't select the specified event
@@ -1993,9 +1997,9 @@ void MainWindow::displayEventTabs() {
}
void MainWindow::updateObjects() {
QList<DraggablePixmapItem *> all_objects = editor->getObjects();
QList<DraggablePixmapItem *> items = editor->getEventPixmapItems();
for (auto i = this->lastSelectedEvent.cbegin(), end = this->lastSelectedEvent.cend(); i != end; i++) {
if (i.value() && !all_objects.contains(i.value()))
if (i.value() && !items.contains(i.value()))
this->lastSelectedEvent.insert(i.key(), nullptr);
}
displayEventTabs();
@@ -2017,7 +2021,7 @@ void MainWindow::updateSelectedObjects() {
DraggablePixmapItem *selectedEvent = all_events.first()->getPixmapItem();
if (selectedEvent) {
editor->selected_events->append(selectedEvent);
editor->redrawObject(selectedEvent);
editor->redrawEventPixmapItem(selectedEvent);
events.append(selectedEvent);
}
}
@@ -2162,7 +2166,7 @@ Event::Group MainWindow::getEventGroupFromTabWidget(QWidget *tab) {
void MainWindow::eventTabChanged(int index) {
if (editor->map) {
Event::Group group = getEventGroupFromTabWidget(ui->tabWidget_EventType->widget(index));
DraggablePixmapItem *selectedEvent = this->lastSelectedEvent.value(group, nullptr);
DraggablePixmapItem *selectedItem = this->lastSelectedEvent.value(group, nullptr);
switch (group) {
case Event::Group::Object:
@@ -2182,18 +2186,11 @@ void MainWindow::eventTabChanged(int index) {
}
if (!isProgrammaticEventTabChange) {
if (!selectedEvent && editor->map->getNumEvents(group)) {
if (!selectedItem) {
Event *event = editor->map->getEvent(group, 0);
for (QGraphicsItem *child : editor->events_group->childItems()) {
DraggablePixmapItem *item = static_cast<DraggablePixmapItem *>(child);
if (item->event == event) {
selectedEvent = item;
break;
}
}
if (event) selectedItem = event->getPixmapItem();
}
if (selectedEvent) editor->selectMapEvent(selectedEvent);
if (selectedItem) editor->selectMapEvent(selectedItem);
}
}

View File

@@ -420,6 +420,7 @@ Map *Project::createNewMap(const Project::NewMapSettings &settings, const Map* t
this->mapNameToMapSectionName.insert(map->name(), map->header()->location());
map->setIsPersistedToFile(false);
this->mapCache.insert(map->name(), map);
emit mapCreated(map, settings.group);

View File

@@ -29,7 +29,7 @@ void DraggablePixmapItem::emitPositionChanged() {
void DraggablePixmapItem::updatePixmap() {
editor->project->setEventPixmap(event, true);
this->updatePosition();
editor->redrawObject(this);
editor->redrawEventPixmapItem(this);
emit spriteChanged(event->getPixmap());
}

View File

@@ -52,7 +52,7 @@ void NewEventToolButton::init()
this->newSecretBaseAction->setIcon(QIcon(":/icons/add.ico"));
connect(this->newSecretBaseAction, &QAction::triggered, this, &NewEventToolButton::newSecretBase);
QMenu *alignMenu = new QMenu();
QMenu *alignMenu = new QMenu(this);
alignMenu->addAction(this->newObjectAction);
alignMenu->addAction(this->newCloneObjectAction);
alignMenu->addAction(this->newWarpAction);

View File

@@ -112,7 +112,7 @@ void ProjectSettingsEditor::initUi() {
// Validate that the border metatiles text is a comma-separated list of metatile values
static const QString regex_Hex = "(0[xX])?[A-Fa-f0-9]+";
static const QRegularExpression expression_HexList(QString("^(%1,)*%1$").arg(regex_Hex)); // Comma-separated list of hex values
QRegularExpressionValidator *validator_HexList = new QRegularExpressionValidator(expression_HexList);
QRegularExpressionValidator *validator_HexList = new QRegularExpressionValidator(expression_HexList, this);
ui->lineEdit_BorderMetatiles->setValidator(validator_HexList);
this->setBorderMetatilesUi(projectConfig.useCustomBorderSize);