Browse Source

Add new map to group that was selected (UI functionality only)

Marcus Huderle 6 years ago
parent
commit
3fcad085c3
5 changed files with 80 additions and 24 deletions
  1. 30
    14
      mainwindow.cpp
  2. 5
    1
      mainwindow.h
  3. 10
    0
      map.h
  4. 29
    7
      project.cpp
  5. 6
    2
      project.h

+ 30
- 14
mainwindow.cpp View File

289
     QIcon folderIcon;
289
     QIcon folderIcon;
290
     folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off);
290
     folderIcon.addFile(QStringLiteral(":/icons/folder_closed.ico"), QSize(), QIcon::Normal, QIcon::Off);
291
 
291
 
292
-    QIcon mapIcon;
293
-    mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
294
-    mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
292
+    mapIcon = new QIcon;
293
+    mapIcon->addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
294
+    mapIcon->addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
295
 
295
 
296
     mapListModel = new QStandardItemModel;
296
     mapListModel = new QStandardItemModel;
297
+    mapGroupsModel = new QList<QStandardItem*>;
297
 
298
 
298
     QStandardItem *entry = new QStandardItem;
299
     QStandardItem *entry = new QStandardItem;
299
     entry->setText(project->getProjectTitle());
300
     entry->setText(project->getProjectTitle());
316
         group->setEditable(false);
317
         group->setEditable(false);
317
         group->setData(group_name, Qt::UserRole);
318
         group->setData(group_name, Qt::UserRole);
318
         group->setData("map_group", MapListUserRoles::TypeRole);
319
         group->setData("map_group", MapListUserRoles::TypeRole);
320
+        group->setData(i, MapListUserRoles::GroupRole);
319
         maps->appendRow(group);
321
         maps->appendRow(group);
322
+        mapGroupsModel->append(group);
320
         QStringList *names = project->groupedMapNames->value(i);
323
         QStringList *names = project->groupedMapNames->value(i);
321
         for (int j = 0; j < names->length(); j++) {
324
         for (int j = 0; j < names->length(); j++) {
322
             QString map_name = names->value(j);
325
             QString map_name = names->value(j);
323
-            QStandardItem *map = new QStandardItem;
324
-            map->setText(QString("[%1.%2] ").arg(i).arg(j, 2, 10, QLatin1Char('0')) + map_name);
325
-            map->setIcon(mapIcon);
326
-            map->setEditable(false);
327
-            map->setData(map_name, Qt::UserRole);
328
-            map->setData("map_name", MapListUserRoles::TypeRole);
326
+            QStandardItem *map = createMapItem(map_name, i, j);
329
             group->appendRow(map);
327
             group->appendRow(map);
330
         }
328
         }
331
     }
329
     }
341
     ui->mapList->repaint();
339
     ui->mapList->repaint();
342
 }
340
 }
343
 
341
 
342
+QStandardItem* MainWindow::createMapItem(QString mapName, int groupNum, int inGroupNum) {
343
+    QStandardItem *map = new QStandardItem;
344
+    map->setText(QString("[%1.%2] ").arg(groupNum).arg(inGroupNum, 2, 10, QLatin1Char('0')) + mapName);
345
+    map->setIcon(*mapIcon);
346
+    map->setEditable(false);
347
+    map->setData(mapName, Qt::UserRole);
348
+    map->setData("map_name", MapListUserRoles::TypeRole);
349
+    return map;
350
+}
351
+
344
 void MainWindow::onOpenMapListContextMenu(const QPoint &point)
352
 void MainWindow::onOpenMapListContextMenu(const QPoint &point)
345
 {
353
 {
346
     QModelIndex index = ui->mapList->indexAt(point);
354
     QModelIndex index = ui->mapList->indexAt(point);
357
     // Build custom context menu depending on which type of item was selected (map group, map name, etc.)
365
     // Build custom context menu depending on which type of item was selected (map group, map name, etc.)
358
     if (itemType == "map_group") {
366
     if (itemType == "map_group") {
359
         QString groupName = selectedItem->data(Qt::UserRole).toString();
367
         QString groupName = selectedItem->data(Qt::UserRole).toString();
368
+        int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt();
360
         QMenu* menu = new QMenu();
369
         QMenu* menu = new QMenu();
361
         QActionGroup* actions = new QActionGroup(menu);
370
         QActionGroup* actions = new QActionGroup(menu);
362
-        actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupName);
363
-        connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(addNewMapToGroup(QAction*)));
371
+        actions->addAction(menu->addAction("Add New Map to Group"))->setData(groupNum);
372
+        connect(actions, SIGNAL(triggered(QAction*)), this, SLOT(onAddNewMapToGroupClick(QAction*)));
364
         menu->exec(QCursor::pos());
373
         menu->exec(QCursor::pos());
365
     }
374
     }
366
 }
375
 }
367
 
376
 
368
-void MainWindow::addNewMapToGroup(QAction* triggeredAction)
377
+void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction)
369
 {
378
 {
370
-    QString groupName = triggeredAction->data().toString();
371
-    qDebug() << "Adding new map " << groupName;
379
+    int groupNum = triggeredAction->data().toInt();
380
+    QStandardItem* groupItem = mapGroupsModel->at(groupNum);
381
+
382
+    QString newMapName = editor->project->getNewMapName();
383
+    editor->project->addNewMapToGroup(newMapName, groupNum);
384
+
385
+    int numMapsInGroup = groupItem->rowCount();
386
+    QStandardItem *newMapItem = createMapItem(newMapName, groupNum, numMapsInGroup);
387
+    groupItem->appendRow(newMapItem);
372
 }
388
 }
373
 
389
 
374
 void MainWindow::on_mapList_activated(const QModelIndex &index)
390
 void MainWindow::on_mapList_activated(const QModelIndex &index)

+ 5
- 1
mainwindow.h View File

67
     void on_toolButton_Dropper_clicked();
67
     void on_toolButton_Dropper_clicked();
68
 
68
 
69
     void onOpenMapListContextMenu(const QPoint &point);
69
     void onOpenMapListContextMenu(const QPoint &point);
70
-    void addNewMapToGroup(QAction* triggeredAction);
70
+    void onAddNewMapToGroupClick(QAction* triggeredAction);
71
 
71
 
72
 private:
72
 private:
73
     Ui::MainWindow *ui;
73
     Ui::MainWindow *ui;
74
     QStandardItemModel *mapListModel;
74
     QStandardItemModel *mapListModel;
75
+    QList<QStandardItem*> *mapGroupsModel;
75
     Editor *editor = NULL;
76
     Editor *editor = NULL;
77
+    QIcon* mapIcon;
76
     void setMap(QString);
78
     void setMap(QString);
77
     void populateMapList();
79
     void populateMapList();
78
     QString getExistingDirectory(QString);
80
     QString getExistingDirectory(QString);
79
     void openProject(QString dir);
81
     void openProject(QString dir);
80
     QString getDefaultMap();
82
     QString getDefaultMap();
81
     void setRecentMap(QString map_name);
83
     void setRecentMap(QString map_name);
84
+    QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
82
 
85
 
83
     void markAllEdited(QAbstractItemModel *model);
86
     void markAllEdited(QAbstractItemModel *model);
84
     void markEdited(QModelIndex index);
87
     void markEdited(QModelIndex index);
89
 };
92
 };
90
 
93
 
91
 enum MapListUserRoles {
94
 enum MapListUserRoles {
95
+    GroupRole = Qt::UserRole + 1, // Used to hold the map group number.
92
     TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view.
96
     TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view.
93
 };
97
 };
94
 
98
 

+ 10
- 0
map.h View File

76
 public:
76
 public:
77
     QString name;
77
     QString name;
78
     QString constantName;
78
     QString constantName;
79
+    QString group_num;
79
     QString attributes_label;
80
     QString attributes_label;
80
     QString events_label;
81
     QString events_label;
81
     QString scripts_label;
82
     QString scripts_label;
190
 public slots:
191
 public slots:
191
 };
192
 };
192
 
193
 
194
+class MapGroup : public QObject
195
+{
196
+    Q_OBJECT
197
+public:
198
+    QString name;
199
+    int group_num;
200
+    QList<Map*> maps;
201
+};
202
+
193
 #endif // MAP_H
203
 #endif // MAP_H

+ 29
- 7
project.cpp View File

8
 #include <QDebug>
8
 #include <QDebug>
9
 #include <QFile>
9
 #include <QFile>
10
 #include <QTextStream>
10
 #include <QTextStream>
11
+#include <QStandardItem>
11
 #include <QMessageBox>
12
 #include <QMessageBox>
12
 #include <QRegularExpression>
13
 #include <QRegularExpression>
13
 
14
 
14
 Project::Project()
15
 Project::Project()
15
 {
16
 {
16
     groupNames = new QStringList;
17
     groupNames = new QStringList;
18
+    map_groups = new QMap<QString, int>;
17
     groupedMapNames = new QList<QStringList*>;
19
     groupedMapNames = new QList<QStringList*>;
18
     mapNames = new QStringList;
20
     mapNames = new QStringList;
19
     map_cache = new QMap<QString, Map*>;
21
     map_cache = new QMap<QString, Map*>;
22
+    mapConstantsToMapNames = new QMap<QString, QString>;
23
+    mapNamesToMapConstants = new QMap<QString, QString>;
20
     tileset_cache = new QMap<QString, Tileset*>;
24
     tileset_cache = new QMap<QString, Tileset*>;
21
 }
25
 }
22
 
26
 
67
                     connection->direction = command.value(1);
71
                     connection->direction = command.value(1);
68
                     connection->offset = command.value(2);
72
                     connection->offset = command.value(2);
69
                     QString mapConstant = command.value(3);
73
                     QString mapConstant = command.value(3);
70
-                    if (mapConstantsToMapNames.contains(mapConstant)) {
71
-                        connection->map_name = mapConstantsToMapNames[mapConstant];
74
+                    if (mapConstantsToMapNames->contains(mapConstant)) {
75
+                        connection->map_name = mapConstantsToMapNames->value(mapConstant);
72
                         map->connections.append(connection);
76
                         map->connections.append(connection);
73
                     } else {
77
                     } else {
74
                         qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
78
                         qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
521
                     QStringList *list = groupedMaps->value(group);
525
                     QStringList *list = groupedMaps->value(group);
522
                     list->append(mapName);
526
                     list->append(mapName);
523
                     maps->append(mapName);
527
                     maps->append(mapName);
528
+                    map_groups->insert(mapName, group);
524
 
529
 
525
                     // Build the mapping and reverse mapping between map constants and map names.
530
                     // Build the mapping and reverse mapping between map constants and map names.
526
                     QString mapConstant = Map::mapConstantFromName(mapName);
531
                     QString mapConstant = Map::mapConstantFromName(mapName);
527
-                    mapConstantsToMapNames.insert(mapConstant, mapName);
528
-                    mapNamesToMapConstants.insert(mapName, mapConstant);
532
+                    mapConstantsToMapNames->insert(mapConstant, mapName);
533
+                    mapNamesToMapConstants->insert(mapName, mapConstant);
529
                 }
534
                 }
530
             }
535
             }
531
         }
536
         }
536
     mapNames = maps;
541
     mapNames = maps;
537
 }
542
 }
538
 
543
 
544
+void Project::addNewMapToGroup(QString mapName, int groupNum) {
545
+    mapNames->append(mapName);
546
+    map_groups->insert(mapName, groupNum);
547
+    groupedMapNames->value(groupNum)->append(mapName);
548
+}
549
+
550
+QString Project::getNewMapName() {
551
+    // Ensure default name doesn't already exist.
552
+    int i = 0;
553
+    QString newMapName;
554
+    do {
555
+        newMapName = QString("NewMap%1").arg(++i);
556
+    } while (mapNames->contains(newMapName));
557
+
558
+    return newMapName;
559
+}
560
+
539
 QList<QStringList>* Project::parse(QString text) {
561
 QList<QStringList>* Project::parse(QString text) {
540
     Asm *parser = new Asm;
562
     Asm *parser = new Asm;
541
     return parser->parse(text);
563
     return parser->parse(text);
739
             text += QString(", %1").arg(warp->get("y"));
761
             text += QString(", %1").arg(warp->get("y"));
740
             text += QString(", %1").arg(warp->get("elevation"));
762
             text += QString(", %1").arg(warp->get("elevation"));
741
             text += QString(", %1").arg(warp->get("destination_warp"));
763
             text += QString(", %1").arg(warp->get("destination_warp"));
742
-            text += QString(", %1").arg(mapNamesToMapConstants[warp->get("destination_map_name")]);
764
+            text += QString(", %1").arg(mapNamesToMapConstants->value(warp->get("destination_map_name")));
743
             text += "\n";
765
             text += "\n";
744
         }
766
         }
745
         text += "\n";
767
         text += "\n";
882
 
904
 
883
             // Ensure the warp destination map constant is valid before adding it to the warps.
905
             // Ensure the warp destination map constant is valid before adding it to the warps.
884
             QString mapConstant = command.value(i++);
906
             QString mapConstant = command.value(i++);
885
-            if (mapConstantsToMapNames.contains(mapConstant)) {
886
-                warp->put("destination_map_name", mapConstantsToMapNames[mapConstant]);
907
+            if (mapConstantsToMapNames->contains(mapConstant)) {
908
+                warp->put("destination_map_name", mapConstantsToMapNames->value(mapConstant));
887
                 warp->put("event_type", "warp");
909
                 warp->put("event_type", "warp");
888
                 map->events["warp"].append(warp);
910
                 map->events["warp"].append(warp);
889
             } else {
911
             } else {

+ 6
- 2
project.h View File

6
 
6
 
7
 #include <QStringList>
7
 #include <QStringList>
8
 #include <QList>
8
 #include <QList>
9
+#include <QStandardItem>
9
 
10
 
10
 class Project
11
 class Project
11
 {
12
 {
13
     Project();
14
     Project();
14
     QString root;
15
     QString root;
15
     QStringList *groupNames = NULL;
16
     QStringList *groupNames = NULL;
17
+    QMap<QString, int> *map_groups;
16
     QList<QStringList*> *groupedMapNames = NULL;
18
     QList<QStringList*> *groupedMapNames = NULL;
17
     QStringList *mapNames = NULL;
19
     QStringList *mapNames = NULL;
18
-    QMap<QString, QString> mapConstantsToMapNames;
19
-    QMap<QString, QString> mapNamesToMapConstants;
20
+    QMap<QString, QString> *mapConstantsToMapNames;
21
+    QMap<QString, QString> *mapNamesToMapConstants;
20
 
22
 
21
     QMap<QString, Map*> *map_cache;
23
     QMap<QString, Map*> *map_cache;
22
     Map* loadMap(QString);
24
     Map* loadMap(QString);
33
     void saveTextFile(QString path, QString text);
35
     void saveTextFile(QString path, QString text);
34
 
36
 
35
     void readMapGroups();
37
     void readMapGroups();
38
+    void addNewMapToGroup(QString mapName, int groupNum);
39
+    QString getNewMapName();
36
     QString getProjectTitle();
40
     QString getProjectTitle();
37
 
41
 
38
     QList<QStringList>* getLabelMacros(QList<QStringList>*, QString);
42
     QList<QStringList>* getLabelMacros(QList<QStringList>*, QString);