The Gnome Chemistry Utils  0.12.11
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-2010 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 2 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 <glib.h>
31 #include <libxml/parser.h>
32 #include <map>
33 #include <set>
34 #include <list>
35 #include <string>
36 #include <stdexcept>
37 #include <gtk/gtk.h>
38 
39 #define square(x) ((x)*(x))
40 
42 namespace gcu
43 {
44 
45 class Dialog;
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 typedef bool (*BuildMenuCb) (Object *target, GtkUIManager *UIManager, Object *object, double x, double y);
108 
121 enum RuleId
122 {
123  RuleMayContain,
124  RuleMustContain,
125  RuleMayBeIn,
126  RuleMustBeIn
127 };
128 
133 typedef unsigned SignalId;
134 
135 class Document;
136 
140 class Object
141 {
142 public:
146  Object (TypeId Id = OtherType);
150  virtual ~Object ();
151 
156  TypeId GetType () const {return m_Type;}
162  void SetId (gchar const *Id);
166  gchar const *GetId () const {return m_Id;}
173  virtual void AddChild (Object* object);
181  Object* GetMolecule () const;
188  Object* GetReaction () const;
196  Object* GetGroup () const;
203  Document* GetDocument () const;
213  Object* GetParentOfType (TypeId Id) const;
220  Object* GetChild (const gchar* Id) const;
227  Object *GetFirstChild (std::map<std::string, Object*>::iterator& i);
228  Object const *GetFirstChild (std::map<std::string, Object*>::const_iterator& i) const;
235  Object *GetNextChild (std::map<std::string, Object*>::iterator& i);
236  Object const *GetNextChild (std::map<std::string, Object*>::const_iterator& i) const;
243  Object* GetDescendant (const gchar* Id) const;
247  Object* GetParent () const {return m_Parent;};
254  void SetParent (Object* Parent);
263  virtual xmlNodePtr Save (xmlDocPtr xml) const;
280  virtual bool Load (xmlNodePtr node);
289  virtual void Move (double x, double y, double z = 0.);
300  virtual void Transform2D (Matrix2D& m, double x, double y);
309  bool SaveChildren (xmlDocPtr xml, xmlNodePtr node) const;
315  void SaveId (xmlNodePtr node) const;
326  xmlNodePtr GetNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
336  xmlNodePtr GetNextNodeByProp (xmlNodePtr node, char const *Property, char const *Id);
346  xmlNodePtr GetNodeByName (xmlNodePtr node, char const *Name);
355  xmlNodePtr GetNextNodeByName (xmlNodePtr node, char const *Name);
359  bool HasChildren () const {return m_Children.size () != 0;}
360 
364  unsigned GetChildrenNumber () const {return m_Children.size ();}
365 
374  virtual Object* GetAtomAt (double x, double y, double z = 0.);
375 
382  virtual bool Build (std::list<Object*>& Children) throw (std::invalid_argument);
383 
389  virtual double GetYAlign ();
390 
404  virtual bool BuildContextualMenu (GtkUIManager *UIManager, Object *object, double x, double y);
405 
412  void EmitSignal (SignalId Signal);
413 
423  virtual bool OnSignal (SignalId Signal, Object *Child);
424 
432  void Lock (bool state = true);
433 
440  bool IsLocked () {return m_Locked > 0;}
441 
449  Object* GetFirstLink (std::set<Object*>::iterator& i);
450 
457  Object* GetNextLink (std::set<Object*>::iterator& i);
458 
464  void Link (Object *object);
465 
471  void Unlink (Object *object);
472 
479  virtual void OnUnlink (Object *object);
480 
486  void GetPossibleAncestorTypes (std::set<TypeId>& types) const;
487 
497  virtual bool SetProperty (unsigned property, char const *value);
498 
505  virtual std::string GetProperty (unsigned property) const;
506 
510  virtual void OnLoaded ();
511 
516  void SetDirty (bool dirty = true);
517 
521  virtual void Clear ();
522 
526  virtual std::string Name ();
527 
532  std::string Identity ();
533 
537  virtual char const *HasPropertiesDialog () const;
538 
542  void ShowPropertiesDialog ();
543 
553  static TypeId AddType (std::string TypeName, Object* (*CreateFunc) (), TypeId id = OtherType);
554 
561  static void AddAlias (TypeId id, std::string TypeName);
562 
573  static Object* CreateObject (const std::string& TypeName, Object* parent = NULL);
574 
580  static TypeId GetTypeId (const std::string& Name);
581 
587  static std::string GetTypeName (TypeId Id);
588 
595  static void AddMenuCallback (TypeId Id, BuildMenuCb cb);
596 
604  static void AddRule (TypeId type1, RuleId rule, TypeId type2);
605 
613  static void AddRule (const std::string& type1, RuleId rule, const std::string& type2);
614 
621  static const std::set<TypeId>& GetRules (TypeId type, RuleId rule);
622 
629  static const std::set<TypeId>& GetRules (const std::string& type, RuleId rule);
630 
638  static void SetCreationLabel (TypeId Id, std::string Label);
639 
645  static const std::string& GetCreationLabel (TypeId Id);
646 
652  static const std::string& GetCreationLabel (const std::string& TypeName);
653 
657  static SignalId CreateNewSignalId ();
658 
659 protected:
663  virtual Dialog *BuildPropertiesDialog ();
664 
665 private:
666  Object* RealGetDescendant (const gchar* Id) const;
667 
668 private:
669  gchar* m_Id;
670  TypeId m_Type;
671  Object *m_Parent;
672  std::map<std::string, Object*> m_Children; //string is Id of object, so each object must have an Id
673  std::set<Object*> m_Links; //objects linked to this but outside of the hierarchy
674 
675 private:
679  int m_Locked;
680 
685 GCU_RO_PROP (bool, Dirty);
686 };
687 
688 }
689 #endif //GCU_OBJECT_H