Skip to content

Commit

Permalink
UP-14 Updated Paste() and Cut() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tatyanakrivonogova committed May 22, 2024
1 parent df415a4 commit 5a3c8eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
26 changes: 13 additions & 13 deletions include/document/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ class Document {
*/
void Remove(Glyph::GlyphPtr& glyph);

// /**
// * @brief Saves selected glyphs from the document to the buffer
// of
// * selected glyphs.
// * @param glyphs Pointer to the list of glyphs.
// */
// void SelectGlyphs(GlyphContainer::GlyphList& glyphs);

/**
* @brief Inserts glyphs from the buffer of selected glyphs into
* the document by the position.
* @param start The starting point of the selected area in document.
* @param end The end point of the selected area in document.
*/
void SelectGlyphs(const Point& start, const Point& end);

/**
* @brief Inserts glyphs from the buffer of selected glyphs into
* the document by the position.
* @param x Horizontal coordinate.
* @param y Vertical coordinate.
* @return List of created and inserted in document glyphs.
*/
void PasteGlyphs(int x, int y);
Glyph::GlyphList PasteGlyphs(int x, int y);

/**
* @brief Removes selected glyphs from the document and saves them
* to the selected buffer.
* @param glyphs Pointer to the list of glyphs.
* @brief Removes selected glyphs from the document and leaves
* them saved in the buffer.
* @param start The starting point of the selected area in document.
* @param end The end point of the selected area in document.
*/
void CutGlyphs(GlyphContainer::GlyphList& glyphs);
void CutGlyphs(const Point& start, const Point& end);

void SetCurrentPage(Page::PagePtr page);
Page::PagePtr GetCurrentPage();
Expand Down
19 changes: 7 additions & 12 deletions src/document/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ Page::PagePtr Document::GetNextPage(const Page::PagePtr& pagePtr) {
return std::static_pointer_cast<Page>(*nextPage);
}

// void Document::SelectGlyphs(GlyphContainer::GlyphList& glyphs) {
// selectedGlyphs.clear();
// for (auto& glyph : glyphs) {
// selectedGlyphs.push_back(glyph);
// }
// }

void Document::SelectGlyphs(const Point& start, const Point& end) {
Glyph::GlyphPtr area = std::make_shared<Column>(
Column(start.x, start.y, end.x - start.x - 1, end.y - start.y - 1));
Expand All @@ -99,26 +92,28 @@ void Document::SelectGlyphs(const Point& start, const Point& end) {
}
}

void Document::PasteGlyphs(int x, int y) {
Glyph::GlyphList Document::PasteGlyphs(int x, int y) {
int currentX = x;
int currentY = y;
Glyph::GlyphPtr glyph;
Glyph::GlyphList copiesList;
for (auto& glyph : selectedGlyphs) {
Glyph::GlyphPtr copy = glyph->Clone();
copy->SetPosition(Point(currentX, currentY)); // set new position
this->Insert(copy);
copiesList.push_back(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
return copiesList;
}

void Document::CutGlyphs(GlyphContainer::GlyphList& glyphs) {
selectedGlyphs.clear();
for (auto& glyph : glyphs) {
selectedGlyphs.push_back(glyph);
void Document::CutGlyphs(const Point& start, const Point& end) {
SelectGlyphs(start, end);
for (auto& glyph : selectedGlyphs) {
this->Remove(glyph);
}
}
11 changes: 2 additions & 9 deletions test/model/text_editor_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1771,10 +1771,7 @@ TEST(Document_CutGlyphs,
document.Insert(c2Ptr);
document.Insert(c3Ptr);

GlyphContainer::GlyphList list;
list.push_back(c1Ptr);
list.push_back(c3Ptr);
document.CutGlyphs(list);
document.CutGlyphs(Point(36, 10), Point(45, 10));

Glyph::GlyphPtr first = document.GetFirstPage()
->GetFirstGlyph()
Expand Down Expand Up @@ -1809,11 +1806,7 @@ TEST(Document_CutPasteGlyphs,
document.Insert(c2Ptr);
document.Insert(c3Ptr);

GlyphContainer::GlyphList list;

list.push_back(c2Ptr);
list.push_back(c3Ptr);
document.CutGlyphs(list);
document.CutGlyphs(Point(30, 10), Point(40, 10));

document.PasteGlyphs(30, 10);

Expand Down

0 comments on commit 5a3c8eb

Please sign in to comment.