This class represents a text that is printed/drawn inside a colorful frame. Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@xxxxxxxxx> --- src/KsPlotTools.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++ src/KsPlotTools.hpp | 40 ++++++++++++++++++ 2 files changed, 139 insertions(+) diff --git a/src/KsPlotTools.cpp b/src/KsPlotTools.cpp index 6c3902a..ef15cde 100644 --- a/src/KsPlotTools.cpp +++ b/src/KsPlotTools.cpp @@ -524,6 +524,105 @@ void Polygon::_draw(const Color &col, float size) const col.color_c_ptr(), size); } +/** The created object will print/draw only the text without the frame. */ +TextBox::TextBox() +: _text(""), + _font(nullptr) +{ + setPos(Point(0, 0)); + _box._visible = false; +} + + +/** The created object will print/draw only the text without the frame. */ +TextBox::TextBox(ksplot_font *f) +: _text(""), + _font(f) +{ + setPos(Point(0, 0)); + _box._visible = false; +} + +/** The created object will print/draw only the text without the frame. */ +TextBox::TextBox(ksplot_font *f, const std::string &text, const Point &pos) +: _text(text), + _font(f) +{ + setPos(pos); + _box._visible = false; +} + +/** The created object will print/draw only the text without the frame. */ +TextBox::TextBox(ksplot_font *f, const std::string &text, const Color &col, + const Point &pos) +: _text(text), + _font(f) +{ + _color = col; + setPos(pos); + _box._visible = false; +} + +/** The created object will print/draw the text and the frame. */ +TextBox::TextBox(ksplot_font *f, const std::string &text, const Color &col, + const Point &pos, int l, int h) +: _text(text), + _font(f) +{ + setPos(pos); + setBoxAppearance(col, l, h); +} + +/** + * @brief Set the position of the bottom-left corner of the frame. + * + * @param p: The coordinates of the bottom-left corner. + */ +void TextBox::setPos(const Point &p) +{ + _box.setPoint(0, p); +} + +/** + * @brief Set the color and the dimensions of the frame. + * + * @param col: The color of the frame. + * @param l: The length of the frame. + * @param h: The height of the frame. + */ +void TextBox::setBoxAppearance(const Color &col, int l, int h) +{ + _box.setFill(true); + _box._color = col; + _box._visible = true; + + if (h <= 0 && _font) + h = _font->height; + + _box.setPoint(1, _box.getPointX(0), _box.getPointY(0) - h); + _box.setPoint(2, _box.getPointX(0) + l, _box.getPointY(0) - h); + _box.setPoint(3, _box.getPointX(0) + l, _box.getPointY(0)); +} + +void TextBox::_draw(const Color &col, float size) const +{ + _box.draw(); + if (!_font || _text.empty()) + return; + + if (_box._visible ) { + int bShift = (_box.getPointY(0) - _box.getPointY(1) - _font->height) / 2; + ksplot_print_text(_font, NULL, + _box.getPointX(0) + _font->height / 4, + _box.getPointY(0) - _font->base - bShift, + _text.c_str()); + } else { + ksplot_print_text(_font, col.color_c_ptr(), + _box.getPointX(0) + _font->height / 4, + _box.getPointY(0) - _font->base, + _text.c_str()); + } +} /** * @brief Create a default Mark. diff --git a/src/KsPlotTools.hpp b/src/KsPlotTools.hpp index 7ffabd8..b270a56 100644 --- a/src/KsPlotTools.hpp +++ b/src/KsPlotTools.hpp @@ -307,6 +307,46 @@ public: virtual ~Rectangle() {} }; +/** + * This class represents a text that is printed/drawn inside a colorful frame. + */ +class TextBox : public PlotObject { +public: + TextBox(); + + TextBox(ksplot_font *f); + + TextBox(ksplot_font *f, const std::string &text, const Point &pos); + + TextBox(ksplot_font *f, const std::string &text, const Color &col, + const Point &pos); + + TextBox(ksplot_font *f, const std::string &text, const Color &col, + const Point &pos, int l, int h = -1); + + /** Destroy the TextBox object. Keep this destructor virtual. */ + virtual ~TextBox() {} + + /** Set the font to be used. */ + void setFont(ksplot_font *f) {_font = f;} + + /** Set the text. */ + void setText(const std::string &t) {_text = t;} + + void setPos(const Point &p); + + void setBoxAppearance(const Color &col, int l, int h); + +private: + void _draw(const Color &, float size = 1.) const override; + + std::string _text; + + ksplot_font *_font; + + Rectangle _box; +}; + /** * This class represents the graphical element of the KernelShark GUI marker. */ -- 2.25.1