VPTissue Reference Manual
EditableCellItem.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 "EditableCellItem.h"
21 
24 
25 #include <QStyleOptionGraphicsItem>
26 
27 class QWidget;
28 namespace SimPT_Sim { class Cell; }
29 
30 namespace SimPT_Editor {
31 
32 const QColor EditableCellItem::DEFAULT_HIGHLIGHT_COLOR(230,0,0,255);//(51, 102, 0, 255)
33 
34 EditableCellItem::EditableCellItem(const std::list<EditableNodeItem*>& nodes, const std::list<EditableEdgeItem*>& edges, SimPT_Sim::Cell* cell)
35  : QGraphicsPolygonItem(), m_cell(cell), m_edges(edges), m_nodes(nodes), m_color(55, 255, 255, 255), m_highlight_color(DEFAULT_HIGHLIGHT_COLOR)
36 {
37  Update();
38  setZValue(0); //Draw cells beneath all other items.
39 
40  //Update the cell when one of its corners has changed.
41  for (EditableNodeItem* node : m_nodes) {
42  connect(node, SIGNAL(Moved()), this, SLOT(Update()));
43  }
44 
45  for (EditableEdgeItem* edge : m_edges) {
46  connect(edge, SIGNAL(EdgeSplitted(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
48  connect(edge, SIGNAL(EdgeMerged(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
50  }
51 }
52 
54 
56 {
57  return m_cell->HasBoundaryWall();
58 }
59 
61 {
62  return m_cell;
63 }
64 
65 const std::list<EditableNodeItem*>& EditableCellItem::Nodes() const
66 {
67  return m_nodes;
68 }
69 
70 const std::list<EditableEdgeItem*>& EditableCellItem::Edges() const
71 {
72  return m_edges;
73 }
74 
76 {
77 
78  return std::find(m_nodes.begin(), m_nodes.end(), node) != m_nodes.end();
79 }
80 
82 {
83  return std::find(m_edges.begin(), m_edges.end(), edge) != m_edges.end();
84 }
85 
86 void EditableCellItem::Highlight(bool highlighted)
87 {
88  QPen pen(this->pen());
89  if (highlighted) {
90  m_highlight_color.setAlpha(brush().color().alpha());
91  setBrush(m_highlight_color);
92  }
93  setPen(pen);
94 }
95 
96 void EditableCellItem::SetColor(const QColor& color)
97 {
98  int alpha = brush().color().alpha();
99  m_color = color;
100  m_color.setAlpha(alpha);
101 }
102 
103 void EditableCellItem::SetHighlightColor(const QColor& color)
104 {
105  int alpha = brush().color().alpha();
106  m_highlight_color = color;
107  m_highlight_color.setAlpha(alpha);
108 }
109 
110 void EditableCellItem::SetTransparent(bool transparent)
111 {
112  int alpha = (transparent ? 64 : 255);
113 
114  m_color.setAlpha(alpha);
115  QColor color = brush().color();
116 
117  setBrush(QBrush(QColor(color.red(), color.green(), color.blue(), alpha)));
118 }
119 
121 {
122  EditableNodeItem* connectedNode = edge1->ConnectingNode(edge2);
123  EditableNodeItem* nodeEdge1 = (edge1->First() != connectedNode) ? edge1->First() : edge1->Second();
124  assert(connectedNode != nullptr && "The two new edges aren't connected.");
125 
126  auto foundEdge = std::find(m_edges.begin(), m_edges.end(), oldEdge);
127  assert(foundEdge != m_edges.end() && "This cell doesn't contain the old edge. [inconsistency]");
128  auto foundNode = std::find(m_nodes.begin(), m_nodes.end(), nodeEdge1);
129  assert(foundNode != m_nodes.end() && "This cell doesn't contain the endpoints of the old edge. [inconsistency]");
130 
131  //Get next edge.
132  foundEdge++;
133  if (foundEdge == m_edges.end()) {
134  foundEdge = m_edges.begin();
135  }
136 
137  if ((*foundEdge)->First() == nodeEdge1 || (*foundEdge)->Second() == nodeEdge1) {
138  //The next edge should be connected to edge1 (while the previous one should be connected to edge2).
139  m_edges.insert(foundEdge, edge2);
140  m_edges.insert(foundEdge, edge1);
141  m_nodes.insert(foundNode, connectedNode);
142  }
143  else {
144  //The next edge should be connected to edge2 (while the previous one should be connected to edge1).
145  m_edges.insert(foundEdge, edge1);
146  m_edges.insert(foundEdge, edge2);
147  m_nodes.insert(++foundNode, connectedNode);
148  }
149  m_edges.remove(oldEdge);
150 
151  connect(connectedNode, SIGNAL(Moved()), this, SLOT(Update()));
152  connect(edge1, SIGNAL(EdgeSplitted(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
154  connect(edge1, SIGNAL(EdgeMerged(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
156  connect(edge2, SIGNAL(EdgeSplitted(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
158  connect(edge2, SIGNAL(EdgeMerged(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
160 
161  Update();
162 }
163 
165 {
166  EditableNodeItem* connectedNode = oldEdge1->ConnectingNode(oldEdge2);
167 
168  assert(connectedNode != nullptr && "The two new edges aren't connected.");
169  assert(std::find(m_nodes.begin(), m_nodes.end(), connectedNode) != m_nodes.end() && "This cell doesn't contain the connected node. [inconsistency]");
170  assert(std::find(m_edges.begin(), m_edges.end(), oldEdge1) != m_edges.end() && "This cell doesn't contain the first old edge. [inconsistency]");
171  assert(std::find(m_edges.begin(), m_edges.end(), oldEdge2) != m_edges.end() && "This cell doesn't contain the second old edge. [inconsistency]");
172 
173  m_nodes.remove(connectedNode);
174 
175  m_edges.insert(std::find(m_edges.begin(), m_edges.end(), oldEdge1), edge);
176  m_edges.remove(oldEdge1);
177  m_edges.remove(oldEdge2);
178 
179  connect(edge, SIGNAL(EdgeSplitted(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
181  connect(edge, SIGNAL(EdgeMerged(EditableEdgeItem*, EditableEdgeItem*, EditableEdgeItem*)),
183 
184  Update();
185 }
186 
188 {
189  QPolygonF endpoints;
190  for (EditableNodeItem* node : m_nodes) {
191  endpoints.append(node->pos());
192  }
193  setPolygon(endpoints);
194 }
195 
196 void EditableCellItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
197 {
198  if (flags().testFlag(QGraphicsItem::ItemIsSelectable)) {
199  setBrush(QBrush(QColor(220, 220, 220, brush().color().alpha())));
200  }
201  else {
202  setBrush(QBrush(m_color));
203  }
204  Highlight(isSelected());
205 
206  QStyleOptionGraphicsItem noRectBorder(*option);
207  noRectBorder.state &= !QStyle::State_Selected;
208  QGraphicsPolygonItem::paint(painter, &noRectBorder, widget);
209 }
210 
211 } // namespace
EditableNodeItem * Second() const
Returns the second endpoint of the edge item.
A cell contains walls and nodes.
Definition: Cell.h:48
Namespace for SimPT tissue editor package.
Definition: Cell.h:32
virtual void SetHighlightColor(const QColor &color=DEFAULT_HIGHLIGHT_COLOR)
Set the color the item should get when it gets highlighted.
void SetColor(const QColor &color)
Set the color of the cell.
void SetTransparent(bool transparent)
Set the transparency of a cell.
const std::list< EditableNodeItem * > & Nodes() const
Get the nodes in this cell.
const std::list< EditableEdgeItem * > & Edges() const
Get the edges in this cell.
see the online Qt documentation
Namespace for the core simulator.
void ReplaceTwoEdgesWithEdge(EditableEdgeItem *oldEdge1, EditableEdgeItem *oldEdge2, EditableEdgeItem *edge)
Replaces two given edges with one other edge.
Editable graphical representation for Edge.
virtual bool IsAtBoundary() const
Checks whether the cell is at the boundary of the cell complex.
Editable graphical representation for Node.
EditableCellItem(const std::list< EditableNodeItem * > &nodes, const std::list< EditableEdgeItem * > &edges, SimPT_Sim::Cell *cell)
Constructor.
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 ContainsEdge(EditableEdgeItem *edge) const
Checks whether this cell contains the given edge.
SimPT_Sim::Cell * Cell() const
Get the logical cell.
void Update()
Update the cell.
see the online Qt documentation
virtual void Highlight(bool highlighted)
Highlights the node in the canvas.
bool ContainsNode(EditableNodeItem *node) const
Checks whether this cell contains the given node.
virtual ~EditableCellItem()
Destructor.
void ReplaceEdgeWithTwoEdges(EditableEdgeItem *oldEdge, EditableEdgeItem *edge1, EditableEdgeItem *edge2)
Replaces a given edge with two other edges.
Interface for EditableEdgeItem.
Interface for EditableCellItem.