]> git.lyx.org Git - lyx.git/blob - src/insets/insetgraphicsParams.C
ed5980e72a83d0e15f937800b05a84ed93c70015
[lyx.git] / src / insets / insetgraphicsParams.C
1 /* This file is part of
2  * =================================================
3  * 
4  *          LyX, The Document Processor
5  *          Copyright 1995 Matthias Ettrich.
6  *          Copyright 1995-2001 The LyX Team.
7  *
8  *          This file Copyright 2000 Baruch Even
9  * ================================================= */
10
11 #include <config.h> 
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif 
16
17 #include "insetgraphicsParams.h"
18
19 #include "support/translator.h"
20 #include "support/filetools.h"
21 #include "support/lyxlib.h"
22 #include "support/LOstream.h"
23
24 #include "support/LAssert.h"
25
26 namespace {
27
28 /// This variable keeps a tab on whether the translator was set with the
29 /// translations.
30 bool translatorsSet = false;
31
32 /// This is the translator between the Resize enum and corresponding lyx
33 /// file strings.
34 Translator< InsetGraphicsParams::Resize, string >
35 resizeTranslator(InsetGraphicsParams::DEFAULT_SIZE, "default");
36
37 /// This is the translator between the Origin enum and corresponding lyx
38 /// file strings.
39 Translator< InsetGraphicsParams::Origin, string >
40 originTranslator(InsetGraphicsParams::DEFAULT, "default");
41
42 /// This is the translator between the Display enum and corresponding lyx
43 /// file strings.
44 Translator< InsetGraphicsParams::DisplayType, string >
45 displayTranslator(InsetGraphicsParams::MONOCHROME, "monochrome");
46
47 } // namespace anon
48
49
50 InsetGraphicsParams::InsetGraphicsParams()
51 {
52         init();
53
54         // Set translators
55         if (! translatorsSet) {
56                 translatorsSet = true;
57
58                 // Fill the resize translator
59                 resizeTranslator.addPair(DEFAULT_SIZE, "default");
60                 resizeTranslator.addPair(CM, "cm");
61                 resizeTranslator.addPair(INCH, "inch");
62                 resizeTranslator.addPair(PERCENT_PAGE, "percentOfPage");
63                 resizeTranslator.addPair(PERCENT_COLUMN, "percentOfColumn");
64
65                 // Fill the origin translator
66                 originTranslator.addPair(DEFAULT, "default");
67                 originTranslator.addPair(LEFTTOP, "leftTop");
68                 originTranslator.addPair(LEFTCENTER, "leftCenter");
69                 originTranslator.addPair(LEFTBASELINE, "leftBaseLine");
70                 originTranslator.addPair(LEFTBOTTOM, "leftBottom");
71                 originTranslator.addPair(CENTERTOP, "centerTop");
72                 originTranslator.addPair(CENTER, "center");
73                 originTranslator.addPair(CENTERBASELINE, "centerBaseLine");
74                 originTranslator.addPair(CENTERBOTTOM, "centerBottom");
75                 originTranslator.addPair(RIGHTTOP, "rightTop");
76                 originTranslator.addPair(RIGHTCENTER, "rightCenter");
77                 originTranslator.addPair(RIGHTBASELINE, "rightBaseLine");
78                 originTranslator.addPair(RIGHTBOTTOM, "rightBottom");
79                 originTranslator.addPair(REFERENCE_POINT, "referencePoint");
80
81                 // Fill the display translator
82                 displayTranslator.addPair(MONOCHROME, "monochrome");
83                 displayTranslator.addPair(GRAYSCALE, "grayscale");
84                 displayTranslator.addPair(COLOR, "color");
85                 displayTranslator.addPair(NONE, "none");
86         }
87
88 }
89
90
91 InsetGraphicsParams::InsetGraphicsParams(InsetGraphicsParams const & igp)
92 {
93         // I decided to skip the initialization since the copy will overwrite
94         // everything anyway.
95         //    init();
96         copy(igp);
97 }
98
99 InsetGraphicsParams &
100 InsetGraphicsParams::operator=(InsetGraphicsParams const & params)
101 {
102         // Are we assigning the object into itself?
103         if (this == &params)
104                 return * this;
105
106         copy(params);
107         return *this;
108 }
109
110 void InsetGraphicsParams::init()
111 {
112         subcaptionText = filename = string();
113         display = MONOCHROME;
114         subcaption = false;
115         keepAspectRatio = true;
116         widthResize = DEFAULT_SIZE;
117         widthSize = 0.0;
118         heightResize = DEFAULT_SIZE;
119         heightSize = 0.0;
120         rotateOrigin = DEFAULT;
121         rotateAngle = 0.0;
122
123         testInvariant();
124 }
125
126 void InsetGraphicsParams::copy(InsetGraphicsParams const & igp)
127 {
128         filename = igp.filename;
129         display = igp.display;
130         subcaption = igp.subcaption;
131         subcaptionText = igp.subcaptionText;
132         keepAspectRatio = igp.keepAspectRatio;
133         widthResize = igp.widthResize;
134         widthSize = igp.widthSize;
135         heightResize = igp.heightResize;
136         heightSize = igp.heightSize;
137         rotateOrigin = igp.rotateOrigin;
138         rotateAngle = igp.rotateAngle;
139
140         testInvariant();
141 }
142
143 void InsetGraphicsParams::testInvariant() const
144 {
145         // Filename might be empty (when the dialog is first created).
146         // Assert(!filename.empty());
147
148         lyx::Assert(display == COLOR ||
149                display == MONOCHROME ||
150                display == GRAYSCALE ||
151                display == NONE
152               );
153
154         lyx::Assert(widthResize == DEFAULT_SIZE ||
155                widthResize == CM ||
156                widthResize == INCH ||
157                widthResize == PERCENT_PAGE ||
158                widthResize == PERCENT_COLUMN
159               );
160
161         lyx::Assert(heightResize == DEFAULT_SIZE ||
162                heightResize == CM ||
163                heightResize == INCH ||
164                heightResize == PERCENT_PAGE
165               );
166
167         lyx::Assert(widthSize >= 0.0);
168         lyx::Assert(heightSize >= 0.0);
169
170         // Angle is in degrees and ranges -360 < angle < 360
171         // The reason for this is that in latex there is a meaning for the
172         // different angles and they are not necessarliy interchangeable,
173         // it depends on the rotation origin.
174         lyx::Assert(rotateAngle < 360.0);
175         lyx::Assert(rotateAngle > -360.0);
176
177 }
178
179 bool operator==(InsetGraphicsParams const & left,
180                 InsetGraphicsParams const & right)
181 {
182         if (left.filename == right.filename &&
183                 left.display == right.display &&
184                 left.subcaption == right.subcaption &&
185                 left.subcaptionText == right.subcaptionText &&
186                 left.keepAspectRatio == right.keepAspectRatio &&
187                 left.widthResize == right.widthResize &&
188                 left.widthSize == right.widthSize &&
189                 left.heightResize == right.heightResize &&
190                 left.heightSize == right.heightSize &&
191                 left.rotateOrigin == right.rotateOrigin &&
192                 lyx::float_equal(left.rotateAngle, right.rotateAngle, 0.001)
193            )
194                 return true;
195
196         return false;
197 }
198
199 bool operator!=(InsetGraphicsParams const & left,
200                 InsetGraphicsParams const & right)
201 {
202         return  !(left == right);
203 }
204
205
206 namespace {
207
208 void writeResize(ostream & os, string const & key,
209                         InsetGraphicsParams::Resize resize, double size)
210 {
211         os << ' ' << key << "Resize ";
212
213         os << resizeTranslator.find(resize);
214         os << ' ' << key << ' ' << size << '\n';
215 }
216
217 void writeOrigin(ostream & os,
218                         InsetGraphicsParams::Origin origin)
219 {
220         os << " rotateOrigin " << originTranslator.find(origin);
221         os << '\n';
222 }
223
224 } // namespace anon
225
226
227 void InsetGraphicsParams::Write(Buffer const * buf, ostream & os) const
228 {
229         // If there is no filename, write nothing for it.
230         if (! filename.empty()) {
231                 os << "filename "
232                 << MakeRelPath(filename, OnlyPath(buf->fileName()))
233                 << '\n';
234         }
235
236         // Save the display type
237         os << " display " << displayTranslator.find(display) << '\n';
238
239         // Save the subcaption status
240         if (subcaption)
241                 os << " subcaption";
242
243         if (! subcaptionText.empty())
244                 os << " subcaptionText \"" << subcaptionText << '\"' << '\n';
245
246         writeResize(os, "width", widthResize, widthSize);
247         writeResize(os, "height", heightResize, heightSize);
248
249         writeOrigin(os, rotateOrigin);
250         if (lyx::float_equal(rotateAngle, 0.0, 0.001))
251                 os << " rotateAngle " << rotateAngle << '\n';
252 }
253
254
255 namespace {
256
257 void readResize(InsetGraphicsParams * igp, bool height,
258                        string const & token)
259 {
260         InsetGraphicsParams::Resize resize = InsetGraphicsParams::DEFAULT_SIZE;
261
262         resize = resizeTranslator.find(token);
263
264         if (height)
265                 igp->heightResize = resize;
266         else
267                 igp->widthResize = resize;
268 }
269
270
271 void readOrigin(InsetGraphicsParams * igp, string const & token)
272 {
273         // TODO: complete this function.
274         igp->rotateOrigin = originTranslator.find(token);
275 }
276
277 } // namespace anon
278
279
280 bool InsetGraphicsParams::Read(Buffer const * buf, LyXLex & lex,
281                                string const& token)
282 {
283         if (token == "filename") {
284                 lex.next();
285                 filename = lex.GetString();
286
287                 if (!filename.empty()) {
288                         // Make the filename with absolute directory.
289                         filename = MakeAbsPath(filename, OnlyPath(buf->fileName()));
290                 }
291         } else if (token == "display") {
292                 lex.next();
293                 string const type = lex.GetString();
294
295                 display = displayTranslator.find(type);
296         } else if (token == "subcaption") {
297                 subcaption = true;
298         } else if (token == "subcaptionText") {
299                 lex.next();
300                 subcaptionText = lex.GetString();
301         } else if (token == "widthResize") {
302                 lex.next();
303                 string const token = lex.GetString();
304
305                 readResize(this, false, token);
306         } else if (token == "width") {
307                 lex.next();
308                 widthSize = lex.GetFloat();
309         } else if (token == "heightResize") {
310                 lex.next();
311                 string const token = lex.GetString();
312
313                 readResize(this, true, token);
314         } else if (token == "height") {
315                 lex.next();
316                 heightSize = lex.GetFloat();
317         } else if (token == "rotateOrigin") {
318                 lex.next();
319                 string const token = lex.GetString();
320
321                 readOrigin(this, token);
322         } else if (token == "rotateAngle") {
323                 lex.next();
324                 rotateAngle = lex.GetFloat();
325         } else {
326                 // If it's none of the above, its not ours.
327                 return false;
328         }
329
330         return true;
331 }