diff --git a/mainwindow.cpp b/mainwindow.cpp index 54cad524..7b367347 100755 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -289,11 +289,12 @@ void MainWindow::populateMapList() { QIcon folderIcon; folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off); - QIcon mapIcon; - mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off); - mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On); + mapIcon = new QIcon; + mapIcon->addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off); + mapIcon->addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On); mapListModel = new QStandardItemModel; + mapGroupsModel = new QList; QStandardItem *entry = new QStandardItem; entry->setText(project->getProjectTitle()); @@ -316,16 +317,13 @@ void MainWindow::populateMapList() { group->setEditable(false); group->setData(group_name, Qt::UserRole); group->setData("map_group", MapListUserRoles::TypeRole); + group->setData(i, MapListUserRoles::GroupRole); maps->appendRow(group); + mapGroupsModel->append(group); QStringList *names = project->groupedMapNames->value(i); for (int j = 0; j < names->length(); j++) { QString map_name = names->value(j); - QStandardItem *map = new QStandardItem; - map->setText(QString("[%1.%2] ").arg(i).arg(j, 2, 10, QLatin1Char('0')) + map_name); - map->setIcon(mapIcon); - map->setEditable(false); - map->setData(map_name, Qt::UserRole); - map->setData("map_name", MapListUserRoles::TypeRole); + QStandardItem *map = createMapItem(map_name, i, j); group->appendRow(map); } } @@ -341,6 +339,16 @@ void MainWindow::populateMapList() { ui->mapList->repaint(); } +QStandardItem* MainWindow::createMapItem(QString mapName, int groupNum, int inGroupNum) { + QStandardItem *map = new QStandardItem; + map->setText(QString("[%1.%2] ").arg(groupNum).arg(inGroupNum, 2, 10, QLatin1Char('0')) + mapName); + map->setIcon(*mapIcon); + map->setEditable(false); + map->setData(mapName, Qt::UserRole); + map->setData("map_name", MapListUserRoles::TypeRole); + return map; +} + void MainWindow::onOpenMapListContextMenu(const QPoint &point) { QModelIndex index = ui->mapList->indexAt(point); @@ -357,18 +365,26 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) // Build custom context menu depending on which type of item was selected (map group, map name, etc.) if (itemType == "map_group") { QString groupName = selectedItem->data(Qt::UserRole).toString(); + int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt(); QMenu* menu = new QMenu(); QActionGroup* actions = new QActionGroup(menu); - actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupName); - connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(addNewMapToGroup(QAction*))); + actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupNum); + connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(onAddNewMapToGroupClick(QAction*))); menu->exec(QCursor::pos()); } } -void MainWindow::addNewMapToGroup(QAction* triggeredAction) +void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) { - QString groupName = triggeredAction->data().toString(); - qDebug() << "Adding new map " << groupName; + int groupNum = triggeredAction->data().toInt(); + QStandardItem* groupItem = mapGroupsModel->at(groupNum); + + QString newMapName = editor->project->getNewMapName(); + editor->project->addNewMapToGroup(newMapName, groupNum); + + int numMapsInGroup = groupItem->rowCount(); + QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup); + groupItem->appendRow(newMapItem); } void MainWindow::on_mapList_activated(const QModelIndex &index) diff --git a/mainwindow.h b/mainwindow.h index 36a8010f..a0dc1c1b 100755 --- a/mainwindow.h +++ b/mainwindow.h @@ -67,18 +67,21 @@ private slots: void on_toolButton_Dropper_clicked(); void onOpenMapListContextMenu(const QPoint &point); - void addNewMapToGroup(QAction* triggeredAction); + void onAddNewMapToGroupClick(QAction* triggeredAction); private: Ui::MainWindow *ui; QStandardItemModel *mapListModel; + QList *mapGroupsModel; Editor *editor = NULL; + QIcon* mapIcon; void setMap(QString); void populateMapList(); QString getExistingDirectory(QString); void openProject(QString dir); QString getDefaultMap(); void setRecentMap(QString map_name); + QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum); void markAllEdited(QAbstractItemModel *model); void markEdited(QModelIndex index); @@ -89,6 +92,7 @@ private: }; enum MapListUserRoles { + GroupRole = Qt::UserRole + 1, // Used to hold the map group number. TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view. }; diff --git a/map.h b/map.h index 30b8bd9b..9703215f 100755 --- a/map.h +++ b/map.h @@ -76,6 +76,7 @@ public: public: QString name; QString constantName; + QString group_num; QString attributes_label; QString events_label; QString scripts_label; @@ -190,4 +191,13 @@ signals: public slots: }; +class MapGroup : public QObject +{ + Q_OBJECT +public: + QString name; + int group_num; + QList maps; +}; + #endif // MAP_H diff --git a/project.cpp b/project.cpp index 812478fc..6e9c55e6 100755 --- a/project.cpp +++ b/project.cpp @@ -8,15 +8,19 @@ #include #include #include +#include #include #include Project::Project() { groupNames = new QStringList; + map_groups = new QMap; groupedMapNames = new QList; mapNames = new QStringList; map_cache = new QMap; + mapConstantsToMapNames = new QMap; + mapNamesToMapConstants = new QMap; tileset_cache = new QMap; } @@ -67,8 +71,8 @@ void Project::loadMapConnections(Map *map) { connection->direction = command.value(1); connection->offset = command.value(2); QString mapConstant = command.value(3); - if (mapConstantsToMapNames.contains(mapConstant)) { - connection->map_name = mapConstantsToMapNames[mapConstant]; + if (mapConstantsToMapNames->contains(mapConstant)) { + connection->map_name = mapConstantsToMapNames->value(mapConstant); map->connections.append(connection); } else { qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant); @@ -521,11 +525,12 @@ void Project::readMapGroups() { QStringList *list = groupedMaps->value(group); list->append(mapName); maps->append(mapName); + map_groups->insert(mapName, group); // Build the mapping and reverse mapping between map constants and map names. QString mapConstant = Map::mapConstantFromName(mapName); - mapConstantsToMapNames.insert(mapConstant, mapName); - mapNamesToMapConstants.insert(mapName, mapConstant); + mapConstantsToMapNames->insert(mapConstant, mapName); + mapNamesToMapConstants->insert(mapName, mapConstant); } } } @@ -536,6 +541,23 @@ void Project::readMapGroups() { mapNames = maps; } +void Project::addNewMapToGroup(QString mapName, int groupNum) { + mapNames->append(mapName); + map_groups->insert(mapName, groupNum); + groupedMapNames->value(groupNum)->append(mapName); +} + +QString Project::getNewMapName() { + // Ensure default name doesn't already exist. + int i = 0; + QString newMapName; + do { + newMapName = QString("NewMap%1").arg(++i); + } while (mapNames->contains(newMapName)); + + return newMapName; +} + QList* Project::parse(QString text) { Asm *parser = new Asm; return parser->parse(text); @@ -739,7 +761,7 @@ void Project::saveMapEvents(Map *map) { text += QString(", %1").arg(warp->get("y")); text += QString(", %1").arg(warp->get("elevation")); text += QString(", %1").arg(warp->get("destination_warp")); - text += QString(", %1").arg(mapNamesToMapConstants[warp->get("destination_map_name")]); + text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name"))); text += "\n"; } text += "\n"; @@ -882,8 +904,8 @@ void Project::readMapEvents(Map *map) { // Ensure the warp destination map constant is valid before adding it to the warps. QString mapConstant = command.value(i++); - if (mapConstantsToMapNames.contains(mapConstant)) { - warp->put("destination_map_name", mapConstantsToMapNames[mapConstant]); + if (mapConstantsToMapNames->contains(mapConstant)) { + warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant)); warp->put("event_type", "warp"); map->events["warp"].append(warp); } else { diff --git a/project.h b/project.h index ebf1ed28..34987668 100755 --- a/project.h +++ b/project.h @@ -6,6 +6,7 @@ #include #include +#include class Project { @@ -13,10 +14,11 @@ public: Project(); QString root; QStringList *groupNames = NULL; + QMap *map_groups; QList *groupedMapNames = NULL; QStringList *mapNames = NULL; - QMap mapConstantsToMapNames; - QMap mapNamesToMapConstants; + QMap *mapConstantsToMapNames; + QMap *mapNamesToMapConstants; QMap *map_cache; Map* loadMap(QString); @@ -33,6 +35,8 @@ public: void saveTextFile(QString path, QString text); void readMapGroups(); + void addNewMapToGroup(QString mapName, int groupNum); + QString getNewMapName(); QString getProjectTitle(); QList* getLabelMacros(QList*, QString);