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 GcuTypeId
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 
00094 typedef unsigned TypeId;
00095 
00108 enum RuleId
00109 {
00110         RuleMayContain,
00111         RuleMustContain,
00112         RuleMayBeIn,
00113         RuleMustBeIn
00114 };
00115 
00120 typedef unsigned SignalId;
00121 
00122 class Document;
00123 
00127 class Object
00128 {
00129 public:
00133         Object (TypeId Id = OtherType);
00137         virtual ~Object ();
00138         
00143         TypeId GetType () {return m_Type;}
00149         void SetId (gchar* Id);
00153         const gchar* GetId () {return m_Id;}
00160         void AddChild (Object* object);
00167         Object* GetMolecule ();
00174         Object* GetReaction ();
00182         Object* GetGroup ();
00189         Document* GetDocument ();
00199         Object* GetParentOfType (TypeId Id);
00206         Object* GetChild (const gchar* Id);
00213         Object* GetFirstChild (map<string, Object*>::iterator& i);
00220         Object* GetNextChild (map<string, Object*>::iterator& i);
00227         Object* GetDescendant (const gchar* Id);
00231         Object* GetParent () {return m_Parent;}
00238         void SetParent (Object* Parent);
00247         virtual xmlNodePtr Save (xmlDocPtr xml);
00264         virtual bool Load (xmlNodePtr node);
00273         virtual void Move (double x, double y, double z = 0.);
00284         virtual void Transform2D (Matrix2D& m, double x, double y);
00293         bool SaveChildren (xmlDocPtr xml, xmlNodePtr node);
00299         void SaveId (xmlNodePtr node);
00310         xmlNodePtr GetNodeByProp (xmlNodePtr node, char* Property, char* Id);
00320         xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char* Property, char* Id);
00330         xmlNodePtr GetNodeByName (xmlNodePtr node, char* Name);
00339         xmlNodePtr GetNextNodeByName (xmlNodePtr node, char* Name);
00346         virtual void Add (GtkWidget* w);
00352         virtual void Print (GnomePrintContext *pc);
00359         virtual void Update (GtkWidget* w);
00367         virtual void SetSelected (GtkWidget* w, int state);
00371         bool HasChildren () {return m_Children.size () != 0;}
00372 
00376         unsigned GetChildrenNumber () {return m_Children.size ();}
00377 
00386         virtual Object* GetAtomAt (double x, double y, double z = 0.);
00387 
00394         virtual bool Build (list<Object*>& Children) throw (invalid_argument);
00395 
00401         virtual double GetYAlign ();
00402 
00413         virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object);
00414 
00421         void EmitSignal (SignalId Signal);
00422 
00432         virtual bool OnSignal (SignalId Signal, Object *Child);
00433 
00441         void Lock (bool state = true);
00442 
00449         bool IsLocked () {return m_Locked > 0;}
00450 
00458         Object* GetFirstLink (set<Object*>::iterator& i);
00459 
00466         Object* GetNextLink (set<Object*>::iterator& i);
00467 
00473         void Unlink (Object *object);
00474 
00481         virtual void OnUnlink (Object *object);
00482 
00488         void GetPossibleAncestorTypes (set<TypeId>& types);
00489 
00499         static TypeId AddType (string TypeName, Object*(*CreateFunc)(), TypeId id = OtherType);
00500 
00511         static Object* CreateObject (const string& TypeName, Object* parent = NULL);
00512 
00518         static TypeId GetTypeId (const string& Name);
00519 
00525         static string GetTypeName (TypeId Id);
00526 
00534         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00535 
00543         static void AddRule (const string& type1, RuleId rule, const string& type2);
00544 
00551         static  const set<TypeId>& GetRules (TypeId type, RuleId rule);
00552 
00559         static const set<TypeId>& GetRules (const string& type, RuleId rule);
00560 
00568         static void SetCreationLabel (TypeId Id, string Label);
00569 
00575         static const string& GetCreationLabel (TypeId Id);
00576 
00582         static const string& GetCreationLabel (const string& TypeName);
00583 
00587         static SignalId CreateNewSignalId ();
00588 
00589 private:
00590         Object* RealGetDescendant (const gchar* Id);
00591 
00592 private:
00593         gchar* m_Id;
00594         TypeId m_Type;
00595         Object *m_Parent;
00596         map<string, Object*> m_Children; //string is Id of object, so each object must have an Id
00597         set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00598 
00599 private:
00603         int m_Locked;
00604 };
00605 
00606 }
00607 #endif //GCU_OBJECT_H

Generated on Wed Feb 21 17:56:11 2007 for The Gnome Chemistry Utils by  doxygen 1.5.1