The Gnome Chemistry Utils  0.15.1
object.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * object.h
6  *
7  * Copyright (C) 2002-2011 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22  * USA
23  */
24 
25 #ifndef GCU_OBJECT_H
26 #define GCU_OBJECT_H
27 
28 #include "macros.h"
29 #include "matrix2d.h"
30 #include <libxml/parser.h>
31 #include <map>
32 #include <set>
33 #include <list>
34 #include <string>
35 #include <stdexcept>
36 
37 #define square(x) ((x)*(x))
38 
40 namespace gcu
41 {
42 
43 class Dialog;
44 class Application;
45 class UIManager;
46 
72 {
73  NoType,
74  AtomType,
75  FragmentType,
76  BondType,
77  MoleculeType,
78  ChainType,
79  CycleType,
80  ReactantType,
81  ReactionArrowType,
82  ReactionOperatorType,
83  ReactionType,
84  MesomeryType,
85  MesomeryArrowType,
86  DocumentType,
87  TextType,
88  OtherType
89 };
90 
95 typedef unsigned TypeId;
96 
97 class Object;
98 
107 // FIXME: create a class for UIManager
108 typedef bool (*BuildMenuCb) (Object *target, UIManager *uim, Object *object, double x, double y);
109 
110 class TypeDesc
111 {
112 public:
113  TypeDesc ();
114 
115  TypeId Id;
116  Object* (*Create) ();
117  std::set <TypeId> PossibleChildren;
118  std::set <TypeId> PossibleParents;
119  std::set <TypeId> RequiredChildren;
120  std::set <TypeId> RequiredParents;
121  std::string CreationLabel;
122  std::list <BuildMenuCb> MenuCbs;
123 };
124 
125 class Object;
126 
139 enum RuleId
140 {
141  RuleMayContain,
142  RuleMustContain,
143  RuleMayBeIn,
144  RuleMustBeIn
145 };
146 
151 typedef unsigned SignalId;
152 
153 class Document;
154 
158 class Object
159 {
160 friend class Application;
161 public:
165  Object (TypeId Id = OtherType);
169  virtual ~Object ();
170 
175  TypeId GetType () const {return m_Type;}
181  void SetId (gchar const *Id);
185  char const *GetId () const {return m_Id;}
192  virtual void AddChild (Object* object);
200  Object* GetMolecule () const;
207  Object* GetReaction () const;
215  Object* GetGroup () const;
222  Document* GetDocument () const;
228  Application* GetApplication () const;
238  Object* GetParentOfType (TypeId Id) const;
245  Object* GetChild (const gchar* Id) const;
252  Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
253  Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
260  Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
261  Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
268  Object* GetDescendant (const char* Id) const;
272  Object* GetParent () const {return m_Parent;};
279  void SetParent (Object* Parent);
283  virtual void ParentChanged ();
292  virtual xmlNodePtr Save (xmlDocPtr xml) const;
309  virtual bool Load (xmlNodePtr node);
318  virtual bool GetCoords (double *x, double *y, double *z = NULL) const;
327  virtual void Move (double x, double y, double z = 0.);
338  virtual void Transform2D (Matrix2D& m, double x, double y);
347  bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
353  void SaveId (xmlNodePtr node) const;
364  xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
374  xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
384  xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
393  xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
397  bool HasChildren () const {return m_Children.size () != 0;}
398 
402  unsigned GetChildrenNumber () const {return m_Children.size ();}
403 
412  virtual Object* GetAtomAt (double x, double y, double z = 0.);
413 
420  virtual bool Build (std::set < Object * > const &Children) throw (std::invalid_argument);
421 
427  virtual double GetYAlign ();
428 
442  virtual bool BuildContextualMenu (UIManager *uim, Object *object, double x, double y);
443 
450  void EmitSignal (SignalId Signal);
451 
461  virtual bool OnSignal (SignalId Signal, Object *Child);
462 
470  void Lock (bool state = true);
471 
478  bool IsLocked () {return m_Locked > 0;}
479 
487  Object* GetFirstLink (std::set<Object*>::iterator& i);
488 
495  Object* GetNextLink (std::set<Object*>::iterator& i);
496 
502  void Link (Object *object);
503 
509  void Unlink (Object *object);
510 
517  virtual void OnUnlink (Object *object);
518 
524  void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
525 
535  virtual bool SetProperty (unsigned property, char const *value);
536 
543  virtual std::string GetProperty (unsigned property) const;
544 
548  virtual void OnLoaded ();
549 
554  void SetDirty (bool dirty = true);
555 
559  virtual void Clear ();
560 
564  virtual std::string Name ();
565 
570  std::string Identity ();
571 
575  virtual char const *HasPropertiesDialog () const;
576 
580  virtual bool CanSelect () const {return true;}
581 
586  virtual void NotifyEmpty () {;}
587 
591  void ShowPropertiesDialog ();
592 
605  static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
606 
613  static void AddAlias (TypeId id, std::string TypeName);
614 
627  static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
628 
634  static TypeId GetTypeId (const std::string& Name);
635 
641  static std::string GetTypeName (TypeId Id);
642 
651  static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
652 
662  static void AddRule (TypeId type1, RuleId rule, TypeId type2);
663 
673  static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
674 
683  static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
684 
693  static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
694 
704  static void SetCreationLabel (TypeId Id, std::string Label);
705 
713  static const std::string& GetCreationLabel (TypeId Id);
714 
722  static const std::string& GetCreationLabel (const std::string& TypeName);
723 
727  static SignalId CreateNewSignalId ();
728 
729 protected:
733  virtual Dialog *BuildPropertiesDialog ();
734 
735 private:
736  Object* RealGetDescendant (const gchar* Id) const;
737 
738 private:
739  char* m_Id;
740  TypeId m_Type;
741  Object *m_Parent;
742  std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
743  std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
744  TypeDesc const *m_TypeDesc;
745 
746 private:
750  int m_Locked;
751 
756 GCU_RO_PROP (bool, Dirty);
757 };
758 
759 }
760 #endif //GCU_OBJECT_H
bool(* BuildMenuCb)(Object *target, UIManager *uim, Object *object, double x, double y)
Definition: object.h:108
virtual xmlNodePtr Save(xmlDocPtr xml) const
void EmitSignal(SignalId Signal)
Object * GetReaction() const
static TypeId GetTypeId(const std::string &Name)
Document * GetDocument() const
#define GCU_RO_PROP(type, member)
Definition: macros.h:142
xmlNodePtr GetNextNodeByName(xmlNodePtr node, char const *Name)
virtual void AddChild(Object *object)
xmlNodePtr GetNodeByName(xmlNodePtr node, char const *Name)
Object * GetParentOfType(TypeId Id) const
virtual bool BuildContextualMenu(UIManager *uim, Object *object, double x, double y)
Object * GetFirstChild(std::map< std::string, Object * >::iterator &i)
static void SetCreationLabel(TypeId Id, std::string Label)
RuleId
Definition: object.h:139
void SaveId(xmlNodePtr node) const
static const std::set< TypeId > & GetRules(TypeId type, RuleId rule)
virtual bool Build(std::set< Object * > const &Children)
xmlNodePtr GetNodeByProp(xmlNodePtr node, char const *Property, char const *Id)
void Lock(bool state=true)
unsigned TypeId
Definition: object.h:95
bool HasChildren() const
Definition: object.h:397
virtual Dialog * BuildPropertiesDialog()
virtual void OnUnlink(Object *object)
Object * GetGroup() const
TypeId GetType() const
Definition: object.h:175
xmlNodePtr GetNextNodeByProp(xmlNodePtr node, char const *Property, char const *Id)
virtual void NotifyEmpty()
Definition: object.h:586
virtual void OnLoaded()
virtual double GetYAlign()
static const std::string & GetCreationLabel(TypeId Id)
Object(TypeId Id=OtherType)
base class for a user interface manager
Definition: ui-manager.h:33
Object * GetMolecule() const
void ShowPropertiesDialog()
void Link(Object *object)
static TypeId AddType(std::string TypeName, Object *(*CreateFunc)(), TypeId id=OtherType)
void SetParent(Object *Parent)
virtual bool Load(xmlNodePtr node)
virtual void Transform2D(Matrix2D &m, double x, double y)
bool IsLocked()
Definition: object.h:478
char const * GetId() const
Definition: object.h:185
static void AddAlias(TypeId id, std::string TypeName)
void SetDirty(bool dirty=true)
virtual bool SetProperty(unsigned property, char const *value)
unsigned SignalId
Definition: object.h:151
virtual Object * GetAtomAt(double x, double y, double z=0.)
Object * GetDescendant(const char *Id) const
virtual void Move(double x, double y, double z=0.)
Object * GetParent() const
Definition: object.h:272
static SignalId CreateNewSignalId()
Object * GetFirstLink(std::set< Object * >::iterator &i)
static Object * CreateObject(const std::string &TypeName, Object *parent=NULL)
static void AddMenuCallback(TypeId Id, BuildMenuCb cb)
void Unlink(Object *object)
bool SaveChildren(xmlDocPtr xml, xmlNodePtr node) const
virtual bool OnSignal(SignalId Signal, Object *Child)
virtual std::string GetProperty(unsigned property) const
static std::string GetTypeName(TypeId Id)
Object * GetNextLink(std::set< Object * >::iterator &i)
virtual std::string Name()
virtual void ParentChanged()
virtual bool CanSelect() const
Definition: object.h:580
void SetId(gchar const *Id)
unsigned GetChildrenNumber() const
Definition: object.h:402
Object * GetNextChild(std::map< std::string, Object * >::iterator &i)
Object * GetChild(const gchar *Id) const
virtual void Clear()
static void AddRule(TypeId type1, RuleId rule, TypeId type2)
GcuTypeId
Definition: object.h:71
virtual bool GetCoords(double *x, double *y, double *z=NULL) const
void GetPossibleAncestorTypes(std::set< TypeId > &types) const
std::string Identity()
virtual char const * HasPropertiesDialog() const
virtual ~Object()
Application * GetApplication() const