00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <config.h>
00011
00012 #include <kdebug.h>
00013 #include <kglobal.h>
00014 #include <klocale.h>
00015 #include <math.h>
00016 #include <qstringlist.h>
00017
00018 #include "pageSize.h"
00019 #include "units.h"
00020
00021 struct pageSizeItem
00022 {
00023 const char *name;
00024 float width;
00025 float height;
00026 const char *preferredUnit;
00027 };
00028
00029 #define defaultMetricPaperSize 4 // Default paper size is "DIN A4"
00030 #define defaultImperialPaperSize 8 // Default paper size is "US Letter"
00031
00032 static pageSizeItem staticList[] = { {"DIN A0", 841.0, 1189.0, "mm"},
00033 {"DIN A1", 594.0, 841.0, "mm"},
00034 {"DIN A2", 420.0, 594.0, "mm"},
00035 {"DIN A3", 297.0, 420.0, "mm"},
00036 {"DIN A4", 210.0, 297.0, "mm"},
00037 {"DIN A5", 148.5, 210.0, "mm"},
00038 {"DIN B4", 250.0, 353.0, "mm"},
00039 {"DIN B5", 176.0, 250.0, "mm"},
00040 {"US Letter", 215.9, 279.4, "in"},
00041 {"US Legal", 215.9, 355.6, "in"},
00042 {0, 0.0, 0.0, 0}
00043 };
00044
00045
00046 pageSize::pageSize()
00047 {
00048 currentSize = defaultPageSize();
00049 pageWidth.setLength_in_mm(staticList[currentSize].width);
00050 pageHeight.setLength_in_mm(staticList[currentSize].height);
00051 }
00052
00053
00054 pageSize::pageSize(const SimplePageSize& s)
00055 {
00056 pageWidth = s.width();
00057 pageHeight = s.height();
00058
00059 rectifySizes();
00060 reconstructCurrentSize();
00061 }
00062
00063
00064 bool pageSize::setPageSize(const QString& name)
00065 {
00066
00067 QString currentName;
00068 for(int i=0; staticList[i].name != 0; i++) {
00069 currentName = staticList[i].name;
00070 if (currentName == name) {
00071 currentSize = i;
00072
00073 pageWidth.setLength_in_mm(staticList[currentSize].width);
00074 pageHeight.setLength_in_mm(staticList[currentSize].height);
00075 emit(sizeChanged(*this));
00076 return true;
00077 }
00078 }
00079
00080
00081
00082
00083 if (name.find('x') >= 0) {
00084 bool wok, hok;
00085 float pageWidth_tmp = name.section('x',0,0).toFloat(&wok);
00086 float pageHeight_tmp = name.section('x',1,1).toFloat(&hok);
00087 if (wok && hok) {
00088 pageWidth.setLength_in_mm(pageWidth_tmp);
00089 pageHeight.setLength_in_mm(pageHeight_tmp);
00090
00091 rectifySizes();
00092 reconstructCurrentSize();
00093 emit(sizeChanged(*this));
00094 return true;
00095 }
00096 }
00097
00098
00099
00100
00101 if (name.find(',') >= 0) {
00102 bool wok, hok;
00103 float pageWidth_tmp = distance::convertToMM(name.section(',',0,0), &wok);
00104 float pageHeight_tmp = distance::convertToMM(name.section(',',1,1), &hok);
00105 if (wok && hok) {
00106 pageWidth.setLength_in_mm(pageWidth_tmp);
00107 pageHeight.setLength_in_mm(pageHeight_tmp);
00108
00109 rectifySizes();
00110 reconstructCurrentSize();
00111 emit(sizeChanged(*this));
00112 return true;
00113 }
00114 }
00115
00116
00117
00118 currentSize = defaultPageSize();
00119 pageWidth.setLength_in_mm(staticList[currentSize].width);
00120 pageHeight.setLength_in_mm(staticList[currentSize].height);
00121 kdError(1223) << "pageSize::setPageSize: could not parse '" << name << "'. Using " << staticList[currentSize].name << " as a default." << endl;
00122 emit(sizeChanged(*this));
00123 return false;
00124 }
00125
00126
00127 void pageSize::setPageSize(double width, double height)
00128 {
00129 SimplePageSize oldPage = *this;
00130
00131 pageWidth.setLength_in_mm(width);
00132 pageHeight.setLength_in_mm(height);
00133
00134 rectifySizes();
00135 reconstructCurrentSize();
00136 if ( !isNearlyEqual(oldPage))
00137 emit(sizeChanged(*this));
00138 }
00139
00140
00141 void pageSize::setPageSize(const QString& width, const QString& _widthUnits, const QString& height, const QString& _heightUnits)
00142 {
00143 SimplePageSize oldPage = *this;
00144
00145 double w = width.toFloat();
00146 double h = height.toFloat();
00147
00148 QString widthUnits = _widthUnits;
00149 if ((widthUnits != "cm") && (widthUnits != "mm") && (widthUnits != "in")) {
00150 kdError(1223) << "Unrecognized page width unit '" << widthUnits << "'. Assuming mm" << endl;
00151 widthUnits = "mm";
00152 }
00153 pageWidth.setLength_in_mm(w);
00154 if (widthUnits == "cm")
00155 pageWidth.setLength_in_cm(w);
00156 if (widthUnits == "in")
00157 pageWidth.setLength_in_inch(w);
00158
00159 QString heightUnits = _heightUnits;
00160 if ((heightUnits != "cm") && (heightUnits != "mm") && (heightUnits != "in")) {
00161 kdError(1223) << "Unrecognized page height unit '" << widthUnits << "'. Assuming mm" << endl;
00162 heightUnits = "mm";
00163 }
00164 pageHeight.setLength_in_mm(h);
00165 if (heightUnits == "cm")
00166 pageHeight.setLength_in_cm(h);
00167 if (heightUnits == "in")
00168 pageHeight.setLength_in_inch(h);
00169
00170 rectifySizes();
00171 reconstructCurrentSize();
00172 if ( !isNearlyEqual(oldPage))
00173 emit(sizeChanged(*this));
00174 }
00175
00176
00177 pageSize &pageSize::operator= (const pageSize &src)
00178 {
00179 SimplePageSize oldPage = *this;
00180
00181 currentSize = src.currentSize;
00182 pageWidth = src.pageWidth;
00183 pageHeight = src.pageHeight;
00184
00185 if ( !isNearlyEqual(oldPage))
00186 emit(sizeChanged(*this));
00187 return *this;
00188 }
00189
00190
00191 void pageSize::rectifySizes()
00192 {
00193
00194
00195 if (pageWidth.getLength_in_mm() < 50)
00196 pageWidth.setLength_in_mm(50.0);
00197 if (pageWidth.getLength_in_mm() > 1200)
00198 pageWidth.setLength_in_mm(1200);
00199 if (pageHeight.getLength_in_mm() < 50)
00200 pageHeight.setLength_in_mm(50);
00201 if (pageHeight.getLength_in_mm() > 1200)
00202 pageHeight.setLength_in_mm(1200);
00203 return;
00204 }
00205
00206
00207 QString pageSize::preferredUnit() const
00208 {
00209 if (currentSize >= 0)
00210 return staticList[currentSize].preferredUnit;
00211
00212
00213 if (KGlobal::locale()-> measureSystem() == KLocale::Metric)
00214 return "mm";
00215 else
00216 return "in";
00217 }
00218
00219
00220 QString pageSize::widthString(const QString& unit) const
00221 {
00222 QString answer = "--";
00223
00224 if (unit == "cm")
00225 answer.setNum(pageWidth.getLength_in_cm());
00226 if (unit == "mm")
00227 answer.setNum(pageWidth.getLength_in_mm());
00228 if (unit == "in")
00229 answer.setNum(pageWidth.getLength_in_inch());
00230
00231 return answer;
00232 }
00233
00234
00235 QString pageSize::heightString(const QString& unit) const
00236 {
00237 QString answer = "--";
00238
00239 if (unit == "cm")
00240 answer.setNum(pageHeight.getLength_in_cm());
00241 if (unit == "mm")
00242 answer.setNum(pageHeight.getLength_in_mm());
00243 if (unit == "in")
00244 answer.setNum(pageHeight.getLength_in_inch());
00245
00246 return answer;
00247 }
00248
00249
00250 QStringList pageSize::pageSizeNames()
00251 {
00252 QStringList names;
00253
00254 for(int i=0; staticList[i].name != 0; i++)
00255 names << staticList[i].name;
00256
00257 return names;
00258 }
00259
00260
00261 QString pageSize::formatName() const
00262 {
00263 if (currentSize >= 0)
00264 return staticList[currentSize].name;
00265 else
00266 return QString::null;
00267 }
00268
00269
00270 int pageSize::getOrientation() const
00271 {
00272 if (currentSize == -1) {
00273 kdError(1223) << "pageSize::getOrientation: getOrientation called for page format that does not have a name." << endl;
00274 return 0;
00275 }
00276
00277 if (pageWidth.getLength_in_mm() == staticList[currentSize].width)
00278 return 0;
00279 else
00280 return 1;
00281 }
00282
00283
00284 void pageSize::setOrientation(int orient)
00285 {
00286 if (currentSize == -1) {
00287 kdError(1223) << "pageSize::setOrientation: setOrientation called for page format that does not have a name." << endl;
00288 return;
00289 }
00290
00291 if (orient == 1) {
00292 pageWidth.setLength_in_mm(staticList[currentSize].height);
00293 pageHeight.setLength_in_mm(staticList[currentSize].width);
00294 } else {
00295 pageWidth.setLength_in_mm(staticList[currentSize].width);
00296 pageHeight.setLength_in_mm(staticList[currentSize].height);
00297 }
00298 emit(sizeChanged(*this));
00299 }
00300
00301
00302 QString pageSize::serialize() const
00303 {
00304 if ((currentSize >= 0) && (fabs(staticList[currentSize].height-pageHeight.getLength_in_mm()) <= 0.5))
00305 return staticList[currentSize].name;
00306 else
00307 return QString("%1x%2").arg(pageWidth.getLength_in_mm()).arg(pageHeight.getLength_in_mm());
00308 }
00309
00310
00311 void pageSize::reconstructCurrentSize()
00312 {
00313 for(int i=0; staticList[i].name != 0; i++) {
00314 if ((fabs(staticList[i].width - pageWidth.getLength_in_mm()) <= 2) && (fabs(staticList[i].height - pageHeight.getLength_in_mm()) <= 2)) {
00315 currentSize = i;
00316 pageWidth.setLength_in_mm(staticList[currentSize].width);
00317 pageHeight.setLength_in_mm(staticList[currentSize].height);
00318 return;
00319 }
00320 if ((fabs(staticList[i].height - pageWidth.getLength_in_mm()) <= 2) && (fabs(staticList[i].width - pageHeight.getLength_in_mm()) <= 2)) {
00321 currentSize = i;
00322 pageWidth.setLength_in_mm(staticList[currentSize].height);
00323 pageHeight.setLength_in_mm(staticList[currentSize].width);
00324 return;
00325 }
00326 }
00327 currentSize = -1;
00328 return;
00329 }
00330
00331 int pageSize::defaultPageSize()
00332 {
00333
00334
00335
00336 if (KGlobal::locale()-> measureSystem() == KLocale::Metric)
00337 return defaultMetricPaperSize;
00338 else
00339 return defaultImperialPaperSize;
00340 }
00341
00342 #include "pageSize.moc"
00343