The Gnome Chemistry Utils 0.13.3
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-2011 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 <libxml/parser.h>
00031 #include <map>
00032 #include <set>
00033 #include <list>
00034 #include <string>
00035 #include <stdexcept>
00036 
00037 #define square(x) ((x)*(x))
00038 
00040 namespace gcu
00041 {
00042 
00043 class Dialog;
00044 class Application;
00045 class UIManager;
00046 
00071 enum GcuTypeId
00072 {
00073         NoType,
00074         AtomType,
00075         FragmentType,
00076         BondType,
00077         MoleculeType,
00078         ChainType,
00079         CycleType,
00080         ReactantType,
00081         ReactionArrowType,
00082         ReactionOperatorType,
00083         ReactionType,
00084         MesomeryType,
00085         MesomeryArrowType,
00086         DocumentType,
00087         TextType,
00088         OtherType
00089 };
00090 
00095 typedef unsigned TypeId;
00096 
00097 class Object;
00098 
00107 // FIXME: create a class for UIManager
00108 typedef bool (*BuildMenuCb) (Object *target, UIManager *uim, Object *object, double x, double y);
00109 
00110 class TypeDesc
00111 {
00112 public:
00113         TypeDesc ();
00114 
00115         TypeId Id;
00116         Object* (*Create) ();
00117         std::set <TypeId> PossibleChildren;
00118         std::set <TypeId> PossibleParents;
00119         std::set <TypeId> RequiredChildren;
00120         std::set <TypeId> RequiredParents;
00121         std::string CreationLabel;
00122         std::list <BuildMenuCb> MenuCbs;
00123 };
00124 
00125 class Object;
00126 
00139 enum RuleId
00140 {
00141         RuleMayContain,
00142         RuleMustContain,
00143         RuleMayBeIn,
00144         RuleMustBeIn
00145 };
00146 
00151 typedef unsigned SignalId;
00152 
00153 class Document;
00154 
00158 class Object
00159 {
00160 friend class Application;
00161 public:
00165         Object (TypeId Id = OtherType);
00169         virtual ~Object ();
00170         
00175         TypeId GetType () const {return m_Type;}
00181         void SetId (gchar const *Id);
00185         char const *GetId () const {return m_Id;}
00192         virtual void AddChild (Object* object);
00200         Object* GetMolecule () const;
00207         Object* GetReaction () const;
00215         Object* GetGroup () const;
00222         Document* GetDocument () const;
00228         Application* GetApplication () const;
00238         Object* GetParentOfType (TypeId Id) const;
00245         Object* GetChild (const gchar* Id) const;
00252         Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
00253         Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
00260         Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
00261         Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
00268         Object* GetDescendant (const char* Id) const;
00272         Object* GetParent () const {return m_Parent;};
00279         void SetParent (Object* Parent);
00288         virtual xmlNodePtr Save (xmlDocPtr xml) const;
00305         virtual bool Load (xmlNodePtr node);
00314         virtual bool GetCoords (double *x, double *y, double *z = NULL) const;
00323         virtual void Move (double x, double y, double z = 0.);
00334         virtual void Transform2D (Matrix2D& m, double x, double y);
00343         bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
00349         void SaveId (xmlNodePtr node) const;
00360         xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00370         xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00380         xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00389         xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00393         bool HasChildren () const {return m_Children.size () != 0;}
00394 
00398         unsigned GetChildrenNumber () const {return m_Children.size ();}
00399 
00408         virtual Object* GetAtomAt (double x, double y, double z = 0.);
00409 
00416         virtual bool Build (std::set < Object * > const &Children) throw (std::invalid_argument);
00417 
00423         virtual double GetYAlign ();
00424 
00438         virtual bool BuildContextualMenu (UIManager *uim, Object *object, double x, double y);
00439 
00446         void EmitSignal (SignalId Signal);
00447 
00457         virtual bool OnSignal (SignalId Signal, Object *Child);
00458 
00466         void Lock (bool state = true);
00467 
00474         bool IsLocked () {return m_Locked > 0;}
00475 
00483         Object* GetFirstLink (std::set<Object*>::iterator& i);
00484 
00491         Object* GetNextLink (std::set<Object*>::iterator& i);
00492 
00498         void Link (Object *object);
00499 
00505         void Unlink (Object *object);
00506 
00513         virtual void OnUnlink (Object *object);
00514 
00520         void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
00521 
00531         virtual bool SetProperty (unsigned property, char const *value);
00532 
00539         virtual std::string GetProperty (unsigned property) const;
00540 
00544         virtual void OnLoaded ();
00545         
00550         void SetDirty (bool dirty = true);
00551 
00555         virtual void Clear ();
00556 
00560         virtual std::string Name ();
00561 
00566         std::string Identity ();
00567 
00571         virtual char const *HasPropertiesDialog () const;
00572 
00576         virtual bool CanSelect () const {return true;}
00577 
00582         virtual void NotifyEmpty () {;}
00583 
00587         void ShowPropertiesDialog ();
00588 
00601         static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00602 
00609         static void AddAlias (TypeId id, std::string TypeName);
00610 
00623         static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00624 
00630         static TypeId GetTypeId (const std::string& Name);
00631 
00637         static std::string GetTypeName (TypeId Id);
00638 
00647         static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00648 
00658         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00659 
00669         static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00670 
00679         static  const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00680 
00689         static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00690 
00700         static void SetCreationLabel (TypeId Id, std::string Label);
00701 
00709         static const std::string& GetCreationLabel (TypeId Id);
00710 
00718         static const std::string& GetCreationLabel (const std::string& TypeName);
00719 
00723         static SignalId CreateNewSignalId ();
00724 
00725 protected:
00729         virtual Dialog *BuildPropertiesDialog ();
00730 
00731 private:
00732         Object* RealGetDescendant (const gchar* Id) const;
00733 
00734 private:
00735         char* m_Id;
00736         TypeId m_Type;
00737         Object *m_Parent;
00738         std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
00739         std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00740         TypeDesc const *m_TypeDesc;
00741 
00742 private:
00746         int m_Locked;
00747 
00752 GCU_RO_PROP (bool, Dirty);
00753 };
00754 
00755 }
00756 #endif //GCU_OBJECT_H