From d6f3bb100803598da3df2693987aa8d9bdbc9eb5 Mon Sep 17 00:00:00 2001 From: garak Date: Tue, 14 Feb 2023 01:50:14 -0500 Subject: [PATCH] allow editing map group names --- include/ui/maplistmodels.h | 25 +++++++++- src/mainwindow.cpp | 2 + src/ui/maplistmodels.cpp | 97 ++++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 37 deletions(-) diff --git a/include/ui/maplistmodels.h b/include/ui/maplistmodels.h index 6a5c1e92..ef21d8ae 100644 --- a/include/ui/maplistmodels.h +++ b/include/ui/maplistmodels.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -33,12 +34,31 @@ public slots: +class GroupNameDelegate : public QStyledItemDelegate { + Q_OBJECT + +public: + GroupNameDelegate(Project *project, QObject *parent = nullptr) : QStyledItemDelegate(parent), project(project) {} + + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void setEditorData(QWidget *editor, const QModelIndex &index) const override; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; + void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + +private: + Project *project = nullptr; +}; + + + +class QRegularExpressionValidator; + class MapGroupModel : public QStandardItemModel { Q_OBJECT public: MapGroupModel(Project *project, QObject *parent = nullptr); - ~MapGroupModel() {} + ~MapGroupModel() { } QVariant data(const QModelIndex &index, int role) const override; @@ -47,6 +67,8 @@ public: virtual QMimeData *mimeData(const QModelIndexList &indexes) const override; virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; + virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + public: void setMap(QString mapName) { this->openMap = mapName; } @@ -69,7 +91,6 @@ private: QMap groupItems; QMap mapItems; - // TODO: if reordering, will the item be the same? QString openMap; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6445d010..d5c985b3 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -1115,6 +1115,8 @@ bool MainWindow::populateMapList() { groupListProxyModel->setSourceModel(this->mapGroupModel); ui->mapList->setModel(groupListProxyModel); + this->ui->mapList->setItemDelegateForColumn(0, new GroupNameDelegate(this->editor->project, this)); + // // connect(this->mapGroupModel, &QStandardItemModel::dataChanged, [=](const QModelIndex &, const QModelIndex &, const QList &){ // qDebug() << "mapGroupModel dataChanged"; diff --git a/src/ui/maplistmodels.cpp b/src/ui/maplistmodels.cpp index dd0b254d..6c0a06d1 100644 --- a/src/ui/maplistmodels.cpp +++ b/src/ui/maplistmodels.cpp @@ -1,6 +1,7 @@ #include "maplistmodels.h" #include +#include #include "project.h" #include "filterchildrenproxymodel.h" @@ -16,6 +17,33 @@ void MapTree::removeSelected() { +QWidget *GroupNameDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const { + QLineEdit *editor = new QLineEdit(parent); + static const QRegularExpression expression("gMapGroup_[A-Za-z0-9_]+"); + QRegularExpressionValidator *validator = new QRegularExpressionValidator(expression, parent); + editor->setValidator(validator); + editor->setFrame(false); + return editor; +} + +void GroupNameDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { + QString groupName = index.data(Qt::UserRole).toString(); + QLineEdit *le = static_cast(editor); + le->setText(groupName); +} + +void GroupNameDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { + QLineEdit *le = static_cast(editor); + QString groupName = le->text(); + model->setData(index, groupName, Qt::UserRole); +} + +void GroupNameDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const { + editor->setGeometry(option.rect); +} + + + MapGroupModel::MapGroupModel(Project *project, QObject *parent) : QStandardItemModel(parent) { this->project = project; this->root = this->invisibleRootItem(); @@ -94,81 +122,66 @@ bool MapGroupModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i firstRow++; } - // updateProject(); - emit dragMoveCompleted(); + updateProject(); return false; } +void MapGroupModel::updateProject() { + if (!this->project) return; -/* QStringList groupNames; QMap mapGroups; QList groupedMapNames; QStringList mapNames; -*/ -void MapGroupModel::updateProject() { - // - QStringList groups; - int numGroups = this->root->rowCount(); - qDebug() << "group count:" << numGroups; for (int g = 0; g < this->root->rowCount(); g++) { QStandardItem *groupItem = this->item(g); - qDebug() << g << "group item" << groupItem->text(); //data(Qt::UserRole).toString(); + QString groupName = groupItem->data(Qt::UserRole).toString(); + groupNames.append(groupName); + mapGroups[groupName] = g; + QStringList mapsInGroup; for (int m = 0; m < groupItem->rowCount(); m++) { - // QStandardItem *mapItem = groupItem->child(m); - qDebug() << " " << m << "map item" << mapItem->data(Qt::UserRole).toString(); + QString mapName = mapItem->data(Qt::UserRole).toString(); + mapsInGroup.append(mapName); + mapNames.append(mapName); } + groupedMapNames.append(mapsInGroup); } - QList maps; - for (auto mapName : this->mapItems.keys()) { - // - QStandardItem *mapItem = this->mapItems[mapName]; - QStandardItem *groupItem = mapItem->parent(); - if (!groupItem) { - qDebug() << "FAIL: no parent" << mapName; - continue; - } - auto mapIndex = this->indexFromItem(mapItem).row(); - auto groupIndex = this->indexFromItem(groupItem).row(); - // qDebug().nospace() << "map: " << mapName << "[" << parentIndex.row() << "." << mapIndex.row() << "]"; - } + this->project->groupNames = groupNames; + this->project->mapGroups = mapGroups; + this->project->groupedMapNames = groupedMapNames; + this->project->mapNames = mapNames; } QStandardItem *MapGroupModel::createGroupItem(QString groupName, int groupIndex) { QStandardItem *group = new QStandardItem; group->setText(groupName); - group->setEditable(true); group->setData(groupName, Qt::UserRole); group->setData("map_group", MapListRoles::TypeRole); group->setData(groupIndex, MapListRoles::GroupRole); - group->setFlags(Qt::ItemIsEditable | /* Qt::ItemIsSelectable | */ Qt::ItemIsEnabled | /* Qt::ItemIsDragEnabled | */ Qt::ItemIsDropEnabled); + group->setFlags(Qt::ItemIsEnabled | Qt::ItemIsDropEnabled | Qt::ItemIsEditable); this->groupItems.insert(groupName, group); return group; } QStandardItem *MapGroupModel::createMapItem(QString mapName, QStandardItem *map) { if (!map) map = new QStandardItem; - map->setEditable(false); map->setData(mapName, Qt::UserRole); map->setData("map_name", MapListRoles::TypeRole); - // map->setData(groupIndex, MapListRoles::GroupRole); map->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled); this->mapItems[mapName] = map; return map; } QStandardItem *MapGroupModel::insertMapItem(QString mapName, QString groupName) { - //int groupIndex = this->project->groupNames.indexOf(groupName); QStandardItem *group = this->groupItems[groupName]; if (!group) { return nullptr; } - //int mapIndex = group->rowCount(); QStandardItem *map = createMapItem(mapName); group->appendRow(map); return map; @@ -181,12 +194,10 @@ void MapGroupModel::initialize() { QString group_name = this->project->groupNames.value(i); QStandardItem *group = createGroupItem(group_name, i); root->appendRow(group); - //this->setItem(0, i, group); QStringList names = this->project->groupedMapNames.value(i); for (int j = 0; j < names.length(); j++) { QString map_name = names.value(j); QStandardItem *map = createMapItem(map_name); - //this->setItem(i, j, map); group->appendRow(map); } } @@ -256,15 +267,31 @@ QVariant MapGroupModel::data(const QModelIndex &index, int role) const { else if (role == Qt::DisplayRole) { // QStandardItem *item = this->getItem(index)->child(row, col); + QString type = item->data(MapListRoles::TypeRole).toString(); - if (item->data(MapListRoles::TypeRole).toString() == "map_name") { + if (type == "map_name") { return QString("[%1.%2] ").arg(this->getItem(index)->row()).arg(row, 2, 10, QLatin1Char('0')) + item->data(Qt::UserRole).toString(); } + else if (type == "map_group") { + return item->data(Qt::UserRole).toString(); + } } return QStandardItemModel::data(index, role); } +bool MapGroupModel::setData(const QModelIndex &index, const QVariant &value, int role) { + if (role == Qt::UserRole && data(index, MapListRoles::TypeRole).toString() == "map_group") { + // verify uniqueness of new group name + if (this->project->groupNames.contains(value.toString())) { + return false; + } + } + if (QStandardItemModel::setData(index, value, role)) { + this->updateProject(); + } +} + MapAreaModel::MapAreaModel(Project *project, QObject *parent) : QStandardItemModel(parent) { @@ -288,7 +315,7 @@ QStandardItem *MapAreaModel::createAreaItem(QString mapsecName, int areaIndex) { QStandardItem *MapAreaModel::createMapItem(QString mapName, int groupIndex, int mapIndex) { QStandardItem *map = new QStandardItem; - map->setText(QString("[%1.%2] ").arg(groupIndex).arg(mapIndex, 2, 10, QLatin1Char('0')) + mapName); + map->setText(mapName); map->setEditable(false); map->setData(mapName, Qt::UserRole); map->setData("map_name", MapListRoles::TypeRole);