From 9b0c9fa68de363bdd6d1e821eb57e50eaa76fc3c Mon Sep 17 00:00:00 2001 From: Samuel Fadel Date: Tue, 6 Oct 2015 15:51:41 -0300 Subject: Solved issue #1 (history autoscroll). --- historygraph.cpp | 30 ++++++++++++++++++++++++++++-- historygraph.h | 4 ++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/historygraph.cpp b/historygraph.cpp index 0aeec20..66249e5 100644 --- a/historygraph.cpp +++ b/historygraph.cpp @@ -126,11 +126,13 @@ HistoryGraph::HistoryGraph(QQuickItem *parent) : QQuickItem(parent) , m_firstNode(0) , m_currentNode(0) + , m_currentWidth(0.0f) , m_needsUpdate(false) { setClip(true); setFlag(QQuickItem::ItemHasContents); setAcceptedMouseButtons(Qt::LeftButton); + setAcceptHoverEvents(true); } HistoryGraph::~HistoryGraph() @@ -193,7 +195,7 @@ QSGNode *HistoryGraph::createNodeTree() //int breadth = m_firstNode->breadth(); //int depth = m_firstNode->depth(); - QSGNode *sceneGraphRoot = new QSGNode; + QSGTransformNode *sceneGraphRoot = new QSGTransformNode; HistoryItemNode *histNode = m_firstNode; float margin = height()*MARGIN; float padding = height()*PADDING; @@ -234,7 +236,12 @@ QSGNode *HistoryGraph::createNodeTree() histNode = children[0]; } while (true); - // setWidth(xPos + radius + margin); + m_currentWidth = x - 2.0f*margin; + + if (m_currentWidth > width()) { + m_viewportTransform.setToIdentity(); + m_viewportTransform.translate(-(m_currentWidth - width() + margin), 0); + } return sceneGraphRoot; } @@ -274,6 +281,8 @@ QSGNode *HistoryGraph::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) updateNodeTree(root); } + static_cast(root->firstChild())->setMatrix(m_viewportTransform); + return root; } @@ -309,6 +318,23 @@ HistoryGraph::HistoryItemNode *HistoryGraph::nodeAt(const QPointF &pos, HistoryG return 0; } +void HistoryGraph::hoverMoveEvent(QHoverEvent *event) +{ + if (m_currentWidth < width()) { + return; + } + + const QPointF &pos = event->posF(); + float margin = MARGIN * height(); + float prop = (pos.x() - margin) / (width() - 2*margin); + float displ = m_currentWidth - width() + margin; + + m_viewportTransform.setToIdentity(); + m_viewportTransform.translate(-displ * prop, 0); + + update(); +} + void HistoryGraph::mousePressEvent(QMouseEvent *event) { HistoryGraph::HistoryItemNode *node = nodeAt(event->localPos()); diff --git a/historygraph.h b/historygraph.h index 60cce94..6c5bd9a 100644 --- a/historygraph.h +++ b/historygraph.h @@ -2,6 +2,7 @@ #define HISTORYGRAPH_H #include +#include #include class HistoryGraph : public QQuickItem @@ -19,6 +20,7 @@ public slots: protected: QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *); + void hoverMoveEvent(QHoverEvent *event); void mousePressEvent(QMouseEvent *event); private: @@ -32,6 +34,8 @@ private: void addScatterplot(QSGNode *node, const HistoryItemNode *historyItemNode, float x, float y, float w, float h); HistoryItemNode *m_firstNode, *m_currentNode; + QMatrix4x4 m_viewportTransform; + float m_currentWidth; bool m_needsUpdate; }; -- cgit v1.2.3