Add right-click context menu for map groups

This commit is contained in:
Marcus Huderle 2018-02-13 21:01:16 -08:00
parent 7f761a5051
commit 794b814c47
2 changed files with 50 additions and 4 deletions

View file

@ -293,13 +293,13 @@ void MainWindow::populateMapList() {
mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off); mapIcon.addFile(QStringLiteral(":/icons/map.ico"), QSize(), QIcon::Normal, QIcon::Off);
mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On); mapIcon.addFile(QStringLiteral(":/icons/image.ico"), QSize(), QIcon::Normal, QIcon::On);
QStandardItemModel *model = new QStandardItemModel; mapListModel = new QStandardItemModel;
QStandardItem *entry = new QStandardItem; QStandardItem *entry = new QStandardItem;
entry->setText(project->getProjectTitle()); entry->setText(project->getProjectTitle());
entry->setIcon(folderIcon); entry->setIcon(folderIcon);
entry->setEditable(false); entry->setEditable(false);
model->appendRow(entry); mapListModel->appendRow(entry);
QStandardItem *maps = new QStandardItem; QStandardItem *maps = new QStandardItem;
maps->setText("maps"); maps->setText("maps");
@ -314,6 +314,8 @@ void MainWindow::populateMapList() {
group->setText(group_name); group->setText(group_name);
group->setIcon(mapFolderIcon); group->setIcon(mapFolderIcon);
group->setEditable(false); group->setEditable(false);
group->setData(group_name, Qt::UserRole);
group->setData("map_group", MapListUserRoles::TypeRole);
maps->appendRow(group); maps->appendRow(group);
QStringList *names = project->groupedMapNames->value(i); QStringList *names = project->groupedMapNames->value(i);
for (int j = 0; j < names->length(); j++) { for (int j = 0; j < names->length(); j++) {
@ -323,17 +325,52 @@ void MainWindow::populateMapList() {
map->setIcon(mapIcon); map->setIcon(mapIcon);
map->setEditable(false); map->setEditable(false);
map->setData(map_name, Qt::UserRole); map->setData(map_name, Qt::UserRole);
map->setData("map_name", MapListUserRoles::TypeRole);
group->appendRow(map); group->appendRow(map);
//ui->mapList->setExpanded(model->indexFromItem(map), false); // redundant
} }
} }
ui->mapList->setModel(model); // Right-clicking on items in the map list tree view brings up a context menu.
ui->mapList->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->mapList, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(onOpenMapListContextMenu(const QPoint &)));
ui->mapList->setModel(mapListModel);
ui->mapList->setUpdatesEnabled(true); ui->mapList->setUpdatesEnabled(true);
ui->mapList->expandToDepth(2); ui->mapList->expandToDepth(2);
ui->mapList->repaint(); ui->mapList->repaint();
} }
void MainWindow::onOpenMapListContextMenu(const QPoint &point)
{
QModelIndex index = ui->mapList->indexAt(point);
if (!index.isValid()) {
return;
}
QStandardItem *selectedItem = mapListModel->itemFromIndex(index);
QVariant itemType = selectedItem->data(MapListUserRoles::TypeRole);
if (!itemType.isValid()) {
return;
}
// 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();
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*)));
menu->exec(QCursor::pos());
}
}
void MainWindow::addNewMapToGroup(QAction* triggeredAction)
{
QString groupName = triggeredAction->data().toString();
qDebug() << "Adding new map " << groupName;
}
void MainWindow::on_mapList_activated(const QModelIndex &index) void MainWindow::on_mapList_activated(const QModelIndex &index)
{ {
QVariant data = index.data(Qt::UserRole); QVariant data = index.data(Qt::UserRole);

View file

@ -4,6 +4,7 @@
#include <QString> #include <QString>
#include <QModelIndex> #include <QModelIndex>
#include <QMainWindow> #include <QMainWindow>
#include <QStandardItemModel>
#include <QGraphicsPixmapItem> #include <QGraphicsPixmapItem>
#include <QGraphicsItemGroup> #include <QGraphicsItemGroup>
#include <QGraphicsSceneMouseEvent> #include <QGraphicsSceneMouseEvent>
@ -65,8 +66,12 @@ private slots:
void on_toolButton_Dropper_clicked(); void on_toolButton_Dropper_clicked();
void onOpenMapListContextMenu(const QPoint &point);
void addNewMapToGroup(QAction* triggeredAction);
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
QStandardItemModel *mapListModel;
Editor *editor = NULL; Editor *editor = NULL;
void setMap(QString); void setMap(QString);
void populateMapList(); void populateMapList();
@ -83,4 +88,8 @@ private:
void checkToolButtons(); void checkToolButtons();
}; };
enum MapListUserRoles {
TypeRole = Qt::UserRole + 10, // Used to differentiate between the different layers of the map list tree view.
};
#endif // MAINWINDOW_H #endif // MAINWINDOW_H