allow editing map group names

This commit is contained in:
garak 2023-02-14 01:50:14 -05:00
parent 0ec8f4fee5
commit d6f3bb1008
3 changed files with 87 additions and 37 deletions

View file

@ -4,6 +4,7 @@
#include <QTreeView> #include <QTreeView>
#include <QFontDatabase> #include <QFontDatabase>
#include <QStyledItemDelegate>
#include <QStandardItemModel> #include <QStandardItemModel>
#include <QMap> #include <QMap>
@ -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 { class MapGroupModel : public QStandardItemModel {
Q_OBJECT Q_OBJECT
public: public:
MapGroupModel(Project *project, QObject *parent = nullptr); MapGroupModel(Project *project, QObject *parent = nullptr);
~MapGroupModel() {} ~MapGroupModel() { }
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
@ -47,6 +67,8 @@ public:
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override; 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 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: public:
void setMap(QString mapName) { this->openMap = mapName; } void setMap(QString mapName) { this->openMap = mapName; }
@ -69,7 +91,6 @@ private:
QMap<QString, QStandardItem *> groupItems; QMap<QString, QStandardItem *> groupItems;
QMap<QString, QStandardItem *> mapItems; QMap<QString, QStandardItem *> mapItems;
// TODO: if reordering, will the item be the same?
QString openMap; QString openMap;

View file

@ -1115,6 +1115,8 @@ bool MainWindow::populateMapList() {
groupListProxyModel->setSourceModel(this->mapGroupModel); groupListProxyModel->setSourceModel(this->mapGroupModel);
ui->mapList->setModel(groupListProxyModel); 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<int> &){ // connect(this->mapGroupModel, &QStandardItemModel::dataChanged, [=](const QModelIndex &, const QModelIndex &, const QList<int> &){
// qDebug() << "mapGroupModel dataChanged"; // qDebug() << "mapGroupModel dataChanged";

View file

@ -1,6 +1,7 @@
#include "maplistmodels.h" #include "maplistmodels.h"
#include <QMouseEvent> #include <QMouseEvent>
#include <QLineEdit>
#include "project.h" #include "project.h"
#include "filterchildrenproxymodel.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<QLineEdit *>(editor);
le->setText(groupName);
}
void GroupNameDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
QLineEdit *le = static_cast<QLineEdit *>(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) { MapGroupModel::MapGroupModel(Project *project, QObject *parent) : QStandardItemModel(parent) {
this->project = project; this->project = project;
this->root = this->invisibleRootItem(); this->root = this->invisibleRootItem();
@ -94,81 +122,66 @@ bool MapGroupModel::dropMimeData(const QMimeData *data, Qt::DropAction action, i
firstRow++; firstRow++;
} }
// updateProject();
emit dragMoveCompleted(); emit dragMoveCompleted();
updateProject();
return false; return false;
} }
void MapGroupModel::updateProject() {
if (!this->project) return;
/*
QStringList groupNames; QStringList groupNames;
QMap<QString, int> mapGroups; QMap<QString, int> mapGroups;
QList<QStringList> groupedMapNames; QList<QStringList> groupedMapNames;
QStringList mapNames; 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++) { for (int g = 0; g < this->root->rowCount(); g++) {
QStandardItem *groupItem = this->item(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++) { for (int m = 0; m < groupItem->rowCount(); m++) {
//
QStandardItem *mapItem = groupItem->child(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<QStringList> maps; this->project->groupNames = groupNames;
for (auto mapName : this->mapItems.keys()) { this->project->mapGroups = mapGroups;
// this->project->groupedMapNames = groupedMapNames;
QStandardItem *mapItem = this->mapItems[mapName]; this->project->mapNames = mapNames;
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() << "]";
}
} }
QStandardItem *MapGroupModel::createGroupItem(QString groupName, int groupIndex) { QStandardItem *MapGroupModel::createGroupItem(QString groupName, int groupIndex) {
QStandardItem *group = new QStandardItem; QStandardItem *group = new QStandardItem;
group->setText(groupName); group->setText(groupName);
group->setEditable(true);
group->setData(groupName, Qt::UserRole); group->setData(groupName, Qt::UserRole);
group->setData("map_group", MapListRoles::TypeRole); group->setData("map_group", MapListRoles::TypeRole);
group->setData(groupIndex, MapListRoles::GroupRole); 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); this->groupItems.insert(groupName, group);
return group; return group;
} }
QStandardItem *MapGroupModel::createMapItem(QString mapName, QStandardItem *map) { QStandardItem *MapGroupModel::createMapItem(QString mapName, QStandardItem *map) {
if (!map) map = new QStandardItem; if (!map) map = new QStandardItem;
map->setEditable(false);
map->setData(mapName, Qt::UserRole); map->setData(mapName, Qt::UserRole);
map->setData("map_name", MapListRoles::TypeRole); map->setData("map_name", MapListRoles::TypeRole);
// map->setData(groupIndex, MapListRoles::GroupRole);
map->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled); map->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
this->mapItems[mapName] = map; this->mapItems[mapName] = map;
return map; return map;
} }
QStandardItem *MapGroupModel::insertMapItem(QString mapName, QString groupName) { QStandardItem *MapGroupModel::insertMapItem(QString mapName, QString groupName) {
//int groupIndex = this->project->groupNames.indexOf(groupName);
QStandardItem *group = this->groupItems[groupName]; QStandardItem *group = this->groupItems[groupName];
if (!group) { if (!group) {
return nullptr; return nullptr;
} }
//int mapIndex = group->rowCount();
QStandardItem *map = createMapItem(mapName); QStandardItem *map = createMapItem(mapName);
group->appendRow(map); group->appendRow(map);
return map; return map;
@ -181,12 +194,10 @@ void MapGroupModel::initialize() {
QString group_name = this->project->groupNames.value(i); QString group_name = this->project->groupNames.value(i);
QStandardItem *group = createGroupItem(group_name, i); QStandardItem *group = createGroupItem(group_name, i);
root->appendRow(group); root->appendRow(group);
//this->setItem(0, i, group);
QStringList names = this->project->groupedMapNames.value(i); QStringList names = this->project->groupedMapNames.value(i);
for (int j = 0; j < names.length(); j++) { for (int j = 0; j < names.length(); j++) {
QString map_name = names.value(j); QString map_name = names.value(j);
QStandardItem *map = createMapItem(map_name); QStandardItem *map = createMapItem(map_name);
//this->setItem(i, j, map);
group->appendRow(map); group->appendRow(map);
} }
} }
@ -256,15 +267,31 @@ QVariant MapGroupModel::data(const QModelIndex &index, int role) const {
else if (role == Qt::DisplayRole) { else if (role == Qt::DisplayRole) {
// //
QStandardItem *item = this->getItem(index)->child(row, col); 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(); 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); 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) { 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 *MapAreaModel::createMapItem(QString mapName, int groupIndex, int mapIndex) {
QStandardItem *map = new QStandardItem; 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->setEditable(false);
map->setData(mapName, Qt::UserRole); map->setData(mapName, Qt::UserRole);
map->setData("map_name", MapListRoles::TypeRole); map->setData("map_name", MapListRoles::TypeRole);