COPASI API  4.16.103
CLGraphicalObject.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 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 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #define USE_LAYOUT 1
16 #define USE_RENDER 1
17 
18 #include <sbml/packages/render/extension/RenderGraphicalObjectPlugin.h>
19 #include <sbml/packages/layout/sbml/GraphicalObject.h>
20 
21 #include "CLGraphicalObject.h"
22 
23 #include "report/CKeyFactory.h"
24 #include "sbml/CSBMLExporter.h"
26 
27 CLGraphicalObject::CLGraphicalObject(const std::string & name,
28  const CCopasiContainer * pParent)
29  : CLBase(),
30  CCopasiContainer(name, pParent, "LayoutElement"),
31  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this)),
32  mModelObjectKey(""),
33  mObjectRole(""),
34  mBBox()
35 {};
36 
38  const CCopasiContainer * pParent)
39  : CLBase(src),
40  CCopasiContainer(src, pParent),
41  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this)),
42  mModelObjectKey(src.mModelObjectKey),
43  mObjectRole(src.mObjectRole),
44  mBBox(src.mBBox)
45 {};
46 
47 CLGraphicalObject::CLGraphicalObject(const GraphicalObject & sbml,
48  std::map<std::string, std::string> & layoutmap,
49  const CCopasiContainer * pParent)
50  : CLBase(sbml),
51  CCopasiContainer(sbml.getId(), pParent, "LayoutElement"),
52  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this)),
53  mModelObjectKey(""),
54  mBBox(*sbml.getBoundingBox())
55 {
56  RenderGraphicalObjectPlugin* rgoPlugin = (RenderGraphicalObjectPlugin*) sbml.getPlugin("render");
57 
58  if (rgoPlugin != NULL)
59  mObjectRole = rgoPlugin->getObjectRole();
60 
61  //add the copasi key to the map
62  layoutmap[sbml.getId()] = mKey;
63 };
64 
66 {
68 }
69 
71 {
72  if (this == &rhs) return * this; //do nothing if lhs and rhs are the same
73 
74  CLBase::operator=(rhs);
75 
76  //warning: in this place we should call the assignment operator of
77  //CCopasiContainer (which does not exist).
78  //We handle this explicitly instead.
80  //object flag cannot be accessed, it is private.
81 
83  this->mObjectRole = rhs.mObjectRole;
84  mBBox = rhs.mBBox;
85 
86  return *this;
87 }
88 
90 {
91  CCopasiObject* pObject = NULL;
92 
93  // as an additional safeguard, we check
94  // if the object is in the same datamodel
95  // This is not foolproof, but should work in most cases.
96  if (this->hasValidModelReference())
97  {
99  }
100 
101  return pObject;
102 }
103 
105 {
106  mBBox.moveBy(p);
107 }
108 
110 {
111  CCopasiObject * tmp = getModelObject();
112 
113  if (tmp)
114  return tmp->getObjectName();
115  else
116  return "";
117 }
118 
119 std::string CLGraphicalObject::getModelObjectDisplayName(bool /* regular */, bool /* richtext */) const
120 {
121  CCopasiObject * tmp = getModelObject();
122 
123  if (tmp)
124  {
125  return tmp->getObjectName();
126  }
127  else
128  {
129  return "";
130  }
131 }
132 
133 void CLGraphicalObject::exportToSBML(GraphicalObject * sbmlobject,
134  const std::map<const CCopasiObject*, SBase*> & /* copasimodelmap */,
135  std::map<std::string, const SBase*>& sbmlIDs) const
136 {
137  if (!sbmlobject) return;
138 
139  //Name and ID
140  std::string id = CSBMLExporter::createUniqueId(sbmlIDs, "layout_glyph", true);
141  sbmlobject->setId(id);
142  sbmlIDs.insert(std::pair<const std::string, const SBase*>(id, sbmlobject));
143 
144  //Bounding box
145  BoundingBox tmpbox = mBBox.getSBMLBoundingBox();
146  sbmlobject->setBoundingBox(&tmpbox);
147 
148  if (this->mObjectRole.find_first_not_of(" \t\r\n") != std::string::npos)
149  {
150  RenderGraphicalObjectPlugin* rgoPlugin = (RenderGraphicalObjectPlugin*) sbmlobject->getPlugin("render");
151 
152  if (rgoPlugin != NULL)
153  rgoPlugin->setObjectRole(this->mObjectRole);
154  }
155 }
156 
157 std::ostream & operator<<(std::ostream &os, const CLGraphicalObject & g)
158 {
159  os << "GraphicalObject \"" << g.getObjectName() << "\" " << g.mBBox << std::endl;
160  std::string tmp = g.getModelObjectDisplayName();
161 
162  if (tmp != "")
163  os << " refers to " << tmp << std::endl;
164 
165  return os;
166 }
167 
168 void CLGraphicalObject::print(std::ostream * ostream) const
169 {*ostream << *this;}
170 
171 #/**
172  * Method to set the role of a graphical object.
173  */
174 void CLGraphicalObject::setObjectRole(const std::string& role)
175 {
176  this->mObjectRole = role;
177 }
178 
179 /**
180  * Method to read the object role of an object.
181  */
182 const std::string& CLGraphicalObject::getObjectRole() const
183 {
184  return this->mObjectRole;
185 }
186 
187 /**
188  * This method is used for the export of several layout elements.
189  * Layout elements can reference model elements, but when a referenced model element
190  * is deleted, the layout is not infomred about this yet, so we end up with dnagling
191  * references.
192  * In order to not write these dangling references to file, we check if the reference
193  * belongs to a valid object in the same datamodel. If not, we issue a warning.
194  * This warning is only issued once during a save process.
195  *
196  * If the key belongs to an object in the same datamodel, true is returned, else false is returned.
197  */
199 {
200  bool result = false;
201  // check if the object for this key actually exists
202  // TODO This is only a workaround because it is theoretically
203  // TODO possible that the key no longer belongs to the same object it
204  // TODO originally did.
206 
207  if (pObj != NULL)
208  {
209  // check if the object actually belongs to the same
210  // model as the text glyph
211  const CCopasiDataModel* pDM1 = NULL;
212  const CCopasiDataModel* pDM2 = NULL;
213  const CCopasiContainer* pParent = pObj->getObjectParent();
214 
215  while (pParent != NULL)
216  {
217  pDM1 = dynamic_cast<const CCopasiDataModel*>(pParent);
218 
219  if (pDM1 != NULL)
220  {
221  break;
222  }
223 
224  pParent = pParent->getObjectParent();
225  }
226 
227  pParent = this->getObjectParent();
228 
229  while (pParent != NULL)
230  {
231  pDM2 = dynamic_cast<const CCopasiDataModel*>(pParent);
232 
233  if (pDM2 != NULL)
234  {
235  break;
236  }
237 
238  pParent = pParent->getObjectParent();
239  }
240 
241  //assert(pDM2 != NULL);
242  if (pDM1 != NULL && pDM2 == NULL)
243  {
244  // if we have been able to resolve the model reference, but
245  // don't have a datamodel parent, that is good enough
246  return true;
247  }
248 
249  if (pDM1 != NULL && pDM1 == pDM2)
250  {
251  result = true;
252  }
253  }
254 
255  return result;
256 }
bool remove(const std::string &key)
std::string mModelObjectKey
std::string getModelObjectDisplayName(bool regular=true, bool richtext=false) const
virtual void moveBy(const CLPoint &p)
Definition: CLBase.h:275
const std::string & getObjectName() const
CCopasiObject * get(const std::string &key)
const std::string & getObjectRole() const
std::ostream & operator<<(std::ostream &os, const CLGraphicalObject &g)
static const std::string createUniqueId(const std::map< std::string, const SBase * > &idMap, const std::string &prefix, bool addIndexForFirst, const std::string &separator="_")
std::string mObjectRole
Definition: CLBase.h:54
CLBoundingBox mBBox
virtual void exportToSBML(GraphicalObject *sbmlobject, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs) const
Definition: CLBase.h:33
std::string getModelObjectName() const
CLGraphicalObject(const std::string &name="GraphicalObject", const CCopasiContainer *pParent=NULL)
CLGraphicalObject & operator=(const CLGraphicalObject &rhs)
CCopasiObject * getModelObject() const
static CKeyFactory * getKeyFactory()
void setObjectRole(const std::string &role)
virtual void moveBy(const CLPoint &p)
void print(std::ostream *ostream) const
BoundingBox getSBMLBoundingBox() const
Definition: CLBase.cpp:60
bool hasValidModelReference() const
bool setObjectName(const std::string &name)
CCopasiContainer * getObjectParent() const