Browse Source

Add right-click context menu for map groups

Marcus Huderle 6 years ago
parent
commit
794b814c47
2 changed files with 50 additions and 4 deletions
  1. 41
    4
      mainwindow.cpp
  2. 9
    0
      mainwindow.h

+ 41
- 4
mainwindow.cpp View File

@@ -293,13 +293,13 @@ void MainWindow::populateMapList() {
293 293
     mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
294 294
     mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
295 295
 
296
-    QStandardItemModel *model = new QStandardItemModel;
296
+    mapListModel = new QStandardItemModel;
297 297
 
298 298
     QStandardItem *entry = new QStandardItem;
299 299
     entry->setText(project->getProjectTitle());
300 300
     entry->setIcon(folderIcon);
301 301
     entry->setEditable(false);
302
-    model->appendRow(entry);
302
+    mapListModel->appendRow(entry);
303 303
 
304 304
     QStandardItem *maps = new QStandardItem;
305 305
     maps->setText("maps");
@@ -314,6 +314,8 @@ void MainWindow::populateMapList() {
314 314
         group->setText(group_name);
315 315
         group->setIcon(mapFolderIcon);
316 316
         group->setEditable(false);
317
+        group->setData(group_name, Qt::UserRole);
318
+        group->setData("map_group", MapListUserRoles::TypeRole);
317 319
         maps->appendRow(group);
318 320
         QStringList *names = project->groupedMapNames->value(i);
319 321
         for (int j = 0; j < names->length(); j++) {
@@ -323,17 +325,52 @@ void MainWindow::populateMapList() {
323 325
             map->setIcon(mapIcon);
324 326
             map->setEditable(false);
325 327
             map->setData(map_name, Qt::UserRole);
328
+            map->setData("map_name", MapListUserRoles::TypeRole);
326 329
             group->appendRow(map);
327
-            //ui->mapList->setExpanded(model->indexFromItem(map), false); // redundant
328 330
         }
329 331
     }
330 332
 
331
-    ui->mapList->setModel(model);
333
+    // Right-clicking on items in the map list tree view brings up a context menu.
334
+    ui->mapList->setContextMenuPolicy(Qt::CustomContextMenu);
335
+    connect(ui->mapList, SIGNAL(customContextMenuRequested(const QPoint &)),
336
+            this, SLOT(onOpenMapListContextMenu(const QPoint &)));
337
+
338
+    ui->mapList->setModel(mapListModel);
332 339
     ui->mapList->setUpdatesEnabled(true);
333 340
     ui->mapList->expandToDepth(2);
334 341
     ui->mapList->repaint();
335 342
 }
336 343
 
344
+void MainWindow::onOpenMapListContextMenu(const QPoint &point)
345
+{
346
+    QModelIndex index = ui->mapList->indexAt(point);
347
+    if (!index.isValid()) {
348
+        return;
349
+    }
350
+
351
+    QStandardItem *selectedItem = mapListModel->itemFromIndex(index);
352
+    QVariant itemType = selectedItem->data(MapListUserRoles::TypeRole);
353
+    if (!itemType.isValid()) {
354
+        return;
355
+    }
356
+
357
+    // Build custom context menu depending on which type of item was selected (map group, map name, etc.)
358
+    if (itemType == "map_group") {
359
+        QString groupName = selectedItem->data(Qt::UserRole).toString();
360
+        QMenu* menu = new QMenu();
361
+        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*)));
364
+        menu->exec(QCursor::pos());
365
+    }
366
+}
367
+
368
+void MainWindow::addNewMapToGroup(QAction* triggeredAction)
369
+{
370
+    QString groupName = triggeredAction->data().toString();
371
+    qDebug() << "Adding new map " << groupName;
372
+}
373
+
337 374
 void MainWindow::on_mapList_activated(const QModelIndex &index)
338 375
 {
339 376
     QVariant data = index.data(Qt::UserRole);

+ 9
- 0
mainwindow.h View File

@@ -4,6 +4,7 @@
4 4
 #include <QString>
5 5
 #include <QModelIndex>
6 6
 #include <QMainWindow>
7
+#include <QStandardItemModel>
7 8
 #include <QGraphicsPixmapItem>
8 9
 #include <QGraphicsItemGroup>
9 10
 #include <QGraphicsSceneMouseEvent>
@@ -65,8 +66,12 @@ private slots:
65 66
 
66 67
     void on_toolButton_Dropper_clicked();
67 68
 
69
+    void onOpenMapListContextMenu(const QPoint &point);
70
+    void addNewMapToGroup(QAction* triggeredAction);
71
+
68 72
 private:
69 73
     Ui::MainWindow *ui;
74
+    QStandardItemModel *mapListModel;
70 75
     Editor *editor = NULL;
71 76
     void setMap(QString);
72 77
     void populateMapList();
@@ -83,4 +88,8 @@ private:
83 88
     void checkToolButtons();
84 89
 };
85 90
 
91
+enum MapListUserRoles {
92
+    TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view.
93
+};
94
+
86 95
 #endif // MAINWINDOW_H