Permalink
Browse files

Add block operations

  • Loading branch information...
feragon committed Jun 26, 2017
1 parent 5edfa90 commit ba6b6757d277dd03a91552c510725cfa578154dc
@@ -14,6 +14,7 @@
#include <cad/operations/builder.h>
#include <cad/operations/layerops.h>
#include <cad/meta/customentitystorage.h>
#include <cad/operations/blockops.h>
#include "lclua.h"
using namespace LuaIntf;
@@ -462,5 +463,27 @@ void LCLua::importLCKernel() {
.addFunction("param", &CustomEntityStorage::param)
.addFunction("setParam", &CustomEntityStorage::setParam)
.endClass()
.beginExtendClass<operation::AddBlock, operation::DocumentOperation>("AddBlock")
.addConstructor(LUA_SP(std::shared_ptr<lc::operation::AddBlock>), LUA_ARGS(
std::shared_ptr<lc::Document>,
const Block_CSPtr
))
.endClass()
.beginExtendClass<operation::RemoveBlock, operation::DocumentOperation>("RemoveBlock")
.addConstructor(LUA_SP(std::shared_ptr<lc::operation::RemoveBlock>), LUA_ARGS(
std::shared_ptr<lc::Document>,
const Block_CSPtr
))
.endClass()
.beginExtendClass<operation::ReplaceBlock, operation::DocumentOperation>("ReplaceBlock")
.addConstructor(LUA_SP(std::shared_ptr<lc::operation::ReplaceBlock>), LUA_ARGS(
std::shared_ptr<lc::Document>,
const Block_CSPtr,
const Block_CSPtr
))
.endClass()
;
}
View
@@ -72,6 +72,7 @@ cad/builders/insert.cpp
cad/builders/layer.cpp
cad/builders/linepattern.cpp
cad/meta/customentitystorage.cpp
cad/operations/blockops.cpp
)
# HEADER FILES
@@ -165,6 +166,7 @@ cad/builders/insert.h
cad/builders/layer.h
cad/builders/linepattern.h
cad/meta/customentitystorage.h
cad/operations/blockops.h
)
# LOG4CXX
@@ -0,0 +1,67 @@
#include "blockops.h"
using namespace lc;
using namespace operation;
AddBlock::AddBlock(const Document_SPtr& document, const Block_CSPtr& block) :
DocumentOperation(document),
Undoable("AddBlock"),
_block(block) {
}
void AddBlock::undo() const {
document()->removeDocumentMetaType(_block);
}
void AddBlock::redo() const {
document()->addDocumentMetaType(_block);
}
void AddBlock::processInternal() {
document()->addDocumentMetaType(_block);
}
RemoveBlock::RemoveBlock(const Document_SPtr& document, const Block_CSPtr& block) :
DocumentOperation(document),
Undoable("RemoveBlock"),
_block(block){
}
void RemoveBlock::undo() const {
document()->addDocumentMetaType(_block);
}
void RemoveBlock::redo() const {
document()->removeDocumentMetaType(_block);
}
void RemoveBlock::processInternal() {
document()->removeDocumentMetaType(_block);
}
ReplaceBlock::ReplaceBlock(const Document_SPtr& document, const Block_CSPtr& oldBlock, const Block_CSPtr& newBlock) :
DocumentOperation(document),
Undoable("ReplaceBlock"),
_oldBlock(oldBlock),
_newBlock(newBlock) {
}
void ReplaceBlock::undo() const {
document()->removeDocumentMetaType(_newBlock);
document()->addDocumentMetaType(_oldBlock);
}
void ReplaceBlock::redo() const {
document()->removeDocumentMetaType(_oldBlock);
document()->addDocumentMetaType(_newBlock);
}
void ReplaceBlock::processInternal() {
document()->removeDocumentMetaType(_oldBlock);
document()->addDocumentMetaType(_newBlock);
}
@@ -0,0 +1,52 @@
#pragma once
#include "cad/document/document.h"
#include "documentoperation.h"
#include "undoable.h"
namespace lc {
namespace operation {
class AddBlock : public DocumentOperation, public Undoable {
public:
AddBlock(const Document_SPtr& document, const Block_CSPtr& block);
virtual void undo() const override;
virtual void redo() const override;
protected:
virtual void processInternal() override;
private:
Block_CSPtr _block;
};
class RemoveBlock : public DocumentOperation, public Undoable {
public:
RemoveBlock(const Document_SPtr& document, const Block_CSPtr& block);
virtual void undo() const override;
virtual void redo() const override;
protected:
virtual void processInternal() override;
private:
Block_CSPtr _block;
};
class ReplaceBlock : public DocumentOperation, public Undoable {
public:
ReplaceBlock(const Document_SPtr& document, const Block_CSPtr& oldBlock, const Block_CSPtr& newBlock);
virtual void undo() const override;
virtual void redo() const override;
protected:
virtual void processInternal() override;
private:
Block_CSPtr _oldBlock;
Block_CSPtr _newBlock;
};
}
}
View
@@ -42,6 +42,7 @@ lckernel/math/testmatrices.cpp
lckernel/geometry/beziertest.cpp
lcviewernoqt/testselection.cpp
lckernel/meta/customentitystorage.cpp
lckernel/operations/blocksopstest.cpp
)
set(hdrs
@@ -0,0 +1,81 @@
#include <gtest/gtest.h>
#include <cad/dochelpers/documentimpl.h>
#include <cad/dochelpers/storagemanagerimpl.h>
#include <cad/operations/blockops.h>
using namespace lc;
TEST(BlockOps, AddBlock) {
auto document = std::make_shared<DocumentImpl>(std::make_shared<StorageManagerImpl>());
auto block = std::make_shared<Block>("Name", geo::Coordinate());
auto op = std::make_shared<operation::AddBlock>(document, block);
auto blocks = document->blocks();
EXPECT_EQ(0, blocks.size());
op->execute();
blocks = document->blocks();
EXPECT_EQ(1, blocks.size());
EXPECT_EQ(block, *blocks.begin());
op->undo();
blocks = document->blocks();
EXPECT_EQ(0, blocks.size());
op->redo();
blocks = document->blocks();
EXPECT_EQ(1, blocks.size());
EXPECT_EQ(block, *blocks.begin());
}
TEST(BlockOps, RemoveBlock) {
auto document = std::make_shared<DocumentImpl>(std::make_shared<StorageManagerImpl>());
auto block = std::make_shared<Block>("Name", geo::Coordinate());
auto addOp = std::make_shared<operation::AddBlock>(document, block);
auto remOp = std::make_shared<operation::RemoveBlock>(document, block);
addOp->execute();
auto blocks = document->blocks();
remOp->execute();
blocks = document->blocks();
EXPECT_EQ(0, blocks.size());
remOp->undo();
blocks = document->blocks();
EXPECT_EQ(1, blocks.size());
EXPECT_EQ(block, *blocks.begin());
remOp->redo();
blocks = document->blocks();
EXPECT_EQ(0, blocks.size());
}
TEST(BlockOps, ReplaceBlock) {
auto document = std::make_shared<DocumentImpl>(std::make_shared<StorageManagerImpl>());
auto block = std::make_shared<Block>("Name", geo::Coordinate());
auto block2 = std::make_shared<Block>("Name 2", geo::Coordinate());
auto addOp = std::make_shared<operation::AddBlock>(document, block);
auto repOp = std::make_shared<operation::ReplaceBlock>(document, block, block2);
addOp->execute();
auto blocks = document->blocks();
repOp->execute();
blocks = document->blocks();
EXPECT_EQ(1, blocks.size());
EXPECT_EQ(block2, *blocks.begin());
repOp->undo();
blocks = document->blocks();
EXPECT_EQ(1, blocks.size());
EXPECT_EQ(block, *blocks.begin());
repOp->redo();
blocks = document->blocks();
EXPECT_EQ(1, blocks.size());
EXPECT_EQ(block2, *blocks.begin());
}

0 comments on commit ba6b675

Please sign in to comment.