COPASI API  4.16.103
CQConnectionGraphicsItem.cpp
Go to the documentation of this file.
1 // Copyright (C) 2013 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 #include <QtGui/QPainter>
7 
11 #include <qlayout/CQLayoutScene.h>
12 #include <layout/CLGlyphs.h>
13 #include <layout/CLReactionGlyph.h>
15 
16 QSharedPointer<QPainterPath> CQConnectionGraphicsItem::getPath(const CLCurve& curve)
17 {
18  QSharedPointer<QPainterPath> result = QSharedPointer<QPainterPath>(new QPainterPath());
19 
20  for (size_t i = 0; i < curve.getNumCurveSegments(); ++i)
21  {
22  const CLLineSegment* segment = curve.getSegmentAt(i);
23  QPainterPath path = QPainterPath(
24  QPointF(
25  segment->getStart().getX(),
26  segment->getStart().getY()));
27 
28  if (segment->isBezier())
29  {
30  path.cubicTo(
31  segment->getBase1().getX(), segment->getBase1().getY(),
32  segment->getBase2().getX(), segment->getBase2().getY(),
33  segment->getEnd().getX(), segment->getEnd().getY()
34  );
35  }
36  else
37  {
38  path.lineTo(segment->getEnd().getX(), segment->getEnd().getY());
39  }
40 
41  result->addPath(path);
42  }
43 
44  return result;
45 }
46 
47 void CQConnectionGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
48 {
49  painter->save();
50  QGraphicsItemGroup::paint(painter, option, widget);
51  painter->restore();
52 }
53 
54 void CQConnectionGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
55 {
56  CQLayoutScene * currentScene = dynamic_cast<CQLayoutScene *>(scene());
57 
58  if (mWasMoved && currentScene)
59  {
60  QPointF currentPos = pos();
61  currentScene->updatePosition(data(COPASI_LAYOUT_KEY).toString(), currentPos);
62  mWasMoved = false;
63  }
64  else
65  {
66  QGraphicsItem::mouseReleaseEvent(event);
67  }
68 }
69 
70 QVariant CQConnectionGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value)
71 {
72  CQLayoutScene * currentScene = dynamic_cast<CQLayoutScene *>(scene());
73 
74  if (change == ItemPositionHasChanged && currentScene)
75  {
76  mWasMoved = true;
77  }
78 
79  return QGraphicsItem::itemChange(change, value);
80 }
81 
83  : CQCopasiGraphicsItem(resolver, resolver != NULL ? resolver->resolveStyle(curveGlyph) : NULL)
84  , mWasMoved(false)
85 {
86  setFlag(QGraphicsItem::ItemIsMovable);
87  setFlag(QGraphicsItem::ItemSendsGeometryChanges);
88  setData(COPASI_LAYOUT_KEY, QString(curveGlyph->getKey().c_str()));
89 
90  QSharedPointer<QPainterPath> path = getPath(curveGlyph->getCurve());
91  QGraphicsPathItem* item;
92  QGraphicsItemGroup* itemGroup;
93 
94  if (curveGlyph->getCurve().getNumCurveSegments() > 0)
95  {
96  item = new QGraphicsPathItem(*path);
97  itemGroup = new QGraphicsItemGroup();
98  itemGroup->addToGroup(item);
99 
100  if (mpStyle != NULL)
101  CQRenderConverter::applyStyle(item, &curveGlyph->getBoundingBox(), mpStyle->getGroup(), resolver, itemGroup);
102 
103  addToGroup(itemGroup);
104  }
105 
106  const CLReactionGlyph* reaction = dynamic_cast<const CLReactionGlyph*>(curveGlyph);
107 
108  if (reaction != NULL)
109  {
111 
112  for (CCopasiVector<CLMetabReferenceGlyph>::const_iterator it = list.begin(); it != list.end(); ++it)
113  {
114  const CLMetabReferenceGlyph* metab = *it;
115  const CLStyle *style = resolver->resolveStyle(metab);
116 
117  if (metab->getCurve().getNumCurveSegments() > 0)
118  {
119  path = getPath(metab->getCurve());
120  item = new QGraphicsPathItem(*path);
121  itemGroup = new QGraphicsItemGroup();
122  itemGroup->addToGroup(item);
123  CQRenderConverter::applyStyle(item, &metab->getBoundingBox(), style == NULL ?
124  mpStyle == NULL ? NULL : mpStyle->getGroup()
125  : style->getGroup(), resolver, itemGroup);
126  addToGroup(itemGroup);
127  }
128  }
129  }
130 
131  const CLGeneralGlyph* general = dynamic_cast<const CLGeneralGlyph*>(curveGlyph);
132 
133  if (general != NULL)
134  {
136 
137  for (CCopasiVector<CLReferenceGlyph>::const_iterator it = list.begin(); it != list.end(); ++it)
138  {
139  const CLReferenceGlyph* glyph = *it;
140  const CLStyle *style = resolver->resolveStyle(glyph);
141 
142  if (glyph ->getCurve().getNumCurveSegments() > 0)
143  {
144  path = getPath(glyph ->getCurve());
145  item = new QGraphicsPathItem(*path);
146  itemGroup = new QGraphicsItemGroup();
147  itemGroup->addToGroup(item);
148  CQRenderConverter::applyStyle(item, &glyph ->getBoundingBox(), style == NULL ?
149  mpStyle == NULL ? NULL : mpStyle->getGroup()
150  : style->getGroup(), resolver, itemGroup);
151  addToGroup(itemGroup);
152  }
153  }
154  }
155 
156  // add decorations on reactions
157  itemGroup = new QGraphicsItemGroup();
158  CQRenderConverter::fillGroupFromStyle(itemGroup, &curveGlyph->getBoundingBox(), mpStyle, resolver);
159 
160  if (itemGroup->childItems().size() > 0)
161  addToGroup(itemGroup);
162  else
163  delete itemGroup;
164 }
165 
167 {
168 }
#define COPASI_LAYOUT_KEY
CQConnectionGraphicsItem(const CLGlyphWithCurve *glyph, const CLRenderResolver *resolver=NULL)
const CLBoundingBox & getBoundingBox() const
static QSharedPointer< QPainterPath > getPath(const CLCurve &curve)
const CLStyle * resolveStyle(const CLGraphicalObject *pObject) const
iterator begin()
const CLPoint & getBase1() const
Definition: CLCurve.h:82
static void fillGroupFromStyle(QGraphicsItemGroup *group, const CLBoundingBox *bounds, const CLStyle *style, const CLRenderResolver *resolver)
virtual const std::string & getKey() const
const CLPoint & getBase2() const
Definition: CLCurve.h:83
iterator end()
const CLPoint & getEnd() const
Definition: CLCurve.h:75
const C_FLOAT64 & getX() const
Definition: CLBase.h:83
const CLGroup * getGroup() const
Definition: CLStyle.cpp:91
void updatePosition(const QString &key, const QPointF &newPos)
static void applyStyle(QGraphicsTextItem *item, const CLBoundingBox *bounds, const CLGroup *style, const CLRenderResolver *resolver)
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
const CCopasiVector< CLReferenceGlyph > & getListOfReferenceGlyphs() const
const C_FLOAT64 & getY() const
Definition: CLBase.h:84
bool isBezier() const
Definition: CLCurve.h:90
const CLLineSegment * getSegmentAt(size_t i) const
Definition: CLCurve.h:156
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option=new QStyleOptionGraphicsItem(), QWidget *widget=0)
const CCopasiVector< CLMetabReferenceGlyph > & getListOfMetabReferenceGlyphs() const
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
const CLCurve & getCurve() const
size_t getNumCurveSegments() const
Definition: CLCurve.h:168
const CLPoint & getStart() const
Definition: CLCurve.h:74