mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-04 08:15:30 +08:00
qt 6.5.1 original
This commit is contained in:
39
examples/widgets/graphicsview/elasticnodes/CMakeLists.txt
Normal file
39
examples/widgets/graphicsview/elasticnodes/CMakeLists.txt
Normal file
@ -0,0 +1,39 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(elasticnodes LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/widgets/graphicsview/elasticnodes")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(elasticnodes
|
||||
edge.cpp edge.h
|
||||
graphwidget.cpp graphwidget.h
|
||||
main.cpp
|
||||
node.cpp node.h
|
||||
)
|
||||
|
||||
set_target_properties(elasticnodes PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(elasticnodes PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS elasticnodes
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
104
examples/widgets/graphicsview/elasticnodes/edge.cpp
Normal file
104
examples/widgets/graphicsview/elasticnodes/edge.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QtMath>
|
||||
|
||||
//! [0]
|
||||
Edge::Edge(Node *sourceNode, Node *destNode)
|
||||
: source(sourceNode), dest(destNode)
|
||||
{
|
||||
setAcceptedMouseButtons(Qt::NoButton);
|
||||
source->addEdge(this);
|
||||
dest->addEdge(this);
|
||||
adjust();
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
Node *Edge::sourceNode() const
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
Node *Edge::destNode() const
|
||||
{
|
||||
return dest;
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void Edge::adjust()
|
||||
{
|
||||
if (!source || !dest)
|
||||
return;
|
||||
|
||||
QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
|
||||
qreal length = line.length();
|
||||
|
||||
prepareGeometryChange();
|
||||
|
||||
if (length > qreal(20.)) {
|
||||
QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
|
||||
sourcePoint = line.p1() + edgeOffset;
|
||||
destPoint = line.p2() - edgeOffset;
|
||||
} else {
|
||||
sourcePoint = destPoint = line.p1();
|
||||
}
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
QRectF Edge::boundingRect() const
|
||||
{
|
||||
if (!source || !dest)
|
||||
return QRectF();
|
||||
|
||||
qreal penWidth = 1;
|
||||
qreal extra = (penWidth + arrowSize) / 2.0;
|
||||
|
||||
return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
|
||||
destPoint.y() - sourcePoint.y()))
|
||||
.normalized()
|
||||
.adjusted(-extra, -extra, extra, extra);
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
|
||||
{
|
||||
if (!source || !dest)
|
||||
return;
|
||||
|
||||
QLineF line(sourcePoint, destPoint);
|
||||
if (qFuzzyCompare(line.length(), qreal(0.)))
|
||||
return;
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
// Draw the line itself
|
||||
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
||||
painter->drawLine(line);
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
// Draw the arrows
|
||||
double angle = std::atan2(-line.dy(), line.dx());
|
||||
|
||||
QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + M_PI / 3) * arrowSize,
|
||||
cos(angle + M_PI / 3) * arrowSize);
|
||||
QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + M_PI - M_PI / 3) * arrowSize,
|
||||
cos(angle + M_PI - M_PI / 3) * arrowSize);
|
||||
QPointF destArrowP1 = destPoint + QPointF(sin(angle - M_PI / 3) * arrowSize,
|
||||
cos(angle - M_PI / 3) * arrowSize);
|
||||
QPointF destArrowP2 = destPoint + QPointF(sin(angle - M_PI + M_PI / 3) * arrowSize,
|
||||
cos(angle - M_PI + M_PI / 3) * arrowSize);
|
||||
|
||||
painter->setBrush(Qt::black);
|
||||
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
|
||||
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
|
||||
}
|
||||
//! [6]
|
38
examples/widgets/graphicsview/elasticnodes/edge.h
Normal file
38
examples/widgets/graphicsview/elasticnodes/edge.h
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef EDGE_H
|
||||
#define EDGE_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
|
||||
class Node;
|
||||
|
||||
//! [0]
|
||||
class Edge : public QGraphicsItem
|
||||
{
|
||||
public:
|
||||
Edge(Node *sourceNode, Node *destNode);
|
||||
|
||||
Node *sourceNode() const;
|
||||
Node *destNode() const;
|
||||
|
||||
void adjust();
|
||||
|
||||
enum { Type = UserType + 2 };
|
||||
int type() const override { return Type; }
|
||||
|
||||
protected:
|
||||
QRectF boundingRect() const override;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
|
||||
private:
|
||||
Node *source, *dest;
|
||||
|
||||
QPointF sourcePoint;
|
||||
QPointF destPoint;
|
||||
qreal arrowSize = 10;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // EDGE_H
|
16
examples/widgets/graphicsview/elasticnodes/elasticnodes.pro
Normal file
16
examples/widgets/graphicsview/elasticnodes/elasticnodes.pro
Normal file
@ -0,0 +1,16 @@
|
||||
QT += widgets
|
||||
|
||||
HEADERS += \
|
||||
edge.h \
|
||||
node.h \
|
||||
graphwidget.h
|
||||
|
||||
SOURCES += \
|
||||
edge.cpp \
|
||||
main.cpp \
|
||||
node.cpp \
|
||||
graphwidget.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/widgets/graphicsview/elasticnodes
|
||||
INSTALLS += target
|
218
examples/widgets/graphicsview/elasticnodes/graphwidget.cpp
Normal file
218
examples/widgets/graphicsview/elasticnodes/graphwidget.cpp
Normal file
@ -0,0 +1,218 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "graphwidget.h"
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#include <QKeyEvent>
|
||||
#include <QRandomGenerator>
|
||||
|
||||
//! [0]
|
||||
GraphWidget::GraphWidget(QWidget *parent)
|
||||
: QGraphicsView(parent)
|
||||
{
|
||||
QGraphicsScene *scene = new QGraphicsScene(this);
|
||||
scene->setItemIndexMethod(QGraphicsScene::NoIndex);
|
||||
scene->setSceneRect(-200, -200, 400, 400);
|
||||
setScene(scene);
|
||||
setCacheMode(CacheBackground);
|
||||
setViewportUpdateMode(BoundingRectViewportUpdate);
|
||||
setRenderHint(QPainter::Antialiasing);
|
||||
setTransformationAnchor(AnchorUnderMouse);
|
||||
scale(qreal(0.8), qreal(0.8));
|
||||
setMinimumSize(400, 400);
|
||||
setWindowTitle(tr("Elastic Nodes"));
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
Node *node1 = new Node(this);
|
||||
Node *node2 = new Node(this);
|
||||
Node *node3 = new Node(this);
|
||||
Node *node4 = new Node(this);
|
||||
centerNode = new Node(this);
|
||||
Node *node6 = new Node(this);
|
||||
Node *node7 = new Node(this);
|
||||
Node *node8 = new Node(this);
|
||||
Node *node9 = new Node(this);
|
||||
scene->addItem(node1);
|
||||
scene->addItem(node2);
|
||||
scene->addItem(node3);
|
||||
scene->addItem(node4);
|
||||
scene->addItem(centerNode);
|
||||
scene->addItem(node6);
|
||||
scene->addItem(node7);
|
||||
scene->addItem(node8);
|
||||
scene->addItem(node9);
|
||||
scene->addItem(new Edge(node1, node2));
|
||||
scene->addItem(new Edge(node2, node3));
|
||||
scene->addItem(new Edge(node2, centerNode));
|
||||
scene->addItem(new Edge(node3, node6));
|
||||
scene->addItem(new Edge(node4, node1));
|
||||
scene->addItem(new Edge(node4, centerNode));
|
||||
scene->addItem(new Edge(centerNode, node6));
|
||||
scene->addItem(new Edge(centerNode, node8));
|
||||
scene->addItem(new Edge(node6, node9));
|
||||
scene->addItem(new Edge(node7, node4));
|
||||
scene->addItem(new Edge(node8, node7));
|
||||
scene->addItem(new Edge(node9, node8));
|
||||
|
||||
node1->setPos(-50, -50);
|
||||
node2->setPos(0, -50);
|
||||
node3->setPos(50, -50);
|
||||
node4->setPos(-50, 0);
|
||||
centerNode->setPos(0, 0);
|
||||
node6->setPos(50, 0);
|
||||
node7->setPos(-50, 50);
|
||||
node8->setPos(0, 50);
|
||||
node9->setPos(50, 50);
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void GraphWidget::itemMoved()
|
||||
{
|
||||
if (!timerId)
|
||||
timerId = startTimer(1000 / 25);
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
void GraphWidget::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
switch (event->key()) {
|
||||
case Qt::Key_Up:
|
||||
centerNode->moveBy(0, -20);
|
||||
break;
|
||||
case Qt::Key_Down:
|
||||
centerNode->moveBy(0, 20);
|
||||
break;
|
||||
case Qt::Key_Left:
|
||||
centerNode->moveBy(-20, 0);
|
||||
break;
|
||||
case Qt::Key_Right:
|
||||
centerNode->moveBy(20, 0);
|
||||
break;
|
||||
case Qt::Key_Plus:
|
||||
zoomIn();
|
||||
break;
|
||||
case Qt::Key_Minus:
|
||||
zoomOut();
|
||||
break;
|
||||
case Qt::Key_Space:
|
||||
case Qt::Key_Enter:
|
||||
shuffle();
|
||||
break;
|
||||
default:
|
||||
QGraphicsView::keyPressEvent(event);
|
||||
}
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
void GraphWidget::timerEvent(QTimerEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
QList<Node *> nodes;
|
||||
const QList<QGraphicsItem *> items = scene()->items();
|
||||
for (QGraphicsItem *item : items) {
|
||||
if (Node *node = qgraphicsitem_cast<Node *>(item))
|
||||
nodes << node;
|
||||
}
|
||||
|
||||
for (Node *node : std::as_const(nodes))
|
||||
node->calculateForces();
|
||||
|
||||
bool itemsMoved = false;
|
||||
for (Node *node : std::as_const(nodes)) {
|
||||
if (node->advancePosition())
|
||||
itemsMoved = true;
|
||||
}
|
||||
|
||||
if (!itemsMoved) {
|
||||
killTimer(timerId);
|
||||
timerId = 0;
|
||||
}
|
||||
}
|
||||
//! [4]
|
||||
|
||||
#if QT_CONFIG(wheelevent)
|
||||
//! [5]
|
||||
void GraphWidget::wheelEvent(QWheelEvent *event)
|
||||
{
|
||||
scaleView(pow(2., -event->angleDelta().y() / 240.0));
|
||||
}
|
||||
//! [5]
|
||||
#endif
|
||||
|
||||
//! [6]
|
||||
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
|
||||
{
|
||||
Q_UNUSED(rect);
|
||||
|
||||
// Shadow
|
||||
QRectF sceneRect = this->sceneRect();
|
||||
QRectF rightShadow(sceneRect.right(), sceneRect.top() + 5, 5, sceneRect.height());
|
||||
QRectF bottomShadow(sceneRect.left() + 5, sceneRect.bottom(), sceneRect.width(), 5);
|
||||
if (rightShadow.intersects(rect) || rightShadow.contains(rect))
|
||||
painter->fillRect(rightShadow, Qt::darkGray);
|
||||
if (bottomShadow.intersects(rect) || bottomShadow.contains(rect))
|
||||
painter->fillRect(bottomShadow, Qt::darkGray);
|
||||
|
||||
// Fill
|
||||
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
|
||||
gradient.setColorAt(0, Qt::white);
|
||||
gradient.setColorAt(1, Qt::lightGray);
|
||||
painter->fillRect(rect.intersected(sceneRect), gradient);
|
||||
painter->setBrush(Qt::NoBrush);
|
||||
painter->drawRect(sceneRect);
|
||||
|
||||
// Text
|
||||
QRectF textRect(sceneRect.left() + 4, sceneRect.top() + 4,
|
||||
sceneRect.width() - 4, sceneRect.height() - 4);
|
||||
QString message(tr("Click and drag the nodes around, and zoom with the mouse "
|
||||
"wheel or the '+' and '-' keys"));
|
||||
|
||||
QFont font = painter->font();
|
||||
font.setBold(true);
|
||||
font.setPointSize(14);
|
||||
painter->setFont(font);
|
||||
painter->setPen(Qt::lightGray);
|
||||
painter->drawText(textRect.translated(2, 2), message);
|
||||
painter->setPen(Qt::black);
|
||||
painter->drawText(textRect, message);
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
void GraphWidget::scaleView(qreal scaleFactor)
|
||||
{
|
||||
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
|
||||
if (factor < 0.07 || factor > 100)
|
||||
return;
|
||||
|
||||
scale(scaleFactor, scaleFactor);
|
||||
}
|
||||
//! [7]
|
||||
|
||||
void GraphWidget::shuffle()
|
||||
{
|
||||
const QList<QGraphicsItem *> items = scene()->items();
|
||||
for (QGraphicsItem *item : items) {
|
||||
if (qgraphicsitem_cast<Node *>(item))
|
||||
item->setPos(-150 + QRandomGenerator::global()->bounded(300), -150 + QRandomGenerator::global()->bounded(300));
|
||||
}
|
||||
}
|
||||
|
||||
void GraphWidget::zoomIn()
|
||||
{
|
||||
scaleView(qreal(1.2));
|
||||
}
|
||||
|
||||
void GraphWidget::zoomOut()
|
||||
{
|
||||
scaleView(1 / qreal(1.2));
|
||||
}
|
42
examples/widgets/graphicsview/elasticnodes/graphwidget.h
Normal file
42
examples/widgets/graphicsview/elasticnodes/graphwidget.h
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef GRAPHWIDGET_H
|
||||
#define GRAPHWIDGET_H
|
||||
|
||||
#include <QGraphicsView>
|
||||
|
||||
class Node;
|
||||
|
||||
//! [0]
|
||||
class GraphWidget : public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GraphWidget(QWidget *parent = nullptr);
|
||||
|
||||
void itemMoved();
|
||||
|
||||
public slots:
|
||||
void shuffle();
|
||||
void zoomIn();
|
||||
void zoomOut();
|
||||
|
||||
protected:
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void timerEvent(QTimerEvent *event) override;
|
||||
#if QT_CONFIG(wheelevent)
|
||||
void wheelEvent(QWheelEvent *event) override;
|
||||
#endif
|
||||
void drawBackground(QPainter *painter, const QRectF &rect) override;
|
||||
|
||||
void scaleView(qreal scaleFactor);
|
||||
|
||||
private:
|
||||
int timerId = 0;
|
||||
Node *centerNode;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // GRAPHWIDGET_H
|
21
examples/widgets/graphicsview/elasticnodes/main.cpp
Normal file
21
examples/widgets/graphicsview/elasticnodes/main.cpp
Normal file
@ -0,0 +1,21 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "graphwidget.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QTime>
|
||||
#include <QMainWindow>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
GraphWidget *widget = new GraphWidget;
|
||||
|
||||
QMainWindow mainWindow;
|
||||
mainWindow.setCentralWidget(widget);
|
||||
|
||||
mainWindow.show();
|
||||
return app.exec();
|
||||
}
|
175
examples/widgets/graphicsview/elasticnodes/node.cpp
Normal file
175
examples/widgets/graphicsview/elasticnodes/node.cpp
Normal file
@ -0,0 +1,175 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "edge.h"
|
||||
#include "node.h"
|
||||
#include "graphwidget.h"
|
||||
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
|
||||
//! [0]
|
||||
Node::Node(GraphWidget *graphWidget)
|
||||
: graph(graphWidget)
|
||||
{
|
||||
setFlag(ItemIsMovable);
|
||||
setFlag(ItemSendsGeometryChanges);
|
||||
setCacheMode(DeviceCoordinateCache);
|
||||
setZValue(-1);
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void Node::addEdge(Edge *edge)
|
||||
{
|
||||
edgeList << edge;
|
||||
edge->adjust();
|
||||
}
|
||||
|
||||
QList<Edge *> Node::edges() const
|
||||
{
|
||||
return edgeList;
|
||||
}
|
||||
//! [1]
|
||||
|
||||
//! [2]
|
||||
void Node::calculateForces()
|
||||
{
|
||||
if (!scene() || scene()->mouseGrabberItem() == this) {
|
||||
newPos = pos();
|
||||
return;
|
||||
}
|
||||
//! [2]
|
||||
|
||||
//! [3]
|
||||
// Sum up all forces pushing this item away
|
||||
qreal xvel = 0;
|
||||
qreal yvel = 0;
|
||||
const QList<QGraphicsItem *> items = scene()->items();
|
||||
for (QGraphicsItem *item : items) {
|
||||
Node *node = qgraphicsitem_cast<Node *>(item);
|
||||
if (!node)
|
||||
continue;
|
||||
|
||||
QPointF vec = mapToItem(node, 0, 0);
|
||||
qreal dx = vec.x();
|
||||
qreal dy = vec.y();
|
||||
double l = 2.0 * (dx * dx + dy * dy);
|
||||
if (l > 0) {
|
||||
xvel += (dx * 150.0) / l;
|
||||
yvel += (dy * 150.0) / l;
|
||||
}
|
||||
}
|
||||
//! [3]
|
||||
|
||||
//! [4]
|
||||
// Now subtract all forces pulling items together
|
||||
double weight = (edgeList.size() + 1) * 10;
|
||||
for (const Edge *edge : std::as_const(edgeList)) {
|
||||
QPointF vec;
|
||||
if (edge->sourceNode() == this)
|
||||
vec = mapToItem(edge->destNode(), 0, 0);
|
||||
else
|
||||
vec = mapToItem(edge->sourceNode(), 0, 0);
|
||||
xvel -= vec.x() / weight;
|
||||
yvel -= vec.y() / weight;
|
||||
}
|
||||
//! [4]
|
||||
|
||||
//! [5]
|
||||
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
|
||||
xvel = yvel = 0;
|
||||
//! [5]
|
||||
|
||||
//! [6]
|
||||
QRectF sceneRect = scene()->sceneRect();
|
||||
newPos = pos() + QPointF(xvel, yvel);
|
||||
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
|
||||
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
|
||||
}
|
||||
//! [6]
|
||||
|
||||
//! [7]
|
||||
bool Node::advancePosition()
|
||||
{
|
||||
if (newPos == pos())
|
||||
return false;
|
||||
|
||||
setPos(newPos);
|
||||
return true;
|
||||
}
|
||||
//! [7]
|
||||
|
||||
//! [8]
|
||||
QRectF Node::boundingRect() const
|
||||
{
|
||||
qreal adjust = 2;
|
||||
return QRectF( -10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust);
|
||||
}
|
||||
//! [8]
|
||||
|
||||
//! [9]
|
||||
QPainterPath Node::shape() const
|
||||
{
|
||||
QPainterPath path;
|
||||
path.addEllipse(-10, -10, 20, 20);
|
||||
return path;
|
||||
}
|
||||
//! [9]
|
||||
|
||||
//! [10]
|
||||
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
|
||||
{
|
||||
painter->setPen(Qt::NoPen);
|
||||
painter->setBrush(Qt::darkGray);
|
||||
painter->drawEllipse(-7, -7, 20, 20);
|
||||
|
||||
QRadialGradient gradient(-3, -3, 10);
|
||||
if (option->state & QStyle::State_Sunken) {
|
||||
gradient.setCenter(3, 3);
|
||||
gradient.setFocalPoint(3, 3);
|
||||
gradient.setColorAt(1, QColor(Qt::yellow).lighter(120));
|
||||
gradient.setColorAt(0, QColor(Qt::darkYellow).lighter(120));
|
||||
} else {
|
||||
gradient.setColorAt(0, Qt::yellow);
|
||||
gradient.setColorAt(1, Qt::darkYellow);
|
||||
}
|
||||
painter->setBrush(gradient);
|
||||
|
||||
painter->setPen(QPen(Qt::black, 0));
|
||||
painter->drawEllipse(-10, -10, 20, 20);
|
||||
}
|
||||
//! [10]
|
||||
|
||||
//! [11]
|
||||
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
|
||||
{
|
||||
switch (change) {
|
||||
case ItemPositionHasChanged:
|
||||
for (Edge *edge : std::as_const(edgeList))
|
||||
edge->adjust();
|
||||
graph->itemMoved();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
};
|
||||
|
||||
return QGraphicsItem::itemChange(change, value);
|
||||
}
|
||||
//! [11]
|
||||
|
||||
//! [12]
|
||||
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
QGraphicsItem::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
||||
{
|
||||
update();
|
||||
QGraphicsItem::mouseReleaseEvent(event);
|
||||
}
|
||||
//! [12]
|
45
examples/widgets/graphicsview/elasticnodes/node.h
Normal file
45
examples/widgets/graphicsview/elasticnodes/node.h
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QList>
|
||||
|
||||
class Edge;
|
||||
class GraphWidget;
|
||||
|
||||
//! [0]
|
||||
class Node : public QGraphicsItem
|
||||
{
|
||||
public:
|
||||
Node(GraphWidget *graphWidget);
|
||||
|
||||
void addEdge(Edge *edge);
|
||||
QList<Edge *> edges() const;
|
||||
|
||||
enum { Type = UserType + 1 };
|
||||
int type() const override { return Type; }
|
||||
|
||||
void calculateForces();
|
||||
bool advancePosition();
|
||||
|
||||
QRectF boundingRect() const override;
|
||||
QPainterPath shape() const override;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
|
||||
protected:
|
||||
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
|
||||
|
||||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||
|
||||
private:
|
||||
QList<Edge *> edgeList;
|
||||
QPointF newPos;
|
||||
GraphWidget *graph;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif // NODE_H
|
Reference in New Issue
Block a user