object.h

00001 // -*- C++ -*-
00002 
00003 /* 
00004  * Gnome Chemistry Utils
00005  * object.h 
00006  *
00007  * Copyright (C) 2002-2007 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 namespace gcu
00042 {
00043 
00068 enum GcuTypeId
00069 {
00070         NoType,
00071         AtomType,
00072         FragmentType,
00073         BondType,
00074         MoleculeType,
00075         ChainType,
00076         CycleType,
00077         ReactantType,
00078         ReactionArrowType,
00079         ReactionOperatorType,
00080         ReactionType,
00081         MesomeryType,
00082         MesomeryArrowType,
00083         DocumentType,
00084         TextType,
00085         OtherType
00086 };
00087 
00092 typedef unsigned TypeId;
00093 
00094 class Object;
00095 
00104 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
00105 
00118 enum RuleId
00119 {
00120         RuleMayContain,
00121         RuleMustContain,
00122         RuleMayBeIn,
00123         RuleMustBeIn
00124 };
00125 
00130 typedef unsigned SignalId;
00131 
00132 class Document;
00133 
00137 class Object
00138 {
00139 public:
00143         Object (TypeId Id = OtherType);
00147         virtual ~Object ();
00148         
00153         TypeId GetType () {return m_Type;}
00159         void SetId (gchar const *Id);
00163         const gchar* GetId () {return m_Id;}
00170         void AddChild (Object* object);
00177         Object* GetMolecule ();
00184         Object* GetReaction ();
00192         Object* GetGroup ();
00199         Document* GetDocument ();
00209         Object* GetParentOfType (TypeId Id);
00216         Object* GetChild (const gchar* Id);
00223         Object* GetFirstChild (std::map<std::string, Object*>::iterator& i);
00230         Object* GetNextChild (std::map<std::string, Object*>::iterator& i);
00237         Object* GetDescendant (const gchar* Id);
00241         Object* GetParent () {return m_Parent;}
00248         void SetParent (Object* Parent);
00257         virtual xmlNodePtr Save (xmlDocPtr xml);
00274         virtual bool Load (xmlNodePtr node);
00283         virtual void Move (double x, double y, double z = 0.);
00294         virtual void Transform2D (Matrix2D& m, double x, double y);
00303         bool SaveChildren (xmlDocPtr xml, xmlNodePtr node);
00309         void SaveId (xmlNodePtr node);
00320         xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00330         xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
00340         xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
00349         xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
00356         virtual void Add (GtkWidget* w);
00362         virtual void Print (GnomePrintContext *pc);
00369         virtual void Update (GtkWidget* w);
00377         virtual void SetSelected (GtkWidget* w, int state);
00381         bool HasChildren () {return m_Children.size () != 0;}
00382 
00386         unsigned GetChildrenNumber () {return m_Children.size ();}
00387 
00396         virtual Object* GetAtomAt (double x, double y, double z = 0.);
00397 
00404         virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
00405 
00411         virtual double GetYAlign ();
00412 
00426         virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
00427 
00434         void EmitSignal (SignalId Signal);
00435 
00445         virtual bool OnSignal (SignalId Signal, Object *Child);
00446 
00454         void Lock (bool state = true);
00455 
00462         bool IsLocked () {return m_Locked > 0;}
00463 
00471         Object* GetFirstLink (std::set<Object*>::iterator& i);
00472 
00479         Object* GetNextLink (std::set<Object*>::iterator& i);
00480 
00486         void Unlink (Object *object);
00487 
00494         virtual void OnUnlink (Object *object);
00495 
00501         void GetPossibleAncestorTypes (std::set<TypeId>& types);
00502 
00512         static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
00513 
00524         static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
00525 
00531         static TypeId GetTypeId (const std::string& Name);
00532 
00538         static std::string GetTypeName (TypeId Id);
00539 
00546         static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
00547 
00555         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
00556 
00564         static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
00565 
00572         static  const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
00573 
00580         static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
00581 
00589         static void SetCreationLabel (TypeId Id, std::string Label);
00590 
00596         static const std::string& GetCreationLabel (TypeId Id);
00597 
00603         static const std::string& GetCreationLabel (const std::string& TypeName);
00604 
00608         static SignalId CreateNewSignalId ();
00609 
00610 private:
00611         Object* RealGetDescendant (const gchar* Id);
00612 
00613 private:
00614         gchar* m_Id;
00615         TypeId m_Type;
00616         Object *m_Parent;
00617         std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
00618         std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
00619 
00620 private:
00624         int m_Locked;
00625 };
00626 
00627 }
00628 #endif //GCU_OBJECT_H

Generated on Thu Dec 20 11:20:45 2007 for The Gnome Chemistry Utils by  doxygen 1.5.4