object.h

00001 // -*- C++ -*-
00002 
00003 /* 
00004  * Gnome Chemistry Utils
00005  * object.h 
00006  *
00007  * Copyright (C) 2002-2005 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 "matrix2d.h"
00029 #include <glib.h>
00030 #include <libxml/parser.h>
00031 #include <map>
00032 #include <set>
00033 #include <list>
00034 #include <string>
00035 #include <stdexcept>
00036 #include <gtk/gtk.h>
00037 #include <libgnomeprint/gnome-print.h>
00038 
00039 #define square(x) ((x)*(x))
00040 
00041 using namespace std;
00042 
00043 namespace gcu
00044 {
00045 
00070 enum
00071 {
00072         NoType,
00073         AtomType,
00074         FragmentType,
00075         BondType,
00076         MoleculeType,
00077         ChainType,
00078         CycleType,
00079         ReactantType,
00080         ReactionArrowType,
00081         ReactionOperatorType,
00082         ReactionType,
00083         MesomeryType,
00084         MesomeryArrowType,
00085         DocumentType,
00086         TextType,
00087         OtherType
00088 };
00089 
00090 typedef unsigned TypeId;
00091 
00104 enum RuleId
00105 {
00106         RuleMayContain,
00107         RuleMustContain,
00108         RuleMayBeIn,
00109         RuleMustBeIn
00110 };
00111 
00112 typedef unsigned SignalId;
00113 
00114 class Document;
00115 
00119 class Object
00120 {
00121 public:
00125         Object (TypeId Id = OtherType);
00129         virtual ~Object ();
00130         
00135         TypeId GetType () {return m_Type;}
00141         void SetId (gchar* Id);
00145         const gchar* GetId () {return m_Id;}
00152         void AddChild (Object* object);
00159         Object* GetMolecule ();
00166         Object* GetReaction ();
00174         Object* GetGroup ();
00181         Document* GetDocument ();
00191         Object* GetParentOfType (TypeId Id);
00198         Object* GetChild (const gchar* Id);
00205         Object* GetFirstChild (map<string, Object*>::iterator& i);
00212         Object* GetNextChild (map<string, Object*>::iterator& i);
00219         Object* GetDescendant (const gchar* Id);
00223         Object* GetParent () {return m_Parent;}
00230         void SetParent (Object* Parent);
00239         virtual xmlNodePtr Save (xmlDocPtr xml);
00256         virtual bool Load (xmlNodePtr node);
00265         virtual void Move (double x, double y, double z = 0.);
00276         virtual void Transform2D (Matrix2D& m, double x, double y);
00285         bool SaveChildren (xmlDocPtr xml, xmlNodePtr node);
00291         void SaveId (xmlNodePtr node);
00302         xmlNodePtr GetNodeByProp (xmlNodePtr node, char* Property, char* Id);
00312         xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char* Property, char* Id);
00322         xmlNodePtr GetNodeByName (xmlNodePtr node, char* Name);
00331         xmlNodePtr GetNextNodeByName (xmlNodePtr node, char* Name);
00338         virtual void Add (GtkWidget* w);
00344         virtual void Print (GnomePrintContext *pc);
00351         virtual void Update (GtkWidget* w);
00359         virtual void SetSelected (GtkWidget* w, int state);
00363         bool HasChildren () {return m_Children.size () != 0;}
00364 
00368         unsigned GetChildrenNumber () {return m_Children.size ();}
00369 
00378         virtual Object* GetAtomAt (double x, double y, double z = 0.);
00379 
00386         virtual bool Build (list<Object*>& Children) throw (invalid_argument);
00387 
00393         virtual double GetYAlign ();
00394 
00405         virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object);
00406 
00413         void EmitSignal (SignalId Signal);
00414 
00424         virtual bool OnSignal (SignalId Signal, Object *Child);
00425 
00433         void Lock (bool state = true);
00434 
00441         bool IsLocked () {return m_Locked > 0;}
00442 
00450         Object* GetFirstLink (set<Object*>::iterator& i);
00451 
00458         Object* GetNextLink (set<Object*>::iterator& i);
00459 
00465         void Unlink (Object *object);
00466 
00473         virtual void OnUnlink (Object *object);
00474 
00480         void GetPossibleAncestorTypes (set<TypeId>& types);
00481 
00491         static TypeId AddType (string TypeName, Object*(*CreateFunc)(), TypeId id = OtherType);
00492 
00503         static Object* CreateObject (const string& TypeName, Object* parent = NULL);
00504 
00510         static TypeId GetTypeId (const string& Name);
00511 
00517         static string GetTypeName (TypeId Id);
00518 
00526         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00527 
00535         static void AddRule (const string& type1, RuleId rule, const string& type2);
00536 
00543         static  const set<TypeId>& GetRules (TypeId type, RuleId rule);
00544 
00551         static const set<TypeId>& GetRules (const string& type, RuleId rule);
00552 
00560         static void SetCreationLabel (TypeId Id, string Label);
00561 
00567         static const string& GetCreationLabel (TypeId Id);
00568 
00574         static const string& GetCreationLabel (const string& TypeName);
00575 
00579         static SignalId CreateNewSignalId ();
00580 
00581 private:
00582         Object* RealGetDescendant (const gchar* Id);
00583 
00584 private:
00585         gchar* m_Id;
00586         TypeId m_Type;
00587         Object *m_Parent;
00588         map<string, Object*> m_Children; //string is Id of object, so each object must have an Id
00589         set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00590 
00591 private:
00595         int m_Locked;
00596 };
00597 
00598 }
00599 #endif //GCU_OBJECT_H

Generated on Tue Aug 15 10:47:35 2006 for The Gnome Chemistry Utils by  doxygen 1.4.7