From 72e6e0401677740a286ab937194c61db1448e4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 5 May 2017 22:35:47 +0200 Subject: Refactor TextEdit widget. --- plugingui/drumkitframecontent.cc | 8 +-- plugingui/filebrowser.cc | 4 +- plugingui/lineedit.cc | 2 +- plugingui/lineedit.h | 2 +- plugingui/textedit.cc | 131 ++++++++++++++++----------------------- plugingui/textedit.h | 38 +++++++----- 6 files changed, 84 insertions(+), 101 deletions(-) (limited to 'plugingui') diff --git a/plugingui/drumkitframecontent.cc b/plugingui/drumkitframecontent.cc index 3752cc3..f90f346 100644 --- a/plugingui/drumkitframecontent.cc +++ b/plugingui/drumkitframecontent.cc @@ -147,7 +147,7 @@ void DrumkitframeContent::resize(std::size_t width, std::size_t height) void DrumkitframeContent::kitBrowseClick() { - std::string path = drumkit_file.getLineEdit().text(); + std::string path = drumkit_file.getLineEdit().getText(); if(path == "") { path = config.lastkit; @@ -155,7 +155,7 @@ void DrumkitframeContent::kitBrowseClick() if(path == "") { - path = midimap_file.getLineEdit().text(); + path = midimap_file.getLineEdit().getText(); } file_browser.setPath(path); @@ -166,7 +166,7 @@ void DrumkitframeContent::kitBrowseClick() void DrumkitframeContent::midimapBrowseClick() { - std::string path = midimap_file.getLineEdit().text(); + std::string path = midimap_file.getLineEdit().getText(); if(path == "") { path = config.lastmidimap; @@ -174,7 +174,7 @@ void DrumkitframeContent::midimapBrowseClick() if(path == "") { - path = drumkit_file.getLineEdit().text(); + path = drumkit_file.getLineEdit().getText(); } file_browser.setPath(path); diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index 76a2744..62f7775 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -158,7 +158,7 @@ void FileBrowser::handleKeyEvent() { listbox.clearSelectedValue(); - std::string value = lineedit.text(); + std::string value = lineedit.getText(); if((value.size() > 1) && (value[0] == '@')) { DEBUG(filebrowser, "Selecting ref-file '%s'\n", value.c_str()); @@ -166,7 +166,7 @@ void FileBrowser::handleKeyEvent() return; } - dir.setPath(lineedit.text()); + dir.setPath(lineedit.getText()); changeDir(); } diff --git a/plugingui/lineedit.cc b/plugingui/lineedit.cc index d2e9dea..96bf56f 100644 --- a/plugingui/lineedit.cc +++ b/plugingui/lineedit.cc @@ -65,7 +65,7 @@ void LineEdit::setText(const std::string& text) textChanged(); } -std::string LineEdit::text() +std::string LineEdit::getText() { return _text; } diff --git a/plugingui/lineedit.h b/plugingui/lineedit.h index dd37776..86ad986 100644 --- a/plugingui/lineedit.h +++ b/plugingui/lineedit.h @@ -45,7 +45,7 @@ public: bool isFocusable() override { return true; } - std::string text(); + std::string getText(); void setText(const std::string& text); void setReadOnly(bool readonly); diff --git a/plugingui/textedit.cc b/plugingui/textedit.cc index 4de6e52..f57a1d4 100644 --- a/plugingui/textedit.cc +++ b/plugingui/textedit.cc @@ -26,14 +26,7 @@ */ #include "textedit.h" -#include "window.h" - -#include -#include -#include -#include - -#define BORDER 10 +#include "painter.h" namespace GUI { @@ -42,7 +35,7 @@ TextEdit::TextEdit(Widget* parent) : Widget(parent), scroll(this) { setReadOnly(true); - scroll.move(width() - 23, 1); + scroll.move(width() - 2*x_border - 3, y_border - 1); scroll.resize(16, 100); CONNECT(&scroll, valueChangeNotifier, this, &TextEdit::scrolled); } @@ -56,8 +49,8 @@ void TextEdit::resize(std::size_t width, std::size_t height) Widget::resize(width, height); needs_preprocessing = true; - scroll.resize(scroll.width(), height - 14); - scroll.move(width - 23, 7); + scroll.move(width - 2*x_border - 3, y_border - 1); + scroll.resize(scroll.width(), height - 2*(y_border - 1)); } void TextEdit::setReadOnly(bool readonly) @@ -72,75 +65,71 @@ bool TextEdit::readOnly() void TextEdit::setText(const std::string& text) { - _text = text; + this->text = text; needs_preprocessing = true; redraw(); textChangedNotifier(); } -std::string TextEdit::text() +std::string TextEdit::getText() { - return _text; + return text; } void TextEdit::preprocessText() { - preprocessedtext.clear(); - std::string text = _text; + std::vector lines; + + preprocessed_text.clear(); + std::string text = this->text; - { // Handle tab characters - for(size_t i = 0; i < text.length(); ++i) + // Handle tab characters + for(std::size_t i = 0; i < text.length(); ++i) + { + char ch = text.at(i); + if(ch == '\t') { - char ch = text.at(i); - if(ch == '\t') - { - text.erase(i, 1); - text.insert(i, 4, ' '); - } + text.erase(i, 1); + text.insert(i, 4, ' '); } } - { // Handle "\r" - for(size_t i = 0; i < text.length(); ++i) + // Handle "\r" + for(std::size_t i = 0; i < text.length(); ++i) + { + char ch = text.at(i); + if(ch == '\r') { - char ch = text.at(i); - if(ch == '\r') - { - text.erase(i, 1); - } + text.erase(i, 1); } } - std::list lines; - { // Handle new line characters - size_t pos = 0; - do - { - pos = text.find("\n"); - lines.push_back(text.substr(0, pos)); - text = text.substr(pos + 1); - } while(pos != std::string::npos); - } + // Handle new line characters + std::size_t pos = 0; + do + { + pos = text.find("\n"); + lines.push_back(text.substr(0, pos)); + text = text.substr(pos + 1); + } while(pos != std::string::npos); - { // Wrap long lines - std::list::iterator it; - for(it = lines.begin(); it != lines.end(); ++it) - { - std::string line = *it; + // Wrap long lines + for(auto it = lines.begin(); it != lines.end(); ++it) + { + auto line = *it; - for(size_t i = 0; i < line.length(); ++i) + for(std::size_t i = 0; i < line.length(); ++i) + { + auto linewidth = font.textWidth(line.substr(0, i)); + if(linewidth >= width() - 2*x_border - 10 - scroll.width()) { - size_t linewidth = font.textWidth(line.substr(0, i)); - if(linewidth >= width() - BORDER - 20 - scroll.width()) - { - preprocessedtext.push_back(line.substr(0, i)); - line = line.substr(i); - i = 0; - } + preprocessed_text.push_back(line.substr(0, i)); + line = line.substr(i); + i = 0; } - preprocessedtext.push_back(line); } + preprocessed_text.push_back(line); } } @@ -155,44 +144,30 @@ void TextEdit::repaintEvent(RepaintEvent* repaintEvent) // update values of scroll bar scroll.setRange(height() / font.textHeight()); - scroll.setMaximum(preprocessedtext.size()); + scroll.setMaximum(preprocessed_text.size()); - int w = width(); - int h = height(); - if((w == 0) || (h == 0)) + if((width() == 0) || (height() == 0)) { return; } - box.setSize(w, h); + box.setSize(width(), height()); p.drawImage(0, 0, box); - p.setColour(Colour(183.0 / 255.0, 219.0 / 255.0, 255.0 / 255.0, 1)); - int skip = scroll.value(); - - int ypos = font.textHeight() + 5 + 1 + 1 + 1; - std::list::iterator it; - it = preprocessedtext.begin(); - - int c = 0; - for(; c < skip; c++) - { - ++it; - } + int ypos = font.textHeight() + y_border; - c = 0; - for(; it != preprocessedtext.end(); it++) + auto scroll_value = scroll.value(); + for(int i = 0; i < preprocessed_text.size() - scroll_value; ++i) { - if((c * font.textHeight()) >= (height() - 8 - font.textHeight())) + if(i * font.textHeight() >= (height() - y_border - font.textHeight())) { break; } - std::string line = *it; - p.drawText(BORDER - 4 + 3, ypos, font, line); + auto const& line = preprocessed_text[scroll_value + i]; + p.drawText(x_border, ypos, font, line); ypos += font.textHeight(); - ++c; } } diff --git a/plugingui/textedit.h b/plugingui/textedit.h index 5959ae9..6ce59b6 100644 --- a/plugingui/textedit.h +++ b/plugingui/textedit.h @@ -27,27 +27,32 @@ #pragma once #include -#include +#include -#include "widget.h" #include "font.h" -#include "painter.h" +#include "notifier.h" #include "scrollbar.h" #include "texturedbox.h" -#include "notifier.h" +#include "widget.h" -namespace GUI { +namespace GUI +{ -class TextEdit : public Widget { +class TextEdit + : public Widget +{ public: - TextEdit(Widget *parent); + TextEdit(Widget* parent); virtual ~TextEdit(); // From Widget - bool isFocusable() override { return true; } + bool isFocusable() override + { + return true; + } void resize(std::size_t width, std::size_t height) override; - std::string text(); + std::string getText(); void setText(const std::string& text); void setReadOnly(bool readonly); @@ -65,20 +70,23 @@ protected: private: void scrolled(int value); - TexturedBox box{getImageCache(), ":resources/widget.png", - 0, 0, // atlas offset (x, y) - 7, 1, 7, // dx1, dx2, dx3 - 7, 63, 7}; // dy1, dy2, dy3 + TexturedBox box{getImageCache(), ":resources/widget.png", 0, + 0, // atlas offset (x, y) + 7, 1, 7, // dx1, dx2, dx3 + 7, 63, 7}; // dy1, dy2, dy3 + + static constexpr std::size_t x_border{10}; + static constexpr std::size_t y_border{8}; ScrollBar scroll; Font font; - std::string _text; + std::string text; bool readonly{true}; bool needs_preprocessing{false}; - std::list< std::string > preprocessedtext; + std::vector preprocessed_text; }; } // GUI:: -- cgit v1.2.3