Show cache plot marker values in cache plot legend
MODIFY
src/cachesim/cacheplotview.cpp
src/cachesim/cacheplotview.cpp
@@ -29,30 +29,31 @@ void constrainInPlotRange(QPointF& pos, QChart* chart) {
namespace Ripes {
-MarkerObjects::MarkerObjects(QObject* parent, QChart* chart, const QLineSeries* _series) : QObject(parent) {
- marker = new ChartLineMarker(chart, _series);
- coordX = new QGraphicsSimpleTextItem(chart);
- coordY = new QGraphicsSimpleTextItem(chart);
- label = new QGraphicsSimpleTextItem(chart);
- label->setText(_series->name() + ":");
- series = _series;
- coordX->setText("Cycle: ");
- coordY->setText("Value: ");
-
- connect(marker, &ChartLineMarker::snappedMarkerChanged,
- [=] { this->updateCoordinateValues(marker->getMarkerValuePos()); });
+CachePlotMarker::CachePlotMarker(QObject* parent, QChart* chart, QLineSeries* _series, const QString& _name)
+ : QObject(parent), marker(new ChartLineMarker(chart, _series)), series(_series), name(_name) {
+ connect(marker, &ChartLineMarker::snappedMarkerChanged, marker,
+ [=] { this->updateCoordinateValues(marker->getMarkerValuePos(), true, false); });
+ // Update initial marker text
+ updateCoordinateValues(QPointF(), false, false);
}
-void MarkerObjects::clear() {
+void CachePlotMarker::clear() {
+ if (marker != nullptr) {
delete marker;
- delete coordX;
- delete coordY;
- delete label;
+ marker = nullptr;
+ }
}
-void MarkerObjects::updateCoordinateValues(const QPointF& pos) {
- coordX->setText(QString("Cycle: %1").arg(pos.x()));
- coordY->setText(QString("Value: %1").arg(pos.y()));
+void CachePlotMarker::updateCoordinateValues(const QPointF& pos, bool showValue, bool showCycles) {
+ QString info = name;
+
+ if (showValue) {
+ info += QString(": %1").arg(pos.y());
+ }
+ if (showCycles) {
+ info += QString(": %1").arg(pos.x());
+ }
+ series->setName(info);
}
CachePlotView::CachePlotView(QWidget* parent) : QGraphicsView(parent) {
@@ -72,6 +73,7 @@ void CachePlotView::leaveEvent(QEvent* event) {
m_mouseInView = false;
for (const auto& marker : m_markers) {
marker->marker->hide();
+ marker->updateCoordinateValues(QPointF(), false, false);
}
QGraphicsView::leaveEvent(event);
}
@@ -80,9 +82,6 @@ QPixmap CachePlotView::getPlotPixmap() {
QVector<QGraphicsItem*> itemsToHide;
for (const auto& marker : m_markers) {
itemsToHide << marker->marker;
- itemsToHide << marker->coordX;
- itemsToHide << marker->coordY;
- itemsToHide << marker->label;
}
for (const auto& i : itemsToHide) {
@@ -100,7 +99,7 @@ QPixmap CachePlotView::getPlotPixmap() {
return p;
};
-void CachePlotView::hideSeriesMarker(const QLineSeries* series) {
+void CachePlotView::hideSeriesMarker(QLineSeries* series) {
auto marker =
std::find_if(m_markers.begin(), m_markers.end(), [=](const auto& mob) { return mob->series == series; });
Q_ASSERT(marker != m_markers.end());
@@ -109,8 +108,8 @@ void CachePlotView::hideSeriesMarker(const QLineSeries* series) {
resizeObjects(scene()->sceneRect().size());
}
-void CachePlotView::showSeriesMarker(const QLineSeries* series) {
- m_markers.emplace_back(std::make_unique<MarkerObjects>(this, m_chart, series));
+void CachePlotView::showSeriesMarker(QLineSeries* series, const QString& name) {
+ m_markers.emplace_back(std::make_unique<CachePlotMarker>(this, m_chart, series, name));
m_markers.rbegin()->get()->marker->setVisible(m_mouseInView);
resizeObjects(scene()->sceneRect().size());
}
@@ -139,13 +138,8 @@ void CachePlotView::setPlot(QChart* chart) {
}
void CachePlotView::resizeObjects(const QSizeF& size) {
- int ySpaceForMarkers = 0;
- if (m_markers.size() > 0) {
- ySpaceForMarkers = m_markers.size() * m_markers.at(0)->coordX->boundingRect().height();
- }
-
scene()->setSceneRect(QRectF(QPoint(0, 0), size));
- m_chart->resize(size - QSizeF(0, ySpaceForMarkers));
+ m_chart->resize(size);
for (auto* item : scene()->items()) {
if (auto* callout = dynamic_cast<Callout*>(item)) {
callout->updateGeometry();
@@ -154,14 +148,6 @@ void CachePlotView::resizeObjects(const QSizeF& size) {
for (unsigned i = 0; i < m_markers.size(); i++) {
auto& marker = m_markers.at(i);
- int yOffset = 0;
- if (i > 0) {
- yOffset = m_markers.at(i - 1)->coordX->boundingRect().height() * i;
- }
- const int y = yOffset + m_chart->size().height() - 20;
- marker->label->setPos(m_chart->size().width() / 4 - marker->label->boundingRect().width(), y);
- marker->coordX->setPos(m_chart->size().width() * 0.33, y);
- marker->coordY->setPos(m_chart->size().width() * 0.55, y);
marker->marker->updateLines();
}
}

MODIFY
src/cachesim/cacheplotview.h
src/cachesim/cacheplotview.h
@@ -22,17 +22,15 @@ QT_CHARTS_USE_NAMESPACE
namespace Ripes {
class ChartLineMarker;
-class MarkerObjects : public QObject {
+class CachePlotMarker : public QObject {
Q_OBJECT
public:
- QGraphicsSimpleTextItem* coordX;
- QGraphicsSimpleTextItem* coordY;
- QGraphicsSimpleTextItem* label;
- ChartLineMarker* marker;
- const QLineSeries* series;
- MarkerObjects(QObject* parent, QChart* chart, const QLineSeries* series);
- void updateCoordinateValues(const QPointF& pos);
+ ChartLineMarker* marker = nullptr;
+ QLineSeries* series = nullptr;
+ QString name;
+ CachePlotMarker(QObject* parent, QChart* chart, QLineSeries* series, const QString& name);
+ void updateCoordinateValues(const QPointF& pos, bool showValue, bool showCycles);
void clear();
};
@@ -60,15 +58,15 @@ public slots:
void keepCallout();
void deleteCallout(Callout* callout);
void tooltip(QPointF point, bool state);
- void showSeriesMarker(const QLineSeries* series);
- void hideSeriesMarker(const QLineSeries* series);
+ void hideSeriesMarker(QLineSeries* series);
+ void showSeriesMarker(QLineSeries* series, const QString& name);
private:
void resizeObjects(const QSizeF& size);
QPointF m_hoverPos;
- std::vector<std::unique_ptr<MarkerObjects>> m_markers;
+ std::vector<std::unique_ptr<CachePlotMarker>> m_markers;
QChart* m_chart = nullptr;
Callout* m_tooltip = nullptr;

MODIFY
src/cachesim/cacheplotwidget.cpp
src/cachesim/cacheplotwidget.cpp
@@ -62,7 +62,10 @@ CachePlotWidget::CachePlotWidget(QWidget* parent) : QWidget(parent), m_ui(new Ui
m_ui->sizeBreakdownButton->setIcon(sizeBreakdownIcon);
connect(m_ui->sizeBreakdownButton, &QPushButton::clicked, this, &CachePlotWidget::showSizeBreakdown);
connect(m_ui->windowed, &QCheckBox::toggled, m_ui->windowCycles, &QWidget::setEnabled);
- connect(m_ui->windowed, &QCheckBox::toggled, m_ui->mavgCursor, &QWidget::setEnabled);
+ connect(m_ui->windowed, &QCheckBox::toggled, [=](bool enabled) {
+ m_mavgMarkerAction->setChecked(enabled);
+ m_mavgMarkerAction->setEnabled(enabled);
+ });
connect(m_ui->maxCyclesButton, &QPushButton::clicked, this, [=] {
QMessageBox::information(
@@ -122,16 +125,14 @@ void CachePlotWidget::setCache(const std::shared_ptr<CacheSim>& cache) {
m_series = new QLineSeries(m_plot);
auto defaultPen = m_series->pen(); // Inherit default pen state
defaultPen.setColor(Colors::FoundersRock);
- m_series->setName("Total");
m_series->setPen(defaultPen);
m_plot->addSeries(m_series);
m_mavgSeries = new QLineSeries(m_plot);
- m_mavgSeries->setName("Moving avg.");
defaultPen.setColor(Colors::Medalist);
m_mavgSeries->setPen(defaultPen);
m_plot->addSeries(m_mavgSeries);
m_plot->createDefaultAxes();
- m_plot->legend()->hide();
+ m_plot->legend()->show();
m_ui->plotView->setPlot(m_plot);
setupPlotActions();
@@ -189,10 +190,10 @@ CachePlotWidget::~CachePlotWidget() {
}
void CachePlotWidget::setupPlotActions() {
- auto showMarkerFunctor = [=](const QLineSeries* series) {
+ auto showMarkerFunctor = [=](QLineSeries* series, const QString& name) {
return [=](bool visible) {
if (visible) {
- m_ui->plotView->showSeriesMarker(series);
+ m_ui->plotView->showSeriesMarker(series, name);
} else {
m_ui->plotView->hideSeriesMarker(series);
}
@@ -203,14 +204,14 @@ void CachePlotWidget::setupPlotActions() {
m_totalMarkerAction->setIcon(QIcon(":/icons/crosshair_blue.svg"));
m_totalMarkerAction->setCheckable(true);
m_ui->ratioCursor->setDefaultAction(m_totalMarkerAction);
- connect(m_totalMarkerAction, &QAction::toggled, showMarkerFunctor(m_series));
+ connect(m_totalMarkerAction, &QAction::toggled, showMarkerFunctor(m_series, "Total"));
m_totalMarkerAction->setChecked(true);
m_mavgMarkerAction = new QAction("Enable moving average crosshair", this);
m_mavgMarkerAction->setIcon(QIcon(":/icons/crosshair_gold.svg"));
m_mavgMarkerAction->setCheckable(true);
m_ui->mavgCursor->setDefaultAction(m_mavgMarkerAction);
- connect(m_mavgMarkerAction, &QAction::toggled, showMarkerFunctor(m_mavgSeries));
+ connect(m_mavgMarkerAction, &QAction::toggled, showMarkerFunctor(m_mavgSeries, "Average"));
m_mavgMarkerAction->setChecked(true);
const QIcon copyIcon = QIcon(":/icons/documents.svg");
@@ -497,15 +498,11 @@ void CachePlotWidget::resetRatioPlot() {
m_xStep = 1;
m_lastDiffValid = false;
- m_mavgMarkerAction->setChecked(m_ui->windowed->isChecked() && m_mavgMarkerAction->isChecked());
-
if (m_ui->windowed->isChecked()) {
m_mavgData = FixedQueue<double>(m_ui->windowCycles->value());
m_mavgSeries->setVisible(true);
- m_plot->legend()->setVisible(true);
} else {
m_mavgSeries->setVisible(false);
- m_plot->legend()->setVisible(false);
}
updateRatioPlot();

Show cache plot marker values in cache plot legend