Skip to content

Commit

Permalink
UP-20, UP-21 Copy and Paste commands added. Serialization of Composit…
Browse files Browse the repository at this point in the history
…or, SimpleCompositor added
  • Loading branch information
extio1 committed May 29, 2024
1 parent d67b8d4 commit 679389d
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 18 deletions.
33 changes: 30 additions & 3 deletions include/compositor/compositor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
#define TEXT_EDITOR_COMPOSITOR_H_

#include <iostream>
#include <boost/serialization/export.hpp>
#include <boost/serialization/access.hpp>

#include "document/document.h"

class Compositor {
public:
enum Alignment { LEFT, CENTER, RIGHT, JUSTIFIED };

Compositor(int topIndent = 5, int bottomIndent = 10, int leftIndent = 3,
int rightIndent = 6, Alignment alignment = LEFT,
int lineSpacing = 5) {
Compositor() {
document = nullptr;
this->topIndent = 5;
this->bottomIndent = 10;
this->leftIndent = 3;
this->rightIndent = 6;
this->alignment = LEFT;
this->lineSpacing = 3;
}

Compositor(int topIndent, int bottomIndent, int leftIndent,
int rightIndent, Alignment alignment,
int lineSpacing) {
document = nullptr;
this->topIndent = topIndent;
this->bottomIndent = bottomIndent;
this->leftIndent = leftIndent;
Expand Down Expand Up @@ -42,6 +55,20 @@ class Compositor {
int rightIndent;
Alignment alignment;
int lineSpacing;

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
std::cout << "0 Compositor\n";
ar & document;
ar & topIndent & bottomIndent & leftIndent;
ar & rightIndent & alignment & lineSpacing;
std::cout << "1 Compositor\n";
}

};
BOOST_CLASS_EXPORT_KEY(Compositor)

#endif // TEXT_EDITOR_COMPOSITOR_H_
23 changes: 20 additions & 3 deletions include/compositor/simple_compositor/simple_compositor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@
#define TEXT_EDITOR_SIMPLECOMPOSITOR_H_

#include <iostream>
#include <boost/serialization/export.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>

#include "compositor/compositor.h"
#include "document/document.h"

class SimpleCompositor : public Compositor {
public:
explicit SimpleCompositor(int topIndent = 5, int bottomIndent = 10,
int leftIndent = 3, int rightIndent = 6,
Alignment alignment = LEFT, int lineSpacing = 5)
explicit SimpleCompositor()
: Compositor(5, 10, 3, 6,
LEFT, 5) {}

explicit SimpleCompositor(int topIndent, int bottomIndent,
int leftIndent, int rightIndent,
Alignment alignment, int lineSpacing)
: Compositor(topIndent, bottomIndent, leftIndent, rightIndent,
alignment, lineSpacing) {}

Expand All @@ -31,6 +38,16 @@ class SimpleCompositor : public Compositor {
int GetNestedGlyphsHeight(Glyph::GlyphPtr& glyph);

GlyphContainer::GlyphList CutAllCharacters();

friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
std::cout << "0 SimpleCompositor\n";
ar & boost::serialization::base_object<Compositor>(*this);
std::cout << "1 SimpleCompositor\n";
}
};
BOOST_CLASS_EXPORT_KEY(SimpleCompositor)

#endif // TEXT_EDITOR_SIMPLECOMPOSITOR_H_
2 changes: 1 addition & 1 deletion include/document/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Document : public IDocument {
{
std::cout << "0 Document\n";
ar & boost::serialization::base_object<IDocument>(*this);
ar & pages;
ar & pages & currentPage & compositor;
std::cout << "1 Document\n";
}
};
Expand Down
4 changes: 3 additions & 1 deletion include/executor/command/copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Copy : public Command {
public:
explicit Copy(std::shared_ptr<IDocument> doc, Point& from, Point& to);
explicit Copy(std::shared_ptr<IDocument> doc, const Point& from, const Point& to);

Copy(Copy&&) = default;
Copy& operator=(Copy&&) = default;
Expand All @@ -21,6 +21,8 @@ class Copy : public Command {

private:
std::shared_ptr<IDocument> doc;
Point from;
Point to;
};


Expand Down
5 changes: 3 additions & 2 deletions include/executor/command/paste.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Paste : public ReversibleCommand {
public:
explicit Paste(std::shared_ptr<IDocument> doc, Point& begin_with);
explicit Paste(std::shared_ptr<IDocument> doc, const Point& begin_with);

Paste(Paste&&) = default;
Paste& operator=(Paste&&) = default;
Expand All @@ -22,7 +22,8 @@ class Paste : public ReversibleCommand {

private:
std::shared_ptr<IDocument> doc;
GlyphContainer::GlyphList pasted_glyphs;
Point begin_with;
Glyph::GlyphList pasted_glyphs;
};


Expand Down
5 changes: 5 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "executor/command/remove_character.h"
#include "executor/command/save_document.h"
#include "executor/command/load_document.h"
#include "executor/command/copy.h"
#include "executor/command/paste.h"

int main() {
auto d = std::make_shared<Document>(std::make_shared<SimpleCompositor>());
Expand Down Expand Up @@ -40,5 +42,8 @@ int main() {
controller->Do(std::make_shared<LoadDocument>(&document, "doc_save.file"));
std::cout << "Loaded: " << document << "\n";

controller->Do(std::make_shared<Copy>(document, Point(3, 5), Point(43, 5)));
controller->Do(std::make_shared<Paste>(document, Point(43, 5)));

return 0;
}
3 changes: 3 additions & 0 deletions src/compositor/compositor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "compositor/compositor.h"
BOOST_CLASS_EXPORT_IMPLEMENT(Compositor)

#include <iostream>

Expand Down
4 changes: 4 additions & 0 deletions src/compositor/simple_compositor/simple_compositor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "compositor/simple_compositor/simple_compositor.h"
BOOST_CLASS_EXPORT_IMPLEMENT(SimpleCompositor)


#include <cmath>

Expand Down
2 changes: 2 additions & 0 deletions src/executor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ set(sources
"command/remove_character.cpp"
"command/save_document.cpp"
"command/load_document.cpp"
"command/copy.cpp"
"command/paste.cpp"
)

add_library(${target} SHARED ${sources})
Expand Down
6 changes: 4 additions & 2 deletions src/executor/command/copy.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "executor/command/copy.h"

Copy::Copy(std::shared_ptr<IDocument> doc, Point& from, Point& to){}
Copy::Copy(std::shared_ptr<IDocument> doc, const Point& from, const Point& to):
doc(doc),from(from), to(to)
{}

void Copy::Execute(){

doc->SelectGlyphs(from, to);
}

Copy::~Copy() {}
13 changes: 7 additions & 6 deletions src/executor/command/paste.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "executor/command/paste.h"

Paste::Paste(std::shared_ptr<IDocument> doc, Point& begin_with){}
Paste::Paste(std::shared_ptr<IDocument> doc, const Point& begin_with):
doc(doc), begin_with(begin_with){}

void Paste::Execute(){

pasted_glyphs = doc->PasteGlyphs(begin_with);
}

void Paste::Unexecute(){

for(auto gl : pasted_glyphs){
doc->Remove(gl);
}
}

Paste::~Paste(){

}
Paste::~Paste(){}

0 comments on commit 679389d

Please sign in to comment.