object.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 /* 
00004  * Gnome Chemistry Utils
00005  * object.h 
00006  *
00007  * Copyright (C) 2002-2008 Jean Bréfort <jean.brefort@normalesup.org>
00008  *
00009  * This program is free software; you can redistribute it and/or 
00010  * modify it under the terms of the GNU General Public License as 
00011  * published by the Free Software Foundation; either version 2 of the
00012  * License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00022  * USA
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 
00512         virtual void Clear ();
00513 
00523         static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00524 
00535         static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00536 
00542         static TypeId GetTypeId (const std::string& Name);
00543 
00549         static std::string GetTypeName (TypeId Id);
00550 
00557         static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00558 
00566         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00567 
00575         static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00576 
00583         static  const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00584 
00591         static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00592 
00600         static void SetCreationLabel (TypeId Id, std::string Label);
00601 
00607         static const std::string& GetCreationLabel (TypeId Id);
00608 
00614         static const std::string& GetCreationLabel (const std::string& TypeName);
00615 
00619         static SignalId CreateNewSignalId ();
00620 
00621 private:
00622         Object* RealGetDescendant (const gchar* Id) const;
00623 
00624 private:
00625         gchar* m_Id;
00626         TypeId m_Type;
00627         Object *m_Parent;
00628         std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
00629         std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00630 
00631 private:
00635         int m_Locked;
00636 
00641 GCU_RO_PROP (bool, Dirty);
00642 };
00643 
00644 }
00645 #endif //GCU_OBJECT_H

Generated on Sun Sep 6 18:29:26 2009 for The Gnome Chemistry Utils by  doxygen 1.6.1