From d79afa253ae07a173616e9b86a115e91b3147f94 Mon Sep 17 00:00:00 2001 From: tatyanakrivonogova Date: Tue, 28 May 2024 21:57:52 +0700 Subject: [PATCH] UP-23, UP-24 Added methods GetLastGlyph() and GetPreviousGlyph() --- include/document/glyphs/button.h | 2 ++ include/document/glyphs/character.h | 2 ++ include/document/glyphs/glyph.h | 13 +++++++------ include/document/glyphs/glyph_container.h | 17 +++++++++-------- include/document/glyphs/monoglyph.h | 2 ++ src/document/document.cpp | 4 +--- src/document/glyphs/button.cpp | 2 ++ src/document/glyphs/character.cpp | 5 ++++- src/document/glyphs/glyph_container.cpp | 21 +++++++++++++++++++++ src/document/glyphs/monoglyph.cpp | 2 ++ 10 files changed, 52 insertions(+), 18 deletions(-) diff --git a/include/document/glyphs/button.h b/include/document/glyphs/button.h index 7b95793..2a8206d 100644 --- a/include/document/glyphs/button.h +++ b/include/document/glyphs/button.h @@ -28,7 +28,9 @@ class Button : public Glyph { void Add(GlyphPtr) override {} GlyphPtr GetFirstGlyph() override; + GlyphPtr GetLastGlyph() override; GlyphPtr GetNextGlyph(GlyphPtr& glyph) override; + GlyphPtr GetPreviousGlyph(GlyphPtr& glyph) override; /** * @brief Returns whether the button is pressed. diff --git a/include/document/glyphs/character.h b/include/document/glyphs/character.h index a6bb554..5fa58ce 100644 --- a/include/document/glyphs/character.h +++ b/include/document/glyphs/character.h @@ -33,7 +33,9 @@ class Character : public Glyph { char GetChar() const; GlyphPtr GetFirstGlyph() override; + GlyphPtr GetLastGlyph() override; GlyphPtr GetNextGlyph(GlyphPtr& glyph) override; + GlyphPtr GetPreviousGlyph(GlyphPtr& glyph) override; std::shared_ptr Clone() const override; diff --git a/include/document/glyphs/glyph.h b/include/document/glyphs/glyph.h index 2840c6c..7378685 100644 --- a/include/document/glyphs/glyph.h +++ b/include/document/glyphs/glyph.h @@ -1,10 +1,10 @@ #ifndef TEXT_EDITOR_GLYPH_H_ #define TEXT_EDITOR_GLYPH_H_ -#include -#include #include +#include #include +#include #include "utils/point.h" @@ -89,7 +89,9 @@ class Glyph { virtual void MoveGlyph(int x, int y); virtual GlyphPtr GetFirstGlyph() = 0; + virtual Glyph::GlyphPtr GetLastGlyph() = 0; virtual GlyphPtr GetNextGlyph(GlyphPtr& glyph) = 0; + virtual GlyphPtr GetPreviousGlyph(GlyphPtr& glyph) = 0; /** * @brief Creates a copy of the glyph and wraps it in a smart @@ -135,11 +137,10 @@ class Glyph { explicit Glyph() {} -private: + private: friend class boost::serialization::access; - template - void serialize(Archive &ar, const unsigned int version) - { + template + void serialize(Archive& ar, const unsigned int version) { std::cout << "0 Glyph\n"; ar & x & y & width & height; std::cout << "1 Glyph\n"; diff --git a/include/document/glyphs/glyph_container.h b/include/document/glyphs/glyph_container.h index 6339f1c..7d248b5 100644 --- a/include/document/glyphs/glyph_container.h +++ b/include/document/glyphs/glyph_container.h @@ -1,10 +1,10 @@ #ifndef TEXT_EDITOR_GLYPH_CONTAINER_H_ #define TEXT_EDITOR_GLYPH_CONTAINER_H_ -#include -#include #include #include +#include +#include #include "glyph.h" @@ -30,22 +30,23 @@ class GlyphContainer : public Glyph { Glyph::GlyphPtr GetGlyphByIndex(int index); GlyphPtr GetFirstGlyph() override; + Glyph::GlyphPtr GetLastGlyph() override; GlyphPtr GetNextGlyph(GlyphPtr& glyph) override; + GlyphPtr GetPreviousGlyph(GlyphPtr& glyph) override; protected: Glyph::GlyphList components; explicit GlyphContainer() {} -private: + + private: friend class boost::serialization::access; - template - void serialize(Archive &ar, const unsigned int version) - { + template + void serialize(Archive& ar, const unsigned int version) { std::cout << "0 GlyphContainer\n"; - ar & boost::serialization::base_object(*this); + ar& boost::serialization::base_object(*this); ar & components; std::cout << "1 GlyphContainer\n"; } - }; BOOST_SERIALIZATION_ASSUME_ABSTRACT(GlyphContainer) diff --git a/include/document/glyphs/monoglyph.h b/include/document/glyphs/monoglyph.h index 9c15f68..0e9bb4c 100644 --- a/include/document/glyphs/monoglyph.h +++ b/include/document/glyphs/monoglyph.h @@ -26,7 +26,9 @@ class MonoGlyph : public Glyph { void Add(GlyphPtr glyph) override; GlyphPtr GetFirstGlyph() override; + GlyphPtr GetLastGlyph() override; GlyphPtr GetNextGlyph(GlyphPtr& glyph) override; + GlyphPtr GetPreviousGlyph(GlyphPtr& glyph) override; std::shared_ptr Clone() const override; diff --git a/src/document/document.cpp b/src/document/document.cpp index 79b8a32..f6ebea0 100644 --- a/src/document/document.cpp +++ b/src/document/document.cpp @@ -82,9 +82,7 @@ void Document::Remove(Glyph::GlyphPtr& glyph) { compositor->Compose(); } -void Document::SetCurrentPage(Page::PagePtr page) { - currentPage = std::move(page); -} +void Document::SetCurrentPage(Page::PagePtr page) { currentPage = page; } Page::PagePtr Document::GetCurrentPage() { return currentPage; } diff --git a/src/document/glyphs/button.cpp b/src/document/glyphs/button.cpp index afd9c09..ca76c20 100644 --- a/src/document/glyphs/button.cpp +++ b/src/document/glyphs/button.cpp @@ -14,8 +14,10 @@ void Button::Draw() { } Glyph::GlyphPtr Button::GetFirstGlyph() { return nullptr; } +Glyph::GlyphPtr Button::GetLastGlyph() { return nullptr; } Glyph::GlyphPtr Button::GetNextGlyph(GlyphPtr& glyph) { return nullptr; } +Glyph::GlyphPtr Button::GetPreviousGlyph(GlyphPtr& glyph) { return nullptr; } std::shared_ptr Button::Clone() const { Button* copy = diff --git a/src/document/glyphs/character.cpp b/src/document/glyphs/character.cpp index cee1448..6ae1fba 100644 --- a/src/document/glyphs/character.cpp +++ b/src/document/glyphs/character.cpp @@ -1,6 +1,7 @@ +#include "document/glyphs/character.h" + #include #include -#include "document/glyphs/character.h" BOOST_CLASS_EXPORT_IMPLEMENT(Character) Character::Character(const int x, const int y, const int width, @@ -16,8 +17,10 @@ void Character::SetChar(char c) { symbol = c; } char Character::GetChar() const { return symbol; } Glyph::GlyphPtr Character::GetFirstGlyph() { return nullptr; } +Glyph::GlyphPtr Character::GetLastGlyph() { return nullptr; } Glyph::GlyphPtr Character::GetNextGlyph(GlyphPtr& glyph) { return nullptr; } +Glyph::GlyphPtr Character::GetPreviousGlyph(GlyphPtr& glyph) { return nullptr; } std::shared_ptr Character::Clone() const { Character* copy = new Character(this->x, this->y, this->width, this->height, diff --git a/src/document/glyphs/glyph_container.cpp b/src/document/glyphs/glyph_container.cpp index f4265b5..be70f80 100644 --- a/src/document/glyphs/glyph_container.cpp +++ b/src/document/glyphs/glyph_container.cpp @@ -53,6 +53,13 @@ Glyph::GlyphPtr GlyphContainer::GetFirstGlyph() { return *(components.begin()); } +Glyph::GlyphPtr GlyphContainer::GetLastGlyph() { + if (components.begin() == components.end()) { + return nullptr; + } + return *(components.end()--); +} + Glyph::GlyphPtr GlyphContainer::GetNextGlyph(GlyphPtr& glyph) { auto currentGlyph = std::find_if(components.begin(), components.end(), @@ -65,4 +72,18 @@ Glyph::GlyphPtr GlyphContainer::GetNextGlyph(GlyphPtr& glyph) { return nullptr; } return *nextGlyph; +} + +Glyph::GlyphPtr GlyphContainer::GetPreviousGlyph(GlyphPtr& glyph) { + auto currentGlyph = + std::find_if(components.begin(), components.end(), + [&](const auto& elem) { return elem == glyph; }); + + assert(currentGlyph != components.end()); + + if (currentGlyph == components.begin()) { + return nullptr; + } + auto previousGlyph = std::prev(currentGlyph); + return *previousGlyph; } \ No newline at end of file diff --git a/src/document/glyphs/monoglyph.cpp b/src/document/glyphs/monoglyph.cpp index e02ff2d..0cf4d35 100644 --- a/src/document/glyphs/monoglyph.cpp +++ b/src/document/glyphs/monoglyph.cpp @@ -32,8 +32,10 @@ void MonoGlyph::Remove(const GlyphPtr& glyph) { } Glyph::GlyphPtr MonoGlyph::GetFirstGlyph() { return component; } +Glyph::GlyphPtr MonoGlyph::GetLastGlyph() { return component; } Glyph::GlyphPtr MonoGlyph::GetNextGlyph(GlyphPtr& glyph) { return nullptr; } +Glyph::GlyphPtr MonoGlyph::GetPreviousGlyph(GlyphPtr& glyph) { return nullptr; } std::shared_ptr MonoGlyph::Clone() const { // MonoGlyph* copy =