diff options
author | Yordan Karadzhov (VMware) <y.karadz@gmail.com> | 2020-12-09 15:45:24 +0200 |
---|---|---|
committer | Steven Rostedt (VMware) <rostedt@goodmis.org> | 2020-12-22 10:52:21 -0500 |
commit | e27dae06dbcbda8f48faa06468ee7d722adf03a4 (patch) | |
tree | addd856c9716b91542b98939d58435d589e29297 | |
parent | 101ef15793547f49071ecd9e9d8bbb0fb2ca1573 (diff) | |
download | kernel-shark-e27dae06dbcbda8f48faa06468ee7d722adf03a4.tar.gz |
kernel-shark: Add TextBox class to KsPlot namespace
This class represents a text that is printed/drawn inside a colorful
frame.
Link: https://lore.kernel.org/linux-trace-devel/20201209134530.428368-5-y.karadz@gmail.com
Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
-rw-r--r-- | src/KsPlotTools.cpp | 116 | ||||
-rw-r--r-- | src/KsPlotTools.hpp | 42 |
2 files changed, 158 insertions, 0 deletions
diff --git a/src/KsPlotTools.cpp b/src/KsPlotTools.cpp index 113a6c06..3f86f16a 100644 --- a/src/KsPlotTools.cpp +++ b/src/KsPlotTools.cpp @@ -97,6 +97,23 @@ void Color::setRainbowColor(int n) set(r, g, b); } +/** Alpha blending with white background. */ +void Color::blend(float alpha) +{ + if (alpha < 0 || alpha > 1.) + return; + + auto lamBld = [alpha](int val) { + return val * alpha + (1 - alpha) * 255; + }; + + int r = lamBld(this->r()); + int g = lamBld(this->g()); + int b = lamBld(this->b()); + + set(r, g, b); +} + /** * @brief Create a Hash table of Rainbow colors. The sorted Pid values are * mapped to the palette of Rainbow colors. @@ -474,6 +491,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 6e663737..7f83af2d 100644 --- a/src/KsPlotTools.hpp +++ b/src/KsPlotTools.hpp @@ -47,6 +47,8 @@ public: void setRainbowColor(int n); + void blend(float alpha); + /** * @brief Get the C struct defining the RGB color. */ @@ -304,6 +306,46 @@ public: }; /** + * 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. */ class Mark : public PlotObject { |