mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
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>
This commit is contained in:
parent
2b961414d7
commit
1a869833d8
3 changed files with 53 additions and 4 deletions
|
@ -14,7 +14,6 @@ class QFontMetrics;
|
||||||
class Legend : public ChartRectItem {
|
class Legend : public ChartRectItem {
|
||||||
public:
|
public:
|
||||||
Legend(StatsView &view, const std::vector<QString> &names);
|
Legend(StatsView &view, const std::vector<QString> &names);
|
||||||
void hover(QPointF pos);
|
|
||||||
void resize(); // called when the chart size changes.
|
void resize(); // called when the chart size changes.
|
||||||
private:
|
private:
|
||||||
// Each entry is a text besides a rectangle showing the color
|
// Each entry is a text besides a rectangle showing the color
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <QGraphicsScene>
|
#include <QGraphicsScene>
|
||||||
#include <QGraphicsSceneHoverEvent>
|
|
||||||
#include <QGraphicsSimpleTextItem>
|
#include <QGraphicsSimpleTextItem>
|
||||||
#include <QQuickItem>
|
#include <QQuickItem>
|
||||||
#include <QQuickWindow>
|
#include <QQuickWindow>
|
||||||
|
@ -35,6 +34,7 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent),
|
||||||
highlightedSeries(nullptr),
|
highlightedSeries(nullptr),
|
||||||
xAxis(nullptr),
|
xAxis(nullptr),
|
||||||
yAxis(nullptr),
|
yAxis(nullptr),
|
||||||
|
draggedItem(nullptr),
|
||||||
rootNode(nullptr)
|
rootNode(nullptr)
|
||||||
{
|
{
|
||||||
setFlag(ItemHasContents, true);
|
setFlag(ItemHasContents, true);
|
||||||
|
@ -42,6 +42,7 @@ StatsView::StatsView(QQuickItem *parent) : QQuickItem(parent),
|
||||||
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &StatsView::replotIfVisible);
|
connect(&diveListNotifier, &DiveListNotifier::numShownChanged, this, &StatsView::replotIfVisible);
|
||||||
|
|
||||||
setAcceptHoverEvents(true);
|
setAcceptHoverEvents(true);
|
||||||
|
setAcceptedMouseButtons(Qt::LeftButton);
|
||||||
|
|
||||||
QFont font;
|
QFont font;
|
||||||
titleFont = QFont(font.family(), font.pointSize(), QFont::Light); // Make configurable
|
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 *)
|
QSGNode *StatsView::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
|
||||||
{
|
{
|
||||||
// The QtQuick drawing interface is utterly bizzare with a distinct 1980ies-style memory management.
|
// The QtQuick drawing interface is utterly bizzare with a distinct 1980ies-style memory management.
|
||||||
|
@ -172,13 +197,33 @@ void StatsView::replotIfVisible()
|
||||||
plot(state);
|
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::hoverEnterEvent(QHoverEvent *)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatsView::hoverMoveEvent(QHoverEvent *event)
|
void StatsView::hoverMoveEvent(QHoverEvent *event)
|
||||||
{
|
{
|
||||||
QPointF pos(event->pos());
|
QPointF pos = event->pos();
|
||||||
|
|
||||||
for (auto &series: series) {
|
for (auto &series: series) {
|
||||||
if (series->hover(pos)) {
|
if (series->hover(pos)) {
|
||||||
if (series.get() != highlightedSeries) {
|
if (series.get() != highlightedSeries) {
|
||||||
|
@ -254,6 +299,7 @@ void StatsView::reset()
|
||||||
{
|
{
|
||||||
highlightedSeries = nullptr;
|
highlightedSeries = nullptr;
|
||||||
xAxis = yAxis = nullptr;
|
xAxis = yAxis = nullptr;
|
||||||
|
draggedItem = nullptr;
|
||||||
items.clear(); // non-owning pointers
|
items.clear(); // non-owning pointers
|
||||||
legend.reset();
|
legend.reset();
|
||||||
series.clear();
|
series.clear();
|
||||||
|
|
|
@ -168,10 +168,14 @@ private:
|
||||||
std::vector<ChartItem *> items; // Attention: currently, items are not automatically removed on destruction!
|
std::vector<ChartItem *> items; // Attention: currently, items are not automatically removed on destruction!
|
||||||
StatsSeries *highlightedSeries;
|
StatsSeries *highlightedSeries;
|
||||||
StatsAxis *xAxis, *yAxis;
|
StatsAxis *xAxis, *yAxis;
|
||||||
|
Legend *draggedItem;
|
||||||
|
QPointF dragStartMouse, dragStartItem;
|
||||||
|
|
||||||
void hoverEnterEvent(QHoverEvent *event) override;
|
void hoverEnterEvent(QHoverEvent *event) override;
|
||||||
void hoverMoveEvent(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;
|
QSGImageNode *rootNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue