Skip to content

Commit

Permalink
i give up
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies committed Nov 29, 2019
1 parent 6e8d5e6 commit 5a3b3f9
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 88 deletions.
7 changes: 6 additions & 1 deletion Editors/BaseEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ BaseEditor::BaseEditor(ProtoModel* treeNodeModel, QWidget* parent)
: QWidget(parent), nodeMapper(new ModelMapper(treeNodeModel, this)) {
buffers::TreeNode* n = static_cast<buffers::TreeNode*>(treeNodeModel->GetBuffer());
resMapper = new ModelMapper(treeNodeModel->GetSubModel(ResTypeFields[n->type_case()]), this);
modelBackup = resMapper->GetModel()->Copy(nullptr);
}

BaseEditor::~BaseEditor() {
delete modelBackup;
}

void BaseEditor::closeEvent(QCloseEvent* event) {
Expand All @@ -20,7 +25,7 @@ void BaseEditor::closeEvent(QCloseEvent* event) {
return;
} else if (reply == QMessageBox::No) {
nodeMapper->clearMapping();
resMapper->RestoreBuffer();
resMapper->ReplaceBuffer(modelBackup->GetBuffer());
resMapper->clearMapping();
}
}
Expand Down
6 changes: 4 additions & 2 deletions Editors/BaseEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class BaseEditor : public QWidget {

public:
explicit BaseEditor(ProtoModel *treeNodeModel, QWidget *parent);
~BaseEditor();

virtual void closeEvent(QCloseEvent *event);
void ReplaceBuffer(google::protobuf::Message *buffer);
Expand All @@ -34,8 +35,9 @@ class BaseEditor : public QWidget {
void OnSave();

protected:
ModelMapper *nodeMapper;
ModelMapper *resMapper;
ModelMapper* nodeMapper;
ModelMapper* resMapper;
ProtoModel* modelBackup;
};

#endif // BASEEDTIOR_H
4 changes: 3 additions & 1 deletion Editors/RoomEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ RoomEditor::RoomEditor(ProtoModel* model, QWidget* parent) : BaseEditor(model, p
[=](int index) { viewMapper->setCurrentIndex(index); });

RepeatedProtoModel* m = roomModel->GetRepeatedSubModel(Room::kInstancesFieldNumber);
ui->layersAssetsView->setModel(m);
QSortFilterProxyModel* mp = new QSortFilterProxyModel(this);
mp->setSourceModel(m);
ui->layersAssetsView->setModel(mp);
connect(ui->layersAssetsView->selectionModel(), &QItemSelectionModel::selectionChanged,
[=](const QItemSelection& selected, const QItemSelection& /*deselected*/) {
if (selected.empty()) return;
Expand Down
2 changes: 1 addition & 1 deletion Editors/RoomEditor.ui
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<bool>false</bool>
</property>
<property name="sortingEnabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="expandsOnDoubleClick">
<bool>false</bool>
Expand Down
4 changes: 1 addition & 3 deletions Models/ModelMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Editors/BaseEditor.h"

ModelMapper::ModelMapper(ProtoModel *model, BaseEditor *parent) : model(model) {
ModelMapper::ModelMapper(ProtoModel *model, BaseEditor *parent) : parentWidget(parent), model(model) {
mapper = new ImmediateDataWidgetMapper(parent);
mapper->setOrientation(Qt::Vertical);
mapper->setModel(model);
Expand All @@ -21,8 +21,6 @@ void ModelMapper::toFirst() { mapper->toFirst(); }

// model

void ModelMapper::RestoreBuffer() { model->RestoreBuffer(); }

void ModelMapper::ReplaceBuffer(google::protobuf::Message *buffer) { model->ReplaceBuffer(buffer); }

void ModelMapper::SetDirty(bool dirty) { model->SetDirty(dirty); }
Expand Down
8 changes: 4 additions & 4 deletions Models/ModelMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ class ModelMapper {
ModelMapper(ProtoModel *model, BaseEditor* parent);

// mapper
void addMapping(QWidget * widget, int section);
void addMapping(QWidget* widget, int section);
void clearMapping();
void toFirst();

// model
ProtoModel* GetModel();
void RestoreBuffer();
void ReplaceBuffer(google::protobuf::Message *buffer);
void SetDirty(bool dirty);
bool IsDirty();
Expand All @@ -37,8 +36,9 @@ class ModelMapper {
Qt::ItemFlags flags(const QModelIndex &index) const;

protected:
ProtoModel *model;
ImmediateDataWidgetMapper *mapper;
BaseEditor* parentWidget;
ProtoModel* model;
ImmediateDataWidgetMapper* mapper;
};

#endif // MODELMAPPER_H
70 changes: 58 additions & 12 deletions Models/ProtoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ ProtoModel::ProtoModel(Message *protobuf, QObject *parent)
emit QAbstractItemModel::dataChanged(topLeft, bottomRight, roles);
});

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

const Descriptor *desc = protobuf->GetDescriptor();
const Reflection *refl = protobuf->GetReflection();
for (int i = 0; i < desc->field_count(); i++) {
Expand Down Expand Up @@ -52,20 +49,20 @@ ProtoModel::ProtoModel(Message *protobuf, QObject *parent)
}
}

ProtoModel::~ProtoModel() { delete protobufBackup; }
ProtoModel::~ProtoModel() {}

ProtoModel* ProtoModel::Copy(QObject *parent) {
google::protobuf::Message *protobufCopy = protobuf->New();
protobufCopy->CopyFrom(*protobuf);
return new ProtoModel(protobufCopy, parent);
}

void ProtoModel::ReplaceBuffer(Message *buffer) {
SetDirty(true);
protobuf->CopyFrom(*buffer);
emit dataChanged(index(0), index(rowCount()));
}

void ProtoModel::RestoreBuffer() {
SetDirty(false);
protobuf->CopyFrom(*protobufBackup);
emit dataChanged(index(0), index(rowCount()));
}

google::protobuf::Message *ProtoModel::GetBuffer() { return protobuf; }

int ProtoModel::rowCount(const QModelIndex & /*parent*/) const {
Expand Down Expand Up @@ -181,11 +178,14 @@ ProtoModel *ProtoModel::GetSubModel(int fieldNum) {
}

ProtoModel *ProtoModel::GetSubModel(int fieldNum, int index) {
return static_cast<ProtoModel *>(repeatedMessages[fieldNum][index].value<void *>());
if (repeatedMessages.contains(fieldNum) && repeatedMessages[fieldNum].count() > index)
return static_cast<ProtoModel *>(repeatedMessages[fieldNum][index].value<void *>());
else
return nullptr;
}

QString ProtoModel::GetString(int fieldNum, int index) const {
if (repeatedMessages.contains(fieldNum) && repeatedMessages[fieldNum].size() > index)
if (repeatedMessages.contains(fieldNum) && repeatedMessages[fieldNum].count() > index)
return repeatedMessages[fieldNum][index].toString();
else
return "";
Expand Down Expand Up @@ -215,3 +215,49 @@ Qt::ItemFlags ProtoModel::flags(const QModelIndex &index) const {
if (index.row() > 0) flags |= Qt::ItemIsEditable;
return flags;
}

void UpdateReferences(ProtoModel* model, const QString& type, const QString& oldName, const QString& newName) {

if (model == nullptr) return;

int rows = model->rowCount();
for (int row = 0; row < rows; row++) {

google::protobuf::Message* protobuf = model->GetBuffer();

const Descriptor *desc = protobuf->GetDescriptor();
const Reflection *refl = protobuf->GetReflection();
const FieldDescriptor *field = desc->FindFieldByNumber(row);
if (field != nullptr) {
if (field->cpp_type() == CppType::CPPTYPE_MESSAGE) {
if (field->is_repeated()) {
int cols = model->GetRepeatedSubModel(row)->columnCount();
for (int col = 0; col < cols; col++) {
UpdateReferences(model->GetSubModel(row, col), type, oldName, newName);
}
} else UpdateReferences(model->GetSubModel(row), type, oldName, newName);
} else if (field->cpp_type() == CppType::CPPTYPE_STRING && !field->is_repeated()) {
const QString refType = QString::fromStdString(field->options().GetExtension(buffers::resource_ref));
if (refType == type && model->data(row, 0).toString() == oldName) {
//qDebug() << "renamed " << oldName << " to " << newName;
model->setData(model->index(row, 0, QModelIndex()), newName, Qt::DisplayRole);
}
}
}
}
}

QString ResTypeAsString(TypeCase type) {
switch (type) {
case TypeCase::kFolder: return "treenode";
case TypeCase::kBackground: return "background";
case TypeCase::kFont: return "font";
case TypeCase::kObject: return "object";
case TypeCase::kPath: return "path";
case TypeCase::kRoom: return "room";
case TypeCase::kSound: return "sound";
case TypeCase::kSprite: return "sprite";
case TypeCase::kTimeline: return "timeline";
}
return "unknown";
}
6 changes: 4 additions & 2 deletions Models/ProtoModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class ProtoModel : public QAbstractItemModel {
explicit ProtoModel(google::protobuf::Message *protobuf, QObject *parent);
~ProtoModel();

ProtoModel* Copy(QObject *parent);
void ReplaceBuffer(google::protobuf::Message *buffer);
void RestoreBuffer();
google::protobuf::Message *GetBuffer();
void SetDirty(bool dirty);
bool IsDirty();
Expand Down Expand Up @@ -55,7 +55,9 @@ class ProtoModel : public QAbstractItemModel {
QHash<int, RepeatedProtoModel*> repeatedModels;
bool dirty;
google::protobuf::Message *protobuf;
google::protobuf::Message *protobufBackup;
};

void UpdateReferences(ProtoModel* model, const QString& type, const QString& oldName, const QString& newName);
QString ResTypeAsString(TypeCase type);

#endif // RESOURCEMODEL_H
11 changes: 11 additions & 0 deletions Models/RepeatedProtoModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "ProtoModel.h"
#include "ResourceModelMap.h"

#include <google/protobuf/repeated_field.h>
#include <google/protobuf/reflection.h>

RepeatedProtoModel::RepeatedProtoModel(Message *protobuf, const FieldDescriptor *field, ProtoModel *parent)
: QAbstractItemModel(parent), protobuf(protobuf), field(field) {}

Expand Down Expand Up @@ -70,3 +73,11 @@ Qt::ItemFlags RepeatedProtoModel::flags(const QModelIndex &index) const {
if (!index.isValid()) return nullptr;
return QAbstractItemModel::flags(index);
}

bool RepeatedProtoModel::removeRows(int row, int count, const QModelIndex &parent) {
const Reflection *refl = protobuf->GetReflection();
auto f = refl->GetMutableRepeatedFieldRef<Message>(protobuf, field);
f->erase(f.begin()+row, f.begin()+row+count);
emit dataChanged(index(0), index(rowCount()));
return true;
}
1 change: 1 addition & 0 deletions Models/RepeatedProtoModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class RepeatedProtoModel : public QAbstractItemModel {
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;

signals:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVariant &oldValue = QVariant(0),
Expand Down
12 changes: 11 additions & 1 deletion Models/ResourceModelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "Editors/BaseEditor.h"
#include "MainWindow.h"

#include <QDebug>

ResourceModelMap::ResourceModelMap(buffers::TreeNode* root, QObject* parent) : QObject(parent) {
recursiveBindRes(root, this);
}
Expand All @@ -22,7 +24,7 @@ void ResourceModelMap::AddResource(buffers::TreeNode* child, QObject* parent) {
QPair<buffers::TreeNode*, ProtoModel*>(child, child->has_folder() ? nullptr : new ProtoModel(child, parent));
}

void ResourceModelMap::RemoveResource(int type, const QString& name) {
void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) {
if (!_resources.contains(type)) return;
if (!_resources[type].contains(name)) return;
delete _resources[type][name].second;
Expand Down Expand Up @@ -58,9 +60,17 @@ ProtoModel* ResourceModelMap::GetResourceByName(int type, const std::string& nam
return GetResourceByName(type, QString::fromStdString(name));
}


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];

for (auto res : _resources) {
for (auto model : res) {
UpdateReferences(model.second, ResTypeAsString(type), oldName, newName);
}
}

_resources[type].remove(oldName);
}

Expand Down
2 changes: 1 addition & 1 deletion Models/ResourceModelMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ResourceModelMap : public QObject {
ProtoModel* GetResourceByName(int type, const QString& name);
ProtoModel* GetResourceByName(int type, const std::string& name);
void AddResource(buffers::TreeNode* node, QObject* parent);
void RemoveResource(int type, const QString& name);
void RemoveResource(TypeCase type, const QString& name);
QString CreateResourceName(TreeNode* node);
QString CreateResourceName(int type, const QString& typeName);

Expand Down
Loading

0 comments on commit 5a3b3f9

Please sign in to comment.