summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleks Kissinger <aleks0@gmail.com>2020-04-13 16:21:39 +0100
committerAleks Kissinger <aleks0@gmail.com>2020-04-13 16:21:39 +0100
commit732499eead71b23a7ba551fe788b47a4cf45124e (patch)
treeac3a770f742a2ebc749afd273037ee60a326ed61
parent6dda16a24dfe7cbd0d90b77c57f1cf789210feb5 (diff)
adding and drawing paths works
-rw-r--r--src/gui/edgeitem.cpp87
-rw-r--r--src/gui/pathitem.cpp65
-rw-r--r--src/gui/pathitem.h28
-rw-r--r--src/gui/tikzscene.cpp36
-rw-r--r--src/gui/tikzscene.h3
-rw-r--r--src/gui/undocommands.cpp25
-rw-r--r--tikzit.pro2
7 files changed, 203 insertions, 43 deletions
diff --git a/src/gui/edgeitem.cpp b/src/gui/edgeitem.cpp
index 45ae159..675ddd7 100644
--- a/src/gui/edgeitem.cpp
+++ b/src/gui/edgeitem.cpp
@@ -71,7 +71,8 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
QPen pen = _edge->style()->pen();
painter->setPen(pen);
painter->setBrush(Qt::NoBrush);
- painter->drawPath(path());
+
+ if (!_edge->path()) painter->drawPath(path());
QPointF ht = _edge->headTangent();
QPointF hLeft(-ht.y(), ht.x());
@@ -83,27 +84,27 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
pen.setStyle(Qt::SolidLine);
painter->setPen(pen);
-
-
- switch (_edge->style()->arrowHead()) {
- case Style::Flat:
- {
- painter->drawLine(
- toScreen(_edge->head() + hLeft),
- toScreen(_edge->head() + hRight));
- break;
- }
- case Style::Pointer:
- {
- QPainterPath pth;
- pth.moveTo(toScreen(_edge->head() + ht + hLeft));
- pth.lineTo(toScreen(_edge->head()));
- pth.lineTo(toScreen(_edge->head() + ht + hRight));
- painter->drawPath(pth);
- break;
- }
- case Style::NoTip:
- break;
+ if (!_edge->path() || _edge->path()->edges().last() == _edge) {
+ switch (_edge->style()->arrowHead()) {
+ case Style::Flat:
+ {
+ painter->drawLine(
+ toScreen(_edge->head() + hLeft),
+ toScreen(_edge->head() + hRight));
+ break;
+ }
+ case Style::Pointer:
+ {
+ QPainterPath pth;
+ pth.moveTo(toScreen(_edge->head() + ht + hLeft));
+ pth.lineTo(toScreen(_edge->head()));
+ pth.lineTo(toScreen(_edge->head() + ht + hRight));
+ painter->drawPath(pth);
+ break;
+ }
+ case Style::NoTip:
+ break;
+ }
}
//QPen outline = QPen(Qt::red);
@@ -111,26 +112,28 @@ void EdgeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidge
//painter->drawPath(_expPath);
//painter->setPen(pen);
- switch (_edge->style()->arrowTail()) {
- case Style::Flat:
- {
- painter->drawLine(
- toScreen(_edge->tail() + tLeft),
- toScreen(_edge->tail() + tRight));
- break;
- }
- case Style::Pointer:
- {
- QPainterPath pth;
- pth.moveTo(toScreen(_edge->tail() + tt + tLeft));
- pth.lineTo(toScreen(_edge->tail()));
- pth.lineTo(toScreen(_edge->tail() + tt + tRight));
- painter->drawPath(pth);
- break;
- }
- case Style::NoTip:
- break;
- }
+ if (!_edge->path() || _edge->path()->edges().first() == _edge) {
+ switch (_edge->style()->arrowTail()) {
+ case Style::Flat:
+ {
+ painter->drawLine(
+ toScreen(_edge->tail() + tLeft),
+ toScreen(_edge->tail() + tRight));
+ break;
+ }
+ case Style::Pointer:
+ {
+ QPainterPath pth;
+ pth.moveTo(toScreen(_edge->tail() + tt + tLeft));
+ pth.lineTo(toScreen(_edge->tail()));
+ pth.lineTo(toScreen(_edge->tail() + tt + tRight));
+ painter->drawPath(pth);
+ break;
+ }
+ case Style::NoTip:
+ break;
+ }
+ }
if (isSelected()) {
QColor draw;
diff --git a/src/gui/pathitem.cpp b/src/gui/pathitem.cpp
new file mode 100644
index 0000000..107281f
--- /dev/null
+++ b/src/gui/pathitem.cpp
@@ -0,0 +1,65 @@
+#include "pathitem.h"
+#include "tikzit.h"
+
+PathItem::PathItem(Path *path)
+{
+ _path = path;
+ readPos();
+}
+
+void PathItem::readPos()
+{
+ QPainterPath painterPath;
+
+ foreach (Edge *e, _path->edges()) {
+ e->updateControls();
+
+ if (e == _path->edges().first())
+ painterPath.moveTo (toScreen(e->tail()));
+
+ if (e->bend() != 0 || !e->basicBendMode()) {
+ painterPath.cubicTo(toScreen(e->cp1()),
+ toScreen(e->cp2()),
+ toScreen(e->head()));
+ } else {
+ painterPath.lineTo(toScreen(e->head()));
+ }
+ }
+
+ setPainterPath(painterPath);
+}
+
+void PathItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
+{
+ Style *st = _path->edges().first()->style();
+ QPen pen = st->pen();
+ painter->setPen(st->pen());
+ painter->setBrush(st->brush());
+ painter->drawPath(painterPath());
+}
+
+Path *PathItem::path() const
+{
+ return _path;
+}
+
+QPainterPath PathItem::painterPath() const
+{
+ return _painterPath;
+}
+
+void PathItem::setPainterPath(const QPainterPath &painterPath)
+{
+ prepareGeometryChange();
+
+ _painterPath = painterPath;
+ float r = GLOBAL_SCALEF * 0.1;
+ _boundingRect = _painterPath.boundingRect().adjusted(-r,-r,r,r);
+
+ update();
+}
+
+QRectF PathItem::boundingRect() const
+{
+ return _boundingRect;
+}
diff --git a/src/gui/pathitem.h b/src/gui/pathitem.h
new file mode 100644
index 0000000..7dc9c80
--- /dev/null
+++ b/src/gui/pathitem.h
@@ -0,0 +1,28 @@
+#ifndef PATHITEM_H
+#define PATHITEM_H
+
+#include "path.h"
+
+#include <QGraphicsItem>
+
+class PathItem : public QGraphicsItem
+{
+public:
+ PathItem(Path *path);
+ void readPos();
+ void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override;
+
+ Path *path() const;
+
+ QPainterPath painterPath() const;
+ void setPainterPath(const QPainterPath &painterPath);
+
+ QRectF boundingRect() const override;
+
+private:
+ Path *_path;
+ QPainterPath _painterPath;
+ QRectF _boundingRect;
+};
+
+#endif // PATHITEM_H
diff --git a/src/gui/tikzscene.cpp b/src/gui/tikzscene.cpp
index cebf5be..da52277 100644
--- a/src/gui/tikzscene.cpp
+++ b/src/gui/tikzscene.cpp
@@ -97,12 +97,25 @@ void TikzScene::graphReplaced()
}
_edgeItems.clear();
+ foreach (PathItem *pi, _pathItems) {
+ removeItem(pi);
+ delete pi;
+ }
+ _pathItems.clear();
+
foreach (Edge *e, graph()->edges()) {
//e->attachStyle();
//e->updateControls();
EdgeItem *ei = new EdgeItem(e);
_edgeItems.insert(e, ei);
addItem(ei);
+
+ Path *p = e->path();
+ if (p && p->edges().first() == e) {
+ PathItem *pi = new PathItem(p);
+ _pathItems.insert(p, pi);
+ addItem(pi);
+ }
}
foreach (Node *n, graph()->nodes()) {
@@ -393,7 +406,12 @@ void TikzScene::refreshZIndices()
{
qreal z = 0.0;
foreach (Edge *e, graph()->edges()) {
- edgeItems()[e]->setZValue(z);
+ if (e->path() && e == e->path()->edges().first()) {
+ pathItems()[e->path()]->setZValue(z);
+ edgeItems()[e]->setZValue(z + 0.1);
+ } else {
+ edgeItems()[e]->setZValue(z);
+ }
z += 1.0;
}
@@ -606,6 +624,8 @@ void TikzScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
}
_modifyEdgeItem->readPos();
+ Path *p = _modifyEdgeItem->edge()->path();
+ if (p) pathItems()[p]->readPos();
} else if (_draggingNodes) { // nodes being dragged
QGraphicsScene::mouseMoveEvent(event);
@@ -1253,6 +1273,8 @@ void TikzScene::refreshSceneBounds() {
void TikzScene::refreshAdjacentEdges(QList<Node*> nodes)
{
if (nodes.empty()) return;
+
+ QSet<Path*> paths;
foreach (Edge *e, _edgeItems.keys()) {
EdgeItem *ei = _edgeItems[e];
@@ -1263,6 +1285,13 @@ void TikzScene::refreshAdjacentEdges(QList<Node*> nodes)
ei->readPos();
}
}
+
+ // only update paths once
+ Path *p = ei->edge()->path();
+ if (p && !paths.contains(p)) {
+ pathItems()[p]->readPos();
+ paths << p;
+ }
}
}
@@ -1289,3 +1318,8 @@ QMap<Edge*,EdgeItem*> &TikzScene::edgeItems()
{
return _edgeItems;
}
+
+QMap<Path *, PathItem *> &TikzScene::pathItems()
+{
+ return _pathItems;
+}
diff --git a/src/gui/tikzscene.h b/src/gui/tikzscene.h
index 5996263..2e72faf 100644
--- a/src/gui/tikzscene.h
+++ b/src/gui/tikzscene.h
@@ -27,6 +27,7 @@
#include "graph.h"
#include "nodeitem.h"
#include "edgeitem.h"
+#include "pathitem.h"
#include "tikzdocument.h"
#include "toolpalette.h"
#include "stylepalette.h"
@@ -48,6 +49,7 @@ public:
Graph *graph();
QMap<Node*,NodeItem*> &nodeItems();
QMap<Edge*,EdgeItem*> &edgeItems();
+ QMap<Path*,PathItem*> &pathItems();
void refreshAdjacentEdges(QList<Node*> nodes);
// void setBounds(QRectF bounds);
@@ -112,6 +114,7 @@ private:
StylePalette *_styles;
QMap<Node*,NodeItem*> _nodeItems;
QMap<Edge*,EdgeItem*> _edgeItems;
+ QMap<Path*,PathItem*> _pathItems;
QGraphicsLineItem *_drawEdgeItem;
QGraphicsRectItem *_rubberBandItem;
EdgeItem *_modifyEdgeItem;
diff --git a/src/gui/undocommands.cpp b/src/gui/undocommands.cpp
index a07f251..87d5a13 100644
--- a/src/gui/undocommands.cpp
+++ b/src/gui/undocommands.cpp
@@ -102,6 +102,9 @@ void EdgeBendCommand::undo()
foreach(EdgeItem *ei, _scene->edgeItems()) {
if (ei->edge() == _edge) {
ei->readPos();
+
+ Path *p = ei->edge()->path();
+ if (p) _scene->pathItems()[p]->readPos();
break;
}
}
@@ -118,6 +121,9 @@ void EdgeBendCommand::redo()
foreach(EdgeItem *ei, _scene->edgeItems()) {
if (ei->edge() == _edge) {
ei->readPos();
+
+ Path *p = ei->edge()->path();
+ if (p) _scene->pathItems()[p]->readPos();
break;
}
}
@@ -287,6 +293,9 @@ void ChangeEdgeModeCommand::undo()
// FIXME: this act strangely sometimes
_edge->setBasicBendMode(!_edge->basicBendMode());
_scene->edgeItems()[_edge]->readPos();
+ Path *p = _edge->path();
+ if (p) _scene->pathItems()[p]->readPos();
+
GraphUpdateCommand::undo();
}
@@ -294,6 +303,9 @@ void ChangeEdgeModeCommand::redo()
{
_edge->setBasicBendMode(!_edge->basicBendMode());
_scene->edgeItems()[_edge]->readPos();
+ Path *p = _edge->path();
+ if (p) _scene->pathItems()[p]->readPos();
+
GraphUpdateCommand::redo();
}
@@ -607,6 +619,12 @@ MakePathCommand::MakePathCommand(TikzScene *scene,
void MakePathCommand::undo()
{
Path *p = _edgeList.first()->path();
+
+ PathItem *pi = _scene->pathItems()[p];
+ _scene->removeItem(pi);
+ _scene->pathItems().remove(p);
+ delete pi;
+
p->removeEdges();
_scene->graph()->removePath(p);
@@ -617,6 +635,7 @@ void MakePathCommand::undo()
}
}
+ _scene->refreshZIndices();
GraphUpdateCommand::undo();
}
@@ -640,5 +659,11 @@ void MakePathCommand::redo()
_scene->graph()->addPath(p);
+ PathItem *pi = new PathItem(p);
+ _scene->addItem(pi);
+ _scene->pathItems().insert(p, pi);
+ pi->readPos();
+
+ _scene->refreshZIndices();
GraphUpdateCommand::redo();
}
diff --git a/tikzit.pro b/tikzit.pro
index 878caaa..46a411a 100644
--- a/tikzit.pro
+++ b/tikzit.pro
@@ -54,6 +54,7 @@ include(bison.pri)
SOURCES += src/gui/mainwindow.cpp \
src/data/path.cpp \
+ src/gui/pathitem.cpp \
src/gui/toolpalette.cpp \
src/gui/tikzscene.cpp \
src/data/graph.cpp \
@@ -87,6 +88,7 @@ SOURCES += src/gui/mainwindow.cpp \
HEADERS += src/gui/mainwindow.h \
src/data/path.h \
+ src/gui/pathitem.h \
src/gui/toolpalette.h \
src/gui/tikzscene.h \
src/data/graph.h \