1 /*
2  * Gnome Chemisty Utils
3  * spectrumdoc.h
4  *
5  * Copyright (C) 2007-2012 Jean Bréfort <jean.brefort@normalesup.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
20  * USA
21  */
22 
23 #ifndef GCU_SPECTRUM_DOC_H
24 #define GCU_SPECTRUM_DOC_H
25 
26 #include <gcu/document.h>
27 #include <gcu/macros.h>
28 #include "printable.h"
29 #include <string>
30 #include <vector>
31 
32 /*!\file*/
33 namespace gcugtk
34 {
35 
36 /*!\enum SpectrumType gcugtk/spectrumdoc.h
37 Represents the list of spectrum types supported (or which should be supported)
38 by the gcu::SpectrumDocument class.
39 */
40 typedef enum {
41 /*!
42 Infrared spectrum.
43 */
44 	GCU_SPECTRUM_INFRARED,
45 /*!
46 Raman spectrum.
47 */
48 	GCU_SPECTRUM_RAMAN,
49 /*!
50 Infrared peaks table.
51 */
52 	GCU_SPECTRUM_INFRARED_PEAK_TABLE,
53 /*!
54 Infrared iterferogram.
55 */
56 	GCU_SPECTRUM_INFRARED_INTERFEROGRAM,
57 /*!
58 Transformed infrared spectrum.
59 */
60 	GCU_SPECTRUM_INFRARED_TRANSFORMED,
61 /*!
62 UV-visible spectrum.
63 */
64 	GCU_SPECTRUM_UV_VISIBLE,
65 /*!
66 NMR spectrum.
67 */
68 	GCU_SPECTRUM_NMR,
69 /*!
70 NMR FID.
71 */
72 	GCU_SPECTRUM_NMR_FID,
73 /*!
74 NMR peaks table.
75 */
76 	GCU_SPECTRUM_NMR_PEAK_TABLE,
77 /*!
78 NMR peaks assignments.
79 */
80 	GCU_SPECTRUM_NMR_PEAK_ASSIGNMENTS,
81 /*!
82 Mass spectrum.
83 */
84 	GCU_SPECTRUM_MASS,
85 /*!
86 Last known value. This value does not represent a valid type, but is
87 equal to the number of supported types.
88 */
89 	GCU_SPECTRUM_MAX
90 } SpectrumType;
91 
92 
93 /*!\enum SpectrumUnitType gcugtk/spectrumdoc.h
94 Represents the list of units supported by the gcu::SpectrumDocument class.
95 */
96 typedef enum {
97 /*!
98 Wave number per cm.
99 */
100 	GCU_SPECTRUM_UNIT_CM_1,
101 /*!
102 Transmittance
103 */
104 	GCU_SPECTRUM_UNIT_TRANSMITTANCE,
105 /*!
106 Absorbance
107 */
108 	GCU_SPECTRUM_UNIT_ABSORBANCE,
109 /*!
110 Part per million.
111 */
112 	GCU_SPECTRUM_UNIT_PPM,
113 /*!
114 Nanometers
115 */
116 	GCU_SPECTRUM_UNIT_NANOMETERS,
117 /*!
118 Micrometers
119 */
120 	GCU_SPECTRUM_UNIT_MICROMETERS,
121 /*!
122 Seconds
123 */
124 	GCU_SPECTRUM_UNIT_SECONDS,
125 /*!
126 Hz.
127 */
128 	GCU_SPECTRUM_UNIT_HZ,
129 /*!
130 M/Z (for mass spectra).
131 */
132 	GCU_SPECTRUM_UNIT_M_Z,
133 /*!
134 Relative abundance.
135 */
136 	GCU_SPECTRUM_UNIT_REL_ABUNDANCE,
137 /*!
138 M/Z (for mass spectra).
139 */
140 	GCU_SPECTRUM_UNIT_RESPONSE_FACTOR,
141 /*!
142 Last known value. This value does not represent a valid unit, but is
143 equal to the number of supported units.
144 */
145 	GCU_SPECTRUM_UNIT_MAX
146 } SpectrumUnitType;
147 
148 /*!\enum SpectrumVarType gcu/spectrumdoc.h
149 Represents the list of varialble types supported by the
150 gcu::SpectrumDocument class.
151 */
152 typedef enum {
153 /*!
154 Independent variable.
155 */
156 	GCU_SPECTRUM_TYPE_INDEPENDENT,
157 /*!
158 Dependent variable.
159 */
160 	GCU_SPECTRUM_TYPE_DEPENDENT,
161 /*!
162 Page number.
163 */
164 	GCU_SPECTRUM_TYPE_PAGE,
165 /*!
166 Last known value. This value does not represent a valid type, but is
167 equal to the number of supported types.
168 */
169 	GCU_SPECTRUM_TYPE_MAX
170 } SpectrumVarType;
171 
172 /*!\enum SpectrumFormatType gcugtk/spectrumdoc.h
173 Represents the list of data formats from the JCAMP-DX specification supported
174 by the gcu::SpectrumDocument class. See the JCAMP-DX specification for
175 more information.
176 */
177 typedef enum {
178 /*!
179 ASCII squeezed difference form.
180 */
181 	GCU_SPECTRUM_FORMAT_ASDF,
182 /*!
183 ASCII free format numeric.
184 */
185 	GCU_SPECTRUM_FORMAT_AFFN,
186 /*!
187 Packed form.
188 */
189 	GCU_SPECTRUM_FORMAT_PAC,
190 /*!
191 Squeezed form.
192 */
193 	GCU_SPECTRUM_FORMAT_SQZ,
194 /*!
195 Difference form.
196 */
197 	GCU_SPECTRUM_FORMAT_DIF,
198 /*!
199 Last known value. This value does not represent a valid type, but is
200 equal to the number of supported formats.
201 */
202 	GCU_SPECTRUM_FORMAT_MAX
203 } SpectrumFormatType;
204 
205 class Application;
206 class SpectrumView;
207 
208 /*!\struct JdxVar gcugtk/spectrumdoc.h
209 Used to store a series of data.
210 */
211 typedef struct  {
212 /*!
213 The name of the series.
214 */
215 	std::string Name;
216 /*!
217 The symbol associated to the series.
218 */
219 	char Symbol;
220 /*!
221 The SpectrumVarType of the data.
222 */
223 	SpectrumVarType Type;
224 /*!
225 The unit stored as a SpectrumUnitType value.
226 */
227 	SpectrumUnitType Unit;
228 /*!
229 The format of the data when loaded from a JCAMP-DX file.
230 */
231 	SpectrumFormatType Format;
232 /*!
233 The values number.
234 */
235 	unsigned NbValues;
236 /*!
237 First vaue in the series.
238 */
239 	double First;
240 /*!
241 Last value in the series
242 */
243 	double Last;
244 /*!
245 Smallest value in  the series.
246 */
247 	double Min;
248 /*!
249 Largest value in the series.
250 */
251 	double Max;
252 /*!
253 Constant value by which each value in the series must be multiplied after
254 loading from a JCAMP-DX file in order to get the real value.
255 */
256 	double Factor;
257 /*!
258 The array of values.
259 */
260 	double *Values;
261 /*!
262 The GogSeries used in the chart. See the GOffice documentation for more
263 information.
264 */
265 	GogSeries *Series;
266 } JdxVar;
267 
268 /*!\class SpectrumDocument gcugtk/spectrumdoc.h
269 The document class used for spectra. This API is still quite unstable and
270 might change in the future.
271 */
272 class SpectrumDocument: public gcu::Document, public Printable
273 {
274 public:
275 /*!
276 Default constructor
277 */
278 	SpectrumDocument ();
279 /*!
280 @param app the application.
281 @param view an optional already existing SpectrumView instance.
282 */
283 	SpectrumDocument (Application *app, SpectrumView *view = NULL);
284 
285 /*!
286 Default destructor
287 */
288 	~SpectrumDocument ();
289 
290 /*!
291 @param uri the uri of the spectrum file.
292 @param mime_type the mime type of the spectrum file.
293 
294 Loads a spaectrum from the provided uri. Default mime type is NULL,
295 "chemical/x-jcamp-dx" is the only one supported at the moment.
296 */
297 	void Load (char const *uri, char const *mime_type = NULL);
298 
299 /*!
300 @param i the rank of the unit to set.
301 
302 Called to change the displayed X axis unit. The values acceptable for the
303 parameter are spectrum type dependent. This is used by the framework after
304 a user made an appropriate request from the graphical interface.
305 */
306 	void OnXUnitChanged (int i);
307 
308 /*!
309 @param i the rank of the unit to set.
310 
311 Called to change the displayed Y axis unit. The values acceptable for the
312 parameter are spectrum type dependent. This is used by the framework after
313 a user made an appropriate request from the graphical interface.
314 */
315 	void OnYUnitChanged (int i);
316 
317 /*!
318 @param inverted whether to invert the X axis scale.
319 
320 Called to change the X axis scale order. This is used by the framework after
321 a user made an appropriate request from the graphical interface.
322 */
323 	void OnXAxisInvert (bool inverted);
324 
325 /*!
326 Used to show or hide the integral curve for an NMR spectrum.
327 */
328 	void OnShowIntegral ();
329 
330 /*!
331 @param btn the clicked GtkButton.
332 
333 Called to transform an FID to the NMR spectrum. The label in the GtkButton
334 might be changed.
335 */
336 	void OnTransformFID (GtkButton *btn);
337 /*!
338 @param property the property id as defined in objprops.h
339 @param value the property value as a string
340 
341 Used when loading to set properties to spectra.
342 @return true if the property could be set, or if the property is not relevant, false otherwise.
343 */
344 	bool SetProperty (unsigned property, char const *value);
345 /*!
346 Called by the application when the document has been loaded.
347 */
348 	bool Loaded () throw (gcu::LoaderError);
349 
350 private:
351 	void LoadJcampDx (char const *data);
352 	void ReadDataLine (char const *data, std::list<double> &l);
353 	void DoPrint (GtkPrintOperation *print, GtkPrintContext *context, int page) const;
354 	GtkWindow *GetGtkWindow ();
355 	void ReadDataTable (std::istream &s, double *x, double *y);
356 	double (*GetConversionFunction (SpectrumUnitType oldu, SpectrumUnitType newu, double &factor, double &offset)) (double, double, double);
357 
358 private:
359 	double *x, *y;
360 	unsigned npoints;
361 	double maxx, maxy, minx, miny;
362 	double firstx, lastx, deltax, firsty;
363 	double xfactor, yfactor;
364 	std::vector <JdxVar> variables;
365 	int X, Xt, Y, R, I, integral, Rt, It, Rp;
366 	double freq;
367 	double offset, refpoint;
368 	GtkWidget *m_XAxisInvertBtn;
369 	guint m_XAxisInvertSgn;
370 
371 /*!\var m_View
372 The SpectrumView instance associated with the document.
373 */
374 /*!\fn GetView()
375 @return the SpectrumView instance associated with the document.
376 */
377 GCU_PROT_PROP (SpectrumView*, View)
378 /*!\fn GetEmpty()
379 @return true if the document does not have any data, false otherwise.
380 */
381 GCU_RO_PROP (bool, Empty)
382 /*!\fn GetSpectrumType()
383 @return the gcu::SpectrumType of the document, or GCU_SPECTRUM_TYPE_MAX.
384 */
385 GCU_RO_PROP (SpectrumType, SpectrumType)
386 /*!\fn GetXUnit()
387 @return the unit of the x-axis as gcu::SpectumUnitType, or
388 GCU_SPECTRUM_UNIT_MAX.
389 */
390 GCU_RO_PROP (SpectrumUnitType, XUnit)
391 /*!\fn GetYUnit()
392 @return the unit of the y-axis as gcu::SpectumUnitType, or
393 GCU_SPECTRUM_UNIT_MAX.
394 */
395 GCU_RO_PROP (SpectrumUnitType, YUnit)
396 /*!\fn GetIntegralVisible()
397 @return true if the integral of an NMR spectrum is visible, false in all
398 other cases.
399 */
400 GCU_RO_PROP (bool, IntegralVisible)
401 };
402 
403 }
404 
405 #endif	//	GCU_SPECTRUM_DOC_H
406