00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef GCU_OBJECT_H
00026 #define GCU_OBJECT_H
00027
00028 #include "macros.h"
00029 #include "matrix2d.h"
00030 #include <glib.h>
00031 #include <libxml/parser.h>
00032 #include <map>
00033 #include <set>
00034 #include <list>
00035 #include <string>
00036 #include <stdexcept>
00037 #include <gtk/gtk.h>
00038
00039 #define square(x) ((x)*(x))
00040
00042 namespace gcu
00043 {
00044
00069 enum GcuTypeId
00070 {
00071 NoType,
00072 AtomType,
00073 FragmentType,
00074 BondType,
00075 MoleculeType,
00076 ChainType,
00077 CycleType,
00078 ReactantType,
00079 ReactionArrowType,
00080 ReactionOperatorType,
00081 ReactionType,
00082 MesomeryType,
00083 MesomeryArrowType,
00084 DocumentType,
00085 TextType,
00086 OtherType
00087 };
00088
00093 typedef unsigned TypeId;
00094
00095 class Object;
00096
00105 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
00106
00119 enum RuleId
00120 {
00121 RuleMayContain,
00122 RuleMustContain,
00123 RuleMayBeIn,
00124 RuleMustBeIn
00125 };
00126
00131 typedef unsigned SignalId;
00132
00133 class Document;
00134
00138 class Object
00139 {
00140 public:
00144 Object (TypeId Id = OtherType);
00148 virtual ~Object ();
00149
00154 TypeId GetType () const {return m_Type;}
00160 void SetId (gchar const *Id);
00164 gchar const *GetId () const {return m_Id;}
00171 virtual void AddChild (Object* object);
00179 Object* GetMolecule () const;
00186 Object* GetReaction () const;
00194 Object* GetGroup () const;
00201 Document* GetDocument () const;
00211 Object* GetParentOfType (TypeId Id) const;
00218 Object* GetChild (const gchar* Id) const;
00225 Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
00226 Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
00233 Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
00234 Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
00241 Object* GetDescendant (const gchar* Id) const;
00245 Object* GetParent () const {return m_Parent;};
00252 void SetParent (Object* Parent);
00261 virtual xmlNodePtr Save (xmlDocPtr xml) const;
00278 virtual bool Load (xmlNodePtr node);
00287 virtual void Move (double x, double y, double z = 0.);
00298 virtual void Transform2D (Matrix2D& m, double x, double y);
00307 bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
00313 void SaveId (xmlNodePtr node) const;
00324 xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00334 xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00344 xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00353 xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00357 bool HasChildren () const {return m_Children.size () != 0;}
00358
00362 unsigned GetChildrenNumber () const {return m_Children.size ();}
00363
00372 virtual Object* GetAtomAt (double x, double y, double z = 0.);
00373
00380 virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00381
00387 virtual double GetYAlign ();
00388
00402 virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00403
00410 void EmitSignal (SignalId Signal);
00411
00421 virtual bool OnSignal (SignalId Signal, Object *Child);
00422
00430 void Lock (bool state = true);
00431
00438 bool IsLocked () {return m_Locked > 0;}
00439
00447 Object* GetFirstLink (std::set<Object*>::iterator& i);
00448
00455 Object* GetNextLink (std::set<Object*>::iterator& i);
00456
00462 void Unlink (Object *object);
00463
00470 virtual void OnUnlink (Object *object);
00471
00477 void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
00478
00488 virtual bool SetProperty (unsigned property, char const *value);
00489
00496 virtual std::string GetProperty (unsigned property) const;
00497
00501 virtual void OnLoaded ();
00502
00507 void SetDirty (bool dirty = true);
00508
00518 static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00519
00530 static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00531
00537 static TypeId GetTypeId (const std::string& Name);
00538
00544 static std::string GetTypeName (TypeId Id);
00545
00552 static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00553
00561 static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00562
00570 static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00571
00578 static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00579
00586 static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00587
00595 static void SetCreationLabel (TypeId Id, std::string Label);
00596
00602 static const std::string& GetCreationLabel (TypeId Id);
00603
00609 static const std::string& GetCreationLabel (const std::string& TypeName);
00610
00614 static SignalId CreateNewSignalId ();
00615
00616 private:
00617 Object* RealGetDescendant (const gchar* Id) const;
00618
00619 private:
00620 gchar* m_Id;
00621 TypeId m_Type;
00622 Object *m_Parent;
00623 std::map<std::string, Object*> m_Children;
00624 std::set<Object*> m_Links;
00625
00626 private:
00630 int m_Locked;
00631
00636 GCU_RO_PROP (bool, Dirty);
00637 };
00638
00639 }
00640 #endif //GCU_OBJECT_H