Skip to content

Commit

Permalink
less broke but still broke
Browse files Browse the repository at this point in the history
  • Loading branch information
fundies committed Jan 17, 2020
1 parent 1140fd0 commit 2eb3e42
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Editors/CodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "Widgets/StackedCodeWidget.h"
#include "ui_CodeEditor.h"

CodeEditor::CodeEditor(QWidget* parent, bool removeSaveBtn) : ui(new Ui::CodeEditor) {
CodeEditor::CodeEditor(QWidget* /*parent*/, bool removeSaveBtn) : ui(new Ui::CodeEditor) {
ui->setupUi(this);

if (removeSaveBtn) ui->mainToolBar->removeAction(ui->actionSave);
Expand Down
4 changes: 2 additions & 2 deletions Editors/FontEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ void FontEditor::UpdateRangeText(int min, int max) {

void FontEditor::on_deleteRangeButton_pressed() {
{ // so our remover gets deleted
//RepeatedMessageModel::RowRemovalOperation remover(_rangesModel);
//remover.RemoveRow(_ui->rangeTableView->selectionModel()->selectedRows().first().row());
RepeatedMessageModel::RowRemovalOperation remover(_rangesModel);
remover.RemoveRow(_ui->rangeTableView->selectionModel()->selectedRows().first().row());
}

if (_rangesModel->rowCount() == 0) {
Expand Down
4 changes: 2 additions & 2 deletions Editors/PathEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ void PathEditor::on_insertPointButton_pressed() {
void PathEditor::on_deletePointButton_pressed() {
int deleteIndex = _ui->pointsTableView->selectionModel()->currentIndex().row();
{
//RepeatedProtoModel::RowRemovalOperation remover(_pointsModel);
//remover.RemoveRow(deleteIndex);
RepeatedMessageModel::RowRemovalOperation remover(_pointsModel);
remover.RemoveRow(deleteIndex);
}

if (_pointsModel->rowCount() > 0) {
Expand Down
4 changes: 2 additions & 2 deletions Editors/TimelineEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ void TimelineEditor::ChangeMoment(int oldIndex, int step) {
}

void TimelineEditor::RemoveMoment(int modelIndex) {
//RepeatedProtoModel::RowRemovalOperation remover(_momentsModel);
//remover.RemoveRow(modelIndex);
RepeatedMessageModel::RowRemovalOperation remover(_momentsModel);
remover.RemoveRow(modelIndex);
_codeEditor->RemoveCodeWidget(modelIndex);
}

Expand Down
27 changes: 26 additions & 1 deletion Models/MessageModel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "MessageModel.h"
#include "Components/ArtManager.h"
#include "Components/Logger.h"
#include "MainWindow.h"
#include "RepeatedImageModel.h"
#include "RepeatedMessageModel.h"
#include "ResourceModelMap.h"
Expand Down Expand Up @@ -91,7 +93,7 @@ QVariant MessageModel::Data(int row, int column) const {

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();
if (role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::DecorationRole) return QVariant();

const Descriptor *desc = _protobuf->GetDescriptor();
const Reflection *refl = _protobuf->GetReflection();
Expand All @@ -105,6 +107,29 @@ QVariant MessageModel::data(const QModelIndex &index, int role) const {
return QVariant();
}

// These are for icons in things like the room's instance list
if (role == Qt::DecorationRole) {
const QString refType = QString::fromStdString(field->options().GetExtension(buffers::resource_ref));
if (refType == "object") {
MessageModel *sprModel = GetObjectSprite(data(index, Qt::DisplayRole).toString());
if (sprModel != nullptr) {
RepeatedImageModel *subImgs = sprModel->GetSubModel<RepeatedImageModel *>(Sprite::kSubimagesFieldNumber);
if (subImgs != nullptr && subImgs->rowCount() > 0) {
return ArtManager::GetIcon(subImgs->Data(0).toString());
}
}
} else if (refType == "background") {
MessageModel *bkgModel =
MainWindow::resourceMap->GetResourceByName(TreeNode::kBackground, data(index, Qt::DisplayRole).toString());
if (bkgModel != nullptr) {
bkgModel = bkgModel->GetSubModel<MessageModel *>(TreeNode::kBackgroundFieldNumber);
if (bkgModel != nullptr) return ArtManager::GetIcon(bkgModel->Data(Background::kImageFieldNumber).toString());
}
}

return QVariant();
}

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

Expand Down
22 changes: 20 additions & 2 deletions Models/RepeatedMessageModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,37 @@ RepeatedMessageModel::RepeatedMessageModel(ProtoModel *parent, Message *message,
}
}

void RepeatedMessageModel::Swap(int left, int right) { std::swap(_subModels[left], _subModels[right]); }

void RepeatedMessageModel::AppendNew() {
MessageModel *parent = GetParentModel<MessageModel *>();
Message *parentBuffer = parent->GetBuffer();
Message *m = parentBuffer->GetReflection()->AddMessage(parentBuffer, _field);
_subModels.append(new MessageModel(parent, m));
}

void RepeatedMessageModel::Resize(int newSize) { _subModels.resize(newSize); }

void RepeatedMessageModel::Clear() {
_fieldRef.Clear();
_subModels.clear();
}

QVariant RepeatedMessageModel::Data(int row, int column) const { return _subModels[row]->Data(column); }

bool RepeatedMessageModel::SetData(const QVariant &value, int row, int column) {
return _subModels[row]->SetData(value, column);
}

int RepeatedMessageModel::columnCount(const QModelIndex & /*parent*/) const {
return _field->message_type()->field_count();
}

bool RepeatedMessageModel::setData(const QModelIndex &index, const QVariant &value, int role) {
return _subModels[index.row()]->setData(_subModels[index.row()]->index(index.column()), value, role);
}

QVariant RepeatedMessageModel::data(const QModelIndex &index, int role) const {
//if (!index.isValid()) return QVariant();
return _subModels[index.row()]->data(_subModels[index.row()]->index(index.column()), role);
}

Expand All @@ -31,7 +50,6 @@ QVariant RepeatedMessageModel::headerData(int section, Qt::Orientation orientati
}

QModelIndex RepeatedMessageModel::index(int row, int column, const QModelIndex & /*parent*/) const {
qDebug() << "indezx: " << row;
return createIndex(row, column);
}

Expand Down
7 changes: 6 additions & 1 deletion Models/RepeatedMessageModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ class RepeatedMessageModel : public RepeatedModel<Message> {
return static_cast<T>(_subModels[index]);
}

virtual void Swap(int /*left*/, int /*right*/) override;
virtual void AppendNew() override;
virtual void Resize(int /*newSize*/) override;
virtual void Clear() override;

virtual QVariant Data(int row, int column) const override;
virtual bool SetData(const QVariant &value, int row, int column) override;

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

virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;

// TODO: implement dropping a message
Expand Down
38 changes: 19 additions & 19 deletions Models/RepeatedModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ class RepeatedModel : public ProtoModel {

// Used to apply changes to any underlying data structure if needed
virtual void Swap(int /*left*/, int /*right*/) {}
virtual void Append() {}
virtual void AppendNew() {}
virtual void Resize(int /*newSize*/) {}
virtual int Size() { return 0; }
virtual void Clear() { _fieldRef.Clear(); }

bool Empty() { return rowCount() == 0; }
Expand Down Expand Up @@ -53,12 +52,12 @@ class RepeatedModel : public ProtoModel {
}

virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override {
//if (!parent.isValid()) return 0;
Q_UNUSED(parent);
return _fieldRef.size();
}

virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override {
if (!parent.isValid()) return 0;
Q_UNUSED(parent);
return 1;
}

Expand All @@ -83,7 +82,7 @@ class RepeatedModel : public ProtoModel {

int p = rowCount();

Append();
AppendNew();

SwapBack(row, p, rowCount());
ParentDataChanged();
Expand All @@ -92,13 +91,13 @@ class RepeatedModel : public ProtoModel {
};

virtual bool removeRows(int position, int count, const QModelIndex & /*parent*/) override {
//RowRemovalOperation remover(this, _fieldRef);
//remover.RemoveRows(position, count);
RowRemovalOperation remover(this);
remover.RemoveRows(position, count);
return true;
}

// Mimedata stuff required for Drag & Drop and clipboard functions
virtual Qt::DropActions supportedDropActions() { return Qt::MoveAction | Qt::CopyAction; }
virtual Qt::DropActions supportedDropActions() const override { return Qt::MoveAction | Qt::CopyAction; }

virtual QMimeData *mimeData(const QModelIndexList & /*indexes*/) const override { return nullptr; }
virtual bool dropMimeData(const QMimeData * /*data*/, Qt::DropAction /*action*/, int /*row*/, int /*column*/,
Expand All @@ -107,7 +106,7 @@ class RepeatedModel : public ProtoModel {
}

virtual QStringList mimeTypes() const override {
return QStringList("RadialGM/"); /*QString::fromStdString(_field.name());*/
return QStringList("RadialGM/" + QString::fromStdString(_field->name()));
}

// Takes the elements in range [part, right) and move them to `left` by swapping.
Expand All @@ -123,13 +122,13 @@ class RepeatedModel : public ProtoModel {
SwapBack(left, left + npart, right);
}

/*class RowRemovalOperation {
class RowRemovalOperation {
std::set<int> _rows;
RepeatedModel<T> _model;
RepeatedModel<T> &_model;
MutableRepeatedFieldRef<T> _field;

public:
RowRemovalOperation(RepeatedModel<T> model, MutableRepeatedFieldRef<T> field) : _model(model), _field(field) {}
RowRemovalOperation(RepeatedModel<T> *model) : _model(*model), _field(model->GetfieldRef()) {}
void RemoveRow(int row) { _rows.insert(row); }
void RemoveRows(int row, int count) {
for (int i = row; i < row + count; ++i) _rows.insert(i);
Expand All @@ -154,22 +153,22 @@ class RepeatedModel : public ProtoModel {
}
}

emit _model->beginResetModel();
emit _model.beginResetModel();

// Basic dense range removal. Move "deleted" rows to the end of the array.
int left = 0, right = 0;
for (auto range : ranges) {
while (right < range.first) {
_field.SwapElements(left, right);
_model->Swap(left, right);
_model.Swap(left, right);
left++;
right++;
}
right = range.last + 1;
}
while (right < _field.size()) {
_field.SwapElements(left, right);
_model->Swap(left, right);
_model.Swap(left, right);
left++;
right++;
}
Expand All @@ -178,17 +177,18 @@ class RepeatedModel : public ProtoModel {
// correct number of rows incrementally, or else various components in Qt
// will bitch, piss, moan, wail, whine, and cry. Actually, they will anyway.
for (Range range : ranges) {
_model->resize(_model.Size() - range.size());
_model.Resize(_field.size() - range.size());
for (int j = range.first; j <= range.last; ++j) _field.RemoveLast();
}

emit _model->endResetModel();
emit _model.endResetModel();

_model->GetParentModel()->SetDirty(true);
_model.GetParentModel<ProtoModel *>()->SetDirty(true);
}
};*/
};

protected:
MutableRepeatedFieldRef<T> GetfieldRef() const { return _fieldRef; }
const FieldDescriptor *_field;
MutableRepeatedFieldRef<T> _fieldRef;
};
Expand Down
3 changes: 2 additions & 1 deletion Models/RepeatedStringModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <algorithm>

void RepeatedStringModel::AppendNew() { _fieldRef.Add(""); }

QVariant RepeatedStringModel::Data(int row, int column) const {
return data(index(row, column, QModelIndex()), Qt::UserRole);
}
Expand All @@ -31,7 +33,6 @@ bool RepeatedStringModel::setData(const QModelIndex& index, const QVariant& valu
}

QVariant RepeatedStringModel::data(const QModelIndex& index, int /*role*/) const {
qDebug() << "RepeatedStringModel::data";
if (index.column() != 0) {
qDebug() << "Invalid column index";
return QVariant();
Expand Down
2 changes: 2 additions & 0 deletions Models/RepeatedStringModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class RepeatedStringModel : public RepeatedModel<std::string> {
: RepeatedModel<std::string>(parent, message, field,
message->GetReflection()->GetMutableRepeatedFieldRef<std::string>(message, field)) {}

virtual void AppendNew() override;
virtual QVariant Data(int row, int column = 0) const override;
virtual bool SetData(const QVariant &value, int row, int column = 0) override;

virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
virtual QVariant data(const QModelIndex &index, int role) const override;
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override;
Expand Down
20 changes: 8 additions & 12 deletions Models/ResourceModelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,23 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) {
for (auto room : _resources[TypeCase::kRoom]) {
MessageModel* roomModel = room.second->GetSubModel<MessageModel*>(TreeNode::kRoomFieldNumber);
RepeatedMessageModel* instancesModel = roomModel->GetSubModel<RepeatedMessageModel*>(Room::kInstancesFieldNumber);
//RepeatedProtoModel::RowRemovalOperation remover(instancesModel);
RepeatedMessageModel::RowRemovalOperation remover(instancesModel);

for (int row = 0; row < instancesModel->rowCount(); ++row) {
if (instancesModel->Data(row, Room::Instance::kObjectTypeFieldNumber).toString() == name)
;
//remover.RemoveRow(row);
remover.RemoveRow(row);
}

// Only models in use in open editors should have backup models
MessageModel* backupModel = roomModel->GetBackupModel();
if (backupModel != nullptr) {
RepeatedMessageModel* instancesModelBak =
backupModel->GetSubModel<RepeatedMessageModel*>(Room::kInstancesFieldNumber);
//RepeatedProtoModel::RowRemovalOperation removerBak(instancesModelBak);
RepeatedMessageModel::RowRemovalOperation removerBak(instancesModelBak);

for (int row = 0; row < instancesModelBak->rowCount(); ++row) {
if (instancesModelBak->Data(row, Room::Instance::kObjectTypeFieldNumber).toString() == name)
;
//removerBak.RemoveRow(row);
removerBak.RemoveRow(row);
}
}
}
Expand All @@ -66,23 +64,21 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) {
for (auto room : _resources[TypeCase::kRoom]) {
MessageModel* roomModel = room.second->GetSubModel<MessageModel*>(TreeNode::kRoomFieldNumber);
RepeatedMessageModel* tilesModel = roomModel->GetSubModel<RepeatedMessageModel*>(Room::kTilesFieldNumber);
//RepeatedProtoModel::RowRemovalOperation remover(tilesModel);
RepeatedMessageModel::RowRemovalOperation remover(tilesModel);

for (int row = 0; row < tilesModel->rowCount(); ++row) {
if (tilesModel->Data(row, Room::Instance::kObjectTypeFieldNumber).toString() == name)
; // remover.RemoveRow(row);
if (tilesModel->Data(row, Room::Instance::kObjectTypeFieldNumber).toString() == name) remover.RemoveRow(row);
}

// Only models in use in open editors should have backup models
MessageModel* backupModel = roomModel->GetBackupModel();
if (backupModel != nullptr) {
RepeatedMessageModel* tilesModelBak = backupModel->GetSubModel<RepeatedMessageModel*>(Room::kTilesFieldNumber);
//RepeatedProtoModel::RowRemovalOperation removerBak(tilesModelBak);
RepeatedMessageModel::RowRemovalOperation removerBak(tilesModelBak);

for (int row = 0; row < tilesModelBak->rowCount(); ++row) {
if (tilesModelBak->Data(row, Room::Tile::kBackgroundNameFieldNumber).toString() == name)
;
//removerBak.RemoveRow(row);
removerBak.RemoveRow(row);
}
}
}
Expand Down

0 comments on commit 2eb3e42

Please sign in to comment.