Skip to content

Commit

Permalink
UP-14 Added copying of glyphs
Browse files Browse the repository at this point in the history
  • Loading branch information
tatyanakrivonogova committed May 21, 2024
1 parent d7e5d17 commit 97e0f22
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 14 deletions.
2 changes: 2 additions & 0 deletions include/document/glyphs/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Button : public Glyph {
*/
bool IsPressed() const;

std::shared_ptr<Glyph> clone() const override;

private:
std::string name;
bool isPressed = false;
Expand Down
2 changes: 2 additions & 0 deletions include/document/glyphs/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Character : public Glyph {
GlyphPtr GetFirstGlyph() override;
GlyphPtr GetNextGlyph(GlyphPtr& glyph) override;

std::shared_ptr<Glyph> clone() const override;

private:
char symbol;
};
Expand Down
2 changes: 2 additions & 0 deletions include/document/glyphs/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Column : public GlyphContainer {
void InsertFront(GlyphPtr& glyph) override;
void Remove(const GlyphPtr& glyph) override;

std::shared_ptr<Glyph> clone() const override;

bool IsEmpty() const;
bool IsFull() const;

Expand Down
2 changes: 2 additions & 0 deletions include/document/glyphs/glyph.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class Glyph {
virtual GlyphPtr GetFirstGlyph() = 0;
virtual GlyphPtr GetNextGlyph(GlyphPtr& glyph) = 0;

virtual std::shared_ptr<Glyph> clone() const = 0;

void SetPosition(const Point& p);
void SetPosition(int x, int y);
void SetWidth(int width);
Expand Down
2 changes: 2 additions & 0 deletions include/document/glyphs/monoglyph.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class MonoGlyph : public Glyph {
GlyphPtr GetFirstGlyph() override;
GlyphPtr GetNextGlyph(GlyphPtr& glyph) override;

std::shared_ptr<Glyph> clone() const override {} //???

protected:
GlyphPtr component;
};
Expand Down
2 changes: 2 additions & 0 deletions include/document/glyphs/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Page : public GlyphContainer {
void InsertFront(GlyphPtr& glyph) override;
void Remove(const GlyphPtr& glyph) override;

std::shared_ptr<Glyph> clone() const override;

size_t GetColumnsCount();
};

Expand Down
2 changes: 2 additions & 0 deletions include/document/glyphs/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Row : public GlyphContainer {

void Remove(const GlyphPtr& glyph) override;

std::shared_ptr<Glyph> clone() const override;

bool IsEmpty() const;
bool IsFull() const;

Expand Down
2 changes: 1 addition & 1 deletion src/compositor/compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <iostream>

void Compositor::SetDocument(Document* document) {
std::cout << "Compositor::SetDocument()" << std::endl;
// std::cout << "Compositor::SetDocument()" << std::endl;
this->document = document;
}

Expand Down
14 changes: 8 additions & 6 deletions src/document/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ void Document::SetCompositor(std::shared_ptr<Compositor> compositor) {
}

void Document::Insert(Glyph::GlyphPtr& glyph) {
std::cout << "Document::Insert()" << std::endl;
currentPage->Insert(glyph);
compositor->Compose();
// glyph->Draw();
Expand Down Expand Up @@ -82,12 +81,15 @@ void Document::PasteGlyphs(int x, int y) {
int currentY = y;
Glyph::GlyphPtr glyph;
for (auto& glyph : selectedGlyphs) {
glyph->SetPosition(Point(currentX, currentY)); // set new position
this->Insert(glyph);
currentX = glyph->GetPosition().x +
glyph->GetWidth(); // insert next glyph after this
currentY = glyph->GetPosition().y; // insert next glyph to the same row
Glyph::GlyphPtr copy = glyph->clone();
copy->SetPosition(Point(currentX, currentY)); // set new position
this->Insert(copy);
currentX = copy->GetPosition().x +
copy->GetWidth(); // insert next glyph after this
currentY = copy->GetPosition().y; // insert next glyph to the same row
}
// selected glyphs is not removed from selectedGlyphs, they can be pasted or
// cut one more time
}

void Document::CutGlyphs(GlyphContainer::GlyphList& glyphs) {
Expand Down
8 changes: 7 additions & 1 deletion src/document/glyphs/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ void Button::Draw() {

Glyph::GlyphPtr Button::GetFirstGlyph() { return nullptr; }

Glyph::GlyphPtr Button::GetNextGlyph(GlyphPtr& glyph) { return nullptr; }
Glyph::GlyphPtr Button::GetNextGlyph(GlyphPtr& glyph) { return nullptr; }

std::shared_ptr<Glyph> Button::clone() const {
Button* copy =
new Button(this->x, this->y, this->width, this->height, this->name);
return std::make_shared<Button>(*copy);
}
8 changes: 7 additions & 1 deletion src/document/glyphs/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ char Character::GetChar() const { return symbol; }

Glyph::GlyphPtr Character::GetFirstGlyph() { return nullptr; }

Glyph::GlyphPtr Character::GetNextGlyph(GlyphPtr& glyph) { return nullptr; }
Glyph::GlyphPtr Character::GetNextGlyph(GlyphPtr& glyph) { return nullptr; }

std::shared_ptr<Glyph> Character::clone() const {
Character* copy = new Character(this->x, this->y, this->width, this->height,
this->symbol);
return std::make_shared<Character>(*copy);
}
5 changes: 5 additions & 0 deletions src/document/glyphs/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ bool Column::IsEmpty() const { return components.empty(); }
bool Column::IsFull() const { return usedHeight >= height; }
int Column::GetFreeSpace() const { return height - usedHeight; }
int Column::GetUsedSpace() const { return usedHeight; }

std::shared_ptr<Glyph> Column::clone() const {
Column* copy = new Column(this->x, this->y, this->width, this->height);
return std::make_shared<Column>(*copy);
}
7 changes: 6 additions & 1 deletion src/document/glyphs/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ void Page::Remove(const GlyphPtr& glyph) {
(*intersectedGlyphIt)->Remove(glyph);
}

size_t Page::GetColumnsCount() { return components.size(); }
size_t Page::GetColumnsCount() { return components.size(); }

std::shared_ptr<Glyph> Page::clone() const {
Page* copy = new Page(this->x, this->y, this->width, this->height);
return std::make_shared<Page>(*copy);
}
7 changes: 6 additions & 1 deletion src/document/glyphs/row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ void Row::Remove(const GlyphPtr& ptr) {
bool Row::IsEmpty() const { return components.empty(); }
bool Row::IsFull() const { return usedWidth >= width; }
int Row::GetFreeSpace() const { return width - usedWidth; }
int Row::GetUsedSpace() const { return usedWidth; }
int Row::GetUsedSpace() const { return usedWidth; }

std::shared_ptr<Glyph> Row::clone() const {
Row* copy = new Row(this->x, this->y, this->width, this->height);
return std::make_shared<Row>(*copy);
}
102 changes: 99 additions & 3 deletions test/model/text_editor_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ TEST(Document_CutGlyphs,
}

TEST(Document_CutPasteGlyphs,
DocumentCutPasteGlyphs_WhenCalled_PasteSelectedGlyphsDueToPosition) {
DocumentCutPasteGlyphs_WhenCalled_PasteCutGlyphsDueToPosition) {
Document document;
document.SetCompositor(std::make_shared<SimpleCompositor>(
10, 20, 30, 40, Compositor::LEFT, 100));
Expand All @@ -1787,11 +1787,11 @@ TEST(Document_CutPasteGlyphs,
->GetFirstGlyph()
->GetFirstGlyph()
->GetFirstGlyph();
EXPECT_EQ(first, c2Ptr);
EXPECT_TRUE(first != c2Ptr);
Glyph::GlyphPtr second =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
first);
EXPECT_EQ(second, c3Ptr);
EXPECT_TRUE(second != c3Ptr);
Glyph::GlyphPtr third =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
second);
Expand All @@ -1818,4 +1818,100 @@ TEST(Document_CutPasteGlyphs,
EXPECT_EQ(third->GetPosition().y, 10);
EXPECT_EQ(third->GetWidth(), 5);
EXPECT_EQ(third->GetHeight(), 5);
}

TEST(Document_SelectPasteGlyphs,
DocumentSelectPasteGlyphs_WhenCalled_PasteSelectedGlyphsDueToPosition) {
Document document;
document.SetCompositor(std::make_shared<SimpleCompositor>(
10, 20, 30, 40, Compositor::LEFT, 100));

Character c1 = Character(100, 10, 5, 5, 'A');
Glyph::GlyphPtr c1Ptr = std::make_shared<Character>(c1);
Character c2 = Character(30, 10, 5, 5, 'B');
Glyph::GlyphPtr c2Ptr = std::make_shared<Character>(c2);
Character c3 = Character(35, 10, 5, 5, 'C');
Glyph::GlyphPtr c3Ptr = std::make_shared<Character>(c3);
document.Insert(c1Ptr);
document.Insert(c2Ptr);
document.Insert(c3Ptr);

GlyphContainer::GlyphList list;

list.push_back(c2Ptr);
list.push_back(c3Ptr);
document.SelectGlyphs(list);

document.PasteGlyphs(30, 10);

Glyph::GlyphPtr first = document.GetFirstPage()
->GetFirstGlyph()
->GetFirstGlyph()
->GetFirstGlyph();
EXPECT_TRUE(first != c2Ptr);
Glyph::GlyphPtr second =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
first);
EXPECT_TRUE(second != c3Ptr);
Glyph::GlyphPtr third =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
second);
EXPECT_EQ(third, c2Ptr);
Glyph::GlyphPtr fourth =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
third);
EXPECT_EQ(fourth, c3Ptr);
Glyph::GlyphPtr fifth =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
fourth);
EXPECT_EQ(fifth, c1Ptr);
Glyph::GlyphPtr sixth =
document.GetFirstPage()->GetFirstGlyph()->GetFirstGlyph()->GetNextGlyph(
fifth);
EXPECT_EQ(sixth, nullptr);

// check first character params
EXPECT_EQ(first->GetPosition().x, 30);
EXPECT_EQ(first->GetPosition().y, 10);
EXPECT_EQ(first->GetWidth(), 5);
EXPECT_EQ(first->GetHeight(), 5);
std::shared_ptr<Character> firstChar =
std::static_pointer_cast<Character>(first);
EXPECT_EQ(firstChar->GetChar(), 'B');

// check second character params
EXPECT_EQ(second->GetPosition().x, 35);
EXPECT_EQ(second->GetPosition().y, 10);
EXPECT_EQ(second->GetWidth(), 5);
EXPECT_EQ(second->GetHeight(), 5);
std::shared_ptr<Character> secondChar =
std::static_pointer_cast<Character>(second);
EXPECT_EQ(secondChar->GetChar(), 'C');

// check third character params
EXPECT_EQ(third->GetPosition().x, 40);
EXPECT_EQ(third->GetPosition().y, 10);
EXPECT_EQ(third->GetWidth(), 5);
EXPECT_EQ(third->GetHeight(), 5);
std::shared_ptr<Character> thirdChar =
std::static_pointer_cast<Character>(third);
EXPECT_EQ(thirdChar->GetChar(), 'B');

// check fourth character params
EXPECT_EQ(fourth->GetPosition().x, 45);
EXPECT_EQ(fourth->GetPosition().y, 10);
EXPECT_EQ(fourth->GetWidth(), 5);
EXPECT_EQ(fourth->GetHeight(), 5);
std::shared_ptr<Character> fourthChar =
std::static_pointer_cast<Character>(fourth);
EXPECT_EQ(fourthChar->GetChar(), 'C');

// check fifth character params
EXPECT_EQ(fifth->GetPosition().x, 50);
EXPECT_EQ(fifth->GetPosition().y, 10);
EXPECT_EQ(fifth->GetWidth(), 5);
EXPECT_EQ(fifth->GetHeight(), 5);
std::shared_ptr<Character> fifthChar =
std::static_pointer_cast<Character>(fifth);
EXPECT_EQ(fifthChar->GetChar(), 'A');
}

0 comments on commit 97e0f22

Please sign in to comment.