Skip to content

Commit

Permalink
Resource Renaming Fix (#46)
Browse files Browse the repository at this point in the history
* Removed the lambda that listens to `QAbstractItemModel::dataChanged` signal of `treeModel` to update the subwindow title for opened editors.
* Changed the `BaseEditor::dataChanged` slot to update the subwindow title.
* Changed `MainWindow::openSubWindow` to set the subwindow title directly on the editor widget that is used to create the subwindow. This stops the subwindow title from overriding the one set in the `BaseEditor::dataChanged` slot.
* Changed `TreeModel::setData` to compare the old and new name and short-circuit without firing any signals if they are the same.
* Added a `ResourceRenamed` signal to `TreeModel` that has the same signature as the signal emitted by `BaseEditor`.
* Changed `ResourceModelMap::ResourceRenamed` slot to compare the old and new name and short-circuit if they are the same or if there was no entry for a resource under the old name. Also made it remove the entry under the old name instead of setting it to a `nullptr` which should be safer and keep the map from growing unnecessarily large.
* Connected the `treeModel` to `resMap` so the latter is informed of when resources are renamed directly from the tree via an edit trigger. This fixes a crash that occurs when attempting to open the editor of a resource after just having renamed it in the tree.
* Connected the `treeModel` to `res` in `MainWindow::openSubWindow` so `res` knows when the resource is renamed from the tree. This ensures the subwindow title is updated when the resource is renamed from the tree.
* Changed `ProtoModel` constructor to forward its `dataChanged` signal that provides the old value as the `QAbstractItemModel` signal by the same name which doesn't. This ensures that components, like ImmediateMapper, that listen for the old signal will still receive the signal even if they don't care about the optional parameter.
  • Loading branch information
RobertBColton authored Nov 13, 2018
1 parent 67bbf72 commit cab1e51
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
1 change: 1 addition & 0 deletions Editors/BaseEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void BaseEditor::dataChanged(const QModelIndex& topLeft, const QModelIndex& /*bo
const QVector<int>& /*roles*/) {
buffers::TreeNode* n = static_cast<buffers::TreeNode*>(nodeMapper->GetModel()->GetBuffer());
if (n == topLeft.internalPointer() && topLeft.row() == TreeNode::kNameFieldNumber) {
this->setWindowTitle(QString::fromStdString(n->name()));
emit ResourceRenamed(n->type_case(), oldValue.toString(), QString::fromStdString(n->name()));
}
}
21 changes: 8 additions & 13 deletions MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ void MainWindow::openSubWindow(buffers::TreeNode *item) {

connect(editor, &BaseEditor::ResourceRenamed, resourceMap.get(), &ResourceModelMap::ResourceRenamed);
connect(editor, &BaseEditor::ResourceRenamed, [=]() { treeModel->dataChanged(QModelIndex(), QModelIndex()); });
connect(treeModel.get(), &TreeModel::ResourceRenamed, editor,
[res](TypeCase /*type*/, const QString & /*oldName*/, const QString & /*newName*/) {
const QModelIndex index = res->index(TreeNode::kNameFieldNumber);
emit res->dataChanged(index, index);
});

subWindow = subWindows[item] = ui->mdiArea->addSubWindow(editor);
subWindow->resize(subWindow->frameSize().expandedTo(editor->size()));
Expand All @@ -150,7 +155,7 @@ void MainWindow::openSubWindow(buffers::TreeNode *item) {
subWindow->connect(subWindow, &QObject::destroyed, [=]() { subWindows.remove(item); });

subWindow->setWindowIcon(subWindow->widget()->windowIcon());
subWindow->setWindowTitle(QString::fromStdString(item->name()));
editor->setWindowTitle(QString::fromStdString(item->name()));
} else {
subWindow = *swIt;
}
Expand Down Expand Up @@ -231,18 +236,8 @@ void MainWindow::openProject(std::unique_ptr<buffers::Project> openedProject) {
treeModel.reset(new TreeModel(project->mutable_game()->mutable_root(), nullptr));

ui->treeView->setModel(treeModel.get());
treeModel->connect(treeModel.get(), &QAbstractItemModel::dataChanged,
[=](const QModelIndex &topLeft, const QModelIndex &bottomRight) {
for (int row = topLeft.row(); row <= bottomRight.row(); ++row) {
for (int column = topLeft.column(); column <= bottomRight.column(); ++column) {
auto index = topLeft.sibling(row, column);
buffers::TreeNode *item = static_cast<buffers::TreeNode *>(index.internalPointer());
if (!subWindows.contains(item)) return;
auto subWindow = subWindows[item];
subWindow->setWindowTitle(QString::fromStdString(item->name()));
}
}
});
treeModel->connect(treeModel.get(), &TreeModel::ResourceRenamed, resourceMap.get(),
&ResourceModelMap::ResourceRenamed);
}

void MainWindow::on_actionNew_triggered() { openNewProject(); }
Expand Down
6 changes: 6 additions & 0 deletions Models/ProtoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ using CppType = FieldDescriptor::CppType;

ProtoModel::ProtoModel(Message *protobuf, QObject *parent)
: QAbstractItemModel(parent), dirty(false), protobuf(protobuf) {
connect(this, &ProtoModel::dataChanged, this,
[this](const QModelIndex &topLeft, const QModelIndex &bottomRight,
const QVariant & /*oldValue*/ = QVariant(0), const QVector<int> &roles = QVector<int>()) {
emit QAbstractItemModel::dataChanged(topLeft, bottomRight, roles);
});

protobufBackup = protobuf->New();
protobufBackup->CopyFrom(*protobuf);

Expand Down
3 changes: 2 additions & 1 deletion Models/ResourceModelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ ProtoModel* ResourceModelMap::GetResourceByName(int type, const std::string& nam
}

void ResourceModelMap::ResourceRenamed(TypeCase type, const QString& oldName, const QString& newName) {
if (oldName == newName || !_resources[type].contains(oldName)) return;
_resources[type][newName] = _resources[type][oldName];
_resources[type][oldName] = nullptr;
_resources[type].remove(oldName);
}

const ProtoModel* GetObjectSprite(const std::string& objName) {
Expand Down
6 changes: 5 additions & 1 deletion Models/TreeModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ int TreeModel::columnCount(const QModelIndex & /*parent*/) const { return 1; }
bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid() || role != Qt::EditRole) return false;
buffers::TreeNode *item = static_cast<buffers::TreeNode *>(index.internalPointer());
item->set_name(value.toString().toStdString());
const QString oldName = QString::fromStdString(item->name());
const QString newName = value.toString();
if (oldName == newName) return true;
item->set_name(newName.toStdString());
emit ResourceRenamed(item->type_case(), oldName, value.toString());
emit dataChanged(index, index);
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions Models/TreeModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class TreeModel : public QAbstractItemModel {

void addNode(buffers::TreeNode *child, buffers::TreeNode *parent);

signals:
void ResourceRenamed(TypeCase type, const QString &oldName, const QString &newName);

private:
buffers::TreeNode *root;
QHash<buffers::TreeNode *, buffers::TreeNode *> parents;
Expand Down

0 comments on commit cab1e51

Please sign in to comment.