1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
4 // Part of KVIEWSHELL - A framework for multipage text/gfx viewers
6 // (C) 2002-2003 Stefan Kebekus
7 // Distributed under the GPL
12 #include "kvs_debug.h"
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.
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();
59 reconstructCurrentSize();
63 bool pageSize::setPageSize(const QString
& name
)
65 // See if we can recognize the string
67 for(int i
=0; staticList
[i
].name
!= 0; i
++) {
68 currentName
= staticList
[i
].name
;
69 if (currentName
== name
) {
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));
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) {
84 float pageWidth_tmp
= name
.section('x',0,0).toFloat(&wok
);
85 float pageHeight_tmp
= name
.section('x',1,1).toFloat(&hok
);
87 pageWidth
.setLength_in_mm(pageWidth_tmp
);
88 pageHeight
.setLength_in_mm(pageHeight_tmp
);
91 reconstructCurrentSize();
92 emit(sizeChanged(*this));
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) {
102 float pageWidth_tmp
= Length::convertToMM(name
.section(',',0,0), &wok
);
103 float pageHeight_tmp
= Length::convertToMM(name
.section(',',1,1), &hok
);
105 pageWidth
.setLength_in_mm(pageWidth_tmp
);
106 pageHeight
.setLength_in_mm(pageHeight_tmp
);
109 reconstructCurrentSize();
110 emit(sizeChanged(*this));
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));
126 void pageSize::setPageSize(double width
, double height
)
128 SimplePageSize oldPage
= *this;
130 pageWidth
.setLength_in_mm(width
);
131 pageHeight
.setLength_in_mm(height
);
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
;
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
;
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
);
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));
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);
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
)
219 QString
pageSize::widthString(const QString
& unit
) const
221 QString answer
= "--";
224 answer
.setNum(pageWidth
.getLength_in_cm());
226 answer
.setNum(pageWidth
.getLength_in_mm());
228 answer
.setNum(pageWidth
.getLength_in_inch());
234 QString
pageSize::heightString(const QString
& unit
) const
236 QString answer
= "--";
239 answer
.setNum(pageHeight
.getLength_in_cm());
241 answer
.setNum(pageHeight
.getLength_in_mm());
243 answer
.setNum(pageHeight
.getLength_in_inch());
249 QStringList
pageSize::pageSizeNames()
253 for(int i
=0; staticList
[i
].name
!= 0; i
++)
254 names
<< staticList
[i
].name
;
260 QString
pageSize::formatName() const
262 if (currentSize
>= 0)
263 return staticList
[currentSize
].name
;
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
;
276 if (pageWidth
.getLength_in_mm() == staticList
[currentSize
].width
)
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
;
291 pageWidth
.setLength_in_mm(staticList
[currentSize
].height
);
292 pageHeight
.setLength_in_mm(staticList
[currentSize
].width
);
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
;
306 return QString("%1x%2").arg(pageWidth
.getLength_in_mm()).arg(pageHeight
.getLength_in_mm());
310 QString
pageSize::description() const
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);
320 size
+= QString("%1x%2 in").arg(width().getLength_in_inch(), 0, 'g', 2).arg(height().getLength_in_inch(), 0, 'g', 2);
322 size
+= formatName() + '/';
323 if (getOrientation() == 0)
324 size
+= i18n("portrait");
326 size
+= i18n("landscape");
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)) {
337 pageWidth
.setLength_in_mm(staticList
[currentSize
].width
);
338 pageHeight
.setLength_in_mm(staticList
[currentSize
].height
);
341 if ((fabs(staticList
[i
].height
- pageWidth
.getLength_in_mm()) <= 2) && (fabs(staticList
[i
].width
- pageHeight
.getLength_in_mm()) <= 2)) {
343 pageWidth
.setLength_in_mm(staticList
[currentSize
].height
);
344 pageHeight
.setLength_in_mm(staticList
[currentSize
].width
);
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
;
360 return defaultImperialPaperSize
;
363 #include "pageSize.moc"