diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-01-13 13:23:41 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2021-01-20 08:47:18 +0100 |
commit | 1a869833d8896b01d04b49a58e08914658e038e9 (patch) | |
tree | 0fb2db90753afb79d7fdf45c27395107611b25d0 /stats | |
parent | 2b961414d7a52d442f5ecd8c2e42e43d044d0d5e (diff) | |
download | subsurface-1a869833d8896b01d04b49a58e08914658e038e9.tar.gz |
statistics: implement moving of legend
Catch mouse move events and move the legend accordingly.
Currently, this is the only item that can be dragged and
therefore there is no need of doing some kind of fancy
interface. Simply keep a pointer to the legend if it is
dragged.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats')
-rw-r--r-- | stats/legend.h | 1 | ||||
-rw-r--r-- | stats/statsview.cpp | 50 | ||||
-rw-r--r-- | stats/statsview.h | 6 |
3 files changed, 53 insertions, 4 deletions
diff --git a/stats/legend.h b/stats/legend.h index a9d42bf39..1e38f705a 100644 --- a/stats/legend.h +++ b/stats/legend.h @@ -14,7 +14,6 @@ class QFontMetrics; class Legend : public ChartRectItem { public: Legend(StatsView &view, const std::vector<QString> &names); - void hover(QPointF pos); void resize(); // called when the chart size changes. private: // Each entry is a text besides a rectangle showing the color diff --git a/stats/statsview.cpp b/stats/statsview.cpp index 61510d60c..5f9f51f6d 100644 --- a/stats/statsview.cpp +++ b/stats/statsview.cpp @@ -18,7 +18,6 @@ #include <cmath> #include <QGraphicsScene> -#include <QGraphicsSceneHoverEvent> #include <QGraphicsSimpleTextItem> #include <QQuickItem> #include <QQuickWindow> @@ -35,6 +34,7 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent), highlightedSeries(nullptr), xAxis(nullptr), yAxis(nullptr), + draggedItem(nullptr), rootNode(nullptr) { setFlag(ItemHasContents, true); @@ -42,6 +42,7 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent), connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &StatsView::replotIfVisible); setAcceptHoverEvents(true); + setAcceptedMouseButtons(Qt::LeftButton); QFont font; titleFont = QFont(font.family(), font.pointSize(), QFont::Light); // Make configurable @@ -55,6 +56,30 @@ StatsView::~StatsView() { } +void StatsView::mousePressEvent(QMouseEvent *event) +{ + // Currently, we only support dragging of the legend. If other objects + // should be made draggable, this needs to be generalized. + if (legend) { + QPointF pos = event->localPos(); + QRectF rect = legend->getRect(); + if (legend->getRect().contains(pos)) { + dragStartMouse = pos; + dragStartItem = rect.topLeft(); + draggedItem = legend.get(); + grabMouse(); + } + } +} + +void StatsView::mouseReleaseEvent(QMouseEvent *) +{ + if (draggedItem) { + draggedItem = nullptr; + ungrabMouse(); + } +} + QSGNode *StatsView::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *) { // The QtQuick drawing interface is utterly bizzare with a distinct 1980ies-style memory management. @@ -172,13 +197,33 @@ void StatsView::replotIfVisible() plot(state); } +void StatsView::mouseMoveEvent(QMouseEvent *event) +{ + if (!draggedItem) + return; + + QSizeF sceneSize = size(); + if (sceneSize.width() <= 1.0 || sceneSize.height() <= 1.0) + return; + QPointF pos = event->pos() - dragStartMouse + dragStartItem;; + QSizeF itemSize = draggedItem->getRect().size(); + double widthHalf = floor(itemSize.width() / 2); + double heightHalf = floor(itemSize.height() / 2); + QSizeF itemSizeHalf(floor(itemSize.width() / 2), floor(itemSize.height() / 2)); + QPointF sanitizedPos(std::clamp(pos.x(), -widthHalf, sceneSize.width() - widthHalf - 1.0), + std::clamp(pos.y(), -heightHalf, sceneSize.height() - heightHalf - 1.0)); + draggedItem->setPos(sanitizedPos); + update(); +} + void StatsView::hoverEnterEvent(QHoverEvent *) { } void StatsView::hoverMoveEvent(QHoverEvent *event) { - QPointF pos(event->pos()); + QPointF pos = event->pos(); + for (auto &series: series) { if (series->hover(pos)) { if (series.get() != highlightedSeries) { @@ -254,6 +299,7 @@ void StatsView::reset() { highlightedSeries = nullptr; xAxis = yAxis = nullptr; + draggedItem = nullptr; items.clear(); // non-owning pointers legend.reset(); series.clear(); diff --git a/stats/statsview.h b/stats/statsview.h index 385a591f4..8270c237f 100644 --- a/stats/statsview.h +++ b/stats/statsview.h @@ -168,10 +168,14 @@ private: std::vector<ChartItem *> items; // Attention: currently, items are not automatically removed on destruction! StatsSeries *highlightedSeries; StatsAxis *xAxis, *yAxis; + Legend *draggedItem; + QPointF dragStartMouse, dragStartItem; void hoverEnterEvent(QHoverEvent *event) override; void hoverMoveEvent(QHoverEvent *event) override; - + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; QSGImageNode *rootNode; }; |