lib Library API Documentation

koGlobal.cc

00001 /* This file is part of the KDE project 00002 Copyright (C) 2001 David Faure <faure@kde.org> 00003 Copyright 2003 Nicolas GOUTTE <goutte@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 #include "config.h" 00022 #include <koGlobal.h> 00023 #include <kdebug.h> 00024 #include <klocale.h> 00025 #include <kprinter.h> 00026 #include <qfont.h> 00027 #include <qfontinfo.h> 00028 #include <kglobalsettings.h> 00029 #include <kglobal.h> 00030 #include <ksimpleconfig.h> 00031 #include <kstandarddirs.h> 00032 #include <kstaticdeleter.h> 00033 #include <kimageio.h> 00034 #include <kiconloader.h> 00035 #include <kstandarddirs.h> 00036 00037 struct PageFormatInfo 00038 { 00039 KoFormat format; 00040 KPrinter::PageSize kprinter; 00041 const char* shortName; // Short name 00042 const char* descriptiveName; // Full name, which will be translated 00043 double width; // in mm 00044 double height; // in mm 00045 }; 00046 00047 // NOTES: 00048 // - the width and height of non-ISO formats are rounded 00049 // - the comments "should be..." indicates the exact values if the inch sizes would be multiplied by 25.4 mm/inch 00050 00051 const PageFormatInfo pageFormatInfo[]= 00052 { 00053 { PG_DIN_A3, KPrinter::A3, "A3", I18N_NOOP("ISO A3"), 297.0, 420.0 }, 00054 { PG_DIN_A4, KPrinter::A4, "A4", I18N_NOOP("ISO A4"), 210.0, 297.0 }, 00055 { PG_DIN_A5, KPrinter::A5, "A5", I18N_NOOP("ISO A5"), 148.0, 210.0 }, 00056 { PG_US_LETTER, KPrinter::Letter, "Letter", I18N_NOOP("US Letter"), 216.0, 279.0 }, // should be 215.9 mm x 279.4 mm 00057 { PG_US_LEGAL, KPrinter::Legal, "Legal", I18N_NOOP("US Legal"), 216.0, 356.0 }, // should be 215.9 mm x 335.6 mm 00058 { PG_SCREEN, KPrinter::A4, "Screen", I18N_NOOP("Screen"), PG_A4_HEIGHT, PG_A4_WIDTH }, // Custom, so fall back to A4 00059 { PG_CUSTOM, KPrinter::A4, "Custom", I18N_NOOP("Custom"), PG_A4_WIDTH, PG_A4_HEIGHT }, // Custom, so fall back to A4 00060 { PG_DIN_B5, KPrinter::B5, "B5", I18N_NOOP("ISO B5"), 182.0, 257.0 }, 00061 { PG_US_EXECUTIVE, KPrinter::Executive, "Executive", I18N_NOOP("US Executive"), 191.0, 254.0 }, // should be 190.5 mm x 254.0 mm 00062 { PG_DIN_A0, KPrinter::A0, "A0", I18N_NOOP("ISO A0"), 841.0, 1189.0 }, 00063 { PG_DIN_A1, KPrinter::A1, "A1", I18N_NOOP("ISO A1"), 594.0, 841.0 }, 00064 { PG_DIN_A2, KPrinter::A2, "A2", I18N_NOOP("ISO A2"), 420.0, 594.0 }, 00065 { PG_DIN_A6, KPrinter::A6, "A6", I18N_NOOP("ISO A6"), 105.0, 148.0 }, 00066 { PG_DIN_A7, KPrinter::A7, "A7", I18N_NOOP("ISO A7"), 74.0, 105.0 }, 00067 { PG_DIN_A8, KPrinter::A8, "A8", I18N_NOOP("ISO A8"), 52.0, 74.0 }, 00068 { PG_DIN_A9, KPrinter::A9, "A9", I18N_NOOP("ISO A9"), 37.0, 52.0 }, 00069 { PG_DIN_B0, KPrinter::B0, "B0", I18N_NOOP("ISO B0"), 1030.0, 1456.0 }, 00070 { PG_DIN_B1, KPrinter::B1, "B1", I18N_NOOP("ISO B1"), 728.0, 1030.0 }, 00071 { PG_DIN_B10, KPrinter::B10, "B10", I18N_NOOP("ISO B10"), 32.0, 45.0 }, 00072 { PG_DIN_B2, KPrinter::B2, "B2", I18N_NOOP("ISO B2"), 515.0, 728.0 }, 00073 { PG_DIN_B3, KPrinter::B3, "B3", I18N_NOOP("ISO B3"), 364.0, 515.0 }, 00074 { PG_DIN_B4, KPrinter::B4, "B4", I18N_NOOP("ISO B4"), 257.0, 364.0 }, 00075 { PG_DIN_B6, KPrinter::B6, "B6", I18N_NOOP("ISO B6"), 128.0, 182.0 }, 00076 { PG_ISO_C5, KPrinter::C5E, "C5", I18N_NOOP("ISO C5"), 163.0, 229.0 }, // Some sources tells: 162 mm x 228 mm 00077 { PG_US_COMM10, KPrinter::Comm10E, "Comm10", I18N_NOOP("US Common 10"), 105.0, 241.0 }, // should be 104.775 mm x 241.3 mm 00078 { PG_ISO_DL, KPrinter::DLE, "DL", I18N_NOOP("ISO DL"), 110.0, 220.0 }, 00079 { PG_US_FOLIO, KPrinter::Folio, "Folio", I18N_NOOP("US Folio"), 210.0, 330.0 }, // should be 209.54 mm x 330.2 mm 00080 { PG_US_LEDGER, KPrinter::Ledger, "Ledger", I18N_NOOP("US Ledger"), 432.0, 279.0 }, // should be 431.8 mm x 297.4 mm 00081 { PG_US_TABLOID, KPrinter::Tabloid, "Tabloid", I18N_NOOP("US Tabloid"), 279.0, 432.0 } // should be 297.4 mm x 431.8 mm 00082 }; 00083 00084 int KoPageFormat::printerPageSize( KoFormat format ) 00085 { 00086 if ( format == PG_SCREEN ) 00087 { 00088 kdWarning() << "You use the page layout SCREEN. Printing in DIN A4 LANDSCAPE." << endl; 00089 return KPrinter::A4; 00090 } 00091 else if ( format == PG_CUSTOM ) 00092 { 00093 kdWarning() << "The used page layout (CUSTOM) is not supported by KPrinter. Printing in A4." << endl; 00094 return KPrinter::A4; 00095 } 00096 else if ( format <= PG_LAST_FORMAT ) 00097 return pageFormatInfo[ format ].kprinter; 00098 else 00099 return KPrinter::A4; 00100 } 00101 00102 double KoPageFormat::width( KoFormat format, KoOrientation orientation ) 00103 { 00104 if ( orientation == PG_LANDSCAPE ) 00105 return height( format, PG_PORTRAIT ); 00106 if ( format <= PG_LAST_FORMAT ) 00107 return pageFormatInfo[ format ].width; 00108 return PG_A4_WIDTH; // should never happen 00109 } 00110 00111 double KoPageFormat::height( KoFormat format, KoOrientation orientation ) 00112 { 00113 if ( orientation == PG_LANDSCAPE ) 00114 return width( format, PG_PORTRAIT ); 00115 if ( format <= PG_LAST_FORMAT ) 00116 return pageFormatInfo[ format ].height; 00117 return PG_A4_HEIGHT; 00118 } 00119 00120 KoFormat KoPageFormat::guessFormat( double width, double height ) 00121 { 00122 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) 00123 { 00124 // We have some tolerance. 1pt is a third of a mm, this is 00125 // barely noticeable for a page size. 00126 if ( i != PG_CUSTOM 00127 && kAbs( width - pageFormatInfo[i].width ) < 1.0 00128 && kAbs( height - pageFormatInfo[i].height ) < 1.0 ) 00129 return static_cast<KoFormat>(i); 00130 } 00131 return PG_CUSTOM; 00132 } 00133 00134 QString KoPageFormat::formatString( KoFormat format ) 00135 { 00136 if ( format <= PG_LAST_FORMAT ) 00137 return QString::fromLatin1( pageFormatInfo[ format ].shortName ); 00138 return QString::fromLatin1( "A4" ); 00139 } 00140 00141 KoFormat KoPageFormat::formatFromString( const QString & string ) 00142 { 00143 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) 00144 { 00145 if (string == QString::fromLatin1( pageFormatInfo[ i ].shortName )) 00146 return pageFormatInfo[ i ].format; 00147 } 00148 // We do not know the format name, so we have a custom format 00149 return PG_CUSTOM; 00150 } 00151 00152 QString KoPageFormat::name( KoFormat format ) 00153 { 00154 if ( format <= PG_LAST_FORMAT ) 00155 return i18n( pageFormatInfo[ format ].descriptiveName ); 00156 return i18n( pageFormatInfo[ PG_DIN_A4 ].descriptiveName ); 00157 } 00158 00159 QStringList KoPageFormat::allFormats() 00160 { 00161 QStringList lst; 00162 for ( int i = 0 ; i <= PG_LAST_FORMAT ; ++i ) 00163 { 00164 lst << i18n( pageFormatInfo[ i ].descriptiveName ); 00165 } 00166 return lst; 00167 } 00168 00169 KoGlobal* KoGlobal::s_global = 0L; 00170 static KStaticDeleter<KoGlobal> sdg; 00171 00172 KoGlobal* KoGlobal::self() 00173 { 00174 if ( !s_global ) 00175 sdg.setObject( s_global, new KoGlobal ); 00176 return s_global; 00177 } 00178 00179 KoGlobal::KoGlobal() 00180 : m_pointSize( -1 ), m_kofficeConfig( 0L ) 00181 { 00182 // Install the libkoffice* translations 00183 KGlobal::locale()->insertCatalogue("koffice"); 00184 00185 KImageIO::registerFormats(); 00186 00187 // Tell KStandardDirs about the koffice prefix 00188 KGlobal::dirs()->addPrefix(PREFIX); 00189 00190 // Tell the iconloader about share/apps/koffice/icons 00191 KGlobal::iconLoader()->addAppDir("koffice"); 00192 } 00193 00194 KoGlobal::~KoGlobal() 00195 { 00196 delete m_kofficeConfig; 00197 } 00198 00199 QFont KoGlobal::_defaultFont() 00200 { 00201 QFont font = KGlobalSettings::generalFont(); 00202 // we have to use QFontInfo, in case the font was specified with a pixel size 00203 if ( font.pointSize() == -1 ) 00204 { 00205 // cache size into m_pointSize, since QFontInfo loads the font -> slow 00206 if ( m_pointSize == -1 ) 00207 m_pointSize = QFontInfo(font).pointSize(); 00208 Q_ASSERT( m_pointSize != -1 ); 00209 font.setPointSize( m_pointSize ); 00210 } 00211 //kdDebug()<<k_funcinfo<<"QFontInfo(font).pointSize() :"<<QFontInfo(font).pointSize()<<endl; 00212 //kdDebug()<<k_funcinfo<<"font.name() :"<<font.family ()<<endl; 00213 return font; 00214 } 00215 00216 QStringList KoGlobal::_listTagOfLanguages() 00217 { 00218 if ( m_languageTag.isEmpty() ) 00219 createListOfLanguages(); 00220 return m_languageTag; 00221 } 00222 00223 QStringList KoGlobal::_listOfLanguages() 00224 { 00225 if ( m_languageList.empty() ) 00226 createListOfLanguages(); 00227 return m_languageList; 00228 } 00229 00230 void KoGlobal::createListOfLanguages() 00231 { 00232 QStringList alllang = KGlobal::dirs()->findAllResources("locale", 00233 QString::fromLatin1("*/entry.desktop")); 00234 QStringList langlist=alllang; 00235 for ( QStringList::ConstIterator it = langlist.begin(); 00236 it != langlist.end(); ++it ) 00237 { 00238 KSimpleConfig entry(*it); 00239 entry.setGroup("KCM Locale"); 00240 QString name = entry.readEntry("Name", 00241 KGlobal::locale()->translate("without name")); 00242 00243 QString tag = *it; 00244 int index = tag.findRev('/'); 00245 tag = tag.left(index); 00246 index = tag.findRev('/'); 00247 tag = tag.mid(index+1); 00248 m_languageList.append(name); 00249 m_languageTag.append(tag); 00250 } 00251 00252 } 00253 00254 QString KoGlobal::tagOfLanguage( const QString & _lang) 00255 { 00256 // Should use iterator... 00257 int pos = self()->m_languageList.findIndex( _lang ); 00258 if ( pos != -1) 00259 { 00260 return self()->m_languageTag[ pos ]; 00261 } 00262 return QString::null; 00263 } 00264 00265 int KoGlobal::languageIndexFromTag( const QString &_lang ) 00266 { 00267 return self()->m_languageTag.findIndex( _lang ); 00268 } 00269 00270 QString KoGlobal::languageFromTag( const QString &_lang ) 00271 { 00272 // should use iterator 00273 int pos = self()->m_languageTag.findIndex( _lang ); 00274 if ( pos != -1) 00275 return self()->m_languageList[ pos ]; 00276 else 00277 return QString::null; 00278 } 00279 00280 KConfig* KoGlobal::_kofficeConfig() 00281 { 00282 if ( !m_kofficeConfig ) { 00283 m_kofficeConfig = new KConfig( "kofficerc" ); 00284 } 00285 return m_kofficeConfig; 00286 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 06:54:15 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003