~ubuntu-branches/debian/sid/scribus/sid

« back to all changes in this revision

Viewing changes to scribus/units.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Oleksandr Moskalenko
  • Date: 2008-07-02 13:42:07 UTC
  • mto: (4.1.1 sid) (1.2.1) (20.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080702134207-h9h384v0wxjmaf8y
Tags: upstream-1.3.3.12.dfsg
ImportĀ upstreamĀ versionĀ 1.3.3.12.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
 
3
to the COPYING file provided with the program. Following this notice may exist
 
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
 
5
for which a new license (GPL+exception) is in place.
 
6
*/
 
7
/***************************************************************************
 
8
    begin                : Jan 2005
 
9
    copyright            : (C) 2005 by Craig Bradney
 
10
    email                : cbradney@zip.com.au
 
11
 ***************************************************************************/
 
12
 
 
13
/***************************************************************************
 
14
 *                                                                         *
 
15
 *   This program is free software; you can redistribute it and/or modify  *
 
16
 *   it under the terms of the GNU General Public License as published by  *
 
17
 *   the Free Software Foundation; either version 2 of the License, or     *
 
18
 *   (at your option) any later version.                                   *
 
19
 *                                                                         *
 
20
 ***************************************************************************/
 
21
 
 
22
#include <qstring.h>
 
23
#include <qobject.h>
 
24
#include "units.h"
 
25
 
 
26
/*!
 
27
 * @brief Returns the ratio to points for the selected unit of measure. Ratios are for: PT, MM, IN, P, CM, C
 
28
 */
 
29
const double unitGetRatioFromIndex(const int index)
 
30
{
 
31
        //PT, MM, IN, P, CM, C (Cicero)
 
32
        //NOTE: Calling functions that divide by this value will crash on divide by 0. They shouldnt be getting
 
33
        // a zero value if they are accessing here with a correct index.
 
34
        if (index<UNITMIN || index>UNITMAX)
 
35
                return 0;
 
36
        double ratio[] = { 1.0, 25.4/72.0, 1.0/72.0, 1.0/12.0, 2.54/72.0, 25.4/72.0/4.512 };
 
37
        return ratio[index];
 
38
}
 
39
 
 
40
/*!
 
41
 * @brief Strip the text from a value and return the double value for the unit
 
42
 */
 
43
const double unitValueFromString(const QString& value)
 
44
{
 
45
        QString lowerValue = value.lower();
 
46
        QString dbl = "0.0";
 
47
        if (lowerValue.find("pt") != -1)
 
48
        {
 
49
                dbl = lowerValue.remove("pt");
 
50
        }
 
51
        else if (lowerValue.find("mm") != -1)
 
52
        {
 
53
                dbl = lowerValue.remove("mm");
 
54
        }
 
55
        else if (lowerValue.find("in") != -1)
 
56
        {
 
57
                dbl = lowerValue.remove("in");
 
58
        }
 
59
        else if (lowerValue.find("p") != -1)
 
60
        {
 
61
                dbl = lowerValue.remove("p");
 
62
        }
 
63
        else if (lowerValue.find("cm") != -1)
 
64
        {
 
65
                dbl = lowerValue.remove("cm");
 
66
        }
 
67
        else if (lowerValue.find("c") != -1)
 
68
        {
 
69
                dbl = lowerValue.remove("c");
 
70
        }
 
71
        else
 
72
                dbl = "0.0";
 
73
 
 
74
        dbl = dbl.stripWhiteSpace();
 
75
        return dbl.toDouble();
 
76
}
 
77
 
 
78
/*!
 
79
 * @brief Strip the text from a value and return the Unit index for the value
 
80
 */
 
81
const Unit unitIndexFromString(const QString& value)
 
82
{
 
83
        QString lowerValue = value.lower();
 
84
        Unit retVal;
 
85
        if (lowerValue.find("pt") != -1)
 
86
        {
 
87
                retVal=SC_PT;
 
88
        }
 
89
        else if (lowerValue.find("mm") != -1)
 
90
        {
 
91
                retVal=SC_MM;
 
92
        }
 
93
        else if (lowerValue.find("in") != -1)
 
94
        {
 
95
                retVal=SC_IN;
 
96
        }
 
97
        else if (lowerValue.find("p") != -1)
 
98
        {
 
99
                retVal=SC_P;
 
100
        }
 
101
        else if (lowerValue.find("cm") != -1)
 
102
        {
 
103
                retVal=SC_CM;
 
104
        }
 
105
        else if (lowerValue.find("c") != -1)
 
106
        {
 
107
                retVal=SC_C;
 
108
        }       
 
109
        else
 
110
                retVal=SC_PT;
 
111
        return retVal;
 
112
}
 
113
 
 
114
/*!
 
115
 * @brief Returns the suffix used in GUI widgets
 
116
 */
 
117
const QString unitGetSuffixFromIndex(const int index)
 
118
{
 
119
        //Could also return " "+unitGetStrFromIndex(indeX);
 
120
        if (index<UNITMIN || index>UNITMAX) 
 
121
                return "";
 
122
        QString suffix[] = { QObject::tr(" pt"), QObject::tr(" mm"), QObject::tr(" in"), QObject::tr(" p"), QObject::tr(" cm"), QObject::tr(" c") };
 
123
        return suffix[index];
 
124
}
 
125
 
 
126
/*!
 
127
 * @brief Returns a general suffix for each of the units
 
128
 */
 
129
const QString unitGetStrFromIndex(const int index)
 
130
{
 
131
        if (index<UNITMIN || index>UNITMAX) 
 
132
                return "";
 
133
        QString suffix[] = { QObject::tr("pt"), QObject::tr("mm"), QObject::tr("in"), QObject::tr("p"), QObject::tr("cm"), QObject::tr("c") };
 
134
        return suffix[index];
 
135
}
 
136
 
 
137
/*!
 
138
 * @brief Returns a general untranslated suffix for each of the units
 
139
 */
 
140
const QString unitGetUntranslatedStrFromIndex(const int index)
 
141
{
 
142
        if (index<UNITMIN || index>UNITMAX) 
 
143
                return "";
 
144
        QString suffix[] = { "pt", "mm", "in", "p", "cm", "c" };
 
145
        return suffix[index];
 
146
}
 
147
/*!
 
148
 * @brief Returns the decimals for the units
 
149
 */
 
150
const int unitGetDecimalsFromIndex(const int index)
 
151
{
 
152
        if (index<UNITMIN || index>UNITMAX) 
 
153
                return 0;
 
154
        //                      PT,   MM,    IN,   P,    CM,   C
 
155
        int decimalPoints[] = {100, 1000, 10000, 100, 10000, 10000};
 
156
        return decimalPoints[index];
 
157
}
 
158
 
 
159
/*!
 
160
 * @brief Returns the precision for the units
 
161
 */
 
162
const int unitGetPrecisionFromIndex(const int index)
 
163
{
 
164
        if (index<UNITMIN || index>UNITMAX) 
 
165
                return 0;
 
166
        //                PT,MM,IN, P,CM, C
 
167
        int precision[] = {2, 3, 4, 2, 4, 4};
 
168
        return precision[index];
 
169
}
 
170
 
 
171
/*!
 
172
 * @brief Returns a QStringList of the units for use in QComboBoxes etc
 
173
 */
 
174
const QStringList unitGetTextUnitList()
 
175
{
 
176
        QStringList suffixList;
 
177
        suffixList.append( QObject::tr( "Points (pt)" ) );
 
178
        suffixList.append( QObject::tr( "Millimeters (mm)" ) );
 
179
        suffixList.append( QObject::tr( "Inches (in)" ) );
 
180
        suffixList.append( QObject::tr( "Picas (p)" ) );
 
181
        suffixList.append( QObject::tr( "Centimeters (cm)" ) );
 
182
        suffixList.append( QObject::tr( "Cicero (c)" ) );
 
183
        return QStringList(suffixList);
 
184
}
 
185
 
 
186
/*!
 
187
 * @brief Returns the maximum index of the units we have now
 
188
 */
 
189
const int unitGetMaxIndex()
 
190
{
 
191
        return UNITMAX;
 
192
}
 
193
 
 
194
/*!
 
195
 * @brief Returns the pts value from the mm value supplied
 
196
 */
 
197
const double mm2pts(double mm)
 
198
{
 
199
        return mm / unitGetRatioFromIndex(SC_MM);
 
200
}
 
201
 
 
202
/*!
 
203
 * @brief Returns the pts value from the in value supplied
 
204
 */
 
205
const double in2pts(double in)
 
206
{
 
207
        return in / unitGetRatioFromIndex(SC_IN);
 
208
}
 
209
 
 
210
/*!
 
211
 * @brief Returns the pts value from the pica value supplied
 
212
 */
 
213
const double p2pts(double p)
 
214
{
 
215
        return p / unitGetRatioFromIndex(SC_P);
 
216
}
 
217
 
 
218
/*!
 
219
 * @brief Returns the pts value from the cm value supplied
 
220
 */
 
221
const double cm2pts(double cm)
 
222
{
 
223
        return cm / unitGetRatioFromIndex(SC_CM);
 
224
}
 
225
 
 
226
/*!
 
227
 * @brief Returns the pts value from the cm value supplied
 
228
 */
 
229
const double c2pts(double c)
 
230
{
 
231
        return c / unitGetRatioFromIndex(SC_C);
 
232
}
 
233
 
 
234
/*!
 
235
 * @brief Returns the mm value from the pt value supplied
 
236
 */
 
237
const double pts2mm(double pts)
 
238
{
 
239
        return pts * unitGetRatioFromIndex(SC_MM);
 
240
}
 
241
 
 
242
/*!
 
243
 * @brief Returns the in value from the pt value supplied
 
244
 */
 
245
const double pts2in(double pts)
 
246
{
 
247
        return pts * unitGetRatioFromIndex(SC_IN);
 
248
}
 
249
 
 
250
/*!
 
251
 * @brief Returns the pica value from the pt value supplied
 
252
 */
 
253
const double pts2p(double pts)
 
254
{
 
255
        return pts * unitGetRatioFromIndex(SC_P);
 
256
}
 
257
 
 
258
/*!
 
259
 * @brief Returns the cm value from the pt value supplied
 
260
 */
 
261
const double pts2cm(double pts)
 
262
{
 
263
        return pts * unitGetRatioFromIndex(SC_CM);
 
264
}
 
265
 
 
266
/*!
 
267
 * @brief Returns the c value from the pt value supplied
 
268
 */
 
269
const double pts2c(double pts)
 
270
{
 
271
        return pts * unitGetRatioFromIndex(SC_C);
 
272
}
 
273
 
 
274
/*!
 
275
 * @brief Returns the value from the pt value supplied based on unit index
 
276
 */
 
277
double pts2value(double unitValue, int unit)
 
278
{
 
279
        double ret = 0.0;
 
280
        switch (unit)
 
281
        {
 
282
                case 0:
 
283
                        ret = unitValue; //dont multiply by 1
 
284
                        break;
 
285
                default:
 
286
                        ret = unitValue * unitGetRatioFromIndex(unit);
 
287
                        break;
 
288
        }
 
289
        return ret;
 
290
}
 
291
 
 
292
/*!
 
293
 * @brief Returns the pt value from the value supplied based on unit index
 
294
 */
 
295
double value2pts(double unitValue, int unit)
 
296
{
 
297
        double ret = 0.0;
 
298
        switch (unit)
 
299
        {
 
300
                case 0:
 
301
                        ret = unitValue; // dont divide by 1
 
302
                        break;
 
303
                default:
 
304
                        ret = unitValue / unitGetRatioFromIndex(unit);
 
305
                        break;
 
306
        }
 
307
        return ret;
 
308
}
 
309
 
 
310
/*!
 
311
 * @brief Returns the secondary unit value from the value supplied based on primary unit
 
312
 */
 
313
double value2value(double unitValue, int primaryUnit, int secondaryUnit)
 
314
{
 
315
        if (primaryUnit==secondaryUnit)
 
316
                return unitValue;
 
317
                
 
318
        double pts = 0.0, ret = 0.0;
 
319
        //Can make this not convert to points at a later stage, for now, the function exists and works.
 
320
        pts= primaryUnit==0 ? unitValue : unitValue / unitGetRatioFromIndex(primaryUnit);
 
321
        ret= secondaryUnit==0 ? pts : pts * unitGetRatioFromIndex(secondaryUnit);
 
322
        return ret;
 
323
}
 
324
 
 
325
/*!
 
326
 * @brief Sets up iteration value 1 for vruler, hruler and tabruler
 
327
 */
 
328
double unitRulerGetIter1FromIndex(const int index)
 
329
{
 
330
        if (index<UNITMIN || index>UNITMAX) 
 
331
                return 0;
 
332
        //                 PT,         MM,   IN,    P,        CM,               C
 
333
        double iter[] = {10.0, 720.0/25.4, 18.0, 12.0, 72.0/25.4, 72.0/25.4*4.512};
 
334
        return iter[index];
 
335
}
 
336
 
 
337
/*!
 
338
 * @brief Sets up iteration value 2 for vruler, hruler and tabruler
 
339
 */
 
340
double unitRulerGetIter2FromIndex(const int index)
 
341
{
 
342
        if (index<UNITMIN || index>UNITMAX) 
 
343
                return 0;
 
344
        //                  PT,          MM,   IN,     P,         CM,                C
 
345
        double iter[] = {100.0, 7200.0/25.4, 72.0, 120.0, 720.0/25.4, 720.0/25.4*4.512};
 
346
        return iter[index];
 
347
}