Sfoglia il codice sorgente

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

Marcus Huderle 6 anni fa
parent
commit
3fcad085c3
5 ha cambiato i file con 80 aggiunte e 24 eliminazioni
  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 Vedi File

@@ -289,11 +289,12 @@ void MainWindow::populateMapList() {
289 289
     QIcon folderIcon;
290 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 296
     mapListModel = new QStandardItemModel;
297
+    mapGroupsModel = new QList<QStandardItem*>;
297 298
 
298 299
     QStandardItem *entry = new QStandardItem;
299 300
     entry->setText(project->getProjectTitle());
@@ -316,16 +317,13 @@ void MainWindow::populateMapList() {
316 317
         group->setEditable(false);
317 318
         group->setData(group_name, Qt::UserRole);
318 319
         group->setData("map_group", MapListUserRoles::TypeRole);
320
+        group->setData(i, MapListUserRoles::GroupRole);
319 321
         maps->appendRow(group);
322
+        mapGroupsModel->append(group);
320 323
         QStringList *names = project->groupedMapNames->value(i);
321 324
         for (int j = 0; j < names->length(); j++) {
322 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 327
             group->appendRow(map);
330 328
         }
331 329
     }
@@ -341,6 +339,16 @@ void MainWindow::populateMapList() {
341 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 352
 void MainWindow::onOpenMapListContextMenu(const QPoint &point)
345 353
 {
346 354
     QModelIndex index = ui->mapList->indexAt(point);
@@ -357,18 +365,26 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point)
357 365
     // Build custom context menu depending on which type of item was selected (map group, map name, etc.)
358 366
     if (itemType == "map_group") {
359 367
         QString groupName = selectedItem->data(Qt::UserRole).toString();
368
+        int groupNum = selectedItem->data(MapListUserRoles::GroupRole).toInt();
360 369
         QMenu* menu = new QMenu();
361 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 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 390
 void MainWindow::on_mapList_activated(const QModelIndex &index)

+ 5
- 1
mainwindow.h Vedi File

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

@@ -76,6 +76,7 @@ public:
76 76
 public:
77 77
     QString name;
78 78
     QString constantName;
79
+    QString group_num;
79 80
     QString attributes_label;
80 81
     QString events_label;
81 82
     QString scripts_label;
@@ -190,4 +191,13 @@ signals:
190 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 203
 #endif // MAP_H

+ 29
- 7
project.cpp Vedi File

@@ -8,15 +8,19 @@
8 8
 #include <QDebug>
9 9
 #include <QFile>
10 10
 #include <QTextStream>
11
+#include <QStandardItem>
11 12
 #include <QMessageBox>
12 13
 #include <QRegularExpression>
13 14
 
14 15
 Project::Project()
15 16
 {
16 17
     groupNames = new QStringList;
18
+    map_groups = new QMap<QString, int>;
17 19
     groupedMapNames = new QList<QStringList*>;
18 20
     mapNames = new QStringList;
19 21
     map_cache = new QMap<QString, Map*>;
22
+    mapConstantsToMapNames = new QMap<QString, QString>;
23
+    mapNamesToMapConstants = new QMap<QString, QString>;
20 24
     tileset_cache = new QMap<QString, Tileset*>;
21 25
 }
22 26
 
@@ -67,8 +71,8 @@ void Project::loadMapConnections(Map *map) {
67 71
                     connection->direction = command.value(1);
68 72
                     connection->offset = command.value(2);
69 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 76
                         map->connections.append(connection);
73 77
                     } else {
74 78
                         qDebug() << QString("Failed to find connected map for map constant '%1'").arg(mapConstant);
@@ -521,11 +525,12 @@ void Project::readMapGroups() {
521 525
                     QStringList *list = groupedMaps->value(group);
522 526
                     list->append(mapName);
523 527
                     maps->append(mapName);
528
+                    map_groups->insert(mapName, group);
524 529
 
525 530
                     // Build the mapping and reverse mapping between map constants and map names.
526 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,6 +541,23 @@ void Project::readMapGroups() {
536 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 561
 QList<QStringList>* Project::parse(QString text) {
540 562
     Asm *parser = new Asm;
541 563
     return parser->parse(text);
@@ -739,7 +761,7 @@ void Project::saveMapEvents(Map *map) {
739 761
             text += QString(", %1").arg(warp->get("y"));
740 762
             text += QString(", %1").arg(warp->get("elevation"));
741 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 765
             text += "\n";
744 766
         }
745 767
         text += "\n";
@@ -882,8 +904,8 @@ void Project::readMapEvents(Map *map) {
882 904
 
883 905
             // Ensure the warp destination map constant is valid before adding it to the warps.
884 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 909
                 warp->put("event_type", "warp");
888 910
                 map->events["warp"].append(warp);
889 911
             } else {

+ 6
- 2
project.h Vedi File

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