diff mbox series

[10/10] kernel-shark: Add double click interface to PlotObject

Message ID 20201209134530.428368-11-y.karadz@gmail.com (mailing list archive)
State Accepted
Headers show
Series kernel-shark: Update the C++ tools for plotting | expand

Commit Message

Yordan Karadzhov Dec. 9, 2020, 1:45 p.m. UTC
The interface consists of two virtual methods that have to be implemented
by the inheriting classes. The first implements the double-click action
of the object while the second one calculates the distance between the
object and the exact position of the mouse click. The second method is
used by the GUI to decide if the double-click action has to be executed
or not. The patch contains a simple method for retrieving the geometrical
center of a Shape. This helper method can be useful when implementing the
calculation of the distance in the inheriting classes.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/KsPlotTools.cpp | 31 +++++++++++++++++++++++++++++++
 src/KsPlotTools.hpp | 14 ++++++++++++++
 2 files changed, 45 insertions(+)
diff mbox series

Patch

diff --git a/src/KsPlotTools.cpp b/src/KsPlotTools.cpp
index 8d38009..ac9c5b2 100644
--- a/src/KsPlotTools.cpp
+++ b/src/KsPlotTools.cpp
@@ -251,6 +251,21 @@  Color getColor(const ColorTable *colors, int id)
 	return {};
 }
 
+/**
+ * @brief A method to be implemented in the inheriting class. It calculates
+ *	  the distance between the position of the click and the shape. This
+ *	  distance is used by the GUI to decide if the corresponding
+ *	  "Double click" action of the shape must be executed. The default
+ *	  implementation returns infinity.
+ *
+ * @param x: The X coordinate of the click.
+ * @param y: The Y coordinate of the click.
+ */
+double PlotObject::distance(int x, int y) const
+{
+	return std::numeric_limits<double>::max();
+}
+
 /**
  * @brief Create a default Shape.
  */
@@ -298,6 +313,22 @@  Shape::~Shape() {
 	delete[] _points;
 }
 
+/** @brief Get the coordinates of the geometrical center of this shape. */
+ksplot_point Shape::center() const
+{
+	ksplot_point c = {0, 0};
+
+	for (size_t i = 0; i < _nPoints; ++i) {
+		c.x += _points[i].x;
+		c.y += _points[i].y;
+	}
+
+	c.x /= _nPoints;
+	c.y /= _nPoints;
+
+	return c;
+}
+
 /** Assignment operator. */
 void Shape::operator=(const Shape &s)
 {
diff --git a/src/KsPlotTools.hpp b/src/KsPlotTools.hpp
index b9b93f3..c993181 100644
--- a/src/KsPlotTools.hpp
+++ b/src/KsPlotTools.hpp
@@ -103,6 +103,16 @@  public:
 			_draw(_color, _size);
 	}
 
+	/**
+	 * Generic action to be executed when the objects is double clicked.
+	 */
+	void doubleClick() const {
+		if (_visible)
+			_doubleClick();
+	}
+
+	virtual double distance(int x, int y) const;
+
 	/** Is this object visible. */
 	bool	_visible;
 
@@ -114,6 +124,8 @@  public:
 
 private:
 	virtual void _draw(const Color &col, float s) const = 0;
+
+	virtual void _doubleClick() const {}
 };
 
 /** List of graphical element. */
@@ -135,6 +147,8 @@  public:
 	/* Keep this destructor virtual. */
 	virtual ~Shape();
 
+	ksplot_point center() const;
+
 	void operator=(const Shape &s);
 
 	void setPoint(size_t i, int x, int y);