Skip to content

Commit

Permalink
Fix GitHub Issue #32: Add mechanism to set arrow direction
Browse files Browse the repository at this point in the history
  • Loading branch information
juzzlin committed Mar 29, 2019
1 parent 2e5bfea commit d7502ef
Show file tree
Hide file tree
Showing 23 changed files with 493 additions and 66 deletions.
Binary file modified data/translations/heimer_fi.qm
Binary file not shown.
120 changes: 100 additions & 20 deletions src/edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <QTimer>
#include <QVector2D>

#include <QtMath>

#include <cassert>
#include <cmath>

Expand All @@ -41,8 +43,10 @@ Edge::Edge(Node & sourceNode, Node & targetNode, bool enableAnimations, bool ena
, m_sourceDot(enableAnimations ? new EdgeDot(this) : nullptr)
, m_targetDot(enableAnimations ? new EdgeDot(this) : nullptr)
, m_label(enableLabel ? new EdgeTextEdit(this) : nullptr)
, m_arrowheadL(new QGraphicsLineItem(this))
, m_arrowheadR(new QGraphicsLineItem(this))
, m_arrowheadL0(new QGraphicsLineItem(this))
, m_arrowheadR0(new QGraphicsLineItem(this))
, m_arrowheadL1(new QGraphicsLineItem(this))
, m_arrowheadR1(new QGraphicsLineItem(this))
, m_sourceDotSizeAnimation(enableAnimations ? new QPropertyAnimation(m_sourceDot, "scale", this) : nullptr)
, m_targetDotSizeAnimation(enableAnimations ? new QPropertyAnimation(m_targetDot, "scale", this) : nullptr)
{
Expand Down Expand Up @@ -121,6 +125,18 @@ void Edge::initDots()
}
}

void Edge::setArrowHeadPen(const QPen & pen)
{
m_arrowheadL0->setPen(pen);
m_arrowheadL0->update();
m_arrowheadR0->setPen(pen);
m_arrowheadR0->update();
m_arrowheadL1->setPen(pen);
m_arrowheadL1->update();
m_arrowheadR1->setPen(pen);
m_arrowheadR1->update();
}

void Edge::setLabelVisible(bool visible)
{
m_label->setVisible(visible);
Expand All @@ -131,22 +147,24 @@ void Edge::setWidth(double width)
EdgeBase::setWidth(width);

setPen(getPen());
m_arrowheadL->setPen(pen());
m_arrowheadL->update();
m_arrowheadR->setPen(pen());
m_arrowheadR->update();
setArrowHeadPen(pen());
updateLine();
}

void Edge::setArrowMode(ArrowMode arrowMode)
{
EdgeBase::setArrowMode(arrowMode);
#ifndef HEIMER_UNIT_TEST
updateLine();
#endif
}

void Edge::setColor(const QColor & color)
{
EdgeBase::setColor(color);

setPen(getPen());
m_arrowheadL->setPen(pen());
m_arrowheadL->update();
m_arrowheadR->setPen(pen());
m_arrowheadR->update();
setArrowHeadPen(pen());
updateLine();
}

Expand All @@ -170,6 +188,20 @@ void Edge::setTextSize(int textSize)
}
}

void Edge::setReversed(bool reversed)
{
EdgeBase::setReversed(reversed);

updateArrowhead();
}

void Edge::setSelected(bool selected)
{
EdgeBase::setSelected(selected);
setGraphicsEffect(GraphicsFactory::createDropShadowEffect(selected));
update();
}

Node & Edge::sourceNode() const
{
auto node = dynamic_cast<Node *>(&sourceNodeBase());
Expand All @@ -186,16 +218,62 @@ Node & Edge::targetNode() const

void Edge::updateArrowhead()
{
QLineF line;
double angle = (-this->line().angle() + Constants::Edge::ARROW_OPENING) / 180 * M_PI;
line.setP1(this->line().p2());
line.setP2(this->line().p2() + QPointF(std::cos(angle), std::sin(angle)) * Constants::Edge::ARROW_LENGTH);
m_arrowheadL->setLine(line);

angle = (-this->line().angle() - Constants::Edge::ARROW_OPENING) / 180 * M_PI;
line.setP1(this->line().p2());
line.setP2(this->line().p2() + QPointF(std::cos(angle), std::sin(angle)) * Constants::Edge::ARROW_LENGTH);
m_arrowheadR->setLine(line);
const auto point0 = reversed() ? this->line().p1() : this->line().p2();
const auto angle0 = reversed() ? -this->line().angle() + 180 : -this->line().angle();
const auto point1 = reversed() ? this->line().p2() : this->line().p1();
const auto angle1 = reversed() ? -this->line().angle() : -this->line().angle() + 180;

QLineF lineL0;
QLineF lineR0;
QLineF lineL1;
QLineF lineR1;

switch (arrowMode()) {
case ArrowMode::Single: {
lineL0.setP1(point0);
const auto angleL = qDegreesToRadians(angle0 + Constants::Edge::ARROW_OPENING);
lineL0.setP2(point0 + QPointF(std::cos(angleL), std::sin(angleL)) * Constants::Edge::ARROW_LENGTH);
lineR0.setP1(point0);
const auto angleR = qDegreesToRadians(angle0 - Constants::Edge::ARROW_OPENING);
lineR0.setP2(point0 + QPointF(std::cos(angleR), std::sin(angleR)) * Constants::Edge::ARROW_LENGTH);
m_arrowheadL0->setLine(lineL0);
m_arrowheadR0->setLine(lineR0);
m_arrowheadL0->show();
m_arrowheadR0->show();
m_arrowheadL1->hide();
m_arrowheadR1->hide();
break;
}
case ArrowMode::Double: {
lineL0.setP1(point0);
const auto angleL0 = qDegreesToRadians(angle0 + Constants::Edge::ARROW_OPENING);
lineL0.setP2(point0 + QPointF(std::cos(angleL0), std::sin(angleL0)) * Constants::Edge::ARROW_LENGTH);
lineR0.setP1(point0);
const auto angleR0 = qDegreesToRadians(angle0 - Constants::Edge::ARROW_OPENING);
lineR0.setP2(point0 + QPointF(std::cos(angleR0), std::sin(angleR0)) * Constants::Edge::ARROW_LENGTH);
lineL1.setP1(point1);
m_arrowheadL0->setLine(lineL0);
m_arrowheadR0->setLine(lineR0);
m_arrowheadL0->show();
m_arrowheadR0->show();
const auto angleL1 = qDegreesToRadians(angle1 + Constants::Edge::ARROW_OPENING);
lineL1.setP2(point1 + QPointF(std::cos(angleL1), std::sin(angleL1)) * Constants::Edge::ARROW_LENGTH);
lineR1.setP1(point1);
const auto angleR1 = qDegreesToRadians(angle1 - Constants::Edge::ARROW_OPENING);
lineR1.setP2(point1 + QPointF(std::cos(angleR1), std::sin(angleR1)) * Constants::Edge::ARROW_LENGTH);
m_arrowheadL1->setLine(lineL1);
m_arrowheadR1->setLine(lineR1);
m_arrowheadL1->show();
m_arrowheadR1->show();
break;
}
case ArrowMode::Hidden:
m_arrowheadL0->hide();
m_arrowheadR0->hide();
m_arrowheadL1->hide();
m_arrowheadR1->hide();
break;
}
}

void Edge::updateDots(const std::pair<EdgePoint, EdgePoint> & nearestPoints)
Expand Down Expand Up @@ -263,10 +341,12 @@ Edge::~Edge()
delete m_targetDot;
}

#ifndef HEIMER_UNIT_TEST
sourceNode().removeGraphicsEdge(*this);
targetNode().removeGraphicsEdge(*this);

// Needed to remove glitches from possibly running dot animations
sourceNode().update();
targetNode().update();
#endif
}
18 changes: 15 additions & 3 deletions src/edge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Edge : public QObject, public QGraphicsLineItem, public EdgeBase

Edge(Node & sourceNode, Node & targetNode, bool enableAnimations = true, bool enableLabel = true);

virtual ~Edge();
virtual ~Edge() override;

Node & sourceNode() const;

Expand All @@ -54,6 +54,8 @@ public slots:

void updateLine();

virtual void setArrowMode(ArrowMode arrowMode) override;

virtual void setColor(const QColor & color) override;

virtual void setWidth(double width) override;
Expand All @@ -62,6 +64,10 @@ public slots:

virtual void setTextSize(int textSize) override;

virtual void setReversed(bool reversed) override;

virtual void setSelected(bool selected) override;

signals:

void undoPointRequested();
Expand All @@ -72,6 +78,8 @@ public slots:

void initDots();

void setArrowHeadPen(const QPen & pen);

void setLabelVisible(bool visible);

void updateArrowhead();
Expand All @@ -90,9 +98,13 @@ public slots:

EdgeTextEdit * m_label;

QGraphicsLineItem * m_arrowheadL;
QGraphicsLineItem * m_arrowheadL0;

QGraphicsLineItem * m_arrowheadR0;

QGraphicsLineItem * m_arrowheadL1;

QGraphicsLineItem * m_arrowheadR;
QGraphicsLineItem * m_arrowheadR1;

QPropertyAnimation * m_sourceDotSizeAnimation;

Expand Down
30 changes: 30 additions & 0 deletions src/edge_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ void EdgeBase::setTargetNode(NodeBase & targetNode)
m_targetNode = &targetNode;
}

EdgeBase::ArrowMode EdgeBase::arrowMode() const
{
return m_arrowMode;
}

void EdgeBase::setArrowMode(EdgeBase::ArrowMode arrowMode)
{
m_arrowMode = arrowMode;
}

QColor EdgeBase::color() const
{
return m_color;
Expand Down Expand Up @@ -67,6 +77,26 @@ void EdgeBase::setTextSize(int textSize)
m_textSize = textSize;
}

bool EdgeBase::reversed() const
{
return m_reversed;
}

void EdgeBase::setReversed(bool reversed)
{
m_reversed = reversed;
}

bool EdgeBase::selected() const
{
return m_selected;
}

void EdgeBase::setSelected(bool selected)
{
m_selected = selected;
}

NodeBase & EdgeBase::sourceNodeBase() const
{
return *m_sourceNode;
Expand Down
31 changes: 28 additions & 3 deletions src/edge_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// You should have received a copy of the GNU General Public License
// along with Heimer. If not, see <http://www.gnu.org/licenses/>.

#ifndef EDGEBASE_HPP
#define EDGEBASE_HPP
#ifndef EDGE_BASE_HPP
#define EDGE_BASE_HPP

#include <memory>

Expand All @@ -27,6 +27,13 @@ class EdgeBase
{
public:

enum class ArrowMode
{
Single = 0,
Double = 1,
Hidden = 2
};

EdgeBase(NodeBase & sourceNode, NodeBase & targetNode);

virtual void setSourceNode(NodeBase & sourceNode);
Expand All @@ -37,6 +44,10 @@ class EdgeBase

virtual NodeBase & targetNodeBase() const;

ArrowMode arrowMode() const;

virtual void setArrowMode(ArrowMode arrowMode);

QColor color() const;

virtual void setColor(const QColor & color);
Expand All @@ -55,6 +66,14 @@ class EdgeBase

virtual void setTextSize(int textSize);

virtual bool reversed() const;

virtual void setReversed(bool reversed);

virtual bool selected() const;

virtual void setSelected(bool selected);

private:

NodeBase * m_sourceNode;
Expand All @@ -68,8 +87,14 @@ class EdgeBase
int m_textSize = 11; // Not sure if we should set yet another default value here..

QColor m_color;

bool m_reversed = false;

bool m_selected = false;

ArrowMode m_arrowMode = ArrowMode::Single;
};

using EdgeBasePtr = std::shared_ptr<EdgeBase>;

#endif // EDGEBASE_HPP
#endif // EDGE_BASE_HPP
8 changes: 7 additions & 1 deletion src/edge_text_edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ EdgeTextEdit::EdgeTextEdit(Edge * parentItem)
});
}

void EdgeTextEdit::contextMenuEvent(QGraphicsSceneContextMenuEvent * event)
{
// Prevents the system context menu from opening.

event->ignore();
}

void EdgeTextEdit::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
{
m_visibilityTimer.stop();
Expand All @@ -53,7 +60,6 @@ void EdgeTextEdit::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
TextEdit::hoverLeaveEvent(event);
}


void EdgeTextEdit::setVisible(bool visible)
{
if (visible)
Expand Down
2 changes: 2 additions & 0 deletions src/edge_text_edit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class EdgeTextEdit : public TextEdit

void setVisible(bool visible);

virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent * event) override;

virtual void hoverEnterEvent(QGraphicsSceneHoverEvent * event) override;

virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent * event) override;
Expand Down
Loading

0 comments on commit d7502ef

Please sign in to comment.