F:/KPlato/koffice/libs/kofficecore/KoPageLayout.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003    Copyright 2002, 2003 David Faure <faure@kde.org>
00004    Copyright 2003 Nicolas GOUTTE <goutte@kde.org>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 #include "KoPageLayout.h"
00022 #include <KoUnit.h>
00023 #include <klocale.h>
00024 #include <kprinter.h>
00025 #include <kdebug.h>
00026 #include <kglobal.h>
00027 #include <KoDom.h>
00028 #include <KoXmlNS.h>
00029 #include <qdom.h>
00030 
00031 // paper formats ( mm )
00032 #define PG_A3_WIDTH             297.0
00033 #define PG_A3_HEIGHT            420.0
00034 #define PG_A4_WIDTH             210.0
00035 #define PG_A4_HEIGHT            297.0
00036 #define PG_A5_WIDTH             148.0
00037 #define PG_A5_HEIGHT            210.0
00038 #define PG_B5_WIDTH             182.0
00039 #define PG_B5_HEIGHT            257.0
00040 #define PG_US_LETTER_WIDTH      216.0
00041 #define PG_US_LETTER_HEIGHT     279.0
00042 #define PG_US_LEGAL_WIDTH       216.0
00043 #define PG_US_LEGAL_HEIGHT      356.0
00044 #define PG_US_EXECUTIVE_WIDTH   191.0
00045 #define PG_US_EXECUTIVE_HEIGHT  254.0
00046 
00047 KoGenStyle KoPageLayout::saveOasis() const
00048 {
00049     KoGenStyle style(KoGenStyle::STYLE_PAGELAYOUT);
00050     style.addPropertyPt("fo:page-width", ptWidth);
00051     style.addPropertyPt("fo:page-height", ptHeight);
00052     style.addPropertyPt("fo:margin-left", ptLeft);
00053     style.addPropertyPt("fo:margin-right", ptRight);
00054     style.addPropertyPt("fo:margin-top", ptTop);
00055     style.addPropertyPt("fo:margin-bottom", ptBottom);
00056     style.addProperty("style:print-orientation", (orientation == PG_LANDSCAPE ? "landscape" : "portrait"));
00057     return style;
00058 }
00059 
00060 void KoPageLayout::loadOasis(const KoXmlElement &style)
00061 {
00062     KoXmlElement properties( KoDom::namedItemNS( style, KoXmlNS::style, "page-layout-properties" ) );
00063     if ( !properties.isNull() )
00064     {
00065         ptWidth = KoUnit::parseValue(properties.attributeNS( KoXmlNS::fo, "page-width", QString::null ) );
00066         ptHeight = KoUnit::parseValue(properties.attributeNS( KoXmlNS::fo, "page-height", QString::null ) );
00067         if (properties.attributeNS( KoXmlNS::style, "print-orientation", QString::null)=="portrait")
00068             orientation=PG_PORTRAIT;
00069         else
00070             orientation=PG_LANDSCAPE;
00071         ptRight = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-right", QString::null ) );
00072         ptBottom = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-bottom", QString::null ) );
00073         ptLeft = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-left", QString::null ) );
00074         ptTop = KoUnit::parseValue( properties.attributeNS( KoXmlNS::fo, "margin-top", QString::null ) );
00075         // guessFormat takes millimeters
00076         if ( orientation == PG_LANDSCAPE )
00077             format = KoPageFormat::guessFormat( POINT_TO_MM(ptHeight), POINT_TO_MM(ptWidth) );
00078         else
00079             format = KoPageFormat::guessFormat( POINT_TO_MM(ptWidth), POINT_TO_MM(ptHeight) );
00080     }
00081 }
00082 
00083 KoPageLayout KoPageLayout::standardLayout()
00084 {
00085     KoPageLayout layout;
00086     layout.format = KoPageFormat::defaultFormat();
00087     layout.orientation = PG_PORTRAIT;
00088     layout.ptWidth = MM_TO_POINT( KoPageFormat::width( layout.format, layout.orientation ) );
00089     layout.ptHeight = MM_TO_POINT( KoPageFormat::height( layout.format, layout.orientation ) );
00090     layout.ptLeft = MM_TO_POINT( 20.0 );
00091     layout.ptRight = MM_TO_POINT( 20.0 );
00092     layout.ptTop = MM_TO_POINT( 20.0 );
00093     layout.ptBottom = MM_TO_POINT( 20.0 );
00094     layout.ptPageEdge = -1;
00095     layout.ptBindingSide = -1;
00096     return layout;
00097 }
00098 
00099 struct PageFormatInfo
00100 {
00101     KoFormat format;
00102     KPrinter::PageSize kprinter;
00103     const char* shortName; // Short name
00104     const char* descriptiveName; // Full name, which will be translated
00105     double width; // in mm
00106     double height; // in mm
00107 };
00108 
00109 // NOTES:
00110 // - the width and height of non-ISO formats are rounded
00111 // http://en.wikipedia.org/wiki/Paper_size can help
00112 // - the comments "should be..." indicates the exact values if the inch sizes would be multiplied by 25.4 mm/inch
00113 
00114 const PageFormatInfo pageFormatInfo[]=
00115 {
00116     { PG_DIN_A3,        KPrinter::A3,           "A3",           I18N_NOOP("ISO A3"),       297.0,  420.0 },
00117     { PG_DIN_A4,        KPrinter::A4,           "A4",           I18N_NOOP("ISO A4"),       210.0,  297.0 },
00118     { PG_DIN_A5,        KPrinter::A5,           "A5",           I18N_NOOP("ISO A5"),       148.0,  210.0 },
00119     { PG_US_LETTER,     KPrinter::Letter,       "Letter",       I18N_NOOP("US Letter"),    215.9,  279.4 },
00120     { PG_US_LEGAL,      KPrinter::Legal,        "Legal",        I18N_NOOP("US Legal"),     215.9,  355.6 },
00121     { PG_SCREEN,        KPrinter::A4,           "Screen",       I18N_NOOP("Screen"), PG_A4_HEIGHT, PG_A4_WIDTH }, // Custom, so fall back to A4
00122     { PG_CUSTOM,        KPrinter::A4,           "Custom",       I18N_NOOP2("Custom size", "Custom"), PG_A4_WIDTH, PG_A4_HEIGHT }, // Custom, so fall back to A4
00123     { PG_DIN_B5,        KPrinter::B5,           "B5",           I18N_NOOP("ISO B5"),       182.0,  257.0 },
00124     // Hmm, wikipedia says 184.15 * 266.7 for executive !
00125     { PG_US_EXECUTIVE,  KPrinter::Executive,    "Executive",    I18N_NOOP("US Executive"), 191.0,  254.0 }, // should be 190.5 mm x 254.0 mm
00126     { PG_DIN_A0,        KPrinter::A0,           "A0",           I18N_NOOP("ISO A0"),       841.0, 1189.0 },
00127     { PG_DIN_A1,        KPrinter::A1,           "A1",           I18N_NOOP("ISO A1"),       594.0,  841.0 },
00128     { PG_DIN_A2,        KPrinter::A2,           "A2",           I18N_NOOP("ISO A2"),       420.0,  594.0 },
00129     { PG_DIN_A6,        KPrinter::A6,           "A6",           I18N_NOOP("ISO A6"),       105.0,  148.0 },
00130     { PG_DIN_A7,        KPrinter::A7,           "A7",           I18N_NOOP("ISO A7"),        74.0,  105.0 },
00131     { PG_DIN_A8,        KPrinter::A8,           "A8",           I18N_NOOP("ISO A8"),        52.0,   74.0 },
00132     { PG_DIN_A9,        KPrinter::A9,           "A9",           I18N_NOOP("ISO A9"),        37.0,   52.0 },
00133     { PG_DIN_B0,        KPrinter::B0,           "B0",           I18N_NOOP("ISO B0"),      1030.0, 1456.0 },
00134     { PG_DIN_B1,        KPrinter::B1,           "B1",           I18N_NOOP("ISO B1"),       728.0, 1030.0 },
00135     { PG_DIN_B10,       KPrinter::B10,          "B10",          I18N_NOOP("ISO B10"),       32.0,   45.0 },
00136     { PG_DIN_B2,        KPrinter::B2,           "B2",           I18N_NOOP("ISO B2"),       515.0,  728.0 },
00137     { PG_DIN_B3,        KPrinter::B3,           "B3",           I18N_NOOP("ISO B3"),       364.0,  515.0 },
00138     { PG_DIN_B4,        KPrinter::B4,           "B4",           I18N_NOOP("ISO B4"),       257.0,  364.0 },
00139     { PG_DIN_B6,        KPrinter::B6,           "B6",           I18N_NOOP("ISO B6"),       128.0,  182.0 },
00140     { PG_ISO_C5,        KPrinter::C5E,          "C5",           I18N_NOOP("ISO C5"),       163.0,  229.0 }, // Some sources tells: 162 mm x 228 mm
00141     { PG_US_COMM10,     KPrinter::Comm10E,      "Comm10",       I18N_NOOP("US Common 10"), 105.0,  241.0 }, // should be 104.775 mm x 241.3 mm
00142     { PG_ISO_DL,        KPrinter::DLE,          "DL",           I18N_NOOP("ISO DL"),       110.0,  220.0 },
00143     { PG_US_FOLIO,      KPrinter::Folio,        "Folio",        I18N_NOOP("US Folio"),     210.0,  330.0 }, // should be 209.54 mm x 330.2 mm
00144     { PG_US_LEDGER,     KPrinter::Ledger,       "Ledger",       I18N_NOOP("US Ledger"),    432.0,  279.0 }, // should be 431.8 mm x 297.4 mm
00145     { PG_US_TABLOID,    KPrinter::Tabloid,      "Tabloid",      I18N_NOOP("US Tabloid"),   279.0,  432.0 }  // should be 297.4 mm x 431.8 mm
00146 };
00147 
00148 int KoPageFormat::printerPageSize( KoFormat format )
00149 {
00150     if ( format == PG_SCREEN )
00151     {
00152             kWarning() << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE." << endl;
00153             return KPrinter::A4;
00154     }
00155     else if ( format == PG_CUSTOM )
00156     {
00157             kWarning() << "The used page layout (CUSTOM) is not supported by KPrinter. Printing in A4." << endl;
00158             return KPrinter::A4;
00159     }
00160     else if ( format <= PG_LAST_FORMAT )
00161         return pageFormatInfo[ format ].kprinter;
00162     else
00163         return KPrinter::A4;
00164 }
00165 
00166 double KoPageFormat::width( KoFormat format, KoOrientation orientation )
00167 {
00168     if ( orientation == PG_LANDSCAPE )
00169         return height( format, PG_PORTRAIT );
00170     if ( format <= PG_LAST_FORMAT )
00171         return pageFormatInfo[ format ].width;
00172     return PG_A4_WIDTH;   // should never happen
00173 }
00174 
00175 double KoPageFormat::height( KoFormat format, KoOrientation orientation )
00176 {
00177     if ( orientation == PG_LANDSCAPE )
00178         return width( format, PG_PORTRAIT );
00179     if ( format <= PG_LAST_FORMAT )
00180         return pageFormatInfo[ format ].height;
00181     return PG_A4_HEIGHT;
00182 }
00183 
00184 KoFormat KoPageFormat::guessFormat( double width, double height )
00185 {
00186     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00187     {
00188         // We have some tolerance. 1pt is a third of a mm, this is
00189         // barely noticeable for a page size.
00190         if ( i != PG_CUSTOM
00191              && qAbs( width - pageFormatInfo[i].width ) < 1.0
00192              && qAbs( height - pageFormatInfo[i].height ) < 1.0 )
00193             return static_cast<KoFormat>(i);
00194     }
00195     return PG_CUSTOM;
00196 }
00197 
00198 QString KoPageFormat::formatString( KoFormat format )
00199 {
00200     if ( format <= PG_LAST_FORMAT )
00201         return QString::fromLatin1( pageFormatInfo[ format ].shortName );
00202     return QString::fromLatin1( "A4" );
00203 }
00204 
00205 KoFormat KoPageFormat::formatFromString( const QString & string )
00206 {
00207     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00208     {
00209         if (string == QString::fromLatin1( pageFormatInfo[ i ].shortName ))
00210             return pageFormatInfo[ i ].format;
00211     }
00212     // We do not know the format name, so we have a custom format
00213     return PG_CUSTOM;
00214 }
00215 
00216 KoFormat KoPageFormat::defaultFormat()
00217 {
00218     int kprinter = KGlobal::locale()->pageSize();
00219     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00220     {
00221         if ( pageFormatInfo[ i ].kprinter == kprinter )
00222             return static_cast<KoFormat>(i);
00223     }
00224     return PG_DIN_A4;
00225 }
00226 
00227 QString KoPageFormat::name( KoFormat format )
00228 {
00229     if ( format <= PG_LAST_FORMAT )
00230         return i18n( pageFormatInfo[ format ].descriptiveName );
00231     return i18n( pageFormatInfo[ PG_DIN_A4 ].descriptiveName );
00232 }
00233 
00234 QStringList KoPageFormat::allFormats()
00235 {
00236     QStringList lst;
00237     for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i )
00238     {
00239         lst << i18n( pageFormatInfo[ i ].descriptiveName );
00240     }
00241     return lst;
00242 }

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1