Skip to content

Commit

Permalink
broke ass templates
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies committed Jan 15, 2020
1 parent 77beab6 commit bda43a4
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 803 deletions.
3 changes: 2 additions & 1 deletion Editors/BaseEditor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "BaseEditor.h"
#include "Models/MessageModel.h"

#include <QCloseEvent>
#include <QDebug>
Expand All @@ -7,7 +8,7 @@
BaseEditor::BaseEditor(ProtoModelPtr treeNodeModel, QWidget* parent)
: QWidget(parent), nodeMapper(new ModelMapper(treeNodeModel, this)), _model(treeNodeModel) {
buffers::TreeNode* n = static_cast<buffers::TreeNode*>(treeNodeModel->GetBuffer());
resMapper = new ModelMapper(treeNodeModel->GetSubModel(ResTypeFields[n->type_case()]), this);
resMapper = new ModelMapper(treeNodeModel->GetSubModel<MessageModel*>(ResTypeFields[n->type_case()]), this);

// Backup should be deleted by Qt's garbage collector when this editor is closed
resMapper->GetModel()->BackupModel(this);
Expand Down
3 changes: 2 additions & 1 deletion Editors/FontEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define FONTEDITOR_H

#include "BaseEditor.h"
#include "Models/RepeatedMessageModel.h"

#include <QFont>
#include <QItemSelection>
Expand Down Expand Up @@ -36,7 +37,7 @@ class FontEditor : public BaseEditor {
void UpdateRangeText(int min, int max);
Ui::FontEditor* ui;
QFont font;
RepeatedProtoModelPtr rangesModel;
RepeatedMessageModel* rangesModel;
};

#endif // FONTEDITOR_H
2 changes: 1 addition & 1 deletion Editors/SpriteEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void SpriteEditor::RebindSubModels() {
ui->subImageList->setGridSize(_subimagesModel->GetIconSize());

ui->subimagePreview->SetResourceModel(_spriteModel);
connect(_subimagesModel, &SpriteSubimageModel::MismatchedImageSize, this, &SpriteEditor::LoadedMismatchedImage);
connect(_subimagesModel, &RepeatedImageModel::MismatchedImageSize, this, &SpriteEditor::LoadedMismatchedImage);

connect(ui->subImageList->selectionModel(), &QItemSelectionModel::selectionChanged, this,
&SpriteEditor::SelectionChanged);
Expand Down
4 changes: 2 additions & 2 deletions Editors/SpriteEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SPRITEEDITOR_H

#include "BaseEditor.h"
#include "Models/SpriteSubimageModel.h"
#include "Models/RepeatedImageModel.h"

#include <QItemSelection>

Expand Down Expand Up @@ -41,7 +41,7 @@ class SpriteEditor : public BaseEditor {
private:
Ui::SpriteEditor* ui;
ProtoModel* _spriteModel;
SpriteSubimageModelPtr _subimagesModel;
RepeatedImageModel* _subimagesModel;
};

#endif // SPRITEEDITOR_H
138 changes: 138 additions & 0 deletions Models/MessageModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include "MessageModel.h"
#include "Components/Logger.h"

#include <google/protobuf/descriptor.h>

using CppType = FieldDescriptor::CppType;

void MessageModel::RebuildSubModels() {
const Descriptor *desc = _protobuf->GetDescriptor();
const Reflection *refl = _protobuf->GetReflection();
for (int i = 0; i < desc->field_count(); i++) {
const FieldDescriptor *field = desc->field(i);

if (field->cpp_type() == CppType::CPPTYPE_MESSAGE) {
if (field->is_repeated()) {
//subModels.repeatedModels[field->number()] = new RepeatedProtoModel(protobuf, field, this);
} else {
const OneofDescriptor *oneof = field->containing_oneof();
if (oneof) {
if (refl->HasOneof(*_protobuf, oneof)) {
field = refl->GetOneofFieldDescriptor(*_protobuf, oneof);
if (field->cpp_type() != CppType::CPPTYPE_MESSAGE) continue; // is prolly folder
} else {
continue; // don't allocate if not set
}
}
//subModels.protoModels[field->number()] = new ProtoModel(refl->MutableMessage(protobuf, field), this);
}
} else if (field->cpp_type() == CppType::CPPTYPE_STRING && field->is_repeated()) {
if (field->name() == "subimages")
;
//subModels.repeatedStringModels[field->number()] =
//new SpriteSubimageModel(refl->GetMutableRepeatedFieldRef<std::string>(_protobuf, field), field, this);
//else
//subModels.repeatedStringModels[field->number()] =
//new RepeatedStringModel(refl->GetMutableRepeatedFieldRef<std::string>(_protobuf, field), field, this);
}
}
}

int MessageModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid()) return 0;
const Descriptor *desc = _protobuf->GetDescriptor();
return desc->field_count();
}

int MessageModel::columnCount(const QModelIndex &parent) const {
return 1; //TODO:
}

bool MessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
R_EXPECT(index.isValid(), false) << "Supplied index was invalid:" << index;

const Descriptor *desc = _protobuf->GetDescriptor();
const Reflection *refl = _protobuf->GetReflection();
const FieldDescriptor *field = desc->FindFieldByNumber(index.row());
if (!field) return false;

const QVariant oldValue = this->data(index, role);

switch (field->cpp_type()) {
case CppType::CPPTYPE_MESSAGE: {
break;
}
case CppType::CPPTYPE_INT32: refl->SetInt32(_protobuf, field, value.toInt()); break;
case CppType::CPPTYPE_INT64: refl->SetInt64(_protobuf, field, value.toLongLong()); break;
case CppType::CPPTYPE_UINT32: refl->SetUInt32(_protobuf, field, value.toUInt()); break;
case CppType::CPPTYPE_UINT64: refl->SetUInt64(_protobuf, field, value.toULongLong()); break;
case CppType::CPPTYPE_DOUBLE: refl->SetDouble(_protobuf, field, value.toDouble()); break;
case CppType::CPPTYPE_FLOAT: refl->SetFloat(_protobuf, field, value.toFloat()); break;
case CppType::CPPTYPE_BOOL: refl->SetBool(_protobuf, field, value.toBool()); break;
case CppType::CPPTYPE_ENUM:
refl->SetEnum(_protobuf, field, field->enum_type()->FindValueByNumber(value.toInt()));
break;
case CppType::CPPTYPE_STRING: refl->SetString(_protobuf, field, value.toString().toStdString()); break;
}

SetDirty(true);
emit dataChanged(index, index, oldValue);
ParentDataChanged();

return true;
}

QVariant MessageModel::data(const QModelIndex &index, int role) const {
R_EXPECT(index.isValid(), QVariant()) << "Supplied index was invalid:" << index;
if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();

const Descriptor *desc = _protobuf->GetDescriptor();
const Reflection *refl = _protobuf->GetReflection();
const FieldDescriptor *field = desc->FindFieldByNumber(index.row());

if (!field) {
// Proto fields always start at one. So this bit of a hack for displaying data in a table
if (index.row() == 0 && role == Qt::DisplayRole) {
return tr("Value");
}
return QVariant();
}

// If the field has't been initialized return an invalid QVariant. (see QVariant.isValid())
if (!refl->HasField(*_protobuf, field)) return QVariant();

switch (field->cpp_type()) {
case CppType::CPPTYPE_MESSAGE: R_EXPECT(false, QVariant()) << "The requested field " << index << " is a message";
case CppType::CPPTYPE_INT32: return refl->GetInt32(*_protobuf, field);
case CppType::CPPTYPE_INT64: return static_cast<long long>(refl->GetInt64(*_protobuf, field));
case CppType::CPPTYPE_UINT32: return refl->GetUInt32(*_protobuf, field);
case CppType::CPPTYPE_UINT64: return static_cast<unsigned long long>(refl->GetUInt64(*_protobuf, field));
case CppType::CPPTYPE_DOUBLE: return refl->GetDouble(*_protobuf, field);
case CppType::CPPTYPE_FLOAT: return refl->GetFloat(*_protobuf, field);
case CppType::CPPTYPE_BOOL: return refl->GetBool(*_protobuf, field);
case CppType::CPPTYPE_ENUM: return refl->GetEnumValue(*_protobuf, field);
case CppType::CPPTYPE_STRING: return QString::fromStdString(refl->GetString(*_protobuf, field));
}

return QVariant();
}

QModelIndex MessageModel::parent(const QModelIndex & /*index*/) const { return QModelIndex(); }

QVariant MessageModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const {
if (role != Qt::DisplayRole) return QVariant();

// Proto fields always start at one. So this bit of a hack for displaying data in a table
if (section == 0) return tr("Property");

const Descriptor *desc = _protobuf->GetDescriptor();
const FieldDescriptor *field = desc->FindFieldByNumber(section);

if (field != nullptr) return QString::fromStdString(field->name());

return "";
}

QModelIndex MessageModel::index(int row, int column, const QModelIndex & /*parent*/) const {
return this->createIndex(row, column, static_cast<void *>(_protobuf));
}
23 changes: 23 additions & 0 deletions Models/MessageModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MESSAGEMODEL_H
#define MESSAGEMODEL_H

#include "ProtoModel.h"

// Model representing a protobuf message
class MessageModel : public ProtoModel {
public:
MessageModel(ProtoModelPtr parent);
// On either intialization or restore of a model all
// refrences to to the submodels it owns recursively must be updated
void RebuildSubModels();

virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override = 0;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override = 0;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override = 0;
virtual QVariant data(const QModelIndex &index, int role) const override = 0;
virtual QModelIndex parent(const QModelIndex &index) const override = 0;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override = 0;
virtual QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override = 0;
};

#endif
10 changes: 0 additions & 10 deletions Models/ModelMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,8 @@ bool ModelMapper::setData(const QModelIndex &index, const QVariant &value, int r
return model->setData(index, value, role);
}

QVariant ModelMapper::data(int row, int column) const { return model->data(row, column); }

QVariant ModelMapper::data(const QModelIndex &index, int role) const { return model->data(index, role); }

RepeatedProtoModelPtr ModelMapper::GetRepeatedSubModel(int fieldNum) { return model->GetRepeatedSubModel(fieldNum); }

RepeatedStringModelPtr ModelMapper::GetRepeatedStringSubModel(int fieldNum) {
return model->GetRepeatedStringSubModel(fieldNum);
}

ProtoModelPtr ModelMapper::GetSubModel(int fieldNum) { return model->GetSubModel(fieldNum); }

QModelIndex ModelMapper::parent(const QModelIndex &index) const { return model->parent(index); }

QVariant ModelMapper::headerData(int section, Qt::Orientation orientation, int role) const {
Expand Down
8 changes: 4 additions & 4 deletions Models/ModelMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class ModelMapper : public QObject {
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
QVariant data(int row, int column = 0) const;
//QVariant data(int row, int column = 0) const;
QVariant data(const QModelIndex &index, int role) const;
RepeatedProtoModelPtr GetRepeatedSubModel(int fieldNum);
RepeatedStringModelPtr GetRepeatedStringSubModel(int fieldNum);
ProtoModelPtr GetSubModel(int fieldNum);
//RepeatedProtoModelPtr GetRepeatedSubModel(int fieldNum);
//RepeatedStringModelPtr GetRepeatedStringSubModel(int fieldNum);
//ProtoModelPtr GetSubModel(int fieldNum);
QModelIndex parent(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const;
Expand Down

0 comments on commit bda43a4

Please sign in to comment.