create dynamic map tab icon

This commit is contained in:
garak 2023-02-08 09:31:39 -05:00
parent f7f06dab29
commit 72eb8f873f
10 changed files with 99 additions and 142 deletions

View file

@ -19,14 +19,12 @@ class Layout : public QObject {
public:
Layout() {}
void copyAttributesFrom(Layout *other);
static QString layoutConstantFromName(QString mapName);
bool loaded = false;
/// !TODO
/* NEW */
QList<Map *> maps;
QString id;
QString name;
@ -126,6 +124,4 @@ signals:
void needsRedrawing();
};
using MapLayout = Layout;
#endif // MAPLAYOUT_H

View file

@ -10,6 +10,7 @@
#include <QCheckBox>
#include <QCursor>
#include <QUndoGroup>
#include <QPointer>
#include "mapconnection.h"
#include "metatileselector.h"
@ -45,8 +46,8 @@ public:
QObject *parent = nullptr;
Project *project = nullptr;
Map *map = nullptr;
Layout *layout = nullptr; /* NEW */
QPointer<Map> map = nullptr; // !TODO: since removed onMapCacheCleared, make sure this works as intended
QPointer<Layout> layout = nullptr; /* NEW */
QUndoGroup editGroup; // Manages the undo history for each map

View file

@ -187,7 +187,6 @@ private slots:
void onWildMonDataChanged();
void openNewMapPopupWindow();
void onNewMapCreated();
void onMapCacheCleared();
void importMapFromAdvanceMap1_92();
void onMapRulerStatusChanged(const QString &);
void applyUserShortcuts();

View file

@ -58,7 +58,7 @@ public:
QString layoutsLabel;
QMap<QString, QString> layoutIdsToNames;
QMap<QString, Layout*> mapLayouts;
QMap<QString, Layout*> mapLayoutsMaster;
// QMap<QString, Layout*> mapLayoutsMaster;
QMap<QString, QString> mapSecToMapHoverName;
QMap<QString, int> mapSectionNameToValue;
QMap<int, QString> mapSectionValueToName;
@ -95,6 +95,7 @@ public:
void clearMapCache();
void clearTilesetCache();
void clearLayoutsTable();
struct DataQualifiers
{
@ -265,7 +266,6 @@ private:
signals:
void reloadProject();
void uncheckMonitorFilesAction();
void mapCacheCleared();
void disableWildEncountersUI();
};

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -5,6 +5,55 @@
#include "scripting.h"
#include "imageproviders.h"
// QString id;
// QString name;
// int width;
// int height;
// int border_width;
// int border_height;
// QString border_path;
// QString blockdata_path;
// QString tileset_primary_label;
// QString tileset_secondary_label;
// Tileset *tileset_primary = nullptr;
// Tileset *tileset_secondary = nullptr;
// Blockdata blockdata;
// QImage image;
// QPixmap pixmap;
// QImage border_image;
// QPixmap border_pixmap;
// QImage collision_image;
// QPixmap collision_pixmap;
// Blockdata border;
// Blockdata cached_blockdata;
// Blockdata cached_collision;
// Blockdata cached_border;
// struct {
// Blockdata blocks;
// QSize mapDimensions;
// Blockdata border;
// QSize borderDimensions;
// } lastCommitBlocks; // to track map changes
// QList<int> metatileLayerOrder;
// QList<float> metatileLayerOpacity;
// LayoutPixmapItem *layoutItem = nullptr;
// CollisionPixmapItem *collisionItem = nullptr;
// BorderMetatilesPixmapItem *borderItem = nullptr;
// QUndoStack editHistory;
void Layout::copyAttributesFrom(Layout *other) {
//
}
QString Layout::layoutConstantFromName(QString mapName) {
// Transform map names of the form 'GraniteCave_B1F` into layout constants like 'LAYOUT_GRANITE_CAVE_B1F'.
static const QRegularExpression caseChange("([a-z])([A-Z])");

View file

@ -7,7 +7,7 @@ MapParser::MapParser()
{
}
MapLayout *MapParser::parse(QString filepath, bool *error, Project *project)
Layout *MapParser::parse(QString filepath, bool *error, Project *project)
{
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
@ -69,7 +69,7 @@ MapLayout *MapParser::parse(QString filepath, bool *error, Project *project)
}
}
MapLayout *mapLayout = new MapLayout();
Layout *mapLayout = new Layout();
mapLayout->width = mapWidth;
mapLayout->height = mapHeight;
mapLayout->border_width = (borderWidth == 0) ? DEFAULT_BORDER_WIDTH : borderWidth;

View file

@ -377,6 +377,15 @@ void MainWindow::showWindowTitle() {
.arg(editor->project->getProjectTitle())
);
}
if (editor && editor->layout) {
// // QPixmap pixmap = editor->layout ? editor->layout->render(true) : QPixmap();
QPixmap pixmap = editor->layout ? editor->layout->render(false) : QPixmap();
if (!pixmap.isNull()) {
ui->mainTabBar->setTabIcon(0, QIcon(pixmap.scaled(16, 16)));
} else {
ui->mainTabBar->setTabIcon(0, QIcon(QStringLiteral(":/icons/map.ico")));
}
}
}
void MainWindow::markMapEdited() {
@ -449,6 +458,7 @@ void MainWindow::applyMapListFilter(QString filterText) {
}
/// !TODO
// ui->mapList->setExpanded(groupListProxyModel->mapFromSource(mapGroupModel->indexOfMap(map_name)), false);
// ui->mapList->setExpanded(mapListProxyModel->mapFromSource(mapListIndexes.value(editor->map->name)), true);
// ui->mapList->scrollTo(mapListProxyModel->mapFromSource(mapListIndexes.value(editor->map->name)), QAbstractItemView::PositionAtCenter);
}
@ -541,7 +551,6 @@ bool MainWindow::openProject(QString dir) {
editor->closeProject();
editor->project = new Project(this);
QObject::connect(editor->project, &Project::reloadProject, this, &MainWindow::on_action_Reload_Project_triggered);
QObject::connect(editor->project, &Project::mapCacheCleared, this, &MainWindow::onMapCacheCleared);
QObject::connect(editor->project, &Project::disableWildEncountersUI, [this]() { this->setWildEncountersUIEnabled(false); });
QObject::connect(editor->project, &Project::uncheckMonitorFilesAction, [this]() {
porymapConfig.setMonitorFiles(false);
@ -555,6 +564,7 @@ bool MainWindow::openProject(QString dir) {
} else {
QString open_map = editor->map->name;
editor->project->fileWatcher.removePaths(editor->project->fileWatcher.files());
editor->project->clearLayoutsTable();
editor->project->clearMapCache();
editor->project->clearTilesetCache();
success = loadDataStructures() && populateMapList() && setMap(open_map, true);
@ -701,6 +711,8 @@ bool MainWindow::setMap(QString map_name, bool scroll) {
this->ui->mainTabBar->setTabEnabled(3, true);
this->ui->mainTabBar->setTabEnabled(4, true);
this->ui->comboBox_LayoutSelector->setEnabled(true);
refreshMapScene();
displayMapProperties();
@ -1129,111 +1141,6 @@ void MainWindow::scrollTreeView(QString itemName) {
}
void MainWindow::sortMapList() {
// Project *project = editor->project;
// QIcon mapFolderIcon;
// mapFolderIcon.addFile(QStringLiteral(":/icons/folder_closed_map.ico"), QSize(), QIcon::Normal, QIcon::Off);
// mapFolderIcon.addFile(QStringLiteral(":/icons/folder_map.ico"), QSize(), QIcon::Normal, QIcon::On);
// QIcon folderIcon;
// folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off);
// //folderIcon.addFile(QStringLiteral(":/icons/folder.ico"), QSize(), QIcon::Normal, QIcon::On);
// ui->mapList->setUpdatesEnabled(false);
// mapListModel->clear();
// mapGroupItemsList->clear();
// QStandardItem *root = mapListModel->invisibleRootItem();
// switch (mapSortOrder)
// {
// case MapSortOrder::SortByGroup:
// for (int i = 0; i < project->groupNames.length(); i++) {
// QString group_name = project->groupNames.value(i);
// QStandardItem *group = new QStandardItem;
// group->setText(group_name);
// group->setIcon(mapFolderIcon);
// group->setEditable(false);
// group->setData(group_name, Qt::UserRole);
// group->setData("map_group", MapListUserRoles::TypeRole);
// group->setData(i, MapListUserRoles::GroupRole);
// root->appendRow(group);
// mapGroupItemsList->append(group);
// QStringList names = project->groupedMapNames.value(i);
// for (int j = 0; j < names.length(); j++) {
// QString map_name = names.value(j);
// QStandardItem *map = createMapItem(map_name, i, j);
// group->appendRow(map);
// mapListIndexes.insert(map_name, map->index());
// }
// }
// break;
// case MapSortOrder::SortByArea:
// {
// QMap<QString, int> mapsecToGroupNum;
// for (int i = 0; i < project->mapSectionNameToValue.size(); i++) {
// QString mapsec_name = project->mapSectionValueToName.value(i);
// QStandardItem *mapsec = new QStandardItem;
// mapsec->setText(mapsec_name);
// mapsec->setIcon(folderIcon);
// mapsec->setEditable(false);
// mapsec->setData(mapsec_name, Qt::UserRole);
// mapsec->setData("map_sec", MapListUserRoles::TypeRole);
// mapsec->setData(i, MapListUserRoles::GroupRole);
// root->appendRow(mapsec);
// mapGroupItemsList->append(mapsec);
// mapsecToGroupNum.insert(mapsec_name, i);
// }
// for (int i = 0; i < project->groupNames.length(); i++) {
// QStringList names = project->groupedMapNames.value(i);
// for (int j = 0; j < names.length(); j++) {
// QString map_name = names.value(j);
// QStandardItem *map = createMapItem(map_name, i, j);
// QString location = project->readMapLocation(map_name);
// QStandardItem *mapsecItem = mapGroupItemsList->at(mapsecToGroupNum[location]);
// mapsecItem->setIcon(mapFolderIcon);
// mapsecItem->appendRow(map);
// mapListIndexes.insert(map_name, map->index());
// }
// }
// break;
// }
// case MapSortOrder::SortByLayout:
// {
// QMap<QString, int> layoutIndices;
// for (int i = 0; i < project->mapLayoutsTable.length(); i++) {
// QString layoutId = project->mapLayoutsTable.value(i);
// MapLayout *layout = project->mapLayouts.value(layoutId);
// QStandardItem *layoutItem = new QStandardItem;
// layoutItem->setText(layout->name);
// layoutItem->setIcon(folderIcon);
// layoutItem->setEditable(false);
// layoutItem->setData(layout->name, Qt::UserRole);
// layoutItem->setData("map_layout", MapListUserRoles::TypeRole);
// layoutItem->setData(layout->id, MapListUserRoles::TypeRole2);
// layoutItem->setData(i, MapListUserRoles::GroupRole);
// root->appendRow(layoutItem);
// mapGroupItemsList->append(layoutItem);
// layoutIndices[layoutId] = i;
// }
// for (int i = 0; i < project->groupNames.length(); i++) {
// QStringList names = project->groupedMapNames.value(i);
// for (int j = 0; j < names.length(); j++) {
// QString map_name = names.value(j);
// QStandardItem *map = createMapItem(map_name, i, j);
// QString layoutId = project->readMapLayoutId(map_name);
// QStandardItem *layoutItem = mapGroupItemsList->at(layoutIndices.value(layoutId));
// layoutItem->setIcon(mapFolderIcon);
// layoutItem->appendRow(map);
// mapListIndexes.insert(map_name, map->index());
// }
// }
// break;
// }
// }
// ui->mapList->setUpdatesEnabled(true);
// ui->mapList->repaint();
// updateMapList();
}
void MainWindow::onOpenMapListContextMenu(const QPoint &point) {
@ -2533,10 +2440,6 @@ void MainWindow::onLayoutNeedsRedrawing() {
redrawLayoutScene();
}
void MainWindow::onMapCacheCleared() {
editor->map = nullptr;
}
void MainWindow::onTilesetsSaved(QString primaryTilesetLabel, QString secondaryTilesetLabel) {
// If saved tilesets are currently in-use, update them and redraw
// Otherwise overwrite the cache for the saved tileset
@ -2612,7 +2515,7 @@ void MainWindow::importMapFromAdvanceMap1_92()
this->editor->project->setImportExportPath(filepath);
MapParser parser;
bool error = false;
MapLayout *mapLayout = parser.parse(filepath, &error, editor->project);
Layout *mapLayout = parser.parse(filepath, &error, editor->project);
if (error) {
QMessageBox msgBox(this);
msgBox.setText("Failed to import map from Advance Map 1.92 .map file.");

View file

@ -45,6 +45,7 @@ Project::Project(QWidget *parent) :
Project::~Project()
{
clearLayoutsTable();
clearMapCache();
clearTilesetCache();
}
@ -108,7 +109,6 @@ void Project::clearMapCache() {
delete map;
}
mapCache.clear();
emit mapCacheCleared();
}
void Project::clearTilesetCache() {
@ -119,6 +119,17 @@ void Project::clearTilesetCache() {
tilesetCache.clear();
}
void Project::clearLayoutsTable() {
// clearMapLayouts
// QMap<QString, Layout*> mapLayouts;
// QMap<QString, Layout*> mapLayoutsMaster;
for (Layout *layout : mapLayouts.values()) {
if (layout)
delete layout;
}
mapLayouts.clear();
}
Map* Project::loadMap(QString map_name) {
Map *map;
if (mapCache.contains(map_name)) {
@ -379,7 +390,7 @@ QString Project::readMapLocation(QString map_name) {
return ParseUtil::jsonToQString(mapObj["region_map_section"]);
}
bool Project::loadLayout(MapLayout *layout) {
bool Project::loadLayout(Layout *layout) {
// !TODO: make sure this doesn't break anything, maybe do something better. new layouts work too?
if (!layout->loaded) {
// Force these to run even if one fails
@ -476,7 +487,7 @@ bool Project::readMapLayouts() {
logError(QString("Layout %1 is missing field(s) in %2.").arg(i).arg(layoutsFilepath));
return false;
}
MapLayout *layout = new MapLayout();
Layout *layout = new Layout();
layout->id = ParseUtil::jsonToQString(layoutObj["id"]);
if (layout->id.isEmpty()) {
logError(QString("Missing 'id' value on layout %1 in %2").arg(i).arg(layoutsFilepath));
@ -557,8 +568,6 @@ bool Project::readMapLayouts() {
}
// Deep copy
mapLayoutsMaster = mapLayouts;
mapLayoutsMaster.detach();
mapLayoutsTableMaster = mapLayoutsTable;
mapLayoutsTableMaster.detach();
return true;
@ -578,7 +587,7 @@ void Project::saveMapLayouts() {
bool useCustomBorderSize = projectConfig.getUseCustomBorderSize();
OrderedJson::array layoutsArr;
for (QString layoutId : mapLayoutsTableMaster) {
MapLayout *layout = mapLayouts.value(layoutId);
Layout *layout = mapLayouts.value(layoutId);
OrderedJson::object layoutObj;
layoutObj["id"] = layout->id;
layoutObj["name"] = layout->name;
@ -1046,7 +1055,7 @@ void Project::saveTilesetPalettes(Tileset *tileset) {
}
}
bool Project::loadLayoutTilesets(MapLayout *layout) {
bool Project::loadLayoutTilesets(Layout *layout) {
layout->tileset_primary = getTileset(layout->tileset_primary_label);
if (!layout->tileset_primary) {
QString defaultTileset = this->getDefaultPrimaryTilesetLabel();
@ -1114,7 +1123,7 @@ Tileset* Project::loadTileset(QString label, Tileset *tileset) {
return tileset;
}
bool Project::loadBlockdata(MapLayout *layout) {
bool Project::loadBlockdata(Layout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->blockdata_path);
layout->blockdata = readBlockdata(path);
layout->lastCommitBlocks.blocks = layout->blockdata;
@ -1143,7 +1152,7 @@ void Project::setNewMapBlockdata(Map *map) {
map->layout->lastCommitBlocks.mapDimensions = QSize(width, height);
}
bool Project::loadLayoutBorder(MapLayout *layout) {
bool Project::loadLayoutBorder(Layout *layout) {
QString path = QString("%1/%2").arg(root).arg(layout->border_path);
layout->border = readBlockdata(path);
layout->lastCommitBlocks.border = layout->border;
@ -1361,10 +1370,10 @@ void Project::updateMapLayout(Map* map) {
mapLayoutsTableMaster.append(map->layoutId);
}
// !TODO
// !TODO: why is[was] this a deep copy??
// Deep copy
// MapLayout *layout = mapLayouts.value(map->layoutId);
// MapLayout *newLayout = new MapLayout();
// Layout *layout = mapLayouts.value(map->layoutId);
// Layout *newLayout = new Layout();
// *newLayout = *layout;
// mapLayoutsMaster.insert(map->layoutId, newLayout);
}

View file

@ -110,12 +110,12 @@ void NewMapPopup::init(MapSortOrder type, QVariant data) {
}
// Creating new map from AdvanceMap import
void NewMapPopup::init(MapLayout *mapLayout) {
void NewMapPopup::init(Layout *mapLayout) {
this->importedMap = true;
useLayoutSettings(mapLayout);
this->map = new Map();
this->map->layout = new MapLayout();
this->map->layout = new Layout();
this->map->layout->blockdata = mapLayout->blockdata;
if (!mapLayout->border.isEmpty()) {
@ -203,7 +203,7 @@ void NewMapPopup::saveSettings() {
settings.floorNumber = ui->spinBox_NewMap_Floor_Number->value();
}
void NewMapPopup::useLayoutSettings(MapLayout *layout) {
void NewMapPopup::useLayoutSettings(Layout *layout) {
if (!layout) return;
settings.width = layout->width;
settings.height = layout->height;
@ -241,7 +241,7 @@ void NewMapPopup::on_pushButton_NewMap_Accept_clicked() {
return;
}
Map *newMap = new Map;
MapLayout *layout;
Layout *layout;
// If map name is not unique, use default value. Also use only valid characters.
// After stripping invalid characters, strip any leading digits.
@ -266,8 +266,8 @@ void NewMapPopup::on_pushButton_NewMap_Accept_clicked() {
layout = this->project->mapLayouts.value(this->layoutId);
newMap->needsLayoutDir = false;
} else {
layout = new MapLayout;
layout->id = MapLayout::layoutConstantFromName(newMapName);
layout = new Layout;
layout->id = Layout::layoutConstantFromName(newMapName);
layout->name = QString("%1_Layout").arg(newMap->name);
layout->width = this->ui->spinBox_NewMap_Width->value();
layout->height = this->ui->spinBox_NewMap_Height->value();