1 /**
2  * @file ListOfDrawables.cpp
3  * @brief Implementation of the ListOfDrawables class.
4  * @author SBMLTeam
5  *
6  * <!--------------------------------------------------------------------------
7  * This file is part of libSBML. Please visit http://sbml.org for more
8  * information about SBML, and the latest version of libSBML.
9  *
10  * Copyright (C) 2020 jointly by the following organizations:
11  *     1. California Institute of Technology, Pasadena, CA, USA
12  *     2. University of Heidelberg, Heidelberg, Germany
13  *     3. University College London, London, UK
14  *
15  * Copyright (C) 2019 jointly by the following organizations:
16  *     1. California Institute of Technology, Pasadena, CA, USA
17  *     2. University of Heidelberg, Heidelberg, Germany
18  *
19  * Copyright (C) 2013-2018 jointly by the following organizations:
20  * 1. California Institute of Technology, Pasadena, CA, USA
21  * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
22  * 3. University of Heidelberg, Heidelberg, Germany
23  *
24  * Copyright (C) 2009-2013 jointly by the following organizations:
25  * 1. California Institute of Technology, Pasadena, CA, USA
26  * 2. EMBL European Bioinformatics Institute (EMBL-EBI), Hinxton, UK
27  *
28  * Copyright (C) 2006-2008 by the California Institute of Technology,
29  * Pasadena, CA, USA
30  *
31  * Copyright (C) 2002-2005 jointly by the following organizations:
32  * 1. California Institute of Technology, Pasadena, CA, USA
33  * 2. Japan Science and Technology Agency, Japan
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the GNU Lesser General Public License as published by the
37  * Free Software Foundation. A copy of the license agreement is provided in the
38  * file named "LICENSE.txt" included with this software distribution and also
39  * available online as http://sbml.org/software/libsbml/license.html
40  * ------------------------------------------------------------------------ -->
41  */
42 #include <sbml/packages/render/sbml/ListOfDrawables.h>
43 #include <sbml/packages/render/validator/RenderSBMLError.h>
44 
45 #include <sbml/packages/render/sbml/Image.h>
46 #include <sbml/packages/render/sbml/Ellipse.h>
47 #include <sbml/packages/render/sbml/Rectangle.h>
48 #include <sbml/packages/render/sbml/Polygon.h>
49 #include <sbml/packages/render/sbml/RenderGroup.h>
50 #include <sbml/packages/render/sbml/LineEnding.h>
51 #include <sbml/packages/render/sbml/ListOfLineEndings.h>
52 #include <sbml/packages/render/sbml/Text.h>
53 #include <sbml/packages/render/sbml/RenderCurve.h>
54 
55 
56 using namespace std;
57 
58 
59 
60 LIBSBML_CPP_NAMESPACE_BEGIN
61 
62 
63 
64 
65 #ifdef __cplusplus
66 
67 
68 /*
69  * Creates a new ListOfDrawables using the given SBML Level, Version and
70  * &ldquo;render&rdquo; package version.
71  */
ListOfDrawables(unsigned int level,unsigned int version,unsigned int pkgVersion)72 ListOfDrawables::ListOfDrawables(unsigned int level,
73                                  unsigned int version,
74                                  unsigned int pkgVersion)
75   : ListOf(level, version)
76 {
77   setSBMLNamespacesAndOwn(new RenderPkgNamespaces(level, version, pkgVersion));
78 }
79 
80 
81 /*
82  * Creates a new ListOfDrawables using the given RenderPkgNamespaces object.
83  */
ListOfDrawables(RenderPkgNamespaces * renderns)84 ListOfDrawables::ListOfDrawables(RenderPkgNamespaces *renderns)
85   : ListOf(renderns)
86 {
87   setElementNamespace(renderns->getURI());
88 }
89 
90 
91 /*
92  * Copy constructor for ListOfDrawables.
93  */
ListOfDrawables(const ListOfDrawables & orig)94 ListOfDrawables::ListOfDrawables(const ListOfDrawables& orig)
95   : ListOf( orig )
96 {
97 }
98 
99 
100 /*
101  * Assignment operator for ListOfDrawables.
102  */
103 ListOfDrawables&
operator =(const ListOfDrawables & rhs)104 ListOfDrawables::operator=(const ListOfDrawables& rhs)
105 {
106   if (&rhs != this)
107   {
108     ListOf::operator=(rhs);
109   }
110 
111   return *this;
112 }
113 
114 
115 /*
116  * Creates and returns a deep copy of this ListOfDrawables object.
117  */
118 ListOfDrawables*
clone() const119 ListOfDrawables::clone() const
120 {
121   return new ListOfDrawables(*this);
122 }
123 
124 
125 /*
126  * Destructor for ListOfDrawables.
127  */
~ListOfDrawables()128 ListOfDrawables::~ListOfDrawables()
129 {
130 }
131 
132 
133 /*
134  * Get a Transformation2D from the ListOfDrawables.
135  */
136 Transformation2D*
get(unsigned int n)137 ListOfDrawables::get(unsigned int n)
138 {
139   return static_cast<Transformation2D*>(ListOf::get(n));
140 }
141 
142 
143 /*
144  * Get a Transformation2D from the ListOfDrawables.
145  */
146 const Transformation2D*
get(unsigned int n) const147 ListOfDrawables::get(unsigned int n) const
148 {
149   return static_cast<const Transformation2D*>(ListOf::get(n));
150 }
151 
152 
153 /*
154  * Get a Transformation2D from the ListOfDrawables based on its identifier.
155  */
156 Transformation2D*
get(const std::string & sid)157 ListOfDrawables::get(const std::string& sid)
158 {
159   return const_cast<Transformation2D*>(static_cast<const
160     ListOfDrawables&>(*this).get(sid));
161 }
162 
163 
164 /*
165  * Get a Transformation2D from the ListOfDrawables based on its identifier.
166  */
167 const Transformation2D*
get(const std::string & sid) const168 ListOfDrawables::get(const std::string& sid) const
169 {
170   vector<SBase*>::const_iterator result;
171   result = find_if(mItems.begin(), mItems.end(), IdEq<Transformation2D>(sid));
172   return (result == mItems.end()) ? 0 : static_cast <const Transformation2D*>
173     (*result);
174 }
175 
176 
177 /*
178  * Removes the nth Transformation2D from this ListOfDrawables and returns a
179  * pointer to it.
180  */
181 Transformation2D*
remove(unsigned int n)182 ListOfDrawables::remove(unsigned int n)
183 {
184   return static_cast<Transformation2D*>(ListOf::remove(n));
185 }
186 
187 
188 /*
189  * Removes the Transformation2D from this ListOfDrawables based on its
190  * identifier and returns a pointer to it.
191  */
192 Transformation2D*
remove(const std::string & sid)193 ListOfDrawables::remove(const std::string& sid)
194 {
195   SBase* item = NULL;
196   vector<SBase*>::iterator result;
197 
198   result = find_if(mItems.begin(), mItems.end(), IdEq<Transformation2D>(sid));
199 
200   if (result != mItems.end())
201   {
202     item = *result;
203     mItems.erase(result);
204   }
205 
206   return static_cast <Transformation2D*> (item);
207 }
208 
209 
210 /*
211  * Adds a copy of the given Transformation2D to this ListOfDrawables.
212  */
213 int
addTransformation2D(const Transformation2D * td)214 ListOfDrawables::addTransformation2D(const Transformation2D* td)
215 {
216   if (td == NULL)
217   {
218     return LIBSBML_OPERATION_FAILED;
219   }
220   else if (td->hasRequiredAttributes() == false)
221   {
222     return LIBSBML_INVALID_OBJECT;
223   }
224   else if (getLevel() != td->getLevel())
225   {
226     return LIBSBML_LEVEL_MISMATCH;
227   }
228   else if (getVersion() != td->getVersion())
229   {
230     return LIBSBML_VERSION_MISMATCH;
231   }
232   else if (matchesRequiredSBMLNamespacesForAddition(static_cast<const
233     SBase*>(td)) == false)
234   {
235     return LIBSBML_NAMESPACES_MISMATCH;
236   }
237   else
238   {
239     return append(td);
240   }
241 }
242 
243 
244 /*
245  * Get the number of Transformation2D objects in this ListOfDrawables.
246  */
247 unsigned int
getNumTransformation2Ds() const248 ListOfDrawables::getNumTransformation2Ds() const
249 {
250   return size();
251 }
252 
253 
254 /*
255  * Creates a new Image object, adds it to this ListOfDrawables object and
256  * returns the Image object created.
257  */
258 Image*
createImage()259 ListOfDrawables::createImage()
260 {
261   Image* i = NULL;
262 
263   try
264   {
265     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
266     i = new Image(renderns);
267     delete renderns;
268   }
269   catch (...)
270   {
271   }
272 
273   if (i != NULL)
274   {
275     appendAndOwn(i);
276   }
277 
278   return i;
279 }
280 
281 
282 /*
283  * Creates a new Ellipse object, adds it to this ListOfDrawables object and
284  * returns the Ellipse object created.
285  */
286 Ellipse*
createEllipse()287 ListOfDrawables::createEllipse()
288 {
289   Ellipse* e = NULL;
290 
291   try
292   {
293     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
294     e = new Ellipse(renderns);
295     delete renderns;
296   }
297   catch (...)
298   {
299   }
300 
301   if (e != NULL)
302   {
303     appendAndOwn(e);
304   }
305 
306   return e;
307 }
308 
309 
310 /*
311  * Creates a new Rectangle object, adds it to this ListOfDrawables object and
312  * returns the Rectangle object created.
313  */
314 Rectangle*
createRectangle()315 ListOfDrawables::createRectangle()
316 {
317   Rectangle* r = NULL;
318 
319   try
320   {
321     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
322     r = new Rectangle(renderns);
323     delete renderns;
324   }
325   catch (...)
326   {
327   }
328 
329   if (r != NULL)
330   {
331     appendAndOwn(r);
332   }
333 
334   return r;
335 }
336 
337 
338 /*
339  * Creates a new Polygon object, adds it to this ListOfDrawables object and
340  * returns the Polygon object created.
341  */
342 Polygon*
createPolygon()343 ListOfDrawables::createPolygon()
344 {
345   Polygon* p = NULL;
346 
347   try
348   {
349     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
350     p = new Polygon(renderns);
351     delete renderns;
352   }
353   catch (...)
354   {
355   }
356 
357   if (p != NULL)
358   {
359     appendAndOwn(p);
360   }
361 
362   return p;
363 }
364 
365 
366 /*
367  * Creates a new RenderGroup object, adds it to this ListOfDrawables object and
368  * returns the RenderGroup object created.
369  */
370 RenderGroup*
createGroup()371 ListOfDrawables::createGroup()
372 {
373   RenderGroup* rg = NULL;
374 
375   try
376   {
377     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
378     rg = new RenderGroup(renderns);
379     delete renderns;
380   }
381   catch (...)
382   {
383   }
384 
385   if (rg != NULL)
386   {
387     appendAndOwn(rg);
388   }
389 
390   return rg;
391 }
392 
393 
394 /*
395  * Creates a new LineEnding object, adds it to this ListOfDrawables object and
396  * returns the LineEnding object created.
397  */
398 LineEnding*
createLineEnding()399 ListOfDrawables::createLineEnding()
400 {
401   LineEnding* le = NULL;
402 
403   try
404   {
405     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
406     le = new LineEnding(renderns);
407     delete renderns;
408   }
409   catch (...)
410   {
411   }
412 
413   if (le != NULL)
414   {
415     appendAndOwn(le);
416   }
417 
418   return le;
419 }
420 
421 
422 /*
423  * Creates a new Text object, adds it to this ListOfDrawables object and
424  * returns the Text object created.
425  */
426 Text*
createText()427 ListOfDrawables::createText()
428 {
429   Text* t = NULL;
430 
431   try
432   {
433     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
434     t = new Text(renderns);
435     delete renderns;
436   }
437   catch (...)
438   {
439   }
440 
441   if (t != NULL)
442   {
443     appendAndOwn(t);
444   }
445 
446   return t;
447 }
448 
449 
450 /*
451  * Creates a new RenderCurve object, adds it to this ListOfDrawables object and
452  * returns the RenderCurve object created.
453  */
454 RenderCurve*
createCurve()455 ListOfDrawables::createCurve()
456 {
457   RenderCurve* rc = NULL;
458 
459   try
460   {
461     RENDER_CREATE_NS(renderns, getSBMLNamespaces());
462     rc = new RenderCurve(renderns);
463     delete renderns;
464   }
465   catch (...)
466   {
467   }
468 
469   if (rc != NULL)
470   {
471     appendAndOwn(rc);
472   }
473 
474   return rc;
475 }
476 
477 
478 /*
479  * Returns the XML element name of this ListOfDrawables object.
480  */
481 const std::string&
getElementName() const482 ListOfDrawables::getElementName() const
483 {
484   static const string name = "listOfDrawables";
485   return name;
486 }
487 
488 
489 /*
490  * Returns the libSBML type code for this ListOfDrawables object.
491  */
492 int
getTypeCode() const493 ListOfDrawables::getTypeCode() const
494 {
495   return SBML_LIST_OF;
496 }
497 
498 
499 /*
500  * Returns the libSBML type code for the SBML objects contained in this
501  * ListOfDrawables object.
502  */
503 int
getItemTypeCode() const504 ListOfDrawables::getItemTypeCode() const
505 {
506   return SBML_RENDER_TRANSFORMATION2D;
507 }
508 
509 
510 
511 /** @cond doxygenLibsbmlInternal */
512 
513 /*
514  * Creates a new Transformation2D in this ListOfDrawables
515  */
516 SBase*
createObject(XMLInputStream & stream)517 ListOfDrawables::createObject(XMLInputStream& stream)
518 {
519   const std::string& name = stream.peek().getName();
520   SBase* object = NULL;
521   RENDER_CREATE_NS(renderns, getSBMLNamespaces());
522 
523   //if (name == "transformation2D")
524   //{
525   //  object = new Transformation2D(renderns);
526   //  appendAndOwn(object);
527   //}
528 
529     if (name == "g")
530     {
531        object = new RenderGroup(renderns);
532     }
533 
534     if(name=="curve")
535     {
536        // only newstyle cures will be supported for L3
537        object = new RenderCurve(renderns);
538     }
539 
540   if (name == "image")
541   {
542     object = new Image(renderns);
543     appendAndOwn(object);
544   }
545 
546   if (name == "ellipse")
547   {
548     object = new Ellipse(renderns);
549     appendAndOwn(object);
550   }
551 
552   if (name == "rectangle")
553   {
554     object = new Rectangle(renderns);
555     appendAndOwn(object);
556   }
557 
558   if (name == "polygon")
559   {
560     object = new Polygon(renderns);
561     appendAndOwn(object);
562   }
563 
564   if (name == "renderGroup")
565   {
566     object = new RenderGroup(renderns);
567     appendAndOwn(object);
568   }
569 
570   if (name == "lineEnding")
571   {
572     object = new LineEnding(renderns);
573     appendAndOwn(object);
574   }
575 
576   if (name == "text")
577   {
578     object = new Text(renderns);
579     appendAndOwn(object);
580   }
581 
582   if (name == "renderCurve")
583   {
584     object = new RenderCurve(renderns);
585     appendAndOwn(object);
586   }
587 
588   delete renderns;
589   return object;
590 }
591 
592 /** @endcond */
593 
594 
595 
596 /** @cond doxygenLibsbmlInternal */
597 
598 /*
599  * Writes the namespace for the Render package
600  */
601 void
writeXMLNS(XMLOutputStream & stream) const602 ListOfDrawables::writeXMLNS(XMLOutputStream& stream) const
603 {
604   XMLNamespaces xmlns;
605   std::string prefix = getPrefix();
606 
607   if (prefix.empty())
608   {
609     const XMLNamespaces* thisxmlns = getNamespaces();
610     if (thisxmlns && thisxmlns->hasURI(RenderExtension::getXmlnsL3V1V1()))
611     {
612       xmlns.add(RenderExtension::getXmlnsL3V1V1(), prefix);
613     }
614   }
615 
616   stream << xmlns;
617 }
618 
619 /** @endcond */
620 
621 
622 
623 /** @cond doxygenLibsbmlInternal */
624 
625 /*
626  * checks concrete types
627  */
628 bool
isValidTypeForList(SBase * item)629 ListOfDrawables::isValidTypeForList(SBase* item)
630 {
631   unsigned int tc = item->getTypeCode();
632 
633   return ((tc == SBML_RENDER_IMAGE) || (tc == SBML_RENDER_ELLIPSE) || (tc ==
634     SBML_RENDER_RECTANGLE) || (tc == SBML_RENDER_POLYGON) || (tc ==
635       SBML_RENDER_GROUP) || (tc == SBML_RENDER_LINEENDING) || (tc ==
636         SBML_RENDER_TEXT) || (tc == SBML_RENDER_CURVE));
637 }
638 
639 /** @endcond */
640 
641 
642 
643 
644 #endif /* __cplusplus */
645 
646 
647 /*
648  * Get a Transformation2D_t from the ListOf_t.
649  */
650 LIBSBML_EXTERN
651 Transformation2D_t*
ListOfDrawables_getTransformation2D(ListOf_t * lo,unsigned int n)652 ListOfDrawables_getTransformation2D(ListOf_t* lo, unsigned int n)
653 {
654   if (lo == NULL)
655   {
656     return NULL;
657   }
658 
659   return static_cast <ListOfDrawables*>(lo)->get(n);
660 }
661 
662 
663 /*
664  * Get a Transformation2D_t from the ListOf_t based on its identifier.
665  */
666 LIBSBML_EXTERN
667 Transformation2D_t*
ListOfDrawables_getById(ListOf_t * lo,const char * sid)668 ListOfDrawables_getById(ListOf_t* lo, const char *sid)
669 {
670   if (lo == NULL)
671   {
672     return NULL;
673   }
674 
675   return (sid != NULL) ? static_cast <ListOfDrawables*>(lo)->get(sid) : NULL;
676 }
677 
678 
679 /*
680  * Removes the nth Transformation2D_t from this ListOf_t and returns a pointer
681  * to it.
682  */
683 LIBSBML_EXTERN
684 Transformation2D_t*
ListOfDrawables_remove(ListOf_t * lo,unsigned int n)685 ListOfDrawables_remove(ListOf_t* lo, unsigned int n)
686 {
687   if (lo == NULL)
688   {
689     return NULL;
690   }
691 
692   return static_cast <ListOfDrawables*>(lo)->remove(n);
693 }
694 
695 
696 /*
697  * Removes the Transformation2D_t from this ListOf_t based on its identifier
698  * and returns a pointer to it.
699  */
700 LIBSBML_EXTERN
701 Transformation2D_t*
ListOfDrawables_removeById(ListOf_t * lo,const char * sid)702 ListOfDrawables_removeById(ListOf_t* lo, const char* sid)
703 {
704   if (lo == NULL)
705   {
706     return NULL;
707   }
708 
709   return (sid != NULL) ? static_cast <ListOfDrawables*>(lo)->remove(sid) :
710     NULL;
711 }
712 
713 
714 
715 
716 LIBSBML_CPP_NAMESPACE_END
717 
718 
719