COPASI API  4.16.103
CLRenderResolver.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 #include "CLRenderResolver.h"
7 
8 #include <set>
9 #include <assert.h>
10 
12 #include "copasi/layout/CLStyle.h"
17 #include "copasi/layout/CLGlyphs.h"
19 
20 #include "CLRenderFlattener.h"
21 
22 /**
23  * Constructor that takes a local render information object as the argument
24  * as well as a list of local and a list of global render information objects
25  * that are needed to resolve external references.
26  */
30  : mpRenderInformation(CLRenderFlattener::flatten_render_information(renderInformation, localList, globalList)),
31  mLocal(true),
32  mpBackgroundColor(NULL)
33 {
34  // fill the maps
36  this->fill_local_maps(dynamic_cast<const CLLocalRenderInformation*>(mpRenderInformation));
37  this->setBackgroundColor();
38 }
39 
41 {
42  if (this->mpRenderInformation)
43  {
44  std::string id = this->mpRenderInformation->getBackgroundColor();
45  assert(!id.empty());
46 
47  // must be a color value
48  if (id[0] == '#')
49  {
50  assert(id.length() == 7 || id.length() == 9);
53  }
54  else // must be an id
55  {
56  const CLColorDefinition* pColor = this->getColorDefinition(id);
57 
58  if (pColor)
59  {
60  // make a copy
61  this->mpBackgroundColor = new CLColorDefinition(*pColor);
62  }
63  }
64  }
65 }
66 
67 /**
68  * Constructor that takes a global render information object as the argument
69  * and a list of additional global render information objects that might be
70  * needed to resolve external references.
71  * means it should contain all information from referenced render information objects.
72  */
75  : mpRenderInformation(CLRenderFlattener::flatten_render_information(renderInformation, globalList)),
76  mLocal(false),
77  mpBackgroundColor(NULL)
78 {
79  // fill the maps
81  this->fill_global_maps(dynamic_cast<const CLGlobalRenderInformation*>(this->mpRenderInformation));
82  this->setBackgroundColor();
83 }
84 
86 {
87  // we need to delete the render information object that has been created by
88  // the flattener
89  if (this->mpRenderInformation != NULL) delete this->mpRenderInformation;
90 
91  // delete the background color
92  if (this->mpBackgroundColor != NULL) delete this->mpBackgroundColor;
93 }
94 
95 /**
96  * This method fills the color, gradient and line ending maps for a render
97  * information object.
98  */
100 {
101  // fill maps for colors, gradients and line endings
102  size_t i, iMax = pRenderInformation->getNumColorDefinitions();
103  const CLColorDefinition* pColor = NULL;
104 
105  for (i = 0; i < iMax; ++i)
106  {
107  pColor = pRenderInformation->getColorDefinition(i);
108  this->mColorMap[pColor->getId()] = pColor;
109  }
110 
111  iMax = pRenderInformation->getNumGradientDefinitions();
112  const CLGradientBase* pGradient = NULL;
113 
114  for (i = 0; i < iMax; ++i)
115  {
116  pGradient = pRenderInformation->getGradientDefinition(i);
117  this->mGradientMap[pGradient->getId()] = pGradient;
118  }
119 
120  iMax = pRenderInformation->getNumLineEndings();
121  const CLLineEnding* pLineEnding = NULL;
122 
123  for (i = 0; i < iMax; ++i)
124  {
125  pLineEnding = pRenderInformation->getLineEnding(i);
126  this->mLineEndingMap[pLineEnding->getId()] = pLineEnding;
127  }
128 }
129 
130 /**
131  * This method fills the type and role maps for a global render information
132  * object.
133  */
135 {
136  size_t i, iMax = pRenderInformation->getNumStyles();
137  const CLStyle* pStyle = NULL;
138 
139  for (i = 0 ; i < iMax ; ++i)
140  {
141  pStyle = pRenderInformation->getStyle(i);
142  const std::set<std::string>& roleList = pStyle->getRoleList();
143  std::set<std::string>::const_iterator it = roleList.begin(), endit = roleList.end();
144 
145  while (it != endit)
146  {
147  this->mRoleMap[*it] = pStyle;
148  ++it;
149  }
150 
151  const std::set<std::string>& typeList = pStyle->getTypeList();
152 
153  it = typeList.begin();
154 
155  endit = typeList.end();
156 
157  while (it != endit)
158  {
159  this->mTypeMap[*it] = pStyle;
160  ++it;
161  }
162  }
163 }
164 
165 /**
166  * This method fills the type, role and id maps for a local render information
167  * object.
168  */
170 {
171  size_t i, iMax = pRenderInformation->getNumStyles();
172  const CLStyle* pStyle = NULL;
173 
174  for (i = 0; i < iMax; ++i)
175  {
176  pStyle = pRenderInformation->getStyle(i);
177  const std::set<std::string>& roleList = pStyle->getRoleList();
178  std::set<std::string>::const_iterator it = roleList.begin(), endit = roleList.end();
179 
180  while (it != endit)
181  {
182  this->mRoleMap[*it] = pStyle;
183  ++it;
184  }
185 
186  const std::set<std::string>& typeList = pStyle->getTypeList();
187 
188  it = typeList.begin();
189 
190  endit = typeList.end();
191 
192  while (it != endit)
193  {
194  this->mTypeMap[*it] = pStyle;
195  ++it;
196  }
197 
198  const CLLocalStyle* pLocalStyle = dynamic_cast<const CLLocalStyle*>(pStyle);
199 
200  const std::set<std::string>& keyList = pLocalStyle->getKeyList();
201 
202  it = keyList.begin();
203 
204  endit = keyList.end();
205 
206  while (it != endit)
207  {
208  this->mKeyMap[*it] = pStyle;
209  ++it;
210  }
211  }
212 }
213 
214 /**
215  * Method that tries to find the style for the given graphical
216  * object that fits best.
217  * If no style is found NULL is returned.
218  */
220 {
221  const CLStyle* pResult = NULL;
222  // try to resolve the id first
223  pResult = this->resolveStyleForKey(pObject->getKey());
224 
225  // next try the role
226  if (pResult == NULL)
227  {
228  std::string role = pObject->getObjectRole();
229  const CLReferenceGlyph* pRG = dynamic_cast<const CLReferenceGlyph*>(pObject);
230 
231  if (pRG != NULL && role.empty())
232  role = pRG->getRole();
233 
234  const CLMetabReferenceGlyph* pSRG = dynamic_cast<const CLMetabReferenceGlyph*>(pObject);
235 
236  if (pSRG != NULL && role.empty())
237  {
238  CLMetabReferenceGlyph::Role role_t = pSRG->getRole();
239 
240  switch (role_t)
241  {
243  role = "substrate";
244  break;
245 
247  role = "product";
248  break;
249 
251  role = "sidesubstrate";
252  break;
253 
255  role = "sideproduct";
256  break;
257 
259  role = "modifier";
260  break;
261 
263  role = "activator";
264  break;
265 
267  role = "inhibitor";
268  break;
269 
270  default:
271  role = "";
272  }
273 
274  // try if we can find a role in the deduced role map
275  if (role.empty())
276  {
277  std::map<const CLMetabReferenceGlyph*, std::string>::const_iterator pos = this->mDeducedObjectRoles.find(pSRG);
278 
279  if (pos != this->mDeducedObjectRoles.end())
280  {
281  role = pos->second;
282  }
283  }
284  }
285 
286  if (!role.empty())
287  {
288  pResult = this->resolveStyleForRole(role);
289  }
290 
291  // last try the type
292  if (pResult == NULL)
293  {
294  std::string type = "GRAPHICALOBJECT";
295 
296  if (dynamic_cast<const CLCompartmentGlyph*>(pObject))
297  {
298  type = "COMPARTMENTGLYPH";
299  }
300  else if (dynamic_cast<const CLMetabGlyph*>(pObject))
301  {
302  type = "SPECIESGLYPH";
303  }
304  else if (dynamic_cast<const CLReactionGlyph*>(pObject))
305  {
306  type = "REACTIONGLYPH";
307  }
308  else if (dynamic_cast<const CLMetabReferenceGlyph*>(pObject))
309  {
310  type = "SPECIESREFERENCEGLYPH";
311  }
312  else if (dynamic_cast<const CLTextGlyph*>(pObject))
313  {
314  type = "TEXTGLYPH";
315  }
316 
317  pResult = this->resolveStyleForType(type);
318  }
319  }
320 
321  return pResult;
322 }
323 
324 /**
325  * Method that tries to find the style for the given role.
326  * If no style is found NULL is returned.
327  */
328 const CLStyle* CLRenderResolver::resolveStyleForRole(const std::string& role) const
329 {
330  const CLStyle* pResult = NULL;
331  std::map<std::string, const CLStyle*>::const_iterator pos = this->mRoleMap.find(role);
332 
333  if (pos != this->mRoleMap.end())
334  {
335  pResult = pos->second;
336  }
337 
338  return pResult;
339 }
340 
341 /**
342  * Method that tries to find the style for the given type.
343  * If no style is found NULL is returned.
344  */
345 const CLStyle* CLRenderResolver::resolveStyleForType(const std::string& type) const
346 {
347  const CLStyle* pResult = NULL;
348  std::map<std::string, const CLStyle*>::const_iterator pos = this->mTypeMap.find(type);
349 
350  if (pos != this->mTypeMap.end())
351  {
352  pResult = pos->second;
353  }
354  // look for a style that is valid for ANY type
355  else
356  {
357  pos = this->mTypeMap.find("ANY");
358 
359  if (pos != this->mTypeMap.end())
360  {
361  pResult = pos->second;
362  }
363  }
364 
365  return pResult;
366 }
367 
368 /**
369  * Method that tries to find the style for the given key.
370  * If no style is found NULL is returned.
371  */
372 const CLStyle* CLRenderResolver::resolveStyleForKey(const std::string& key) const
373 {
374  const CLStyle* pResult = NULL;
375  std::map<std::string, const CLStyle*>::const_iterator pos = this->mKeyMap.find(key);
376 
377  if (pos != this->mKeyMap.end())
378  {
379  pResult = pos->second;
380  }
381 
382  return pResult;
383 }
384 
385 /**
386  * Returns the gradient definition for a given id.
387  */
388 const CLGradientBase* CLRenderResolver::getGradientBase(const std::string& id) const
389 {
390  const CLGradientBase* pResult = NULL;
391  std::map<std::string, const CLGradientBase*>::const_iterator pos = this->mGradientMap.find(id);
392 
393  if (pos != this->mGradientMap.end())
394  {
395  pResult = pos->second;
396  }
397 
398  return pResult;
399 }
400 
401 /**
402  * Returns the color definition for a given id.
403  */
404 const CLColorDefinition* CLRenderResolver::getColorDefinition(const std::string& id) const
405 {
406  const CLColorDefinition* pResult = NULL;
407  std::map<std::string, const CLColorDefinition*>::const_iterator pos = this->mColorMap.find(id);
408 
409  if (pos != this->mColorMap.end())
410  {
411  pResult = pos->second;
412  }
413 
414  return pResult;
415 }
416 
417 /**
418  * Returns the line ending for a given id.
419  */
420 const CLLineEnding* CLRenderResolver::getLineEnding(const std::string& id) const
421 {
422  const CLLineEnding* pResult = NULL;
423  std::map<std::string, const CLLineEnding*>::const_iterator pos = this->mLineEndingMap.find(id);
424 
425  if (pos != this->mLineEndingMap.end())
426  {
427  pResult = pos->second;
428  }
429 
430  return pResult;
431 }
432 
433 /**
434  * Returns the background color.
435  */
437 {
438  return this->mpBackgroundColor;
439 }
440 
441 /**
442  * Sets the deduced object roles.
443  */
444 void CLRenderResolver::setDeducedObjectRoles(const std::map<const CLMetabReferenceGlyph*, std::string>& deducedObjectRoles)
445 {
446  this->mDeducedObjectRoles = deducedObjectRoles;
447 }
std::map< std::string, const CLStyle * > mKeyMap
std::map< const CLMetabReferenceGlyph *, std::string > mDeducedObjectRoles
std::map< std::string, const CLGradientBase * > mGradientMap
const CLColorDefinition * getColorDefinition(const std::string &id) const
const CLStyle * resolveStyle(const CLGraphicalObject *pObject) const
void setDeducedObjectRoles(const std::map< const CLMetabReferenceGlyph *, std::string > &deducedObjectRoles)
const std::string & getBackgroundColor() const
const std::string & getObjectRole() const
bool setColorValue(const std::string &valueString)
const std::string & getId() const
void fill_global_maps(const CLGlobalRenderInformation *pRenderInformation)
const CLStyle * resolveStyleForType(const std::string &type) const
const std::set< std::string > & getKeyList() const
std::map< std::string, const CLStyle * > mRoleMap
CLLocalStyle * getStyle(size_t i)
const std::string & getRole() const
virtual const std::string & getKey() const
CLColorDefinition * getColorDefinition(size_t index)
void fill_base_maps(const CLRenderInformationBase *pRenderInformation)
const std::set< std::string > & getRoleList() const
Definition: CLStyle.cpp:201
const std::set< std::string > & getTypeList() const
Definition: CLStyle.cpp:206
CLLineEnding * getLineEnding(size_t index)
std::map< std::string, const CLColorDefinition * > mColorMap
CLRenderResolver(const CLRenderResolver &)
const std::string & getId() const
const CArrayAnnotation * pResult
const CLColorDefinition * getBackgroundColor() const
std::map< std::string, const CLStyle * > mTypeMap
CLColorDefinition * mpBackgroundColor
CLGradientBase * getGradientDefinition(size_t index)
const CLLineEnding * getLineEnding(const std::string &id) const
const std::string & getId() const
void fill_local_maps(const CLLocalRenderInformation *pRenderInformation)
const CLStyle * resolveStyleForRole(const std::string &role) const
const CLGradientBase * getGradientBase(const std::string &id) const
const CLStyle * resolveStyleForKey(const std::string &key) const
CLRenderInformationBase * mpRenderInformation
std::map< std::string, const CLLineEnding * > mLineEndingMap