The Gnome Chemistry Utils  0.14.0
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);
288  virtual xmlNodePtr Save (xmlDocPtr xml) const;
305  virtual bool Load (xmlNodePtr node);
314  virtual bool GetCoords (double *x, double *y, double *z = NULL) const;
323  virtual void Move (double x, double y, double z = 0.);
334  virtual void Transform2D (Matrix2D& m, double x, double y);
343  bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
349  void SaveId (xmlNodePtr node) const;
360  xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
370  xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
380  xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
389  xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
393  bool HasChildren () const {return m_Children.size () != 0;}
394 
398  unsigned GetChildrenNumber () const {return m_Children.size ();}
399 
408  virtual Object* GetAtomAt (double x, double y, double z = 0.);
409 
416  virtual bool Build (std::set < Object * > const &Children) throw (std::invalid_argument);
417 
423  virtual double GetYAlign ();
424 
438  virtual bool BuildContextualMenu (UIManager *uim, Object *object, double x, double y);
439 
446  void EmitSignal (SignalId Signal);
447 
457  virtual bool OnSignal (SignalId Signal, Object *Child);
458 
466  void Lock (bool state = true);
467 
474  bool IsLocked () {return m_Locked > 0;}
475 
483  Object* GetFirstLink (std::set<Object*>::iterator& i);
484 
491  Object* GetNextLink (std::set<Object*>::iterator& i);
492 
498  void Link (Object *object);
499 
505  void Unlink (Object *object);
506 
513  virtual void OnUnlink (Object *object);
514 
520  void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
521 
531  virtual bool SetProperty (unsigned property, char const *value);
532 
539  virtual std::string GetProperty (unsigned property) const;
540 
544  virtual void OnLoaded ();
545 
550  void SetDirty (bool dirty = true);
551 
555  virtual void Clear ();
556 
560  virtual std::string Name ();
561 
566  std::string Identity ();
567 
571  virtual char const *HasPropertiesDialog () const;
572 
576  virtual bool CanSelect () const {return true;}
577 
582  virtual void NotifyEmpty () {;}
583 
587  void ShowPropertiesDialog ();
588 
601  static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
602 
609  static void AddAlias (TypeId id, std::string TypeName);
610 
623  static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
624 
630  static TypeId GetTypeId (const std::string& Name);
631 
637  static std::string GetTypeName (TypeId Id);
638 
647  static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
648 
658  static void AddRule (TypeId type1, RuleId rule, TypeId type2);
659 
669  static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
670 
679  static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
680 
689  static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
690 
700  static void SetCreationLabel (TypeId Id, std::string Label);
701 
709  static const std::string& GetCreationLabel (TypeId Id);
710 
718  static const std::string& GetCreationLabel (const std::string& TypeName);
719 
723  static SignalId CreateNewSignalId ();
724 
725 protected:
729  virtual Dialog *BuildPropertiesDialog ();
730 
731 private:
732  Object* RealGetDescendant (const gchar* Id) const;
733 
734 private:
735  char* m_Id;
736  TypeId m_Type;
737  Object *m_Parent;
738  std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
739  std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
740  TypeDesc const *m_TypeDesc;
741 
742 private:
746  int m_Locked;
747 
752 GCU_RO_PROP (bool, Dirty);
753 };
754 
755 }
756 #endif //GCU_OBJECT_H