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
     mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
293
     mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
294
     mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
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
     QStandardItem *entry = new QStandardItem;
298
     QStandardItem *entry = new QStandardItem;
299
     entry->setText(project->getProjectTitle());
299
     entry->setText(project->getProjectTitle());
300
     entry->setIcon(folderIcon);
300
     entry->setIcon(folderIcon);
301
     entry->setEditable(false);
301
     entry->setEditable(false);
302
-    model->appendRow(entry);
302
+    mapListModel->appendRow(entry);
303
 
303
 
304
     QStandardItem *maps = new QStandardItem;
304
     QStandardItem *maps = new QStandardItem;
305
     maps->setText("maps");
305
     maps->setText("maps");
314
         group->setText(group_name);
314
         group->setText(group_name);
315
         group->setIcon(mapFolderIcon);
315
         group->setIcon(mapFolderIcon);
316
         group->setEditable(false);
316
         group->setEditable(false);
317
+        group->setData(group_name, Qt::UserRole);
318
+        group->setData("map_group", MapListUserRoles::TypeRole);
317
         maps->appendRow(group);
319
         maps->appendRow(group);
318
         QStringList *names = project->groupedMapNames->value(i);
320
         QStringList *names = project->groupedMapNames->value(i);
319
         for (int j = 0; j < names->length(); j++) {
321
         for (int j = 0; j < names->length(); j++) {
323
             map->setIcon(mapIcon);
325
             map->setIcon(mapIcon);
324
             map->setEditable(false);
326
             map->setEditable(false);
325
             map->setData(map_name, Qt::UserRole);
327
             map->setData(map_name, Qt::UserRole);
328
+            map->setData("map_name", MapListUserRoles::TypeRole);
326
             group->appendRow(map);
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
     ui->mapList->setUpdatesEnabled(true);
339
     ui->mapList->setUpdatesEnabled(true);
333
     ui->mapList->expandToDepth(2);
340
     ui->mapList->expandToDepth(2);
334
     ui->mapList->repaint();
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
 void MainWindow::on_mapList_activated(const QModelIndex &index)
374
 void MainWindow::on_mapList_activated(const QModelIndex &index)
338
 {
375
 {
339
     QVariant data = index.data(Qt::UserRole);
376
     QVariant data = index.data(Qt::UserRole);

+ 9
- 0
mainwindow.h View File

4
 #include <QString>
4
 #include <QString>
5
 #include <QModelIndex>
5
 #include <QModelIndex>
6
 #include <QMainWindow>
6
 #include <QMainWindow>
7
+#include <QStandardItemModel>
7
 #include <QGraphicsPixmapItem>
8
 #include <QGraphicsPixmapItem>
8
 #include <QGraphicsItemGroup>
9
 #include <QGraphicsItemGroup>
9
 #include <QGraphicsSceneMouseEvent>
10
 #include <QGraphicsSceneMouseEvent>
65
 
66
 
66
     void on_toolButton_Dropper_clicked();
67
     void on_toolButton_Dropper_clicked();
67
 
68
 
69
+    void onOpenMapListContextMenu(const QPoint &point);
70
+    void addNewMapToGroup(QAction* triggeredAction);
71
+
68
 private:
72
 private:
69
     Ui::MainWindow *ui;
73
     Ui::MainWindow *ui;
74
+    QStandardItemModel *mapListModel;
70
     Editor *editor = NULL;
75
     Editor *editor = NULL;
71
     void setMap(QString);
76
     void setMap(QString);
72
     void populateMapList();
77
     void populateMapList();
83
     void checkToolButtons();
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
 #endif // MAINWINDOW_H
95
 #endif // MAINWINDOW_H