mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 05:00:20 +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 {
|
||||
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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue