@@ -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.
@@ -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.
*/
This class represents a text that is printed/drawn inside a colorful frame. Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com> --- src/KsPlotTools.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++ src/KsPlotTools.hpp | 40 ++++++++++++++++++ 2 files changed, 139 insertions(+)