Simplify map list icon updating

This commit is contained in:
GriffinR 2024-08-08 17:26:42 -04:00
parent 4af1c4d463
commit edb4a67994
3 changed files with 44 additions and 35 deletions

View file

@ -340,7 +340,6 @@ private:
QMap<Event::Group, DraggablePixmapItem*> lastSelectedEvent; QMap<Event::Group, DraggablePixmapItem*> lastSelectedEvent;
bool isProgrammaticEventTabChange; bool isProgrammaticEventTabChange;
bool projectHasUnsavedChanges;
bool newMapDefaultsSet = false; bool newMapDefaultsSet = false;
MapSortOrder mapSortOrder; MapSortOrder mapSortOrder;
@ -366,7 +365,7 @@ private:
QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum); QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
void refreshRecentProjectsMenu(); void refreshRecentProjectsMenu();
void drawMapListIcons(QAbstractItemModel *model); void updateMapListIcon(const QString &mapName);
void updateMapList(); void updateMapList();
void displayMapProperties(); void displayMapProperties();

View file

@ -981,7 +981,6 @@ void Editor::updateDiveEmergeVisibility() {
} }
} }
// TODO: Fold into PixmapItem
QPoint Editor::calculateConnectionPosition(MapConnection *connection, const QPixmap &pixmap) { QPoint Editor::calculateConnectionPosition(MapConnection *connection, const QPixmap &pixmap) {
const QString direction = connection->direction(); const QString direction = connection->direction();
int offset = connection->offset(); int offset = connection->offset();

View file

@ -421,9 +421,7 @@ void MainWindow::markMapEdited(Map* map) {
return; return;
map->hasUnsavedDataChanges = true; map->hasUnsavedDataChanges = true;
// TODO: Only update the necessary list icon updateMapListIcon(map->name);
updateMapList();
if (editor && editor->map == map) if (editor && editor->map == map)
showWindowTitle(); showWindowTitle();
} }
@ -807,8 +805,10 @@ bool MainWindow::setMap(QString map_name, bool scrollTreeView) {
connect(editor->map, &Map::mapNeedsRedrawing, this, &MainWindow::onMapNeedsRedrawing); connect(editor->map, &Map::mapNeedsRedrawing, this, &MainWindow::onMapNeedsRedrawing);
// Swap the "currently-open" icon from the old map to the new map
updateMapListIcon(userConfig.recentMap);
userConfig.recentMap = map_name; userConfig.recentMap = map_name;
updateMapList(); updateMapListIcon(userConfig.recentMap);
Scripting::cb_MapOpened(map_name); Scripting::cb_MapOpened(map_name);
prefab.updatePrefabUi(editor->map); prefab.updatePrefabUi(editor->map);
@ -1535,46 +1535,54 @@ void MainWindow::on_mapList_activated(const QModelIndex &index)
userSetMap(data.toString()); userSetMap(data.toString());
} }
void MainWindow::drawMapListIcons(QAbstractItemModel *model) { void MainWindow::updateMapListIcon(const QString &mapName) {
projectHasUnsavedChanges = false; if (!editor->project || !editor->project->mapCache.contains(mapName))
return;
QStandardItem *item = mapListModel->itemFromIndex(mapListIndexes.value(mapName));
if (!item)
return;
if (editor->map && editor->map->name == mapName) {
item->setIcon(*mapOpenedIcon);
} else if (editor->project->mapCache.value(mapName)->hasUnsavedChanges()) {
item->setIcon(*mapEditedIcon);
} else {
item->setIcon(*mapIcon);
}
}
void MainWindow::updateMapList() {
QList<QModelIndex> list; QList<QModelIndex> list;
list.append(QModelIndex()); list.append(QModelIndex());
while (list.length()) { while (list.length()) {
QModelIndex parent = list.takeFirst(); QModelIndex parent = list.takeFirst();
for (int i = 0; i < model->rowCount(parent); i++) { for (int i = 0; i < mapListModel->rowCount(parent); i++) {
QModelIndex index = model->index(i, 0, parent); QModelIndex index = mapListModel->index(i, 0, parent);
if (model->hasChildren(index)) { if (mapListModel->hasChildren(index)) {
list.append(index); list.append(index);
} }
QVariant data = index.data(Qt::UserRole); QVariant data = index.data(Qt::UserRole);
if (!data.isNull()) { if (!data.isNull()) {
QString map_name = data.toString(); updateMapListIcon(data.toString());
if (editor->project && editor->project->mapCache.contains(map_name)) {
QStandardItem *map = mapListModel->itemFromIndex(mapListIndexes.value(map_name));
map->setIcon(*mapIcon);
if (editor->project->mapCache.value(map_name)->hasUnsavedChanges()) {
map->setIcon(*mapEditedIcon);
projectHasUnsavedChanges = true;
}
if (editor->map->name == map_name) {
map->setIcon(*mapOpenedIcon);
}
}
} }
} }
} }
} }
void MainWindow::updateMapList() {
drawMapListIcons(mapListModel);
}
void MainWindow::on_action_Save_Project_triggered() { void MainWindow::on_action_Save_Project_triggered() {
editor->saveProject(); editor->saveProject();
updateMapList(); updateMapList();
showWindowTitle(); showWindowTitle();
} }
void MainWindow::on_action_Save_triggered() {
editor->save();
if (editor->map)
updateMapListIcon(editor->map->name);
showWindowTitle();
}
void MainWindow::duplicate() { void MainWindow::duplicate() {
editor->duplicateSelectedEvents(); editor->duplicateSelectedEvents();
} }
@ -1827,12 +1835,6 @@ void MainWindow::paste() {
} }
} }
void MainWindow::on_action_Save_triggered() {
editor->save();
updateMapList();
showWindowTitle();
}
void MainWindow::on_mapViewTab_tabBarClicked(int index) void MainWindow::on_mapViewTab_tabBarClicked(int index)
{ {
int oldIndex = ui->mapViewTab->currentIndex(); int oldIndex = ui->mapViewTab->currentIndex();
@ -3128,7 +3130,16 @@ bool MainWindow::closeProject() {
if (!isProjectOpen()) if (!isProjectOpen())
return true; return true;
if (projectHasUnsavedChanges || (editor->map && editor->map->hasUnsavedChanges())) { // Check loaded maps for unsaved changes
bool unsavedChanges = false;
for (auto map : editor->project->mapCache.values()) {
if (map && map->hasUnsavedChanges()) {
unsavedChanges = true;
break;
}
}
if (unsavedChanges) {
QMessageBox::StandardButton result = QMessageBox::question( QMessageBox::StandardButton result = QMessageBox::question(
this, "porymap", "The project has been modified, save changes?", this, "porymap", "The project has been modified, save changes?",
QMessageBox::No | QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes); QMessageBox::No | QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Yes);