From cb13c409477fed5d05919145f41791977bf10e8f Mon Sep 17 00:00:00 2001 From: Robert Borzecki Date: Tue, 20 Aug 2019 13:02:35 +0200 Subject: [PATCH] Fixed kbd profile error with loading hex values. --- image/assets/profiles/lang_eng_lower.kprof | 4 +- module-apps/Application.cpp | 16 ++- .../windows/NotesEditWindow.cpp | 1 + module-apps/windows/AppWindow.cpp | 10 ++ module-apps/windows/AppWindow.hpp | 2 + module-gui/gui/input/Profile.cpp | 7 +- module-gui/gui/widgets/Text.cpp | 123 +++++++++++++++++- module-gui/gui/widgets/Text.hpp | 20 ++- module-gui/gui/widgets/TopBar.cpp | 8 ++ module-gui/gui/widgets/TopBar.hpp | 3 + module-utils/utf8/UTF8.cpp | 14 ++ module-utils/utf8/UTF8.hpp | 5 + 12 files changed, 203 insertions(+), 10 deletions(-) diff --git a/image/assets/profiles/lang_eng_lower.kprof b/image/assets/profiles/lang_eng_lower.kprof index 55c8c683b..2fe0596c1 100644 --- a/image/assets/profiles/lang_eng_lower.kprof +++ b/image/assets/profiles/lang_eng_lower.kprof @@ -51,12 +51,12 @@ false ' ' 0 #NumericKeyAst = 61, -31 +61 false 0x08 0 #NumericKeyPnd = 63, -31 +63 false 0x0A 0 diff --git a/module-apps/Application.cpp b/module-apps/Application.cpp index 933a2ba45..0701b5a06 100644 --- a/module-apps/Application.cpp +++ b/module-apps/Application.cpp @@ -6,6 +6,8 @@ * @copyright Copyright (C) 2019 mudita.com * @details */ +#include +#include //module-utils #include "log/log.hpp" //module-services @@ -200,10 +202,20 @@ sys::Message_t Application::DataReceivedHandler(sys::DataMessage* msgl) { time_t timestamp = msg->timestamp; struct tm time; time =*localtime(×tamp); - //LOG_INFO("%02d:%02d", time.tm_hour - time.tm_gmtoff / 3600, time.tm_min); uint32_t hour = (timestamp % 86400) / 3600; uint32_t min = (timestamp % 3600) / 60; - LOG_INFO("%02d:%02d", hour , min); + + std::ostringstream stringStream; + stringStream << std::setfill('0') << std::setw(2) << hour << ":" << min; + + std::string timeStr = stringStream.str(); + + LOG_INFO("%s", timeStr.c_str() ); + + currentWindow->updateTime( timeStr ); + if( state == State::ACTIVE_FORGROUND ) + refreshWindow( gui::RefreshModes::GUI_REFRESH_DEEP ); + handled = true; } diff --git a/module-apps/application-notes/windows/NotesEditWindow.cpp b/module-apps/application-notes/windows/NotesEditWindow.cpp index 3d28a5b69..63d1d8a0a 100644 --- a/module-apps/application-notes/windows/NotesEditWindow.cpp +++ b/module-apps/application-notes/windows/NotesEditWindow.cpp @@ -69,6 +69,7 @@ NotesEditWindow::~NotesEditWindow() { } void NotesEditWindow::onBeforeShow( ShowMode mode, uint32_t command, SwitchData* data ) { + application->setKeyboardProfile( "lang_eng_lower" ); setFocusItem( text ); LOG_INFO("SETTING TEXT"); text->setText("Very long test line ABCDEFGHIJKLMNOPQRST123456789\nabcdefghijklmnopqrs 123456789 ABCDEFGHIJKLMONPQRSTUW 12345\n test\nnew line\n\n\n12345"); diff --git a/module-apps/windows/AppWindow.cpp b/module-apps/windows/AppWindow.cpp index f020f7bf0..2fcb72554 100644 --- a/module-apps/windows/AppWindow.cpp +++ b/module-apps/windows/AppWindow.cpp @@ -83,6 +83,16 @@ bool AppWindow::updateSignalStrength( uint32_t strength ) { return true; } +bool AppWindow::updateTime( const UTF8& timeStr ) { + topBar->setTime(timeStr); + return true; +} + +bool AppWindow::updateTime( const uint32_t& timestamp, bool mode24H ) { + topBar->setTime( timestamp, mode24H ); + return true; +} + std::list AppWindow::buildDrawList() { return Window::buildDrawList(); } diff --git a/module-apps/windows/AppWindow.hpp b/module-apps/windows/AppWindow.hpp index 5a6a399f5..59d071458 100644 --- a/module-apps/windows/AppWindow.hpp +++ b/module-apps/windows/AppWindow.hpp @@ -54,6 +54,8 @@ public: bool updateBatteryLevel( uint32_t percentage ); //updates battery level in the window bool updateSignalStrength( uint32_t strength ); + bool updateTime( const UTF8& timeStr ); + bool updateTime( const uint32_t& timestamp, bool mode24H ); void rebuild() override; void buildInterface() override; diff --git a/module-gui/gui/input/Profile.cpp b/module-gui/gui/input/Profile.cpp index 74bae4b3e..64c816425 100644 --- a/module-gui/gui/input/Profile.cpp +++ b/module-gui/gui/input/Profile.cpp @@ -7,6 +7,7 @@ * @details */ #include +#include #include #include #include "log/log.hpp" @@ -127,8 +128,10 @@ void Profile::addCharacters( KeyProfile* pk, const std::string& s ) { pk->chars.push_back(charKey); } else if( ts.substr(0,2) == "0x"){ - std::stringstream(ts) >> charKey; - pk->chars.push_back(charKey); + std::stringstream ss; + ss << std::hex << ts; + ss >> charKey; +c pk->chars.push_back(charKey); } } } diff --git a/module-gui/gui/widgets/Text.cpp b/module-gui/gui/widgets/Text.cpp index 51fd8f010..9abbef387 100644 --- a/module-gui/gui/widgets/Text.cpp +++ b/module-gui/gui/widgets/Text.cpp @@ -211,12 +211,107 @@ void Text::splitTextToLines( const UTF8& text) { firstLine = textLines.begin(); lastLine = textLines.begin(); -// for( TextLine* tl : textLines ) -// LOG_INFO("Text Input Line: [%s]", tl->text.c_str()); +} + +bool Text::splitText( UTF8& source, UTF8& remaining, LineEndType& endType, uint32_t availableSpace ){ + + uint32_t spaceConsumed; + uint32_t charCount = font->getCharCountInSpace( source, availableSpace, spaceConsumed ); + //this is sub-string that fits available space. + UTF8 searchStr = source.substr( 0, charCount ); + + //try to find first enter. + uint32_t enterIndex = searchStr.find( "\n",0); + if( enterIndex != UTF8::npos ) { + endType = LineEndType::BREAK; + remaining = source.substr( enterIndex, source.length() - 1 - enterIndex ); + source.split( enterIndex ); + LOG_INFO("Split Text: source: [%s] remaining: [%s]", source.c_str(), remaining.c_str()); + return true; + } + else { + //if there was no enter look for last space in the source and break line on it + uint32_t spaceIndex = searchStr.findLast( " ",searchStr.length()-1); + + //if there was no space take as many characters as possible and add CONTINUE ending + if( spaceIndex == UTF8::npos ) { + remaining = source.split( charCount ); + endType = LineEndType::CONTINUE; + LOG_INFO("Split Text: source: [%s] remaining: [%s]", source.c_str(), remaining.c_str()); + return true; + } + else { + endType = LineEndType::CONTINUE_SPACE; + + remaining = source.substr( spaceIndex+1, source.length() - 1 - spaceIndex); + source.split( spaceIndex ); + LOG_INFO("Split Text: source: [%s] remaining: [%s]", source.c_str(), remaining.c_str()); + return true; + } + } + return false; } void Text::reworkLines( std::list::iterator it ) { + //iterate until end of text lines or till line that fits available space has break line ending (enter). + while( it != textLines.end() ) { + + //if current line has BREAK of EOT line ending check if current text fits available space + //finish procedure + uint32_t availableSpace = getAvailableHPixelSpace(); + uint32_t consumedSpace; + + if( ((*it)->endType == LineEndType::BREAK) || ((*it)->endType == LineEndType::EOT) ) { + consumedSpace = font->getPixelWidth( (*it)->getText() ); + if( consumedSpace < availableSpace ) + break; + } + + //check if there is next line + auto itNext = it; + itNext++; + + UTF8 mergedLinesText = (*it)->getTextWithEnding(); + + //if processed text line is not finished with break end type + if( ((*it)->endType != LineEndType::BREAK ) && (itNext != textLines.end()) ) { + + //merge text from two lines + mergedLinesText += (*itNext)->getTextWithEnding(); + //assign end type from next line to the current line + (*it)->endType = (*itNext)->endType; + + //remove next line as the text was taken to the current line + delete (*itNext ); + textLines.erase( itNext ); + } + + LineEndType endType; + UTF8 remainingText; + bool splitFlag = splitText( mergedLinesText, remainingText, endType, availableSpace ); + + //if there was a split update current and next item in the list + if( splitFlag ) { + + (*it)->text = std::move(mergedLinesText); + (*it)->pixelLength = font->getPixelWidth( (*it)->getText() ); + + itNext = it; + itNext++; + textLines.insert( itNext, new TextLine( remainingText, + 0, remainingText.length(), (*it)->endType, font->getPixelWidth( remainingText ) ) ); + + (*it)->endType = endType; + } + + //proceed to next line + it++; + + } + + //TODO starting from first modified line up to last modified line update start and end index + } @@ -338,6 +433,28 @@ bool Text::handleNavigation( const InputEvent& inputEvent ) { } bool Text::handleEnter() { + + //get textline where cursor is located + auto it = firstLine; + std::advance(it, cursorRow ); + reworkLines( it ); + + //split current text in line using cursors position + UTF8 remainingText = (*it)->text.split( cursorColumn ); + + //store old type of line ending set new type of ending to the current line + LineEndType endType = (*it)->endType; + (*it)->endType = LineEndType::BREAK; + + //create and add new line using remaining parts of text + auto itNext = it; + ++itNext; + textLines.insert( itNext, new TextLine( remainingText, 0, remainingText.length(), endType, font->getPixelWidth( remainingText) ) ); + cursorRow++; + cursorColumn = 0; + + recalculateDrawParams(); + return true; } @@ -366,7 +483,7 @@ bool Text::handleChar( const InputEvent& inputEvent ) { std::advance(it, cursorRow ); reworkLines( it ); - //change cursor position + //change cursor position to the end of current line } //no line splitting, update pixel width and proceed else { diff --git a/module-gui/gui/widgets/Text.hpp b/module-gui/gui/widgets/Text.hpp index 5dcfac3e9..7efe8c071 100644 --- a/module-gui/gui/widgets/Text.hpp +++ b/module-gui/gui/widgets/Text.hpp @@ -74,6 +74,20 @@ protected: uint32_t pixelLength = 0; TextLine( const UTF8& text, uint32_t startIndex, uint32_t endIndex, LineEndType endType, uint32_t pixelLength ); + /** + * Returns text with appropriate ending. + */ + UTF8 getText() { + return text; + } + UTF8 getTextWithEnding() { + UTF8 retText = text; + if( endType == LineEndType::BREAK ) + retText.insert("\n"); + else if( endType == LineEndType::CONTINUE_SPACE ) + retText.insert(" "); + return retText; + } }; //holds list of all lines that text was divided to. @@ -112,8 +126,12 @@ protected: * Iterate over lines starting from the one that is provided. Function concatenate lines and performs new split. * Function stops on the last line or it there is a lines break ( enter ) or if last concatenated line doesn;t change after update. */ - void reworkLines( std::list::iterator it ); + /** + * Splites text in two on ENTER, space or dot. + * @return true if line was actually splitted + */ + bool splitText( UTF8& source, UTF8& remaining, LineEndType& endType, uint32_t availableSpace ); /** * Moves cursor in specified direction */ diff --git a/module-gui/gui/widgets/TopBar.cpp b/module-gui/gui/widgets/TopBar.cpp index 7e498b12b..93d4bd335 100644 --- a/module-gui/gui/widgets/TopBar.cpp +++ b/module-gui/gui/widgets/TopBar.cpp @@ -120,4 +120,12 @@ void TopBar::setSignalStrength( uint32_t sth) { signal[signalStrength]->setVisible( true ); }; +void TopBar::setTime( const UTF8& time ) { + timeLabel->setText(time); +} + +void TopBar::setTime( const uint32_t& time, bool mode24H ) { + +} + } /* namespace gui */ diff --git a/module-gui/gui/widgets/TopBar.hpp b/module-gui/gui/widgets/TopBar.hpp index 0208f4803..ca0c0602f 100644 --- a/module-gui/gui/widgets/TopBar.hpp +++ b/module-gui/gui/widgets/TopBar.hpp @@ -68,6 +68,9 @@ public: void setSignalStrength( uint32_t sth); uint32_t getBatteryLevel() {return batteryLevel; }; uint32_t getSignalStrength() {return signalStrength; }; + + void setTime( const UTF8& time ); + void setTime( const uint32_t& time, bool mode24H ); }; } /* namespace gui */ diff --git a/module-utils/utf8/UTF8.cpp b/module-utils/utf8/UTF8.cpp index 0a47a9e93..60049db82 100644 --- a/module-utils/utf8/UTF8.cpp +++ b/module-utils/utf8/UTF8.cpp @@ -330,6 +330,20 @@ bool UTF8::operator==( const UTF8& utf ) const { const char* UTF8::c_str() const { return reinterpret_cast(data); } + +void UTF8::clear() { + + if( data ) + delete data; + + data = new uint8_t[stringExpansion]; + sizeAllocated = stringExpansion; + sizeUsed = 1; + strLength = 0; + lastIndex = 0; + lastIndexData = data; +} + UTF8 UTF8::substr( const uint32_t begin, const uint32_t length) const { if( ( begin + length > this->length() ) || diff --git a/module-utils/utf8/UTF8.hpp b/module-utils/utf8/UTF8.hpp index a0ae87eb1..7c29e44e9 100644 --- a/module-utils/utf8/UTF8.hpp +++ b/module-utils/utf8/UTF8.hpp @@ -71,6 +71,11 @@ public: uint32_t used() { return sizeUsed; }; const char* c_str() const ; + /** + * @brief Removes all content from the string and reduce assigned memory to default value. + */ + void clear(); + /** *@brief Creates substring from current string. New string starts from begin parameter and contains number of characters passed by length. *@param begin Index of the first character in newly created string.