diff --git a/Editors/CodeEditor.cpp b/Editors/CodeEditor.cpp index 23de41358..7a0a08d3c 100644 --- a/Editors/CodeEditor.cpp +++ b/Editors/CodeEditor.cpp @@ -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); diff --git a/Editors/FontEditor.cpp b/Editors/FontEditor.cpp index 6dcae6aba..9eb4bae74 100644 --- a/Editors/FontEditor.cpp +++ b/Editors/FontEditor.cpp @@ -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) { diff --git a/Editors/PathEditor.cpp b/Editors/PathEditor.cpp index 6cb1dbf92..3b781b4f3 100644 --- a/Editors/PathEditor.cpp +++ b/Editors/PathEditor.cpp @@ -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) { diff --git a/Editors/TimelineEditor.cpp b/Editors/TimelineEditor.cpp index b7979cc08..d98f85a2e 100644 --- a/Editors/TimelineEditor.cpp +++ b/Editors/TimelineEditor.cpp @@ -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); } diff --git a/Models/MessageModel.cpp b/Models/MessageModel.cpp index 19908a153..02d3e838c 100644 --- a/Models/MessageModel.cpp +++ b/Models/MessageModel.cpp @@ -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" @@ -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(); @@ -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(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(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(); diff --git a/Models/RepeatedMessageModel.cpp b/Models/RepeatedMessageModel.cpp index 8a24787d5..11aa0eb77 100644 --- a/Models/RepeatedMessageModel.cpp +++ b/Models/RepeatedMessageModel.cpp @@ -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(); + 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); } @@ -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); } diff --git a/Models/RepeatedMessageModel.h b/Models/RepeatedMessageModel.h index ea74a7580..13e452e24 100644 --- a/Models/RepeatedMessageModel.h +++ b/Models/RepeatedMessageModel.h @@ -18,6 +18,11 @@ class RepeatedMessageModel : public RepeatedModel { return static_cast(_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; @@ -25,7 +30,7 @@ class RepeatedMessageModel : public RepeatedModel { 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 diff --git a/Models/RepeatedModel.h b/Models/RepeatedModel.h index e55483cbb..5e4517fbe 100644 --- a/Models/RepeatedModel.h +++ b/Models/RepeatedModel.h @@ -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; } @@ -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; } @@ -83,7 +82,7 @@ class RepeatedModel : public ProtoModel { int p = rowCount(); - Append(); + AppendNew(); SwapBack(row, p, rowCount()); ParentDataChanged(); @@ -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*/, @@ -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. @@ -123,13 +122,13 @@ class RepeatedModel : public ProtoModel { SwapBack(left, left + npart, right); } - /*class RowRemovalOperation { + class RowRemovalOperation { std::set _rows; - RepeatedModel _model; + RepeatedModel &_model; MutableRepeatedFieldRef _field; public: - RowRemovalOperation(RepeatedModel model, MutableRepeatedFieldRef field) : _model(model), _field(field) {} + RowRemovalOperation(RepeatedModel *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); @@ -154,14 +153,14 @@ 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++; } @@ -169,7 +168,7 @@ class RepeatedModel : public ProtoModel { } while (right < _field.size()) { _field.SwapElements(left, right); - _model->Swap(left, right); + _model.Swap(left, right); left++; right++; } @@ -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()->SetDirty(true); } - };*/ + }; protected: + MutableRepeatedFieldRef GetfieldRef() const { return _fieldRef; } const FieldDescriptor *_field; MutableRepeatedFieldRef _fieldRef; }; diff --git a/Models/RepeatedStringModel.cpp b/Models/RepeatedStringModel.cpp index 8e21e03e2..a9563da02 100644 --- a/Models/RepeatedStringModel.cpp +++ b/Models/RepeatedStringModel.cpp @@ -6,6 +6,8 @@ #include +void RepeatedStringModel::AppendNew() { _fieldRef.Add(""); } + QVariant RepeatedStringModel::Data(int row, int column) const { return data(index(row, column, QModelIndex()), Qt::UserRole); } @@ -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(); diff --git a/Models/RepeatedStringModel.h b/Models/RepeatedStringModel.h index 48a4a76f4..074d0522b 100644 --- a/Models/RepeatedStringModel.h +++ b/Models/RepeatedStringModel.h @@ -12,8 +12,10 @@ class RepeatedStringModel : public RepeatedModel { : RepeatedModel(parent, message, field, message->GetReflection()->GetMutableRepeatedFieldRef(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; diff --git a/Models/ResourceModelMap.cpp b/Models/ResourceModelMap.cpp index 4883b4b6e..863521243 100644 --- a/Models/ResourceModelMap.cpp +++ b/Models/ResourceModelMap.cpp @@ -37,12 +37,11 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) { for (auto room : _resources[TypeCase::kRoom]) { MessageModel* roomModel = room.second->GetSubModel(TreeNode::kRoomFieldNumber); RepeatedMessageModel* instancesModel = roomModel->GetSubModel(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 @@ -50,12 +49,11 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) { if (backupModel != nullptr) { RepeatedMessageModel* instancesModelBak = backupModel->GetSubModel(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); } } } @@ -66,23 +64,21 @@ void ResourceModelMap::RemoveResource(TypeCase type, const QString& name) { for (auto room : _resources[TypeCase::kRoom]) { MessageModel* roomModel = room.second->GetSubModel(TreeNode::kRoomFieldNumber); RepeatedMessageModel* tilesModel = roomModel->GetSubModel(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(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); } } }