Fix issue in callout ownership and deletion
MODIFY
src/cachesim/cacheplotview.cpp
src/cachesim/cacheplotview.cpp
@@ -32,6 +32,7 @@ CachePlotView::CachePlotView(QWidget* parent) : QGraphicsView(new QGraphicsScene
void CachePlotView::setPlot(QChart* chart) {
// Clear any previous plot or plot markers
scene()->clear();
+ m_tooltip = nullptr;
m_chart = chart;
scene()->addItem(chart);
@@ -82,10 +83,17 @@ void CachePlotView::keepCallout() {
m_tooltipLock.unlock();
}
+void CachePlotView::deleteCallout(Callout* callout) {
+ if (m_tooltip == callout) {
+ m_tooltip = nullptr;
+ }
+ delete callout;
+}
+
void CachePlotView::tooltip(QPointF point, bool state) {
m_tooltipLock.lock();
if (m_tooltip == nullptr)
- m_tooltip = new Callout(m_chart);
+ m_tooltip = new Callout(this, m_chart);
if (state) {
m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));

MODIFY
src/cachesim/cacheplotview.h
src/cachesim/cacheplotview.h
@@ -34,6 +34,7 @@ protected:
public slots:
void keepCallout();
+ void deleteCallout(Callout* callout);
void tooltip(QPointF point, bool state);
private:

MODIFY
src/cachesim/callout.cpp
src/cachesim/callout.cpp
#include <QtGui/QPainter>
#include <QtWidgets/QGraphicsSceneMouseEvent>
-Callout::Callout(QChart* chart) : QGraphicsItem(chart), m_chart(chart) {}
+#include "cacheplotview.h"
+
+Callout::Callout(Ripes::CachePlotView* view, QChart* chart) : QGraphicsItem(chart), m_view(view), m_chart(chart) {}
QRectF Callout::boundingRect() const {
QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
@@ -100,7 +102,7 @@ void Callout::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, Q
}
void Callout::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) {
- delete this;
+ m_view->deleteCallout(this);
}
void Callout::mousePressEvent(QGraphicsSceneMouseEvent* event) {

MODIFY
src/cachesim/callout.h
src/cachesim/callout.h
@@ -44,9 +44,13 @@ QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
+namespace Ripes {
+class CachePlotView;
+}
+
class Callout : public QGraphicsItem {
public:
- Callout(QChart* parent);
+ Callout(Ripes::CachePlotView* view, QChart* parent);
void setText(const QString& text);
void setAnchor(QPointF point);
@@ -61,6 +65,7 @@ protected:
void contextMenuEvent(QGraphicsSceneContextMenuEvent* event) override;
private:
+ Ripes::CachePlotView* m_view = nullptr;
QString m_text;
QRectF m_textRect;
QRectF m_rect;

Fix issue in callout ownership and deletion