1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQApplication.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
21 #include <QApplication>
23 #include <unistd.h> // access()
25 #include <qfiledialog.h>
26 #include <qmessagebox.h>
28 #define y2log_component "qt-ui"
29 #include <ycp/y2log.h>
34 #include "YQApplication.h"
37 YQApplication::YQApplication()
42 , _fontFamily( "Sans Serif" )
44 , _qtTranslations( 0 )
46 , _autoNormalFontSize( -1 )
47 , _autoHeadingFontSize( -1 )
49 y2debug( "YQApplication constructor start" );
51 setIconBasePath( ICONDIR "/icons/22x22/apps/" );
52 loadPredefinedQtTranslations();
54 y2debug( "YQApplication constructor end" );
58 YQApplication::~YQApplication()
63 if ( _qtTranslations )
64 delete _qtTranslations;
71 YQApplication::setLanguage( const string & language,
72 const string & encoding )
74 YApplication::setLanguage( language, encoding );
75 loadPredefinedQtTranslations();
76 setLangFonts( language, encoding );
82 YQApplication::loadPredefinedQtTranslations()
84 QString path = QT_LOCALEDIR;
85 QString language = QLocale::system().name();
87 QString transFile = QString( "qt_%1.qm")
88 .arg( language.toLower().replace('_','-') );
92 y2warning( "Qt locale directory not set - "
93 "no translations for predefined Qt dialogs" );
97 if ( ! _qtTranslations )
98 _qtTranslations = new QTranslator();
100 _qtTranslations->load( transFile, path );
102 if ( _qtTranslations->isEmpty() )
105 transFile = QString( "qt_%1.qm").arg( language.toLower().left(2) );
106 _qtTranslations->load( transFile, path );
109 if ( _qtTranslations->isEmpty() )
111 y2warning( "Can't load translations for predefined Qt dialogs from %s/%s",
112 qPrintable(path), qPrintable(transFile) );
116 y2milestone( "Loaded translations for predefined Qt dialogs from %s/%s",
117 qPrintable(path), qPrintable(transFile) );
119 qApp->installTranslator( _qtTranslations );
123 // Force reverse layout for Arabic and Hebrew
125 if ( ( language.startsWith( "ar" ) || // Arabic
126 language.startsWith( "he" ) ) // Hebrew
127 && ! (qApp->layoutDirection() == Qt::RightToLeft) )
129 y2warning( "Using fallback rule for reverse layout for language '%s'",
130 qPrintable(language) );
132 qApp->setLayoutDirection( Qt::RightToLeft );
138 YQApplication::setLangFonts( const string & language, const string & encoding )
140 QString oldFontFamily = _fontFamily;
144 _langFonts = new QY2Settings( LANG_FONTS_FILE );
145 Q_CHECK_PTR( _langFonts );
147 if ( _langFonts->readError() )
148 y2error( "Error reading %s", qPrintable(_langFonts->fileName()) );
150 y2milestone( "%s read OK", qPrintable(_langFonts->fileName()) );
153 QString lang = language.c_str();
155 if ( ! encoding.empty() )
156 lang += QString( "." ) + encoding.c_str();
160 if ( ! _langFonts->hasKey( fontKey( lang ) ) ) // Try with encoding ("zh_CN.UTF8" etc.)
162 lang = language.c_str(); // Try without encoding ("zh_CN")
164 if ( ! _langFonts->hasKey( fontKey( lang ) ) )
165 lang.replace( QRegExp( "_.*$" ), "" ); // Cut off trailing country ("_CN")
168 if ( _langFonts->hasKey( fontKey( lang ) ) )
170 _fontFamily = _langFonts->get( fontKey( lang ), "Sans Serif" );
171 y2milestone( "%s = \"%s\"", qPrintable(fontKey( lang )), qPrintable(_fontFamily) );
175 _fontFamily = _langFonts->get( fontKey( "" ), "Sans Serif" );
176 y2milestone( "Using fallback for %s: font = \"%s\"",
177 qPrintable(lang), qPrintable(_fontFamily) );
180 if ( _fontFamily != oldFontFamily && ! _fontFamily.isEmpty() )
182 y2milestone( "New font family: %s", qPrintable(_fontFamily) );
184 int size = qApp->font().pointSize();
185 QFont font( _fontFamily );
186 font.setPointSize( size );
187 qApp->setFont(font); // font, informWidgets
188 y2milestone( "Reloading fonts - now using \"%s\"",
189 qPrintable(font.toString()) );
193 y2debug( "No font change" );
199 YQApplication::fontKey( const QString & lang )
201 if ( lang.isEmpty() )
204 return QString( "font[%1]").arg( lang );
209 YQApplication::currentFont()
212 * Brute force approach to make sure we'll really get a complete Unicode font:
213 * Explicitly load the one font that we made sure to contain all required
214 * characters, including Latin1, Latin2, Japanese, Korean, and the
215 * characters used for glyphs.
217 * There are many fonts that claim to be Unicode, but most of them contain
218 * just a sorry excuse for a complete Unicode character set. Qt can't know
219 * how complete a font is, so it chooses one that might be better in otherf
220 * aspects, but lacks necessary characters.
223 if ( ! _currentFont )
229 _currentFont = new QFont( _fontFamily );
230 _currentFont->setPixelSize( _autoNormalFontSize );
231 _currentFont->setWeight( QFont::Normal );
233 y2milestone( "Loaded %d pixel font: %s", _autoNormalFontSize,
234 qPrintable(_currentFont->toString()) );
236 qApp->setFont( * _currentFont); // font, informWidgets
240 // y2debug( "Copying QApplication::font()" );
241 _currentFont = new QFont( qApp->font() );
245 return * _currentFont;
250 YQApplication::boldFont()
254 _boldFont = new QFont( currentFont() );
255 _boldFont->setBold( true );
263 YQApplication::headingFont()
266 * Brute force load the heading font - see currentFont() above for more.
269 if ( ! _headingFont )
275 _headingFont = new QFont( _fontFamily );
276 _headingFont->setPixelSize( _autoHeadingFontSize );
277 _headingFont->setWeight( QFont::Bold );
279 y2milestone( "Loaded %d pixel bold font: %s", _autoHeadingFontSize,
280 qPrintable(_headingFont->toString()) );
284 _headingFont = new QFont( _fontFamily, 14, QFont::Bold );
288 return * _headingFont;
293 YQApplication::deleteFonts()
311 YQApplication::setAutoFonts( bool useAutoFonts )
313 _autoFonts = useAutoFonts;
318 YQApplication::pickAutoFonts()
320 if ( _autoNormalFontSize >= 0 ) // Use cached values
323 #warning FIXME: defaultSize
325 int x = _default_size.width();
326 int y = _default_size.height();
334 if ( x >= 800 && y >= 600 )
340 if ( x >= 1024 && y >= 768 )
346 if ( x >= 1280 && y >= 1024 )
364 if ( x >= 2048 ) // Sounds futuristic? Just wait one or two years...
370 _autoNormalFontSize = normal;
371 _autoHeadingFontSize = heading;
373 y2milestone( "Selecting auto fonts - normal: %d, heading: %d (bold)",
374 _autoNormalFontSize, _autoHeadingFontSize );
379 YQApplication::askForExistingDirectory( const string & startDir,
380 const string & headline )
387 QFileDialog::getExistingDirectory( 0,
388 fromUTF8( startDir ),
389 fromUTF8( headline ) ); // caption
394 return toUTF8( dirName );
399 YQApplication::askForExistingFile( const string & startWith,
400 const string & filter,
401 const string & headline )
408 QFileDialog::getOpenFileName( 0, fromUTF8( startWith ),
410 fromUTF8( headline ) ); // caption
416 return toUTF8( fileName );
421 YQApplication::askForSaveFileName( const string & startWith,
422 const string & filter,
423 const string & headline )
429 QString fileName = askForSaveFileName( fromUTF8( startWith ),
431 fromUTF8( headline ) );
436 return toUTF8( fileName );
442 YQApplication::askForSaveFileName( const QString & startWith,
443 const QString & filter,
444 const QString & headline )
447 bool tryAgain = false;
451 // Leave the mouse cursor alone - this function might be called from
452 // some other widget, not only from UI::AskForSaveFileName().
454 fileName = QFileDialog::getSaveFileName( 0, startWith,
456 headline ); // caption
458 if ( fileName.isEmpty() ) // this includes fileName.isNull()
459 return QString::null;
462 if ( access( QFile::encodeName( fileName ), F_OK ) == 0 ) // file exists?
466 if ( access( QFile::encodeName( fileName ), W_OK ) == 0 )
468 // Confirm if the user wishes to overwrite an existing file
469 msg = ( _( "%1 exists! Really overwrite?" ) ).arg( fileName );
473 // Confirm if the user wishes to overwrite a write-protected file %1
474 msg = ( _( "%1 exists and is write-protected!\nReally overwrite?" ) ).arg( fileName );
477 int buttonNo = QMessageBox::information( 0, // parent widget
478 // Translators: Window title for confirmation dialog
483 tryAgain = ( buttonNo != 0 );
486 } while ( tryAgain );
492 #include "YQApplication.moc"