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-2010 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 
00045 class Dialog;
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 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
00108 
00121 enum RuleId
00122 {
00123         RuleMayContain,
00124         RuleMustContain,
00125         RuleMayBeIn,
00126         RuleMustBeIn
00127 };
00128 
00133 typedef unsigned SignalId;
00134 
00135 class Document;
00136 
00140 class Object
00141 {
00142 public:
00146         Object (TypeId Id = OtherType);
00150         virtual ~Object ();
00151         
00156         TypeId GetType () const {return m_Type;}
00162         void SetId (gchar const *Id);
00166         gchar const *GetId () const {return m_Id;}
00173         virtual void AddChild (Object* object);
00181         Object* GetMolecule () const;
00188         Object* GetReaction () const;
00196         Object* GetGroup () const;
00203         Document* GetDocument () const;
00213         Object* GetParentOfType (TypeId Id) const;
00220         Object* GetChild (const gchar* Id) const;
00227         Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
00228         Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
00235         Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
00236         Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
00243         Object* GetDescendant (const gchar* Id) const;
00247         Object* GetParent () const {return m_Parent;};
00254         void SetParent (Object* Parent);
00263         virtual xmlNodePtr Save (xmlDocPtr xml) const;
00280         virtual bool Load (xmlNodePtr node);
00289         virtual void Move (double x, double y, double z = 0.);
00300         virtual void Transform2D (Matrix2D& m, double x, double y);
00309         bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
00315         void SaveId (xmlNodePtr node) const;
00326         xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00336         xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00346         xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00355         xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00359         bool HasChildren () const {return m_Children.size () != 0;}
00360 
00364         unsigned GetChildrenNumber () const {return m_Children.size ();}
00365 
00374         virtual Object* GetAtomAt (double x, double y, double z = 0.);
00375 
00382         virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00383 
00389         virtual double GetYAlign ();
00390 
00404         virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00405 
00412         void EmitSignal (SignalId Signal);
00413 
00423         virtual bool OnSignal (SignalId Signal, Object *Child);
00424 
00432         void Lock (bool state = true);
00433 
00440         bool IsLocked () {return m_Locked > 0;}
00441 
00449         Object* GetFirstLink (std::set<Object*>::iterator& i);
00450 
00457         Object* GetNextLink (std::set<Object*>::iterator& i);
00458 
00464         void Link (Object *object);
00465 
00471         void Unlink (Object *object);
00472 
00479         virtual void OnUnlink (Object *object);
00480 
00486         void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
00487 
00497         virtual bool SetProperty (unsigned property, char const *value);
00498 
00505         virtual std::string GetProperty (unsigned property) const;
00506 
00510         virtual void OnLoaded ();
00511         
00516         void SetDirty (bool dirty = true);
00517 
00521         virtual void Clear ();
00522 
00526         virtual std::string Name ();
00527 
00532         std::string Identity ();
00533 
00537         virtual char const *HasPropertiesDialog () const;
00538 
00542         void ShowPropertiesDialog ();
00543 
00553         static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00554 
00561         static void AddAlias (TypeId id, std::string TypeName);
00562 
00573         static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00574 
00580         static TypeId GetTypeId (const std::string& Name);
00581 
00587         static std::string GetTypeName (TypeId Id);
00588 
00595         static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00596 
00604         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00605 
00613         static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00614 
00621         static  const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00622 
00629         static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00630 
00638         static void SetCreationLabel (TypeId Id, std::string Label);
00639 
00645         static const std::string& GetCreationLabel (TypeId Id);
00646 
00652         static const std::string& GetCreationLabel (const std::string& TypeName);
00653 
00657         static SignalId CreateNewSignalId ();
00658 
00659 protected:
00663         virtual Dialog *BuildPropertiesDialog ();
00664 
00665 private:
00666         Object* RealGetDescendant (const gchar* Id) const;
00667 
00668 private:
00669         gchar* m_Id;
00670         TypeId m_Type;
00671         Object *m_Parent;
00672         std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
00673         std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00674 
00675 private:
00679         int m_Locked;
00680 
00685 GCU_RO_PROP (bool, Dirty);
00686 };
00687 
00688 }
00689 #endif //GCU_OBJECT_H

Generated on Fri Sep 24 15:58:41 2010 for The Gnome Chemistry Utils by  doxygen 1.5.9