]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQApplication.cc
drawContent is no more
[duncan/yast2-qt4.git] / src / YQApplication.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQApplication.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <Qt/qapplication.h>
21 #include <Qt/qlocale.h>
22 #include <qregexp.h>
23
24 #define y2log_component "qt-ui"
25 #include <ycp/y2log.h>
26
27 #include "YQApplication.h"
28
29
30 YQApplication::YQApplication()
31     : YApplication()
32     , _currentFont( 0 )
33     , _headingFont( 0 )
34     , _boldFont( 0 )
35     , _fontFamily( "Sans Serif" )
36     , _langFonts( 0 )
37     , _qtTranslations( 0 )
38     , _autoFonts( false )
39     , _autoNormalFontSize( -1 )
40     , _autoHeadingFontSize( -1 )
41 {
42     y2debug( "YQApplication constructor start" );
43
44     setIconBasePath( ICONDIR "/icons/22x22/apps/" );
45     loadPredefinedQtTranslations();
46
47     y2debug( "YQApplication constructor end" );
48 }
49
50
51 YQApplication::~YQApplication()
52 {
53     if ( _langFonts )
54         delete _langFonts;
55
56     if ( _qtTranslations )
57         delete _qtTranslations;
58
59     deleteFonts();
60 }
61
62
63 void
64 YQApplication::setLanguage( const string & language,
65                             const string & encoding )
66 {
67     YApplication::setLanguage( language, encoding );
68     loadPredefinedQtTranslations();
69     setLangFonts( language, encoding );
70 }
71
72
73
74 void
75 YQApplication::loadPredefinedQtTranslations()
76 {
77     QString path = QT_LOCALEDIR;
78     QString language = QLocale::system().name();
79
80     QString transFile = QString( "qt_%1.qm")
81               .arg( language.toLower().replace('_','-') );
82
83     if ( path.isEmpty() )
84     {
85         y2warning( "Qt locale directory not set - "
86                    "no translations for predefined Qt dialogs" );
87         return;
88     }
89
90     if ( ! _qtTranslations )
91         _qtTranslations = new QTranslator();
92
93     _qtTranslations->load( transFile, path );
94
95     if ( _qtTranslations->isEmpty() )
96     {
97         // try fallback
98         transFile = QString( "qt_%1.qm").arg( language.toLower().left(2) );
99         _qtTranslations->load( transFile, path );
100     }
101
102     if ( _qtTranslations->isEmpty() )
103     {
104         y2warning( "Can't load translations for predefined Qt dialogs from %s/%s",
105                    qPrintable(path), qPrintable(transFile) );
106     }
107     else
108     {
109         y2milestone( "Loaded translations for predefined Qt dialogs from %s/%s",
110                      qPrintable(path), qPrintable(transFile) );
111
112         qApp->installTranslator( _qtTranslations );
113     }
114
115
116     // Force reverse layout for Arabic and Hebrew
117
118     if ( ( language.startsWith( "ar" ) ||       // Arabic
119            language.startsWith( "he" ) )        // Hebrew
120          && ! (qApp->layoutDirection() == Qt::RightToLeft) )
121     {
122         y2warning( "Using fallback rule for reverse layout for language '%s'",
123                    qPrintable(language) );
124
125         qApp->setLayoutDirection( Qt::RightToLeft );
126     }
127 }
128
129
130 void
131 YQApplication::setLangFonts( const string & language, const string & encoding )
132 {
133     QString oldFontFamily = _fontFamily;
134
135     if ( ! _langFonts )
136     {
137         _langFonts = new QY2Settings( LANG_FONTS_FILE );
138         Q_CHECK_PTR( _langFonts );
139
140         if ( _langFonts->readError() )
141             y2error( "Error reading %s", qPrintable(_langFonts->fileName()) );
142         else
143             y2milestone( "%s read OK", qPrintable(_langFonts->fileName()) );
144     }
145
146     QString lang = language.c_str();
147
148     if ( ! encoding.empty() )
149         lang += QString( "." ) + encoding.c_str();
150
151     QString key;
152
153     if ( ! _langFonts->hasKey( fontKey( lang ) ) )      // Try with encoding ("zh_CN.UTF8" etc.)
154     {
155         lang = language.c_str();                        // Try without encoding ("zh_CN")
156
157         if ( ! _langFonts->hasKey( fontKey( lang ) ) )
158             lang.replace( QRegExp( "_.*$" ), "" );      // Cut off trailing country ("_CN")
159     }
160
161     if ( _langFonts->hasKey( fontKey( lang ) ) )
162     {
163         _fontFamily = _langFonts->get( fontKey( lang ), "Sans Serif" );
164         y2milestone( "%s = \"%s\"", qPrintable(fontKey( lang )), qPrintable(_fontFamily) );
165     }
166     else
167     {
168         _fontFamily = _langFonts->get( fontKey( "" ), "Sans Serif" );
169         y2milestone( "Using fallback for %s: font = \"%s\"",
170                      qPrintable(lang), qPrintable(_fontFamily) );
171     }
172
173     if ( _fontFamily != oldFontFamily && ! _fontFamily.isEmpty() )
174     {
175         y2milestone( "New font family: %s", qPrintable(_fontFamily) );
176         deleteFonts();
177         int size = qApp->font().pointSize();
178         QFont font( _fontFamily );
179         font.setPointSize( size );
180         qApp->setFont(font);    // font, informWidgets
181         y2milestone( "Reloading fonts - now using \"%s\"",
182                      qPrintable(font.toString()) );
183     }
184     else
185     {
186         y2debug( "No font change" );
187     }
188 }
189
190
191 QString
192 YQApplication::fontKey( const QString & lang )
193 {
194     if ( lang.isEmpty() )
195         return "font";
196     else
197         return QString( "font[%1]").arg( lang );
198 }
199
200
201 const QFont &
202 YQApplication::currentFont()
203 {
204     /**
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.
209      *
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.
214      **/
215
216     if ( ! _currentFont )
217     {
218         if ( autoFonts() )
219         {
220             pickAutoFonts();
221
222             _currentFont = new QFont( _fontFamily );
223             _currentFont->setPixelSize( _autoNormalFontSize );
224             _currentFont->setWeight( QFont::Normal );
225
226             y2milestone( "Loaded %d pixel font: %s", _autoNormalFontSize,
227                          qPrintable(_currentFont->toString()) );
228
229             qApp->setFont( * _currentFont);     // font, informWidgets
230         }
231         else
232         {
233             // y2debug( "Copying QApplication::font()" );
234             _currentFont = new QFont( qApp->font() );
235         }
236     }
237
238     return * _currentFont;
239 }
240
241
242 const QFont &
243 YQApplication::boldFont()
244 {
245     if ( ! _boldFont )
246     {
247         _boldFont = new QFont( currentFont() );
248         _boldFont->setBold( true );
249     }
250
251     return * _boldFont;
252 }
253
254
255 const QFont &
256 YQApplication::headingFont()
257 {
258     /**
259      * Brute force load the heading font - see currentFont() above for more.
260      **/
261
262     if ( ! _headingFont )
263     {
264         if ( autoFonts() )
265         {
266             pickAutoFonts();
267
268             _headingFont = new QFont( _fontFamily );
269             _headingFont->setPixelSize( _autoHeadingFontSize );
270             _headingFont->setWeight( QFont::Bold );
271
272             y2milestone( "Loaded %d pixel bold font: %s", _autoHeadingFontSize,
273                          qPrintable(_headingFont->toString()) );
274         }
275         else
276         {
277             _headingFont = new QFont( _fontFamily, 14, QFont::Bold );
278         }
279     }
280
281     return * _headingFont;
282 }
283
284
285 void
286 YQApplication::deleteFonts()
287 {
288     if ( _currentFont )
289         delete _currentFont;
290
291     if ( _headingFont )
292         delete _headingFont;
293
294     if ( _boldFont )
295         delete _boldFont;
296
297     _currentFont = 0;
298     _headingFont = 0;
299     _boldFont    = 0;
300 }
301
302
303 void
304 YQApplication::setAutoFonts( bool useAutoFonts )
305 {
306     _autoFonts = useAutoFonts;
307 }
308
309
310 void
311 YQApplication::pickAutoFonts()
312 {
313     if ( _autoNormalFontSize >= 0 )     // Use cached values
314         return;
315
316 #warning FIXME: defaultSize
317 #if 0
318     int x = _default_size.width();
319     int y = _default_size.height();
320 #endif
321     int x = 800;
322     int y = 600;
323
324
325     int normal  = 10;
326     int heading = 12;
327
328     if ( x >= 800 && y >= 600 )
329     {
330         normal  = 10;
331         heading = 12;
332     }
333
334     if ( x >= 1024 && y >= 768 )
335     {
336         normal  = 12;
337         heading = 14;
338     }
339
340     if ( x >= 1280 && y >= 1024 )
341     {
342         normal  = 14;
343         heading = 18;
344     }
345
346     if ( x >= 1400 )
347     {
348         normal  = 16;
349         heading = 20;
350     }
351
352     if ( x >= 1600 )
353     {
354         normal  = 18;
355         heading = 24;
356     }
357
358     if ( x >= 2048 )    // Sounds futuristic? Just wait one or two years...
359     {
360         normal  = 20;
361         heading = 28;
362     }
363
364     _autoNormalFontSize  = normal;
365     _autoHeadingFontSize = heading;
366
367     y2milestone( "Selecting auto fonts - normal: %d, heading: %d (bold)",
368                  _autoNormalFontSize, _autoHeadingFontSize );
369 }
370
371
372
373
374 #include "YQApplication.moc"