# 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-2008 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
    00069 enum GcuTypeId
    00070 {
    00071         NoType,
    00072         AtomType,
    00073         FragmentType,
    00074         BondType,
    00075         MoleculeType,
    00076         ChainType,
    00077         CycleType,
    00078         ReactantType,
    00079         ReactionArrowType,
    00080         ReactionOperatorType,
    00081         ReactionType,
    00082         MesomeryType,
    00083         MesomeryArrowType,
    00084         DocumentType,
    00085         TextType,
    00086         OtherType
    00087 };
    00088
    00093 typedef unsigned TypeId;
    00094
    00095 class Object;
    00096
    00105 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
    00106
    00119 enum RuleId
    00120 {
    00121         RuleMayContain,
    00122         RuleMustContain,
    00123         RuleMayBeIn,
    00124         RuleMustBeIn
    00125 };
    00126
    00131 typedef unsigned SignalId;
    00132
    00133 class Document;
    00134
    00138 class Object
    00139 {
    00140 public:
    00144         Object (TypeId Id = OtherType);
    00148         virtual ~Object ();
    00149
    00154         TypeId GetType () const {return m_Type;}
    00160         void SetId (gchar const *Id);
    00164         gchar const *GetId () const {return m_Id;}
    00171         virtual void AddChild (Object* object);
    00179         Object* GetMolecule () const;
    00186         Object* GetReaction () const;
    00194         Object* GetGroup () const;
    00201         Document* GetDocument () const;
    00211         Object* GetParentOfType (TypeId Id) const;
    00218         Object* GetChild (const gchar* Id) const;
    00225         Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
    00226         Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
    00233         Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
    00234         Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
    00241         Object* GetDescendant (const gchar* Id) const;
    00245         Object* GetParent () const {return m_Parent;};
    00252         void SetParent (Object* Parent);
    00261         virtual xmlNodePtr Save (xmlDocPtr xml) const;
    00278         virtual bool Load (xmlNodePtr node);
    00287         virtual void Move (double x, double y, double z = 0.);
    00298         virtual void Transform2D (Matrix2D& m, double x, double y);
    00307         bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
    00313         void SaveId (xmlNodePtr node) const;
    00324         xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
    00334         xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
    00344         xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
    00353         xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
    00360         virtual void Add (GtkWidget* w) const;
    00367         virtual void Update (GtkWidget* w) const;
    00375         virtual void SetSelected (GtkWidget* w, int state);
    00379         bool HasChildren () const {return m_Children.size () != 0;}
    00380
    00384         unsigned GetChildrenNumber () const {return m_Children.size ();}
    00385
    00394         virtual Object* GetAtomAt (double x, double y, double z = 0.);
    00395
    00402         virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
    00403
    00409         virtual double GetYAlign ();
    00410
    00424         virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
    00425
    00432         void EmitSignal (SignalId Signal);
    00433
    00443         virtual bool OnSignal (SignalId Signal, Object *Child);
    00444
    00452         void Lock (bool state = true);
    00453
    00460         bool IsLocked () {return m_Locked > 0;}
    00461
    00469         Object* GetFirstLink (std::set<Object*>::iterator& i);
    00470
    00477         Object* GetNextLink (std::set<Object*>::iterator& i);
    00478
    00484         void Unlink (Object *object);
    00485
    00492         virtual void OnUnlink (Object *object);
    00493
    00499         void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
    00500
    00510         virtual bool SetProperty (unsigned property, char const *value);
    00511
    00518         virtual std::string GetProperty (unsigned property) const;
    00519
    00523         virtual void OnLoaded ();
    00524
    00529         void SetDirty (bool dirty = true);
    00530
    00540         static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
    00541
    00552         static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
    00553
    00559         static TypeId GetTypeId (const std::string& Name);
    00560
    00566         static std::string GetTypeName (TypeId Id);
    00567
    00574         static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
    00575
    00583         static void AddRule (TypeId type1, RuleId rule, TypeId type2);
    00584
    00592         static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
    00593
    00600         static  const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
    00601
    00608         static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
    00609
    00617         static void SetCreationLabel (TypeId Id, std::string Label);
    00618
    00624         static const std::string& GetCreationLabel (TypeId Id);
    00625
    00631         static const std::string& GetCreationLabel (const std::string& TypeName);
    00632
    00636         static SignalId CreateNewSignalId ();
    00637
    00638 private:
    00639         Object* RealGetDescendant (const gchar* Id) const;
    00640
    00641 private:
    00642         gchar* m_Id;
    00643         TypeId m_Type;
    00644         Object *m_Parent;
    00645         std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
    00646         std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
    00647
    00648 private:
    00652         int m_Locked;
    00653
    00658 GCU_RO_PROP (bool, Dirty);
    00659 };
    00660
    00661 }
    00662 #endif //GCU_OBJECT_H