From cd89787351b8f50e89d14962692eb1470f6e6547 Mon Sep 17 00:00:00 2001 From: Przemyslaw Brudny Date: Mon, 27 Jul 2020 18:55:29 +0200 Subject: [PATCH] [EGD-3410] TextLine inheritance to Rect added. --- .../data/PhonebookStyle.hpp | 6 +- .../models/NewContactModel.cpp | 94 +++++++++---------- .../widgets/InputLineWithLabelItem.cpp | 13 +-- module-gui/gui/widgets/Item.hpp | 2 +- module-gui/gui/widgets/Text.cpp | 2 +- module-gui/gui/widgets/TextFixedSize.cpp | 59 +++++++++--- module-gui/gui/widgets/TextFixedSize.hpp | 3 +- module-gui/gui/widgets/TextLine.cpp | 21 ++++- module-gui/gui/widgets/TextLine.hpp | 12 +-- 9 files changed, 129 insertions(+), 83 deletions(-) diff --git a/module-apps/application-phonebook/data/PhonebookStyle.hpp b/module-apps/application-phonebook/data/PhonebookStyle.hpp index 282f94b849e97cde644a9a7facf8eec2ca79721f..0824011e5e24ef37760b89af53a4eb5e35aba208 100644 --- a/module-apps/application-phonebook/data/PhonebookStyle.hpp +++ b/module-apps/application-phonebook/data/PhonebookStyle.hpp @@ -129,10 +129,10 @@ namespace phonebookStyle namespace inputLineWithLabelItem { constexpr uint32_t w = style::window::default_body_width; - constexpr uint32_t h = 200; + constexpr uint32_t h = 75; constexpr uint32_t title_label_h = 20; - constexpr uint32_t input_text_h = 100; - constexpr uint32_t span_size = 15; + constexpr uint32_t input_text_h = 33; + constexpr uint32_t span_size = 100; } // namespace inputLineWithLabelItem namespace multiLineTextWithLabelItem diff --git a/module-apps/application-phonebook/models/NewContactModel.cpp b/module-apps/application-phonebook/models/NewContactModel.cpp index 5046f9236348195d14a8f5f1982947084b00f7b3..8ac65e4295806d31de25d3f57e7eaa6420f43d31 100644 --- a/module-apps/application-phonebook/models/NewContactModel.cpp +++ b/module-apps/application-phonebook/models/NewContactModel.cpp @@ -41,53 +41,53 @@ void NewContactModel::createData() [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); - internalData.push_back(new gui::InputLineWithLabelItem( - phonebookInternals::ListItemName::SecondName, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, - [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); - - internalData.push_back(new gui::InputLineWithLabelItem( - phonebookInternals::ListItemName::Number, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, - [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); - - internalData.push_back(new gui::InputLineWithLabelItem( - phonebookInternals::ListItemName::OtherNumber, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, - [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); - - internalData.push_back(new gui::InputLineWithLabelItem( - phonebookInternals::ListItemName::Email, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, - [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); - - internalData.push_back(new gui::InputBoxWithLabelAndIconItem(phonebookInternals::ListItemName::SpeedDialKey)); - - internalData.push_back(new gui::InputBoxWithLabelAndIconItem( - phonebookInternals::ListItemName::AddToFavourites, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); })); - - internalData.push_back(new gui::InputBoxWithLabelAndIconItem( - phonebookInternals::ListItemName::AddToICE, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); })); - - internalData.push_back(new gui::InputLineWithLabelItem( - phonebookInternals::ListItemName::Address, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, - [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); - - internalData.push_back(new gui::InputLineWithLabelItem( - phonebookInternals::ListItemName::Note, - [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, - [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, - [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); + // internalData.push_back(new gui::InputLineWithLabelItem( + // phonebookInternals::ListItemName::SecondName, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, + // [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); + // + // internalData.push_back(new gui::InputLineWithLabelItem( + // phonebookInternals::ListItemName::Number, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, + // [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); + // + // internalData.push_back(new gui::InputLineWithLabelItem( + // phonebookInternals::ListItemName::OtherNumber, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, + // [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); + // + // internalData.push_back(new gui::InputLineWithLabelItem( + // phonebookInternals::ListItemName::Email, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, + // [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); + // + // internalData.push_back(new gui::InputBoxWithLabelAndIconItem(phonebookInternals::ListItemName::SpeedDialKey)); + // + // internalData.push_back(new gui::InputBoxWithLabelAndIconItem( + // phonebookInternals::ListItemName::AddToFavourites, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); })); + // + // internalData.push_back(new gui::InputBoxWithLabelAndIconItem( + // phonebookInternals::ListItemName::AddToICE, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); })); + // + // internalData.push_back(new gui::InputLineWithLabelItem( + // phonebookInternals::ListItemName::Address, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, + // [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); + // + // internalData.push_back(new gui::InputLineWithLabelItem( + // phonebookInternals::ListItemName::Note, + // [app](const UTF8 &text) { app->getCurrentWindow()->bottomBarTemporaryMode(text, false); }, + // [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); }, + // [app]() { app->getCurrentWindow()->selectSpecialCharacter(); })); for (auto item : internalData) { item->deleteByList = false; diff --git a/module-apps/application-phonebook/widgets/InputLineWithLabelItem.cpp b/module-apps/application-phonebook/widgets/InputLineWithLabelItem.cpp index 4c743c985a5c02a967ff8b91d960d1f0ec7b1aa6..a50042d988a37e5d1b7f42e187067a067b04e140 100644 --- a/module-apps/application-phonebook/widgets/InputLineWithLabelItem.cpp +++ b/module-apps/application-phonebook/widgets/InputLineWithLabelItem.cpp @@ -14,8 +14,7 @@ namespace gui std::function selectSpecialCharacter) : listItemName(listItemName) { - setMinimumSize(phonebookStyle::inputLineWithLabelItem::w, phonebookStyle::inputLineWithLabelItem::h); - setMaximumSize(phonebookStyle::inputLineWithLabelItem::w, phonebookStyle::inputLineWithLabelItem::h); + setMinimumSize(phonebookStyle::inputLineWithLabelItem::w, phonebookStyle::inputLineWithLabelItem::h * 4); setMargins(gui::Margins(0, style::margins::very_big, 0, 0)); vBox = new VBox(this, 0, 0, 0, phonebookStyle::inputLineWithLabelItem::title_label_h); @@ -27,11 +26,12 @@ namespace gui titleLabel->setFont(style::window::font::small); titleLabel->activeItem = false; - new gui::Span(vBox, Axis::Y, phonebookStyle::inputLineWithLabelItem::span_size); // spread title & inputText + inputText = new TextFixedSize(vBox, 0, 0, 0, 0); + inputText->setMaximumSize(phonebookStyle::inputLineWithLabelItem::w, 600); + inputText->setMargins(Margins(0, phonebookStyle::inputLineWithLabelItem::span_size, 0, 0)); - inputText = new Text(vBox, 0, 0, 0, phonebookStyle::inputLineWithLabelItem::input_text_h); inputText->setEdges(RectangleEdgeFlags::GUI_RECT_EDGE_NO_EDGES); - inputText->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Bottom)); + inputText->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top)); inputText->setFont(style::window::font::medium); inputText->setInputMode(new InputMode( {InputMode::ABC, InputMode::abc, InputMode::digit}, @@ -58,9 +58,6 @@ namespace gui vBox->setPosition(0, 0); vBox->setSize(newDim.w, newDim.h); - // this shouldn't be needed - without it there will be nothing shown in place of digit labels - titleLabel->setSize(newDim.w, phonebookStyle::inputLineWithLabelItem::title_label_h); - inputText->setSize(newDim.w, phonebookStyle::inputLineWithLabelItem::input_text_h); return true; } diff --git a/module-gui/gui/widgets/Item.hpp b/module-gui/gui/widgets/Item.hpp index 4b496c078e48ea6a3416018373d50cb06ea5177a..6371432ce9fabc25f8ab2fc6977aed0e1c577fe8 100644 --- a/module-gui/gui/widgets/Item.hpp +++ b/module-gui/gui/widgets/Item.hpp @@ -287,7 +287,7 @@ namespace gui { return (widgetArea.y); } - [[nodiscard]] uint32_t getWidth() const + [[nodiscard]] virtual uint32_t getWidth() const { return (widgetArea.w); } diff --git a/module-gui/gui/widgets/Text.cpp b/module-gui/gui/widgets/Text.cpp index 6c279b22144129d115a05eb5753f834c10e71bad..8d66fb337c5ba7f884347c145f25ce3ada1f3d89 100644 --- a/module-gui/gui/widgets/Text.cpp +++ b/module-gui/gui/widgets/Text.cpp @@ -296,7 +296,7 @@ namespace gui auto line_x_position = padding.left; do { - auto text_line = gui::TextLine(document.get(), cursor, w); + auto text_line = gui::TextLine(this, document.get(), cursor, w); cursor += text_line.length(); if (text_line.length() == 0) { diff --git a/module-gui/gui/widgets/TextFixedSize.cpp b/module-gui/gui/widgets/TextFixedSize.cpp index d21fa9388af425059c34aa13d989285814f8aede..bbb59309b8c30bd80e32d2d96ddb7c7b965f8735 100644 --- a/module-gui/gui/widgets/TextFixedSize.cpp +++ b/module-gui/gui/widgets/TextFixedSize.cpp @@ -7,9 +7,6 @@ namespace gui { this->linesCount = linesCount; setEditMode(EditMode::EDIT); - // - // inputText->setMinimumSize(phonebookStyle::inputLineWithLabelItem::w,phonebookStyle::inputLineWithLabelItem::input_text_h); - // inputText->setMaximumSize(phonebookStyle::inputLineWithLabelItem::w,phonebookStyle::inputLineWithLabelItem::input_text_h); } void TextFixedSize::setLines(const unsigned int val) @@ -32,24 +29,33 @@ namespace gui return size; }; - uint32_t w = sizeMinusPadding(Axis::X, Area::Max); - // uint32_t h = sizeMinusPadding(Axis::Y, Area::Max); + uint32_t w = sizeMinusPadding(Axis::X, Area::Normal); + uint32_t h = sizeMinusPadding(Axis::Y, Area::Normal); auto line_y_position = padding.top; auto cursor = 0; unsigned int currentLine = 0; + unsigned int lineHeight = 0; auto line_x_position = padding.left; do { - auto text_line = gui::TextLine(document.get(), cursor, w); + auto text_line = gui::TextLine(this, document.get(), cursor, w); cursor += text_line.length(); - if (text_line.length() == 0) { + if (text_line.length() == 0 && currentLine == 0) { + break; + } + + if (text_line.height() > 0 && lineHeight != text_line.height()) { + lineHeight = text_line.height(); + } + + LOG_INFO("Wtf? %d", line_y_position + lineHeight); + LOG_INFO("wtf 2 %d", h); + + if (line_y_position + lineHeight > h) { // no more space for next line break; } - // if (line_y_position + text_line.height() > h) { // no more space for next line - // break; - // } // for each different text which fits in line, addWidget last - to not trigger callbacks to parent // on resizes while not needed, after detach there can be no `break` othervise there will be leak - hence @@ -58,13 +64,38 @@ namespace gui auto &line = lines.last(); line.setPosition(line_x_position, line_y_position); line.setParent(this); - line.alignH(getAlignment(Axis::X), w); + // line.alignH(getAlignment(Axis::X), w); + + line_y_position += lineHeight; + + LOG_INFO("Jaka wyskosc lini %d", text_line.height()); + LOG_INFO("Ile mamy lini %d", currentLine); + + LOG_INFO("debug text drawing: \n start cursor: %d line length: %d end cursor %d : document length " + "%d \n x: %d, y: %d \n%s", + cursor - lines.last().length(), + lines.last().length(), + cursor, + document->getText().length(), + line_x_position, + line_y_position, + [&]() -> std::string { + std::string text = document->getText(); + return std::string(text.begin() + cursor - lines.last().length(), text.begin() + cursor); + }() + .c_str()); - line_y_position += line.height(); currentLine++; - } while (currentLine != linesCount); + } while (true); - lines.linesVAlign(sizeMinusPadding(Axis::Y, Area::Normal)); + // lines.linesVAlign(sizeMinusPadding(Axis::Y, Area::Normal)); + } + + bool TextFixedSize::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) + { + Text::onDimensionChanged(oldDim, newDim); + drawLines(); + return true; } } // namespace gui diff --git a/module-gui/gui/widgets/TextFixedSize.hpp b/module-gui/gui/widgets/TextFixedSize.hpp index 52e97d3b70e53ff56c6807efe5a7d8c1e8c88006..3c176391a7f0dbc737a8937cd1a4561cf2e511f0 100644 --- a/module-gui/gui/widgets/TextFixedSize.hpp +++ b/module-gui/gui/widgets/TextFixedSize.hpp @@ -8,9 +8,10 @@ namespace gui class TextFixedSize : public Text { protected: - unsigned int linesCount = 1; + unsigned int linesCount = 3; void drawLines() override; + bool onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) override; public: TextFixedSize(Item *parent, Position x, Position y, Length w, Length h, unsigned int linesCount = 1); diff --git a/module-gui/gui/widgets/TextLine.cpp b/module-gui/gui/widgets/TextLine.cpp index c2f89ab235a91f9bb33f082d127677fae42b3933..5ae32e563356719c2ea57ef791e06bdfd1421208 100644 --- a/module-gui/gui/widgets/TextLine.cpp +++ b/module-gui/gui/widgets/TextLine.cpp @@ -5,6 +5,7 @@ #include "TextDocument.hpp" #include #include +#include "log/log.hpp" namespace gui { @@ -23,17 +24,22 @@ namespace gui /// Note - line breaking could be done here with different TextLines to return /// or via different block types (i.e. numeric block tyle could be not "breakable" - TextLine::TextLine(TextDocument *document, unsigned int start_position, unsigned int max_width) + TextLine::TextLine(Item *parent, TextDocument *document, unsigned int start_position, unsigned int max_width) { if (document == nullptr) { return; } + + this->parent = parent; + auto cursor = document->getBlockCursor(start_position); auto old_cursor = cursor; do { + if (!cursor) { // cursor is faulty return; } + // it would be better if cursor would know what to do when it jumps over blocks if (old_cursor.getBlockNr() != cursor.getBlockNr() && (*document)(old_cursor).getEnd() == TextBlock::End::Newline) { @@ -46,6 +52,7 @@ namespace gui if (text_format->getFont() == nullptr) { return; } + auto can_show = text_format->getFont()->getCharCountInSpace(text_part.text, max_width - width_used); // we can show nothing - this is the end of this line @@ -58,6 +65,7 @@ namespace gui number_letters_shown += can_show; width_used += item->getTextNeedSpace(); height_used = std::max(height_used, item->getTextHeight()); + elements_to_show_in_line.emplace_back(item); // not whole text shown, try again for next line if you want @@ -109,7 +117,7 @@ namespace gui } }; - void TextLine::setPosition(int32_t x, int32_t y) + void TextLine::setPosition(const short &x, const short &y) { auto line_x_position = x; for (auto &el : elements_to_show_in_line) { @@ -125,6 +133,15 @@ namespace gui if (parent == nullptr) { return; } + + setArea({int16_t(0), int16_t(0), uint16_t(100), uint16_t(100)}); + setEdges(RectangleEdgeFlags::GUI_RECT_ALL_EDGES); + setFillColor(ColorFullBlack); + setFilled(true); + updateDrawArea(); + + LOG_INFO("Rozmiar i pozycja tego gówna %d, %d, %d, %d", widgetArea.x, widgetArea.y, widgetArea.w, widgetArea.h); + for (auto &el : elements_to_show_in_line) { parent->addWidget(el); } diff --git a/module-gui/gui/widgets/TextLine.hpp b/module-gui/gui/widgets/TextLine.hpp index 367edf27471255b57746182baf5034076176d2f6..b4c9d659db107ef7d1adb378f23925c00257be81 100644 --- a/module-gui/gui/widgets/TextLine.hpp +++ b/module-gui/gui/widgets/TextLine.hpp @@ -9,7 +9,7 @@ namespace gui { /// interface element for TextDocument->getLine() <-- Text - class TextLine + class TextLine : public Rect { unsigned int number_letters_shown = 0; Length width_used = 0; @@ -19,7 +19,7 @@ namespace gui public: /// creates TextLine with data from text from start position in `TextDocument` filling max_width /// @note might be better to have TextBlockIterator which could hop through TextBlock inside TextDocument - TextLine(TextDocument *, unsigned int start_position, unsigned int max_width); + TextLine(Item *parent, TextDocument *, unsigned int start_position, unsigned int max_width); ~TextLine(); TextLine(TextLine &) = delete; TextLine(TextLine &&); @@ -63,11 +63,11 @@ namespace gui return elements_to_show_in_line.front()->area().pos(Axis::X); } - void setPosition(int32_t x, int32_t y); + void setPosition(const short &x, const short &y) override; void setParent(Item *parent); - Length getWidth() const; - Length getWidthTo(unsigned int pos) const; - void erase(); + [[nodiscard]] Length getWidth() const override; + [[nodiscard]] Length getWidthTo(unsigned int pos) const; + void erase() override; /// align TextLine due to alignment axis in parent_width /// /// moves Text parts in Text. To not call n times callbacks on resize, call prior to setting parent