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 .DS_Store
.vscode .vscode
build/

View file

@ -7,6 +7,8 @@ qt_add_executable(scribbles
Edge.cpp Edge.cpp
Node.cpp Node.cpp
GraphWidget.cpp GraphWidget.cpp
DrawOptionsToggleWidget.h
DrawOptionsToggleWidget.cpp
) )
target_link_libraries(scribbles PRIVATE Qt6::Widgets) 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 <QPainter>
#include <QtMath> #include <QtMath>
//! [0]
Edge::Edge(Node *sourceNode, Node *destNode) Edge::Edge(Node *sourceNode, Node *destNode)
: source(sourceNode), dest(destNode) : source(sourceNode), dest(destNode)
{ {
@ -13,9 +12,7 @@ Edge::Edge(Node *sourceNode, Node *destNode)
dest->addEdge(this); dest->addEdge(this);
adjust(); adjust();
} }
//! [0]
//! [1]
Node *Edge::sourceNode() const Node *Edge::sourceNode() const
{ {
return source; return source;
@ -25,9 +22,7 @@ Node *Edge::destNode() const
{ {
return dest; return dest;
} }
//! [1]
//! [2]
void Edge::adjust() void Edge::adjust()
{ {
if (!source || !dest) if (!source || !dest)
@ -46,9 +41,7 @@ void Edge::adjust()
sourcePoint = destPoint = line.p1(); sourcePoint = destPoint = line.p1();
} }
} }
//! [2]
//! [3]
QRectF Edge::boundingRect() const QRectF Edge::boundingRect() const
{ {
if (!source || !dest) if (!source || !dest)
@ -62,9 +55,7 @@ QRectF Edge::boundingRect() const
.normalized() .normalized()
.adjusted(-extra, -extra, extra, extra); .adjusted(-extra, -extra, extra, extra);
} }
//! [3]
//! [4]
void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{ {
if (!source || !dest) if (!source || !dest)
@ -73,16 +64,10 @@ void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
QLineF line(sourcePoint, destPoint); QLineF line(sourcePoint, destPoint);
if (qFuzzyCompare(line.length(), qreal(0.))) if (qFuzzyCompare(line.length(), qreal(0.)))
return; return;
//! [4]
//! [5]
// Draw the line itself
painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter->drawLine(line); painter->drawLine(line);
//! [5]
//! [6]
// Draw the arrows
double angle = std::atan2(-line.dy(), line.dx()); double angle = std::atan2(-line.dy(), line.dx());
QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + M_PI / 3) * arrowSize, 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.p1() << sourceArrowP1 << sourceArrowP2);
painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2); painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
} }
//! [6]

View file

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

View file

@ -7,7 +7,6 @@
#include <QKeyEvent> #include <QKeyEvent>
#include <QRandomGenerator> #include <QRandomGenerator>
//! [0]
GraphWidget::GraphWidget(QWidget *parent) GraphWidget::GraphWidget(QWidget *parent)
: QGraphicsView(parent) : QGraphicsView(parent)
{ {
@ -22,9 +21,8 @@ GraphWidget::GraphWidget(QWidget *parent)
scale(qreal(0.8), qreal(0.8)); scale(qreal(0.8), qreal(0.8));
setMinimumSize(400, 400); setMinimumSize(400, 400);
setWindowTitle(tr("Elastic Nodes")); setWindowTitle(tr("Elastic Nodes"));
//! [0]
//! [1] /*
Node *node1 = new Node(this); Node *node1 = new Node(this);
Node *node2 = new Node(this); Node *node2 = new Node(this);
Node *node3 = new Node(this); Node *node3 = new Node(this);
@ -65,18 +63,15 @@ GraphWidget::GraphWidget(QWidget *parent)
node7->setPos(-50, 50); node7->setPos(-50, 50);
node8->setPos(0, 50); node8->setPos(0, 50);
node9->setPos(50, 50); node9->setPos(50, 50);
*/
} }
//! [1]
//! [2]
void GraphWidget::itemMoved() void GraphWidget::itemMoved()
{ {
if (!timerId) if (!timerId)
timerId = startTimer(1000 / 25); timerId = startTimer(1000 / 25);
} }
//! [2]
//! [3]
void GraphWidget::keyPressEvent(QKeyEvent *event) void GraphWidget::keyPressEvent(QKeyEvent *event)
{ {
switch (event->key()) { switch (event->key()) {
@ -106,9 +101,7 @@ void GraphWidget::keyPressEvent(QKeyEvent *event)
QGraphicsView::keyPressEvent(event); QGraphicsView::keyPressEvent(event);
} }
} }
//! [3]
//! [4]
void GraphWidget::timerEvent(QTimerEvent *event) void GraphWidget::timerEvent(QTimerEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
@ -134,18 +127,27 @@ void GraphWidget::timerEvent(QTimerEvent *event)
timerId = 0; timerId = 0;
} }
} }
//! [4]
#if QT_CONFIG(wheelevent) #if QT_CONFIG(wheelevent)
//! [5]
void GraphWidget::wheelEvent(QWheelEvent *event) void GraphWidget::wheelEvent(QWheelEvent *event)
{ {
scaleView(pow(2., -event->angleDelta().y() / 240.0)); scaleView(pow(2., -event->angleDelta().y() / 240.0));
} }
//! [5]
#endif #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) void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
{ {
Q_UNUSED(rect); Q_UNUSED(rect);
@ -162,7 +164,7 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
// Fill // Fill
QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight()); QLinearGradient gradient(sceneRect.topLeft(), sceneRect.bottomRight());
gradient.setColorAt(0, Qt::white); gradient.setColorAt(0, Qt::white);
gradient.setColorAt(1, Qt::lightGray); gradient.setColorAt(1, Qt::darkRed);
painter->fillRect(rect.intersected(sceneRect), gradient); painter->fillRect(rect.intersected(sceneRect), gradient);
painter->setBrush(Qt::NoBrush); painter->setBrush(Qt::NoBrush);
painter->drawRect(sceneRect); painter->drawRect(sceneRect);
@ -182,9 +184,7 @@ void GraphWidget::drawBackground(QPainter *painter, const QRectF &rect)
painter->setPen(Qt::black); painter->setPen(Qt::black);
painter->drawText(textRect, message); painter->drawText(textRect, message);
} }
//! [6]
//! [7]
void GraphWidget::scaleView(qreal scaleFactor) void GraphWidget::scaleView(qreal scaleFactor)
{ {
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width(); 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); scale(scaleFactor, scaleFactor);
} }
//! [7]
void GraphWidget::shuffle() void GraphWidget::shuffle()
{ {

View file

@ -1,11 +1,9 @@
#ifndef GRAPHWIDGET_H #pragma once
#define GRAPHWIDGET_H
#include <QGraphicsView> #include <QGraphicsView>
class Node; class Node;
//! [0]
class GraphWidget : public QGraphicsView class GraphWidget : public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
@ -25,15 +23,17 @@ protected:
void timerEvent(QTimerEvent *event) override; void timerEvent(QTimerEvent *event) override;
#if QT_CONFIG(wheelevent) #if QT_CONFIG(wheelevent)
void wheelEvent(QWheelEvent *event) override; void wheelEvent(QWheelEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
#endif #endif
void drawBackground(QPainter *painter, const QRectF &rect) override; void drawBackground(QPainter *painter, const QRectF &rect) override;
void scaleView(qreal scaleFactor); void scaleView(qreal scaleFactor);
private: 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 "ScribbleArea.h"
#include "GraphWidget.h" #include "GraphWidget.h"
#include "DrawOptionsToggleWidget.h"
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
@ -22,10 +23,14 @@ using namespace Qt::StringLiterals;
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
//ui(new Ui::MainWindow), //ui(new Ui::MainWindow),
m_scribble_area(new ScribbleArea), m_scribble_area(new ScribbleArea)
m_graph_widget(new GraphWidget)
{ {
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")); setWindowTitle(tr("Scribbles"));
//resize(500, 500); //resize(500, 500);

View file

@ -7,7 +7,6 @@
#include <QPainter> #include <QPainter>
#include <QStyleOption> #include <QStyleOption>
//! [0]
Node::Node(GraphWidget *graphWidget) Node::Node(GraphWidget *graphWidget)
: graph(graphWidget) : graph(graphWidget)
{ {
@ -16,9 +15,7 @@ Node::Node(GraphWidget *graphWidget)
setCacheMode(DeviceCoordinateCache); setCacheMode(DeviceCoordinateCache);
setZValue(-1); setZValue(-1);
} }
//! [0]
//! [1]
void Node::addEdge(Edge *edge) void Node::addEdge(Edge *edge)
{ {
edgeList << edge; edgeList << edge;
@ -29,19 +26,14 @@ QList<Edge *> Node::edges() const
{ {
return edgeList; return edgeList;
} }
//! [1]
//! [2]
void Node::calculateForces() void Node::calculateForces()
{ {
if (!scene() || scene()->mouseGrabberItem() == this) { if (!scene() || scene()->mouseGrabberItem() == this) {
newPos = pos(); newPos = pos();
return; return;
} }
//! [2]
//! [3]
// Sum up all forces pushing this item away
qreal xvel = 0; qreal xvel = 0;
qreal yvel = 0; qreal yvel = 0;
const QList<QGraphicsItem *> items = scene()->items(); const QList<QGraphicsItem *> items = scene()->items();
@ -59,9 +51,7 @@ void Node::calculateForces()
yvel += (dy * 150.0) / l; yvel += (dy * 150.0) / l;
} }
} }
//! [3]
//! [4]
// Now subtract all forces pulling items together // Now subtract all forces pulling items together
double weight = (edgeList.size() + 1) * 10; double weight = (edgeList.size() + 1) * 10;
for (const Edge *edge : std::as_const(edgeList)) { for (const Edge *edge : std::as_const(edgeList)) {
@ -73,22 +63,16 @@ void Node::calculateForces()
xvel -= vec.x() / weight; xvel -= vec.x() / weight;
yvel -= vec.y() / weight; yvel -= vec.y() / weight;
} }
//! [4]
//! [5]
if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1) if (qAbs(xvel) < 0.1 && qAbs(yvel) < 0.1)
xvel = yvel = 0; xvel = yvel = 0;
//! [5]
//! [6]
QRectF sceneRect = scene()->sceneRect(); QRectF sceneRect = scene()->sceneRect();
newPos = pos() + QPointF(xvel, yvel); newPos = pos() + QPointF(xvel, yvel);
newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10)); newPos.setX(qMin(qMax(newPos.x(), sceneRect.left() + 10), sceneRect.right() - 10));
newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10)); newPos.setY(qMin(qMax(newPos.y(), sceneRect.top() + 10), sceneRect.bottom() - 10));
} }
//! [6]
//! [7]
bool Node::advancePosition() bool Node::advancePosition()
{ {
if (newPos == pos()) if (newPos == pos())
@ -97,26 +81,20 @@ bool Node::advancePosition()
setPos(newPos); setPos(newPos);
return true; return true;
} }
//! [7]
//! [8]
QRectF Node::boundingRect() const QRectF Node::boundingRect() const
{ {
qreal adjust = 2; qreal adjust = 2;
return QRectF( -10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust); return QRectF( -10 - adjust, -10 - adjust, 23 + adjust, 23 + adjust);
} }
//! [8]
//! [9]
QPainterPath Node::shape() const QPainterPath Node::shape() const
{ {
QPainterPath path; QPainterPath path;
path.addEllipse(-10, -10, 20, 20); path.addEllipse(-10, -10, 20, 20);
return path; return path;
} }
//! [9]
//! [10]
void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *) void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{ {
painter->setPen(Qt::NoPen); painter->setPen(Qt::NoPen);
@ -138,9 +116,7 @@ void Node::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWid
painter->setPen(QPen(Qt::black, 0)); painter->setPen(QPen(Qt::black, 0));
painter->drawEllipse(-10, -10, 20, 20); painter->drawEllipse(-10, -10, 20, 20);
} }
//! [10]
//! [11]
QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value) QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
{ {
switch (change) { switch (change) {
@ -155,9 +131,7 @@ QVariant Node::itemChange(GraphicsItemChange change, const QVariant &value)
return QGraphicsItem::itemChange(change, value); return QGraphicsItem::itemChange(change, value);
} }
//! [11]
//! [12]
void Node::mousePressEvent(QGraphicsSceneMouseEvent *event) void Node::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
update(); update();
@ -169,4 +143,3 @@ void Node::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
update(); update();
QGraphicsItem::mouseReleaseEvent(event); QGraphicsItem::mouseReleaseEvent(event);
} }
//! [12]

View file

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

View file

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