1 // -*- C++ -*-
2 
3 /*
4  * GChemPaint library
5  * bond.h
6  *
7  * Copyright (C) 2001-2012 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 GCHEMPAINT_BOND_H
26 #define GCHEMPAINT_BOND_H
27 
28 /*!\file*/
29 
30 #include <gccv/item-client.h>
31 #include <gcu/bond.h>
32 #include <list>
33 
34 namespace gcu {
35 class UIManager;
36 }
37 
38 namespace gcp {
39 
40 /*!\enum BondType gcp/bond.h
41 The bond types recognized in GChemPaint. Possible values are:
42  - NormalBondType: normal bonds,
43  - UpBondType: wedge bond,
44  - DownBondType: hash bond,
45  - ForeBondType: large bond,
46  - UndeterminedBondType: squiggled bond.
47  - NewmanBondType: the bond displayed as a circle in a Newman projection.
48 */
49 typedef enum
50 {
51 	NormalBondType,
52 	UpBondType,
53 	DownBondType,
54 	ForeBondType,
55 	UndeterminedBondType,
56 	NewmanBondType
57 } BondType;
58 
59 /*!\enum DoubleBondPosition gcp/bond.h
60 Used to set the second line position of a double bond.
61  - DoubleBondAuto: automatic, GChemPaint places it on the side it thinks the best.
62  - DoubleBondCenter: the two lines are symetric relative to the line joining the two ends.
63  - DoubleBondLeft: the second line is on the left side when viewing from the start.
64  - DoubleBondRight: the second line is on the right side when viewing from the start.
65 
66 */
67 typedef enum
68 {
69 	DoubleBondAuto,
70 	DoubleBondCenter,
71 	DoubleBondLeft,
72 	DoubleBondRight
73 } DoubleBondPosition;
74 
75 /*!\struct BondCrossing gcp/bond.h
76 This structure is used for crossing bonds, so that the bond behind the other is
77 partially hidden.
78 */
79 typedef struct {
80 /*!
81 The position of the crossing alog the bond: 0 if at start, 1 at end.
82 */
83 	double a;
84 /*!
85 Whether the bond is above the other or not.
86 */
87 	bool is_before;
88 } BondCrossing;
89 
90 class Atom;
91 class WidgetData;
92 
93 /*!\class Bond gcp/bond.h
94 This class is used to represent bonds.
95 */
96 
97 class Bond: public gcu::Bond, public gccv::ItemClient
98 {
99 public:
100 /*!
101 The default constructor.
102 */
103 	Bond ();
104 /*!
105 @param first the first bonded atom.
106 @param last the last bonded atom.
107 @param order the bond order
108 
109 Constructs a bond given its two terminal atoms and its order.
110 */
111 	Bond (Atom* first, Atom* last, unsigned char order);
112 /*!
113 The destructor.
114 */
115 	virtual ~Bond ();
116 
117 /*!
118 @param x the x coordinate
119 @param y the y coordinate
120 @param z the z coordinate
121 
122 @return a pointer to a bonded atom at or near position defined by the
123 coordinates passed as parameters.
124 */
125 	Object* GetAtomAt (double x, double y, double z = 0.);
126 /*!
127 @return the type of the bond.
128 */
GetType()129 	BondType GetType() const {return m_type;}
130 /*!
131 @param type the new bond type.
132 
133 Sets the bond type.
134 */
135 	void SetType (BondType type);
136 /*!
137 @param pAtom one of the bonded atoms
138 
139 @return the angle (0 to 306°) that the bond makes from the horizontal when
140 starting from \a pAtom.
141 */
142 	double GetAngle2D (Atom* pAtom);
143 /*!
144 @param pCycle a cycle containing the bond.
145 
146 Notifies the bond it is in the cycle.
147 */
148 	void AddCycle (gcu::Cycle* pCycle);
149 /*!
150 @param pCycle a cycle.
151 
152 Notifies the bond that it is not anymore in the cycle.
153 */
154 	void RemoveCycle (gcu::Cycle* pCycle);
155 /*!
156 Clears the list of the cycles containing the bond.
157 */
158 	void RemoveAllCycles ();
159 /*!
160 @param Num the index of the line representing a, possibly, multiple bond.
161 @param x1 where to store the first x coordinate.
162 @param y1 where to store the first y coordinate.
163 @param x2 where to store the second x coordinate.
164 @param y2 where to store the second y coordinate.
165 
166 Retrievers the coordinates of one of the lines representing the bond. Num must
167 be lower than the bond order to succeed.
168 @return true on success, false otherwise.
169 */
170 	bool GetLine2DCoords (unsigned Num, double* x1, double* y1, double* x2, double* y2);
171 /*!
172 @param xml the xmlDoc used to save the document.
173 @param node a pointer to the xmlNode to which this Bond is serialized.
174 
175 Saves the GChemPaint Bond class specific properties, such as the bond type
176 (see gcp::BondType for more information).
177 */
178 	virtual bool SaveNode (xmlDocPtr xml, xmlNodePtr node) const;
179 /*!
180 @param node a pointer to the xmlNode containing the serialized Bond.
181 
182 Loads properties specific to GChemPaint bonds.
183 */
184 	bool LoadNode (xmlNodePtr node);
185 /*!
186 @param x the x component of the transation vector.
187 @param y the y component of the transation vector.
188 @param z the z component of the transation vector.
189 
190 Used to move a bond. Just tells the bond it has been moved and that it's
191 coordinates need to be reevaluated from the new atomic positions.
192 */
193 	void Move (double x, double y, double z = 0);
194 /*!
195 @param m the Matrix2D of the transformation.
196 @param x the x component of the center of the transformation.
197 @param y the y component of the center of the transformation.
198 
199 Used to move and/or transform a bond. Just tells the bond it has been moved
200 and /or rotated and that it's coordinates need to be reevaluated from the new
201 atomic positions.
202 */
203 	void Transform2D (gcu::Matrix2D& m, double x, double y);
204 /*!
205 @param x the x coordinate.
206 @param y the x coordinate.
207 
208 Evaluates the distance from the point with coordinates \a x and \a y to the bond.
209 The line segment joining the two atoms is used whatever the type and the order
210 of the bond.
211 @return the calculated distance in pm.
212 */
213 	double GetDist (double x, double y);
214 /*!
215 Tells that the bond has changed, and that the items representing it should be
216 updated accordingly.
217 */
218 	void SetDirty ();
219 /*!
220 Exchanges the start and end atoms.
221 */
222 	void Revert ();
223 /*!
224 @param n the bond order increment. If not given, the default is 1.
225 
226 Tries to increment the bond order by n units. If something goes wrong, the
227 bond order is set to 1.
228 */
229 	void IncOrder (int n = 1);
230 /*!
231 Used to add a representation of the bond in the view.
232 */
233 	void AddItem ();
234 /*!
235 Used to update the representation of the bond in the view.
236 */
237 	void UpdateItem ();
238 /*!
239 @param state the selection state of the bond.
240 
241 Used to set the selection state of the bond inside the widget.
242 The values of state might be gcp::SelStateUnselected, gcp::SelStateSelected,
243 gcp::SelStateUpdating, or gcp::SelStateErasing.
244 */
245 	void SetSelected (int state);
246 /*!
247 Used to retrieve the y coordinate for alignment.
248 @return y coordinate of the bond center.
249 */
250 	double GetYAlign ();
251 /*!
252 @param pBond a bond which might cross.
253 
254 The lines representing two bonds might cross. This method detects this
255 condition.
256 @return true if bonds cross, false otherwise.
257 */
258 	bool IsCrossing (Bond *pBond);
259 /*!
260 @param UIManager: the gcu::UIManager to populate.
261 @param object the atom on which occured the mouse click.
262 @param x x coordinate of the mouse click.
263 @param y y coordinate of the mouse click.
264 
265 This method is called to build a contextual menu for the bond.
266 */
267 	bool BuildContextualMenu (gcu::UIManager *UIManager, Object *object, double x, double y);
268 /*!
269 Move the bond to the lowest position. It has an effect only for crossing bonds.
270 */
271 	void MoveToBack ();
272 /*!
273 Brings the bond to front. It has an effect only for crossing bonds.
274 */
275 	void BringToFront ();
276 /*!
277 @param property the identity of the property as defined in objprops.h.
278 
279 Used by the gcu::Loader mechanism to retrieve properties of bonds.
280 @return the value of the property as a string.
281 */
282 	std::string GetProperty (unsigned property) const;
283 /*!
284 @param property the property id as defined in objprops.h
285 @param value the property value as a string
286 
287 Used when loading to set properties for the bond. This method supports
288 GCU_PROP_BOND_TYPE and calls gcu::Bond::SetProperty() for other properties.
289 @return true if the property could be set, or if the property is not relevant, false otherwise.
290 */
291 	bool SetProperty (unsigned property, char const *value);
292 
293 /*!
294 @param x the x coordinate to adjust
295 @param y the y coordinate to adjust
296 
297 On entering coordinates are considered relative to the bond axis. This method
298 adjust them so that they are put farer from the bond taking line width or
299 multiple bonds line into account.
300 */
301 	void AdjustPosition (double &x, double &y);
302 
303 /*!
304 This method should be called when a bond has been fully loaded.
305 */
306 	void OnLoaded ();
307 
308 private:
309 //	GnomeCanvasPathDef* BuildPathDef (WidgetData* pData);
310 //	GnomeCanvasPathDef* BuildCrossingPathDef (WidgetData* pData);
311 	BondType m_type;
312 	double m_coords[16];//coordinates of the lines used to represent the bond in the canvas
313 	bool m_CoordsCalc; //true if m_coords have been calculated, false else
314 	std::map<Bond*, BondCrossing> m_Crossing;
315 	int m_level; // to know which bond should be considered front
316 
317 GCU_PROP (DoubleBondPosition, DoublePosition)
318 };
319 
320 }	//	namespace gcp
321 
322 #endif // GCHEMPAINT_BOND_H
323