SVN_SILENT made messages (.desktop file)
[kdegraphics.git] / okular / generators / dvi / pageSize.cpp
blob49bce0ff30cc9b7d3c9d4a7e50c1233affd6432a
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
2 // pageSize.cpp
3 //
4 // Part of KVIEWSHELL - A framework for multipage text/gfx viewers
5 //
6 // (C) 2002-2003 Stefan Kebekus
7 // Distributed under the GPL
9 #include <config.h>
11 #include "pageSize.h"
12 #include "kvs_debug.h"
13 #include "length.h"
15 #include <kglobal.h>
16 #include <klocale.h>
20 struct pageSizeItem
22 const char *name;
23 float width; // in mm
24 float height; // in mm
25 const char *preferredUnit;
28 #define defaultMetricPaperSize 4 // Default paper size is "DIN A4"
29 #define defaultImperialPaperSize 8 // Default paper size is "US Letter"
31 static pageSizeItem staticList[] = { {"DIN A0", 841.0f, 1189.0f, "mm"},
32 {"DIN A1", 594.0f, 841.0f, "mm"},
33 {"DIN A2", 420.0f, 594.0f, "mm"},
34 {"DIN A3", 297.0f, 420.0f, "mm"},
35 {"DIN A4", 210.0f, 297.0f, "mm"},
36 {"DIN A5", 148.5f, 210.0f, "mm"},
37 {"DIN B4", 250.0f, 353.0f, "mm"},
38 {"DIN B5", 176.0f, 250.0f, "mm"},
39 {"US Letter", 215.9f, 279.4f, "in"},
40 {"US Legal", 215.9f, 355.6f, "in"},
41 {0, 0.0f, 0.0f, 0} // marks the end of the list.
45 pageSize::pageSize()
47 currentSize = defaultPageSize();
48 pageWidth.setLength_in_mm(staticList[currentSize].width);
49 pageHeight.setLength_in_mm(staticList[currentSize].height);
53 pageSize::pageSize(const SimplePageSize& s)
55 pageWidth = s.width();
56 pageHeight = s.height();
58 rectifySizes();
59 reconstructCurrentSize();
63 bool pageSize::setPageSize(const QString& name)
65 // See if we can recognize the string
66 QString currentName;
67 for(int i=0; staticList[i].name != 0; i++) {
68 currentName = staticList[i].name;
69 if (currentName == name) {
70 currentSize = i;
71 // Set page width/height accordingly
72 pageWidth.setLength_in_mm(staticList[currentSize].width);
73 pageHeight.setLength_in_mm(staticList[currentSize].height);
74 emit(sizeChanged(*this));
75 return true;
79 // Check if the string contains 'x'. If yes, we assume it is of type
80 // "<number>x<number>". If yes, the first number is interpreted as
81 // the width in mm, the second as the height in mm
82 if (name.indexOf('x') >= 0) {
83 bool wok, hok;
84 float pageWidth_tmp = name.section('x',0,0).toFloat(&wok);
85 float pageHeight_tmp = name.section('x',1,1).toFloat(&hok);
86 if (wok && hok) {
87 pageWidth.setLength_in_mm(pageWidth_tmp);
88 pageHeight.setLength_in_mm(pageHeight_tmp);
90 rectifySizes();
91 reconstructCurrentSize();
92 emit(sizeChanged(*this));
93 return true;
97 // Check if the string contains ','. If yes, we assume it is of type
98 // "<number><unit>,<number><uni>". The first number is supposed to
99 // be the width, the second the height.
100 if (name.indexOf(',') >= 0) {
101 bool wok, hok;
102 float pageWidth_tmp = Length::convertToMM(name.section(',',0,0), &wok);
103 float pageHeight_tmp = Length::convertToMM(name.section(',',1,1), &hok);
104 if (wok && hok) {
105 pageWidth.setLength_in_mm(pageWidth_tmp);
106 pageHeight.setLength_in_mm(pageHeight_tmp);
108 rectifySizes();
109 reconstructCurrentSize();
110 emit(sizeChanged(*this));
111 return true;
115 // Last resource. Set the default, in case the string is
116 // unintelligible to us.
117 currentSize = defaultPageSize();
118 pageWidth.setLength_in_mm(staticList[currentSize].width);
119 pageHeight.setLength_in_mm(staticList[currentSize].height);
120 kError(kvs::shell) << "pageSize::setPageSize: could not parse '" << name << "'. Using " << staticList[currentSize].name << " as a default." << endl;
121 emit(sizeChanged(*this));
122 return false;
126 void pageSize::setPageSize(double width, double height)
128 SimplePageSize oldPage = *this;
130 pageWidth.setLength_in_mm(width);
131 pageHeight.setLength_in_mm(height);
133 rectifySizes();
134 reconstructCurrentSize();
135 if ( !isNearlyEqual(oldPage))
136 emit(sizeChanged(*this));
140 void pageSize::setPageSize(const QString& width, const QString& _widthUnits, const QString& height, const QString& _heightUnits)
142 SimplePageSize oldPage = *this;
144 double w = width.toFloat();
145 double h = height.toFloat();
147 QString widthUnits = _widthUnits;
148 if ((widthUnits != "cm") && (widthUnits != "mm") && (widthUnits != "in")) {
149 kError(kvs::shell) << "Unrecognized page width unit '" << widthUnits << "'. Assuming mm" << endl;
150 widthUnits = "mm";
152 pageWidth.setLength_in_mm(w);
153 if (widthUnits == "cm")
154 pageWidth.setLength_in_cm(w);
155 if (widthUnits == "in")
156 pageWidth.setLength_in_inch(w);
158 QString heightUnits = _heightUnits;
159 if ((heightUnits != "cm") && (heightUnits != "mm") && (heightUnits != "in")) {
160 kError(kvs::shell) << "Unrecognized page height unit '" << widthUnits << "'. Assuming mm" << endl;
161 heightUnits = "mm";
163 pageHeight.setLength_in_mm(h);
164 if (heightUnits == "cm")
165 pageHeight.setLength_in_cm(h);
166 if (heightUnits == "in")
167 pageHeight.setLength_in_inch(h);
169 rectifySizes();
170 reconstructCurrentSize();
171 if ( !isNearlyEqual(oldPage))
172 emit(sizeChanged(*this));
176 pageSize &pageSize::operator= (const pageSize &src)
178 SimplePageSize oldPage = *this;
180 currentSize = src.currentSize;
181 pageWidth = src.pageWidth;
182 pageHeight = src.pageHeight;
184 if ( !isNearlyEqual(oldPage))
185 emit(sizeChanged(*this));
186 return *this;
190 void pageSize::rectifySizes()
192 // Now do some sanity checks to make sure that values are not
193 // outrageous. We allow values between 5cm and 50cm.
194 if (pageWidth.getLength_in_mm() < 50)
195 pageWidth.setLength_in_mm(50.0);
196 if (pageWidth.getLength_in_mm() > 1200)
197 pageWidth.setLength_in_mm(1200);
198 if (pageHeight.getLength_in_mm() < 50)
199 pageHeight.setLength_in_mm(50);
200 if (pageHeight.getLength_in_mm() > 1200)
201 pageHeight.setLength_in_mm(1200);
202 return;
206 QString pageSize::preferredUnit() const
208 if (currentSize >= 0)
209 return staticList[currentSize].preferredUnit;
211 // User-defined size. Give a preferred unit depening on the locale.
212 if (KGlobal::locale()-> measureSystem() == KLocale::Metric)
213 return "mm";
214 else
215 return "in";
219 QString pageSize::widthString(const QString& unit) const
221 QString answer = "--";
223 if (unit == "cm")
224 answer.setNum(pageWidth.getLength_in_cm());
225 if (unit == "mm")
226 answer.setNum(pageWidth.getLength_in_mm());
227 if (unit == "in")
228 answer.setNum(pageWidth.getLength_in_inch());
230 return answer;
234 QString pageSize::heightString(const QString& unit) const
236 QString answer = "--";
238 if (unit == "cm")
239 answer.setNum(pageHeight.getLength_in_cm());
240 if (unit == "mm")
241 answer.setNum(pageHeight.getLength_in_mm());
242 if (unit == "in")
243 answer.setNum(pageHeight.getLength_in_inch());
245 return answer;
249 QStringList pageSize::pageSizeNames()
251 QStringList names;
253 for(int i=0; staticList[i].name != 0; i++)
254 names << staticList[i].name;
256 return names;
260 QString pageSize::formatName() const
262 if (currentSize >= 0)
263 return staticList[currentSize].name;
264 else
265 return QString();
269 int pageSize::getOrientation() const
271 if (currentSize == -1) {
272 kError(kvs::shell) << "pageSize::getOrientation: getOrientation called for page format that does not have a name." << endl;
273 return 0;
276 if (pageWidth.getLength_in_mm() == staticList[currentSize].width)
277 return 0;
278 else
279 return 1;
283 void pageSize::setOrientation(int orient)
285 if (currentSize == -1) {
286 kError(kvs::shell) << "pageSize::setOrientation: setOrientation called for page format that does not have a name." << endl;
287 return;
290 if (orient == 1) {
291 pageWidth.setLength_in_mm(staticList[currentSize].height);
292 pageHeight.setLength_in_mm(staticList[currentSize].width);
293 } else {
294 pageWidth.setLength_in_mm(staticList[currentSize].width);
295 pageHeight.setLength_in_mm(staticList[currentSize].height);
297 emit(sizeChanged(*this));
301 QString pageSize::serialize() const
303 if ((currentSize >= 0) && (fabs(staticList[currentSize].height-pageHeight.getLength_in_mm()) <= 0.5))
304 return staticList[currentSize].name;
305 else
306 return QString("%1x%2").arg(pageWidth.getLength_in_mm()).arg(pageHeight.getLength_in_mm());
310 QString pageSize::description() const
312 if (!isValid())
313 return QString();
315 QString size = " ";
316 if (formatNumber() == -1) {
317 if (KGlobal::locale()-> measureSystem() == KLocale::Metric)
318 size += QString("%1x%2 mm").arg(width().getLength_in_mm(), 0, 'f', 0).arg(height().getLength_in_mm(), 0, 'f', 0);
319 else
320 size += QString("%1x%2 in").arg(width().getLength_in_inch(), 0, 'g', 2).arg(height().getLength_in_inch(), 0, 'g', 2);
321 } else {
322 size += formatName() + '/';
323 if (getOrientation() == 0)
324 size += i18n("portrait");
325 else
326 size += i18n("landscape");
328 return size + ' ';
332 void pageSize::reconstructCurrentSize()
334 for(int i=0; staticList[i].name != 0; i++) {
335 if ((fabs(staticList[i].width - pageWidth.getLength_in_mm()) <= 2) && (fabs(staticList[i].height - pageHeight.getLength_in_mm()) <= 2)) {
336 currentSize = i;
337 pageWidth.setLength_in_mm(staticList[currentSize].width);
338 pageHeight.setLength_in_mm(staticList[currentSize].height);
339 return;
341 if ((fabs(staticList[i].height - pageWidth.getLength_in_mm()) <= 2) && (fabs(staticList[i].width - pageHeight.getLength_in_mm()) <= 2)) {
342 currentSize = i;
343 pageWidth.setLength_in_mm(staticList[currentSize].height);
344 pageHeight.setLength_in_mm(staticList[currentSize].width);
345 return;
348 currentSize = -1;
349 return;
352 int pageSize::defaultPageSize()
354 // FIXME: static_cast<QPrinter::PageSize>(KGlobal::locale()->pageSize())
355 // is the proper solution here. Then you can determine the values
356 // without using your hardcoded table too!
357 if (KGlobal::locale()-> measureSystem() == KLocale::Metric)
358 return defaultMetricPaperSize;
359 else
360 return defaultImperialPaperSize;
363 #include "pageSize.moc"