diff --git a/include/mainwindow.h b/include/mainwindow.h index 536f659c..0c3bd4ee 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -389,6 +389,11 @@ private: void refreshRecentProjectsMenu(); void updateMapList(); + void mapListAddItem(); + void mapListRemoveItem(); + void mapListAddGroup(); + void mapListAddLayout(); + void mapListAddArea(); void displayMapProperties(); void checkToolButtons(); diff --git a/include/ui/maplistmodels.h b/include/ui/maplistmodels.h index 10270a13..a2babda6 100644 --- a/include/ui/maplistmodels.h +++ b/include/ui/maplistmodels.h @@ -75,6 +75,7 @@ public: QStandardItem *createGroupItem(QString groupName, int groupIndex, QStandardItem *fromItem = nullptr); QStandardItem *createMapItem(QString mapName, QStandardItem *fromItem = nullptr); + QStandardItem *insertGroupItem(QString groupName); QStandardItem *insertMapItem(QString mapName, QString groupName); QStandardItem *getItem(const QModelIndex &index) const; @@ -83,6 +84,7 @@ public: void initialize(); private: + friend class MapTree; void updateProject(); private: diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 61265135..4d31fb6c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -210,6 +210,24 @@ void MainWindow::initCustomUI() { WheelFilter *wheelFilter = new WheelFilter(this); ui->mainTabBar->installEventFilter(wheelFilter); this->ui->mapListContainer->tabBar()->installEventFilter(wheelFilter); + + // Create buttons for adding and removing items from the mapList + QFrame *frame = new QFrame(this->ui->mapListContainer); + frame->setFrameShape(QFrame::NoFrame); + QHBoxLayout *layout = new QHBoxLayout(frame); + + QPushButton *buttonAdd = new QPushButton(QIcon(":/icons/add.ico"), ""); + connect(buttonAdd, &QPushButton::clicked, [this]() { this->mapListAddItem(); }); + QPushButton *buttonRemove = new QPushButton(QIcon(":/icons/delete.ico"), ""); + connect(buttonRemove, &QPushButton::clicked, [this]() { this->mapListRemoveItem(); }); + + layout->addWidget(buttonAdd); + layout->addWidget(buttonRemove); + + layout->setSpacing(0); + layout->setContentsMargins(0, 0, 0, 0); + + this->ui->mapListContainer->setCornerWidget(frame, Qt::TopRightCorner); } void MainWindow::initExtraSignals() { @@ -1178,12 +1196,6 @@ bool MainWindow::populateMapList() { this->layoutListProxyModel->setSourceModel(this->layoutTreeModel); ui->layoutList->setModel(layoutListProxyModel); - /// !TODO - // ui->mapList->setSelectionMode(QAbstractItemView::ExtendedSelection); - // ui->mapList->setDragEnabled(true); - // ui->mapList->setAcceptDrops(true); - // ui->mapList->setDropIndicatorShown(true); - // ui->mapList->setDragDropMode(QAbstractItemView::InternalMove); on_toolButton_EnableDisable_EditGroups_clicked(); return success; @@ -1270,6 +1282,58 @@ void MainWindow::onOpenMapListContextMenu(const QPoint &point) { (this->*addFunction)(menu.exec(QCursor::pos())); } +void MainWindow::mapListAddGroup() { + QDialog dialog(this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint); + dialog.setWindowModality(Qt::ApplicationModal); + QDialogButtonBox newItemButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog); + connect(&newItemButtonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); + connect(&newItemButtonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); + + QLineEdit *newNameEdit = new QLineEdit(&dialog); + newNameEdit->setClearButtonEnabled(true); + + static const QRegularExpression re_validChars("[_A-Za-z0-9]*$"); + QRegularExpressionValidator *validator = new QRegularExpressionValidator(re_validChars); + newNameEdit->setValidator(validator); + + QFormLayout form(&dialog); + + form.addRow("New Group Name", newNameEdit); + form.addRow(&newItemButtonBox); + + if (dialog.exec() == QDialog::Accepted) { + QString newFieldName = newNameEdit->text(); + if (newFieldName.isEmpty()) return; + this->mapGroupModel->insertGroupItem(newFieldName); + } +} + +void MainWindow::mapListAddLayout() { + // this->layoutTreeModel->insertMapItem(newMapName, newMap->layout->id); +} + +void MainWindow::mapListAddArea() { + // this->mapAreaModel->insertMapItem(newMapName, newMap->location, newMapGroup); +} + +void MainWindow::mapListAddItem() { + switch (this->ui->mapListContainer->currentIndex()) { + case 0: + this->mapListAddGroup(); + break; + case 1: + this->mapListAddLayout(); + break; + case 2: + this->mapListAddArea(); + break; + } +} + +void MainWindow::mapListRemoveItem() { + // !TODO +} + void MainWindow::onAddNewMapToGroupClick(QAction* triggeredAction) { if (!triggeredAction) return; diff --git a/src/ui/maplistmodels.cpp b/src/ui/maplistmodels.cpp index d22f2af6..ee60affc 100644 --- a/src/ui/maplistmodels.cpp +++ b/src/ui/maplistmodels.cpp @@ -156,15 +156,26 @@ bool MapGroupModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i rowCount++; } - this->insertRows(firstRow, rowCount, parentIndex); - - int newItemIndex = 0; - for (QString mapName : droppedMaps) { - QModelIndex mapIndex = index(firstRow, 0, parentIndex); - QStandardItem *mapItem = this->itemFromIndex(mapIndex); - createMapItem(mapName, mapItem); - firstRow++; + QStandardItem *groupItem = this->itemFromIndex(parentIndex); + if (groupItem->hasChildren()) { + this->insertRows(firstRow, rowCount, parentIndex); + for (QString mapName : droppedMaps) { + QModelIndex mapIndex = index(firstRow, 0, parentIndex); + QStandardItem *mapItem = this->itemFromIndex(mapIndex); + createMapItem(mapName, mapItem); + firstRow++; + } } + // for whatever reason insertRows doesn't work as I expected with childless items + // so just append all the new maps instead + else { + for (QString mapName : droppedMaps) { + QStandardItem *mapItem = createMapItem(mapName); + groupItem->appendRow(mapItem); + firstRow++; + } + } + } emit dragMoveCompleted(); @@ -189,6 +200,10 @@ void MapGroupModel::updateProject() { QStringList mapsInGroup; for (int m = 0; m < groupItem->rowCount(); m++) { QStandardItem *mapItem = groupItem->child(m); + if (!mapItem) { + logError("An error occured while trying to apply updates to map group structure."); + return; + } QString mapName = mapItem->data(Qt::UserRole).toString(); mapsInGroup.append(mapName); mapNames.append(mapName); @@ -222,10 +237,17 @@ QStandardItem *MapGroupModel::createMapItem(QString mapName, QStandardItem *map) return map; } +QStandardItem *MapGroupModel::insertGroupItem(QString groupName) { + QStandardItem *group = createGroupItem(groupName, this->groupItems.size()); + this->root->appendRow(group); + this->updateProject(); + return group; +} + QStandardItem *MapGroupModel::insertMapItem(QString mapName, QString groupName) { QStandardItem *group = this->groupItems[groupName]; if (!group) { - return nullptr; + group = insertGroupItem(groupName); } QStandardItem *map = createMapItem(mapName); group->appendRow(map);