VPTissue Reference Manual
EditableEdgeItem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "EditableEdgeItem.h"
21 
22 #include "EditableNodeItem.h"
23 
24 #include <QLineF>
25 #include <QColor>
26 #include <QGraphicsPolygonItem>
27 #include <QStyleOptionGraphicsItem>
28 #include <cassert>
29 
30 class QWidget;
31 
32 namespace SimPT_Editor {
33 
34 
35 
36 const QColor EditableEdgeItem::DEFAULT_HIGHLIGHT_COLOR(230,0,0,255);//(51, 102, 0, 255)
37 
39  : QGraphicsLineItem(), m_endpoint1(node1), m_endpoint2(node2), m_highlight_color(DEFAULT_HIGHLIGHT_COLOR)
40 {
41  Update();
42  setZValue(1); //Draw edges on top of cells.
43 
44  //Update the edge when one of its endpoints has changed.
45  connect(m_endpoint1, SIGNAL(Moved()), this, SLOT(Update()));
46  connect(m_endpoint2, SIGNAL(Moved()), this, SLOT(Update()));
47 }
48 
50 
52 {
53  return m_endpoint1->IsAtBoundary() && m_endpoint2->IsAtBoundary();
54 }
55 
57 {
58  return (m_endpoint1 == endpoint || m_endpoint2 == endpoint);
59 }
60 
62 {
63  return (ContainsEndpoint(endpoint1) && ContainsEndpoint(endpoint2));
64 }
65 
67 {
68  if (m_endpoint1 == edge->m_endpoint1 || m_endpoint1 == edge->m_endpoint2) {
69  return m_endpoint1;
70  }
71  else if (m_endpoint2 == edge->m_endpoint1 || m_endpoint2 == edge->m_endpoint2) {
72  return m_endpoint2;
73  }
74  else {
75  return nullptr;
76  }
77 }
78 
80 {
81  return SimPT_Sim::Edge(m_endpoint1->Node(), m_endpoint2->Node());
82 }
83 
84 void EditableEdgeItem::Highlight(bool highlighted)
85 {
86  QPen pen(this->pen());
87  if (highlighted) {
88  pen.setBrush(m_highlight_color);
89  }
90  setPen(pen);
91 }
92 
93 void EditableEdgeItem::SetHighlightColor(const QColor& color)
94 {
95  m_highlight_color = color;
96 }
97 
99 {
100  EditableNodeItem* firstNode;
101  EditableNodeItem* secondNode;
102 
103  EditableNodeItem* connectedNode = ConnectingNode(edge);
104  assert(connectedNode != nullptr && "The edges don't have a connecting point.");
105  assert(!ContainsEndpoints(edge->First(), edge->Second()) && "The edges are the same.");
106 
107  if (m_endpoint1 != connectedNode) {
108  firstNode = m_endpoint1;
109  }
110  else {
111  firstNode = m_endpoint2;
112  }
113  if (edge->First() != connectedNode) {
114  secondNode = edge->First();
115  }
116  else {
117  secondNode = edge->Second();
118  }
119 
120  EditableEdgeItem* newEdge = new EditableEdgeItem(firstNode, secondNode);
121  emit EdgeMerged(this, edge, newEdge);
122 
123  return newEdge;
124 }
125 
126 std::pair<EditableEdgeItem*, EditableEdgeItem*> EditableEdgeItem::SplitEdge(EditableNodeItem* node)
127 {
128  EditableEdgeItem* edge1 = new EditableEdgeItem(m_endpoint1, node);
129  EditableEdgeItem* edge2 = new EditableEdgeItem(node, m_endpoint2);
130  emit EdgeSplitted(this, edge1, edge2);
131  return std::make_pair(edge1, edge2);
132 }
133 
135 {
136  setLine(QLineF(m_endpoint1->pos(), m_endpoint2->pos()));
137 }
138 
139 void EditableEdgeItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
140 {
141  QPen pen(this->pen());
142  if (flags().testFlag(QGraphicsItem::ItemIsSelectable)) {
143  pen.setBrush(QBrush(QColor(190,190,190,255)));//QColor(127,127,127,255)
144  pen.setWidthF(0.7);//0.5
145  }
146  else {
147  pen.setBrush(QBrush(QColor(0,0,0,255)));
148  pen.setWidthF(0.1);
149  }
150  setPen(pen);
151  Highlight(isSelected());
152 
153  QStyleOptionGraphicsItem noRectBorder(*option);
154  noRectBorder.state &= !QStyle::State_Selected;
155  QGraphicsLineItem::paint(painter, &noRectBorder, widget);
156 }
157 
158 } // namespace
virtual void Highlight(bool highlighted)
Highlights the edge in the canvas.
EditableNodeItem * Second() const
Returns the second endpoint of the edge item.
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
SimPT_Sim::Node * Node() const
Get the node associated with this item.
virtual bool IsAtBoundary() const
Checks whether the node is at the boundary of the complex.
bool ContainsEndpoint(EditableNodeItem *endpoint) const
Checks whether the given endpoint is an endpoint of this edge.
see the online Qt documentation
virtual void SetHighlightColor(const QColor &color=DEFAULT_HIGHLIGHT_COLOR)
Set the color the item should get when it gets highlighted.
SimPT_Sim::Edge Edge() const
Returns the edge associated with this item.
virtual void Update()
Update the edge.
An Edge connects two nodes and is ambidextrous.
Definition: Edge.h:31
EditableEdgeItem * MergeEdge(EditableEdgeItem *edge)
Merge the given edge with this edge into one.
void EdgeMerged(EditableEdgeItem *oldEdge1, EditableEdgeItem *oldEdge2, EditableEdgeItem *edge)
Emitted when the two given edges should be replaced by the new edge.
Editable graphical representation for Edge.
EditableEdgeItem(EditableNodeItem *node1, EditableNodeItem *node2)
Constructor.
Editable graphical representation for Node.
EditableNodeItem * First() const
Returns the first endpoint of the edge item.
Interface for EditableNodeItem.
EditableNodeItem * ConnectingNode(EditableEdgeItem *edge) const
Returns the connecting node when this edge and the given edge connect.
bool ContainsEndpoints(EditableNodeItem *endpoint1, EditableNodeItem *endpoint2) const
Checks whether the given endpoints match the endpoints of this edge.
std::pair< EditableEdgeItem *, EditableEdgeItem * > SplitEdge(EditableNodeItem *node)
Split the edge in two edges on the given node.
virtual bool IsAtBoundary() const
Checks whether the endpoints of the edge are at the boundary of the cell complex. ...
see the online Qt documentation
virtual ~EditableEdgeItem()
Destructor.
void EdgeSplitted(EditableEdgeItem *oldEdge, EditableEdgeItem *edge1, EditableEdgeItem *edge2)
Emitted when a given edge should be replaced with two other edges.
Interface for EditableEdgeItem.