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 
00415         virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00416 
00423         void EmitSignal (SignalId Signal);
00424 
00434         virtual bool OnSignal (SignalId Signal, Object *Child);
00435 
00443         void Lock (bool state = true);
00444 
00451         bool IsLocked () {return m_Locked > 0;}
00452 
00460         Object* GetFirstLink (set<Object*>::iterator& i);
00461 
00468         Object* GetNextLink (set<Object*>::iterator& i);
00469 
00475         void Unlink (Object *object);
00476 
00483         virtual void OnUnlink (Object *object);
00484 
00490         void GetPossibleAncestorTypes (set<TypeId>& types);
00491 
00501         static TypeId AddType (string TypeName, Object*(*CreateFunc)(), TypeId id = OtherType);
00502 
00513         static Object* CreateObject (const string& TypeName, Object* parent = NULL);
00514 
00520         static TypeId GetTypeId (const string& Name);
00521 
00527         static string GetTypeName (TypeId Id);
00528 
00536         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00537 
00545         static void AddRule (const string& type1, RuleId rule, const string& type2);
00546 
00553         static  const set<TypeId>& GetRules (TypeId type, RuleId rule);
00554 
00561         static const set<TypeId>& GetRules (const string& type, RuleId rule);
00562 
00570         static void SetCreationLabel (TypeId Id, string Label);
00571 
00577         static const string& GetCreationLabel (TypeId Id);
00578 
00584         static const string& GetCreationLabel (const string& TypeName);
00585 
00589         static SignalId CreateNewSignalId ();
00590 
00591 private:
00592         Object* RealGetDescendant (const gchar* Id);
00593 
00594 private:
00595         gchar* m_Id;
00596         TypeId m_Type;
00597         Object *m_Parent;
00598         map<string, Object*> m_Children; //string is Id of object, so each object must have an Id
00599         set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00600 
00601 private:
00605         int m_Locked;
00606 };
00607 
00608 }
00609 #endif //GCU_OBJECT_H

Generated on Tue Mar 18 14:48:19 2008 for The Gnome Chemistry Utils by  doxygen 1.5.5