Small cleaning up to start supporting canvas area.

This commit is contained in:
jgrogan 2024-05-10 16:08:35 +01:00
parent fc9d1fe08a
commit 8286609061
12 changed files with 59 additions and 90 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
.DS_Store
.vscode
build/

View file

@ -7,6 +7,8 @@ qt_add_executable(scribbles
Edge.cpp
Node.cpp
GraphWidget.cpp
DrawOptionsToggleWidget.h
DrawOptionsToggleWidget.cpp
)
target_link_libraries(scribbles PRIVATE Qt6::Widgets)

View file

View file

@ -0,0 +1,15 @@
#pragma once
#include <QWidget>
class DrawOptionsToggleWidget : public QWidget
{
Q_OBJECT
public:
DrawOptionsToggleWidget(QWidget* parent)
: QWidget(parent)
{
}
};

View file

@ -4,7 +4,6 @@
#include <QPainter>
#include <QtMath>
//! [0]
Edge::Edge(Node *sourceNode, Node *destNode)
: source(sourceNode), dest(destNode)
{
@ -13,9 +12,7 @@ Edge::Edge(Node *sourceNode, Node *destNode)
dest->addEdge(this);
adjust();
}
//! [0]
//! [1]
Node *Edge::sourceNode() const
{
return source;
@ -25,9 +22,7 @@ Node *Edge::destNode() const
{
return dest;
}
//! [1]
//! [2]
void Edge::adjust()
{
if (!source || !dest)
@ -46,9 +41,7 @@ void Edge::adjust()
sourcePoint = destPoint = line.p1();
}
}
//! [2]
//! [3]
QRectF Edge::boundingRect() const
{
if (!source || !dest)
@ -62,9 +55,7 @@ QRectF Edge::boundingRect() const
.normalized()
.adjusted(-extra, -extra, extra, extra);
}
//! [3]
//! [4]
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
if (!source || !dest)
@ -73,16 +64,10 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
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,
@ -98,4 +83,3 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
}
//! [6]

View file

@ -1,11 +1,9 @@
#ifndef EDGE_H
#define EDGE_H
#pragma once
#include <QGraphicsItem>
class Node;
//! [0]
class Edge : public QGraphicsItem
{
public:
@ -30,6 +28,3 @@ private:
QPointF destPoint;
qreal arrowSize = 10;
};
//! [0]
#endif // EDGE_H

View file

@ -7,7 +7,6 @@
#include <QKeyEvent>
#include <QRandomGenerator>
//! [0]
GraphWidget::GraphWidget(QWidget *parent)
: QGraphicsView(parent)
{
@ -22,9 +21,8 @@ GraphWidget::GraphWidget(QWidget *parent)
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);
@ -65,18 +63,15 @@ GraphWidget::GraphWidget(QWidget *parent)
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()) {
@ -106,9 +101,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
QGraphicsView::keyPressEvent(event);
}
}
//! [3]
//! [4]
void GraphWidget::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
@ -134,18 +127,27 @@ void GraphWidget::timerEvent(QTimerEvent *event)
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::mousePressEvent(QMouseEvent *event)
{
Node* node = new Node(this);
node->setPos(50, 50);
scene()->addItem(node);
QGraphicsView::mousePressEvent(event);
}
void GraphWidget::mouseReleaseEvent(QMouseEvent *event)
{
QGraphicsView::mouseReleaseEvent(event);
}
void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{
Q_UNUSED(rect);
@ -162,7 +164,7 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
// Fill
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::lightGray);
gradient.setColorAt(1, Qt::darkRed);
painter->fillRect(rect.intersected(sceneRect), gradient);
painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect);
@ -182,9 +184,7 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
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();
@ -193,7 +193,6 @@ void GraphWidget::scaleView(qreal scaleFactor)
scale(scaleFactor, scaleFactor);
}
//! [7]
void GraphWidget::shuffle()
{

View file

@ -1,11 +1,9 @@
#ifndef GRAPHWIDGET_H
#define GRAPHWIDGET_H
#pragma once
#include <QGraphicsView>
class Node;
//! [0]
class GraphWidget : public QGraphicsView
{
Q_OBJECT
@ -25,15 +23,17 @@ protected:
void timerEvent(QTimerEvent *event) override;
#if QT_CONFIG(wheelevent)
void wheelEvent(QWheelEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *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
int timerId = 0;
Node *centerNode{nullptr};
};

View file

@ -15,6 +15,7 @@
#include "ScribbleArea.h"
#include "GraphWidget.h"
#include "DrawOptionsToggleWidget.h"
using namespace Qt::StringLiterals;
@ -22,10 +23,14 @@ using namespace Qt::StringLiterals;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
//ui(new Ui::MainWindow),
m_scribble_area(new ScribbleArea),
m_graph_widget(new GraphWidget)
m_scribble_area(new ScribbleArea)
{
setCentralWidget(m_graph_widget.get());
auto central_widget = new QWidget(this);
auto draw_options_toggle = new DrawOptionsToggleWidget(central_widget);
auto graph_widget = new GraphWidget(central_widget);
setCentralWidget(central_widget);
setWindowTitle(tr("Scribbles"));
//resize(500, 500);

View file

@ -7,7 +7,6 @@
#include <QPainter>
#include <QStyleOption>
//! [0]
Node::Node(GraphWidget *graphWidget)
: graph(graphWidget)
{
@ -16,9 +15,7 @@ Node::Node(GraphWidget *graphWidget)
setCacheMode(DeviceCoordinateCache);
setZValue(-1);
}
//! [0]
//! [1]
void Node::addEdge(Edge *edge)
{
edgeList << edge;
@ -29,19 +26,14 @@ 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();
@ -59,9 +51,7 @@ void Node::calculateForces()
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)) {
@ -73,22 +63,16 @@ void Node::calculateForces()
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())
@ -97,26 +81,20 @@ bool Node::advancePosition()
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);
@ -138,9 +116,7 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-10, -10, 20, 20);
}
//! [10]
//! [11]
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{
switch (change) {
@ -155,9 +131,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
return QGraphicsItem::itemChange(change, value);
}
//! [11]
//! [12]
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
update();
@ -169,4 +143,3 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
update();
QGraphicsItem::mouseReleaseEvent(event);
}
//! [12]

View file

@ -1,5 +1,4 @@
#ifndef NODE_H
#define NODE_H
#pragma once
#include <QGraphicsItem>
#include <QList>
@ -7,7 +6,6 @@
class Edge;
class GraphWidget;
//! [0]
class Node : public QGraphicsItem
{
public:
@ -37,6 +35,3 @@ private:
QPointF newPos;
GraphWidget *graph;
};
//! [0]
#endif // NODE_H

View file

@ -21,8 +21,8 @@ int main(int argc, char *argv[])
"A Scribbles file to open"));
parser.process(app);
const QStringList &positionalArguments = parser.positionalArguments();
const QString &fileName = (positionalArguments.count() > 0) ? positionalArguments.at(0)
const auto &positionalArguments = parser.positionalArguments();
const auto &fileName = (positionalArguments.count() > 0) ? positionalArguments.at(0)
: QString();
MainWindow w;