Skip to content

Commit

Permalink
UP-23, UP-24 Added setting of cursor into new row
Browse files Browse the repository at this point in the history
  • Loading branch information
tatyanakrivonogova committed May 28, 2024
1 parent 24c4093 commit 2dbf713
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
30 changes: 17 additions & 13 deletions include/document/document.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef TEXT_EDITOR_DOCUMENT_H_
#define TEXT_EDITOR_DOCUMENT_H_

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

#include "glyphs/glyph.h"
#include "glyphs/page.h"
Expand All @@ -25,10 +25,13 @@ class IDocument {
virtual Glyph::GlyphList PasteGlyphs(const Point& to_point) = 0;
virtual void CutGlyphs(const Point& start, const Point& end) = 0;
virtual ~IDocument() = default;
private:

private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version){ std::cout << "IDocument\n";}
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
std::cout << "IDocument\n";
}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(IDocument)

Expand Down Expand Up @@ -82,6 +85,8 @@ class Document : public IDocument {
void SetCurrentPage(Page::PagePtr page);
Page::PagePtr GetCurrentPage();

Glyph::GlyphPtr GetSelectedGlyph(); // will be deleted

size_t GetPagesCount() const;
size_t GetPageWidth() const;
size_t GetPageHeight() const;
Expand All @@ -95,22 +100,21 @@ class Document : public IDocument {
std::shared_ptr<Compositor> compositor;
Page::PagePtr currentPage;
PageList pages;
Glyph::GlyphPtr selectedGlyph;

GlyphContainer::GlyphList selectedGlyphs;

explicit Document(){}
explicit Document() {}
void updateCursor();
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
std::cout << "0 Document\n";
ar & boost::serialization::base_object<IDocument>(*this);
ar& boost::serialization::base_object<IDocument>(*this);
ar & pages & currentPage & compositor;
std::cout << "1 Document\n";
}
};
BOOST_CLASS_EXPORT_KEY(Document)



#endif // TEXT_EDITOR_DOCUMENT_H_
10 changes: 5 additions & 5 deletions include/document/glyphs/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
class Character : public Glyph {
public:
using CharPtr = std::shared_ptr<Character>;
/**
* @brief Creates a character with the specified parameters and
* symbol.
Expand Down Expand Up @@ -39,15 +40,14 @@ class Character : public Glyph {
private:
char symbol;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
template <class Archive>
void serialize(Archive& ar, const unsigned int version) {
std::cout << "0 Character\n";
ar & boost::serialization::base_object<Glyph>(*this);
ar& boost::serialization::base_object<Glyph>(*this);
ar & symbol;
std::cout << "1 Character\n";
}
explicit Character(){}
explicit Character() {}
};
BOOST_CLASS_EXPORT_KEY(Character)

Expand Down
34 changes: 31 additions & 3 deletions src/document/document.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#include "document/document.h"

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include "document/document.h"
BOOST_CLASS_EXPORT_IMPLEMENT(Document)

#include <algorithm>
#include <cassert>

#include "compositor/compositor.h"
#include "document/glyphs/character.h"
#include "document/glyphs/column.h"
#include "document/glyphs/glyph.h"
#include "document/glyphs/page.h"
#include "document/glyphs/column.h"
#include "document/glyphs/row.h"

Document::Document(std::shared_ptr<Compositor> compositor) {
currentPage = std::make_shared<Page>(0, 0, pageWidth, pageHeight);
AddPage(currentPage);
// set cursor on the first row on page
selectedGlyph = this->GetFirstPage()->GetFirstGlyph()->GetFirstGlyph();
this->updateCursor();

this->compositor = compositor;
compositor->SetDocument(this);
compositor->Compose();
Expand All @@ -29,6 +36,28 @@ std::shared_ptr<Compositor> Document::GetCompositor() {
return this->compositor;
}

Glyph::GlyphPtr Document::GetSelectedGlyph() { return selectedGlyph; }

void Document::updateCursor() {
// check if selectedGlyph is a row or a character

Row::RowPtr selectedRow = std::dynamic_pointer_cast<Row>(selectedGlyph);
if (selectedRow != nullptr) {
// set cursor in the beginning of this row
Point cursorPoint =
Point(selectedRow->GetPosition().x, selectedRow->GetPosition().y);
// window->SetCursor(cursorPoint);
} else {
Character::CharPtr selectedChar =
std::dynamic_pointer_cast<Character>(selectedGlyph);
assert(selectedChar != nullptr && "Selected glyph has invalid type");
Point cursorPoint =
Point(selectedChar->GetPosition().x + selectedChar->GetWidth(),
selectedChar->GetPosition().y);
// window->SetCursor(cursorPoint);
}
}

void Document::Insert(Glyph::GlyphPtr& glyph) {
currentPage->Insert(glyph);
compositor->Compose();
Expand All @@ -45,7 +74,6 @@ void Document::Remove(Glyph::GlyphPtr& glyph) {

// what if this glyph is not from current page ???? glyph won't be found and
// assertion will failed
std::cout << "Document::Remove()" << std::endl;
assert(glyph != nullptr && "Cannot remove glyph by nullptr");
currentPage->Remove(glyph);
compositor->Compose();
Expand Down
10 changes: 10 additions & 0 deletions test/model/text_editor_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2060,4 +2060,14 @@ TEST(Tmp_test, Tmp_test) {
std::cout << pasted.size() << std::endl;
std::cout << *pasted.front() << std::endl;
std::cout << *pasted.back() << std::endl;
}

TEST(Document_Cursor1,
DocumentCursorInTheFirstEmptyRow_WhenCalles_ReturnGlyphNextToCursor) {
auto d = std::make_shared<Document>(std::make_shared<SimpleCompositor>());

Glyph::GlyphPtr selectedGlyph = d->GetSelectedGlyph();
std::cout << *selectedGlyph << std::endl;
Row::RowPtr selectedRow = std::dynamic_pointer_cast<Row>(selectedGlyph);
EXPECT_TRUE(selectedRow != nullptr);
}

0 comments on commit 2dbf713

Please sign in to comment.