COPASI API  4.16.103
CQLayoutScene.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 <QtCore/QCoreApplication>
7 #include <QtGui/QGraphicsScene>
8 #include <QtGui/QGraphicsItem>
9 #include <QtGui/QPainter>
10 #include <QtGui/QPrinter>
11 #include <QtGui/QImage>
12 #include <QtGui/QGraphicsEffect>
13 
14 #include "copasi.h"
15 
16 #include "qlayout/CQLayoutScene.h"
17 #include "qlayout/CQCopasiEffect.h"
22 #include "layout/CLayout.h"
23 #include "layout/CLGlyphs.h"
24 #include "layout/CLText.h"
25 #include "layout/CLReactionGlyph.h"
28 #include "layout/CListOfLayouts.h"
30 #include "layout/CLDefaultStyles.h"
32 
36 
38  : QGraphicsScene()
39  , mpLayout(layout)
40  , mpRender(renderInformation)
41  , mpResolver(NULL)
42 {
43  initializeResolver(model, renderInformation);
44  connect(this, SIGNAL(recreateNeeded()), this, SLOT(recreate()), Qt::QueuedConnection);
45 }
46 
48 {
49  mpLayout = layout;
50  setRenderInformation(model, renderInformation);
51 }
52 
54 {
55  initializeResolver(model, renderInformation);
56 }
57 
59 {
60  return mpLayout;
61 }
62 
64 {
65  return mpLayout;
66 }
67 
69 {
70  return mpRender;
71 }
72 
73 void CQLayoutScene::saveToFile(const std::string& fileName, const std::string& fileType /*= "pdf"*/)
74 {
75  if (fileType == "pdf")
76  {
77  QPrinter printer(QPrinter::HighResolution);
78  printer.setOutputFormat(QPrinter::PdfFormat);
79  printer.setOutputFileName(fileName.c_str());
80  QPainter painter(&printer);
81  painter.setRenderHints(
82  QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
83  render(&painter, QRect(), itemsBoundingRect());
84  painter.end();
85  }
86  else
87  {
88  const int scale = 2;
89  QImage image(QSize(width()*scale, height()*scale), QImage::Format_ARGB32);
90  QPainter painter(&image);
91  painter.setRenderHints(
92  QPainter::Antialiasing | QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform);
93  render(&painter, image.rect(), itemsBoundingRect());
94  painter.end();
95  image.save(fileName.c_str(), fileType.c_str());
96  }
97 }
98 
100 {
101  if (model == NULL)
102  return;
103 
104  if (renderInformation == NULL)
105  {
110  else
112  }
113  else
114  mpRender = renderInformation;
115 
116  if (mpLayout == NULL || mpRender == NULL)
117  return;
118 
120 
121  if (local != NULL)
123  else
124  mpResolver = QSharedPointer<CLRenderResolver>(new CLRenderResolver(*dynamic_cast<CLGlobalRenderInformation*>(mpRender), model->getListOfLayouts()->getListOfGlobalRenderInformationObjects()));
125 }
126 
128 {
129  mpResolver = QSharedPointer<CLRenderResolver>(resolver);
130 }
131 
133 {
134  return mpResolver.data();
135 }
136 
138 {
139 }
140 
142 {
144  invalidate();
145 }
146 
148 {
149  if (go == NULL) return;
150 
151  const CLGlyphWithCurve* curveGlyph = dynamic_cast<const CLGlyphWithCurve*>(go);
152  const CLReactionGlyph* reaction = dynamic_cast<const CLReactionGlyph*>(go);
153  const CLTextGlyph* text = dynamic_cast<const CLTextGlyph*>(go);
154  const CLGeneralGlyph* general = dynamic_cast<const CLGeneralGlyph*>(go);
155  QGraphicsItem *item = NULL;
156 
157  if (curveGlyph != NULL)
158  {
159  if (curveGlyph->getCurve().getNumCurveSegments() > 0 || reaction != NULL || general != NULL)
160  item = new CQConnectionGraphicsItem(curveGlyph,
161  mpResolver == NULL ? NULL : mpResolver.data());
162  }
163  else if (text != NULL)
164  {
165  item = new CQLabelGraphicsItem(text, mpResolver == NULL ? NULL : mpResolver.data());
166  }
167  else
168  {
169  item = new CQStyledGraphicsItem(go, mpResolver == NULL ? NULL : mpResolver.data());
170  }
171 
172  if (item != NULL)
173  {
174  CCopasiObject* obj = go->getModelObject();
175 
176  if (obj != NULL && text == NULL)
177  {
178  item->setData(COPASI_OBJECT_CN, QString(obj->getCN().c_str()));
179  mItems[obj->getCN()] = item;
180  }
181 
182  addItem(item);
183  }
184 
185  if (general != NULL)
186  {
187  const CCopasiVector<CLGraphicalObject> & subGlyphs = general->getListOfSubglyphs();
189 
190  while (it != subGlyphs.end())
191  {
192  addGlyph(*it);
193  ++it;
194  }
195  }
196 }
197 
198 QGraphicsItem* CQLayoutScene::getItemFor(const std::string& cn)
199 {
200  return mItems[cn];
201 }
202 
204 {
205  if (layout == NULL) return;
206 
207  clear();
208  mItems.clear();
209 
210  if (mpRender != NULL && mpResolver != NULL)
211  {
213  }
214 
216 
218 
219  while (itComp != comps.end())
220  {
221  addGlyph(*itComp);
222  ++itComp;
223  }
224 
225  const CCopasiVector<CLReactionGlyph> & reactions = layout->getListOfReactionGlyphs();
226 
227  CCopasiVector<CLReactionGlyph>::const_iterator itReactions = reactions.begin();
228 
229  while (itReactions != reactions.end())
230  {
231  addGlyph(*itReactions);
232  ++itReactions;
233  }
234 
235  const CCopasiVector<CLMetabGlyph> & species = layout->getListOfMetaboliteGlyphs();
236 
238 
239  while (itSpecies != species.end())
240  {
241  addGlyph(*itSpecies);
242  ++itSpecies;
243  }
244 
245  const CCopasiVector<CLTextGlyph> & texts = layout->getListOfTextGlyphs();
246 
248 
249  while (itTexts != texts.end())
250  {
251  addGlyph(*itTexts);
252  ++itTexts;
253  }
254 
255  const CCopasiVector<CLGeneralGlyph> & list = layout->getListOfGeneralGlyphs();
256 
258 
259  while (itList != list.end())
260  {
261  addGlyph(*itList);
262  ++itList;
263  }
264 }
265 
267 {
268  const CCopasiVector<CLTextGlyph> & texts = layout->getListOfTextGlyphs();
270 
271  while (it != texts.end())
272  {
273  if ((*it)->getGraphicalObjectKey() == obj->getKey())
274  return *it;
275 
276  ++it;
277  }
278 
279  return NULL;
280 }
281 
282 #include <model/CCompartment.h>
283 #include <model/CMetab.h>
284 
286 {
287  const CCopasiVector<CLMetabGlyph> & metabs = layout->getListOfMetaboliteGlyphs();
289 
290  while (it != metabs.end())
291  {
292  if ((*it)->getModelObjectKey() == metab->getKey())
293  return *it;
294 
295  ++it;
296  }
297 
298  return NULL;
299 }
300 
301 void moveObject(CLGraphicalObject* obj, const CLPoint& delta, CLayout* layout)
302 {
303  if (obj == NULL) return;
304 
305  // move object
306  obj->moveBy(delta);
307 
308  // move its label
309  CLGraphicalObject* text = getTextForItem(layout, obj);
310 
311  if (text != NULL)
312  text->moveBy(delta);
313 
314  // move species within compartments as well
315  CLCompartmentGlyph* lcomp = dynamic_cast<CLCompartmentGlyph*>(obj);
316 
317  if (lcomp == NULL)
318  return;
319 
320  CCompartment* comp = dynamic_cast<CCompartment*>(lcomp ->getModelObject());
321 
322  if (comp == NULL)
323  return;
324 
325  CCopasiVectorNS < CMetab > & metabs = comp->getMetabolites();
327 
328  while (it != metabs.end())
329  {
330  moveObject(getMetabGlyphForKey(layout, (*it)), delta, layout);
331  ++it;
332  }
333 }
334 
335 void CQLayoutScene::updatePosition(const QString& key, const QPointF& newPos)
336 {
338 
339  if (kf == NULL) return;
340 
341  CLGraphicalObject* obj = dynamic_cast<CLGraphicalObject*>(kf->get(key.toStdString()));
342 
343  if (obj == NULL) return;
344 
345  CLPoint delta(newPos.x(), newPos.y());
346  moveObject(obj, delta, mpLayout);
347 
348  // restore lines
351  l.finalizeState();
352 
353  emit recreateNeeded();
354 }
std::map< std::string, QGraphicsItem * > mItems
Definition: CQLayoutScene.h:52
const CLayout * getCurrentLayout() const
QSharedPointer< CLRenderResolver > mpResolver
Definition: CQLayoutScene.h:50
virtual ~CQLayoutScene()
void saveToFile(const std::string &fileName, const std::string &fileType="pdf")
const CCopasiVector< CLTextGlyph > & getListOfTextGlyphs() const
Definition: CLayout.h:120
CLRenderInformationBase * mpRender
Definition: CQLayoutScene.h:49
CCopasiVectorNS< CMetab > & getMetabolites()
void setLayout(CLayout *layout, CCopasiDataModel *model=NULL, CLRenderInformationBase *renderInformation=NULL)
virtual CCopasiObjectName getCN() const
const CCopasiVector< CLLocalRenderInformation > & getListOfLocalRenderInformationObjects() const
Definition: CLayout.h:149
virtual size_t size() const
CCopasiObject * get(const std::string &key)
void fillFromLayout(const CLayout *layout)
const std::string & getBackgroundColor() const
iterator begin()
static void setBackground(QGraphicsScene *scene, const std::string &fill, const CLRenderResolver *resolver)
CLayout * mpLayout
Definition: CQLayoutScene.h:48
const CLRenderInformationBase * getCurrentRenderInfo() const
const CCopasiVector< CLGraphicalObject > & getListOfSubglyphs() const
Definition: CMetab.h:178
CQLayoutScene(CLayout *layout, CCopasiDataModel *model=NULL, CLRenderInformationBase *renderInformation=NULL)
void initializeResolver(CCopasiDataModel *model, CLRenderInformationBase *renderInformation)
const CCopasiVector< CLCompartmentGlyph > & getListOfCompartmentGlyphs() const
Definition: CLayout.h:81
virtual const std::string & getKey() const
CLGraphicalObject * getMetabGlyphForKey(const CLayout *layout, const CMetab *metab)
CLGlobalRenderInformation * getDefaultStyle(size_t index)
iterator end()
void setRenderInformation(CCopasiDataModel *model, CLRenderInformationBase *renderInformation)
Definition: CLBase.h:54
virtual const std::string & getKey() const
void updatePosition(const QString &key, const QPointF &newPos)
const CCopasiVector< CLMetabGlyph > & getListOfMetaboliteGlyphs() const
Definition: CLayout.h:94
void recreateNeeded()
CListOfLayouts * getListOfLayouts()
const CCopasiVector< CLGeneralGlyph > & getListOfGeneralGlyphs() const
Definition: CLayout.h:133
CLGraphicalObject * getTextForItem(const CLayout *layout, const CLGraphicalObject *obj)
CCopasiObject * getModelObject() const
static CKeyFactory * getKeyFactory()
virtual void moveBy(const CLPoint &p)
void addGlyph(const CLGraphicalObject *go)
#define COPASI_OBJECT_CN
void setResolver(CLRenderResolver *resolver)
const CLCurve & getCurve() const
size_t getNumCurveSegments() const
Definition: CLCurve.h:168
const CLRenderResolver * getResolver() const
void moveObject(CLGraphicalObject *obj, const CLPoint &delta, CLayout *layout)
const CCopasiVector< CLReactionGlyph > & getListOfReactionGlyphs() const
Definition: CLayout.h:107
QGraphicsItem * getItemFor(const std::string &cn)
const CCopasiVector< CLGlobalRenderInformation > & getListOfGlobalRenderInformationObjects() const