From cc76e4f278bd969a201230875b02e5cb02ca256c Mon Sep 17 00:00:00 2001 From: Robert Borzecki Date: Fri, 30 Aug 2019 10:25:57 +0200 Subject: [PATCH] Added line feature for label widget. --- .../windows/MenuWindow.cpp | 1 + module-gui/gui/core/Font.cpp | 9 +++ module-gui/gui/core/Font.hpp | 7 +++ module-gui/gui/widgets/Label.cpp | 59 +++++++++++++++---- module-gui/gui/widgets/Label.hpp | 9 +++ source/main.cpp | 11 ---- 6 files changed, 75 insertions(+), 21 deletions(-) diff --git a/module-apps/application-desktop/windows/MenuWindow.cpp b/module-apps/application-desktop/windows/MenuWindow.cpp index 7efa1450d..18e7bbfd5 100644 --- a/module-apps/application-desktop/windows/MenuWindow.cpp +++ b/module-apps/application-desktop/windows/MenuWindow.cpp @@ -31,6 +31,7 @@ MenuPage::MenuPage( gui::Item* parent, int32_t x, int32_t y, uint32_t w, uint32_ title->setFont("gt_pressura_regular_24"); title->setAlignement( gui::Alignment(gui::Alignment::ALIGN_HORIZONTAL_CENTER, gui::Alignment::ALIGN_VERTICAL_CENTER)); title->setText( titleName ); + title->setLineMode(true); uint32_t rowCount = tilesDescription.size()/3; diff --git a/module-gui/gui/core/Font.cpp b/module-gui/gui/core/Font.cpp index 180654444..388560d84 100644 --- a/module-gui/gui/core/Font.cpp +++ b/module-gui/gui/core/Font.cpp @@ -312,6 +312,15 @@ uint32_t Font::getCharPixelWidth( uint32_t charCode ) { return 0; } +uint32_t Font::getCharPixelHeight( uint32_t charCode ) { + FontGlyph* glyph = glyphs.find(charCode)->second; + + if( glyph != NULL) + return glyph->height ; + + return 0; +} + FontManager::FontManager() { } diff --git a/module-gui/gui/core/Font.hpp b/module-gui/gui/core/Font.hpp index ae7c27a13..ddbf88297 100644 --- a/module-gui/gui/core/Font.hpp +++ b/module-gui/gui/core/Font.hpp @@ -134,7 +134,14 @@ public: * @return Number of pixels in width occupied by string. */ uint32_t getPixelWidth( const UTF8& str ); + /** + * @brief returns number of pixels occupied by character horizontally. + */ uint32_t getCharPixelWidth( uint32_t charCode ); + /** + * @brief Returns number of pixels occupied by the character vertically. + */ + uint32_t getCharPixelHeight( uint32_t charCode ); const std::string getName() { return info.face; diff --git a/module-gui/gui/widgets/Label.cpp b/module-gui/gui/widgets/Label.cpp index ceacf8d30..574e774ae 100644 --- a/module-gui/gui/widgets/Label.cpp +++ b/module-gui/gui/widgets/Label.cpp @@ -115,21 +115,13 @@ void Label::calculateDisplayText() { textArea.y = (widgetArea.h - font->info.line_height )/2 + font->info.base; -// textArea.y = -// (drawArea.h - font->info.line_height )/2 + -// font->info.base; } else if( alignment.isAligned(Alignment::ALIGN_VERTICAL_TOP )) { -// textArea.y = font->info.base + margins.top; textArea.y = font->info.base + margins.top; } else if( alignment.isAligned(Alignment::ALIGN_VERTICAL_BOTTOM )) { -// textArea.y = -// drawArea.h - -// font->info.line_height + font->info.base - -// margins.bottom; textArea.y = widgetArea.h - font->info.line_height + font->info.base - @@ -139,7 +131,6 @@ void Label::calculateDisplayText() { //calculate horizontal position o text if( alignment.isAligned(Alignment::ALIGN_HORIZONTAL_CENTER )) { -// textArea.x =( drawArea.w - textArea.w )/2; textArea.x =( widgetArea.w - textArea.w )/2; } else if( alignment.isAligned(Alignment::ALIGN_HORIZONTAL_LEFT )) @@ -148,9 +139,36 @@ void Label::calculateDisplayText() { } else if( alignment.isAligned(Alignment::ALIGN_HORIZONTAL_RIGHT )) { -// textArea.x = drawArea.w - textArea.w - margins.right; textArea.x = widgetArea.w - textArea.w - margins.right; } + + //if dots mode is disabled and line mode is enabled calculate positiona and width of the line + if( (!dotsMode) && (lineMode) && (lineFront!=nullptr) ) { + uint32_t spaceWidth = font->getCharPixelWidth(' '); + int32_t lineW = availableSpace - stringPixelWidth ; + uint32_t lineY = textArea.y - font->getCharPixelHeight('a')/2; + if( lineW < 0 ) lineW = 0; + + lineFront->setVisible(true); + lineBack->setVisible(true); + //both lines are visible + if( alignment.isAligned(Alignment::ALIGN_HORIZONTAL_CENTER )) { + lineFront->setPosition( 0, lineY ); + lineFront->setSize( lineW/2 - spaceWidth, 2 ); + lineBack->setPosition( lineW/2 + stringPixelWidth +spaceWidth, lineY); + lineBack->setSize( lineW/2 -spaceWidth, 2 ); + } + else if( alignment.isAligned(Alignment::ALIGN_HORIZONTAL_RIGHT)) { + lineFront->setPosition( 0, lineY ); + lineFront->setSize( lineW-spaceWidth, 2 ); + lineBack->setVisible(false); + } + else if( alignment.isAligned(Alignment::ALIGN_HORIZONTAL_LEFT )) { + lineBack->setPosition( stringPixelWidth +spaceWidth, lineY); + lineBack->setSize( lineW-spaceWidth, 2 ); + lineFront->setVisible(false); + } + } } void Label::setText( const UTF8& text ) { @@ -181,6 +199,27 @@ void Label::setDotsMode( const bool val ) { calculateDisplayText(); } +void Label::setLineMode( const bool& val ) { + //if line mode is disable remove the line if it was previously created + if( val == false ) { + if( lineFront != nullptr ) { + this->removeWidget( lineFront ); + this->removeWidget( lineBack ); + delete lineFront; + delete lineBack; + lineFront = nullptr; + lineBack = nullptr; + } + } + else { + if( lineFront == nullptr ) { + lineFront = new Rect( this, 0,0,0,0 ); + lineBack = new Rect( this, 0,0,0,0 ); + } + } + calculateDisplayText(); +} + std::list Label::buildDrawList() { std::list commands; diff --git a/module-gui/gui/widgets/Label.hpp b/module-gui/gui/widgets/Label.hpp index 01895580e..1fc0b283e 100644 --- a/module-gui/gui/widgets/Label.hpp +++ b/module-gui/gui/widgets/Label.hpp @@ -42,10 +42,15 @@ protected: Alignment alignment; //flag that defines if 3 dots are to be displayed at the end of label's text. bool dotsMode; + bool lineMode; //area specified in pixels occupied by text inside label space. //This defines also position of the text considering alignment and margins. BoundingBox textArea; + //widgets to add line + Rect* lineFront = nullptr; + Rect* lineBack = nullptr; + void calculateDisplayText(); public: @@ -60,6 +65,10 @@ public: virtual void setAlignement( const Alignment& alignment ); virtual void setMargins( const Margins& margins ); void setDotsMode( const bool val ); + /** + * @brief Defines if remaining area of the label has a horizontal line. + */ + void setLineMode( const bool& val ); void setTextColor( Color color ); void setFont( const UTF8& fontName ); diff --git a/source/main.cpp b/source/main.cpp index 518ac1de2..2b50f3d5f 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -145,22 +145,11 @@ int main() { //vector with launchers to applications std::vector > applications; - //launcher for viewer applications.push_back(std::unique_ptr(new app::ApplicationViewerLauncher())); - - //launcher for desktop application applications.push_back(std::unique_ptr(new app::ApplicationDesktopLauncher())); - - //launcher for call application applications.push_back(std::unique_ptr(new app::ApplicationCallLauncher())); - - //launcher for settings application applications.push_back(std::unique_ptr(new app::ApplicationSettingsLauncher())); - - //launcher for notes application applications.push_back(std::unique_ptr(new app::ApplicationNotesLauncher())); - - // create launcher for phonebook, all launchers could be created like that applications.push_back(app::CreateLauncher("ApplicationPhonebook")); //start application manager