1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQApplication.cc
15 Author: Stefan Hundhammer <sh@suse.de>
20 #include <Qt/qapplication.h>
21 #include <Qt/qlocale.h>
24 #define y2log_component "qt-ui"
25 #include <ycp/y2log.h>
27 #include "YQApplication.h"
30 YQApplication::YQApplication()
35 , _fontFamily( "Sans Serif" )
37 , _qtTranslations( 0 )
39 , _autoNormalFontSize( -1 )
40 , _autoHeadingFontSize( -1 )
42 y2debug( "YQApplication constructor start" );
44 setIconBasePath( ICONDIR "/icons/22x22/apps/" );
45 loadPredefinedQtTranslations();
47 y2debug( "YQApplication constructor end" );
51 YQApplication::~YQApplication()
56 if ( _qtTranslations )
57 delete _qtTranslations;
64 YQApplication::setLanguage( const string & language,
65 const string & encoding )
67 YApplication::setLanguage( language, encoding );
68 loadPredefinedQtTranslations();
69 setLangFonts( language, encoding );
75 YQApplication::loadPredefinedQtTranslations()
77 QString path = QT_LOCALEDIR;
78 QString language = QLocale::system().name();
80 QString transFile = QString( "qt_%1.qm")
81 .arg( language.lower().replace('_','-') );
85 y2warning( "Qt locale directory not set - "
86 "no translations for predefined Qt dialogs" );
90 if ( ! _qtTranslations )
91 _qtTranslations = new QTranslator();
93 _qtTranslations->load( transFile, path );
95 if ( _qtTranslations->isEmpty() )
98 transFile = QString( "qt_%1.qm").arg( language.lower().left(2) );
99 _qtTranslations->load( transFile, path );
102 if ( _qtTranslations->isEmpty() )
104 y2warning( "Can't load translations for predefined Qt dialogs from %s/%s",
105 (const char *) path, (const char *) transFile );
109 y2milestone( "Loaded translations for predefined Qt dialogs from %s/%s",
110 (const char *) path, (const char *) transFile );
112 qApp->installTranslator( _qtTranslations );
116 // Force reverse layout for Arabic and Hebrew
118 if ( ( language.startsWith( "ar" ) || // Arabic
119 language.startsWith( "he" ) ) // Hebrew
120 && ! qApp->reverseLayout() )
122 y2warning( "Using fallback rule for reverse layout for language '%s'",
123 (const char *) language );
125 qApp->setReverseLayout( true );
131 YQApplication::setLangFonts( const string & language, const string & encoding )
133 QString oldFontFamily = _fontFamily;
137 _langFonts = new QY2Settings( LANG_FONTS_FILE );
138 Q_CHECK_PTR( _langFonts );
140 if ( _langFonts->readError() )
141 y2error( "Error reading %s", (const char *) _langFonts->fileName() );
143 y2milestone( "%s read OK", (const char *) _langFonts->fileName() );
146 QString lang = language.c_str();
148 if ( ! encoding.empty() )
149 lang += QString( "." ) + encoding.c_str();
153 if ( ! _langFonts->hasKey( fontKey( lang ) ) ) // Try with encoding ("zh_CN.UTF8" etc.)
155 lang = language.c_str(); // Try without encoding ("zh_CN")
157 if ( ! _langFonts->hasKey( fontKey( lang ) ) )
158 lang.replace( QRegExp( "_.*$" ), "" ); // Cut off trailing country ("_CN")
161 if ( _langFonts->hasKey( fontKey( lang ) ) )
163 _fontFamily = _langFonts->get( fontKey( lang ), "Sans Serif" );
164 y2milestone( "%s = \"%s\"", (const char *) fontKey( lang ), (const char *) _fontFamily );
168 _fontFamily = _langFonts->get( fontKey( "" ), "Sans Serif" );
169 y2milestone( "Using fallback for %s: font = \"%s\"",
170 (const char *) lang, (const char *) _fontFamily );
173 if ( _fontFamily != oldFontFamily && ! _fontFamily.isEmpty() )
175 y2milestone( "New font family: %s", _fontFamily.latin1() );
177 int size = qApp->font().pointSize();
178 QFont font( _fontFamily );
179 font.setPointSize( size );
180 qApp->setFont( font, true ); // font, informWidgets
181 y2milestone( "Reloading fonts - now using \"%s\"",
182 (const char *) font.toString() );
186 y2debug( "No font change" );
192 YQApplication::fontKey( const QString & lang )
194 if ( lang.isEmpty() )
197 return QString( "font[%1]").arg( lang );
202 YQApplication::currentFont()
205 * Brute force approach to make sure we'll really get a complete Unicode font:
206 * Explicitly load the one font that we made sure to contain all required
207 * characters, including Latin1, Latin2, Japanese, Korean, and the
208 * characters used for glyphs.
210 * There are many fonts that claim to be Unicode, but most of them contain
211 * just a sorry excuse for a complete Unicode character set. Qt can't know
212 * how complete a font is, so it chooses one that might be better in otherf
213 * aspects, but lacks necessary characters.
216 if ( ! _currentFont )
222 _currentFont = new QFont( _fontFamily );
223 _currentFont->setPixelSize( _autoNormalFontSize );
224 _currentFont->setWeight( QFont::Normal );
226 y2milestone( "Loaded %d pixel font: %s", _autoNormalFontSize,
227 (const char *) _currentFont->toString() );
229 qApp->setFont( * _currentFont, true ); // font, informWidgets
233 // y2debug( "Copying QApplication::font()" );
234 _currentFont = new QFont( qApp->font() );
238 return * _currentFont;
243 YQApplication::boldFont()
247 _boldFont = new QFont( currentFont() );
248 _boldFont->setBold( true );
256 YQApplication::headingFont()
259 * Brute force load the heading font - see currentFont() above for more.
262 if ( ! _headingFont )
268 _headingFont = new QFont( _fontFamily );
269 _headingFont->setPixelSize( _autoHeadingFontSize );
270 _headingFont->setWeight( QFont::Bold );
272 y2milestone( "Loaded %d pixel bold font: %s", _autoHeadingFontSize,
273 (const char *) _headingFont->toString() );
277 _headingFont = new QFont( _fontFamily, 14, QFont::Bold );
281 return * _headingFont;
286 YQApplication::deleteFonts()
304 YQApplication::setAutoFonts( bool useAutoFonts )
306 _autoFonts = useAutoFonts;
311 YQApplication::pickAutoFonts()
313 if ( _autoNormalFontSize >= 0 ) // Use cached values
316 #warning FIXME: defaultSize
318 int x = _default_size.width();
319 int y = _default_size.height();
328 if ( x >= 800 && y >= 600 )
334 if ( x >= 1024 && y >= 768 )
340 if ( x >= 1280 && y >= 1024 )
358 if ( x >= 2048 ) // Sounds futuristic? Just wait one or two years...
364 _autoNormalFontSize = normal;
365 _autoHeadingFontSize = heading;
367 y2milestone( "Selecting auto fonts - normal: %d, heading: %d (bold)",
368 _autoNormalFontSize, _autoHeadingFontSize );
374 #include "YQApplication.moc"