28 #include <QApplication> 31 #include <QFileDialog> 32 #include <QDesktopWidget> 33 #include <QMessageBox> 35 #include <QFontDatabase> 37 #include <QLibraryInfo> 39 #include <fontconfig/fontconfig.h> 41 #define YUILogComponent "qt-ui" 42 #include <yui/YUILog.h> 43 #include <yui/YUISymbols.h> 44 #include <yui/Libyui_config.h> 51 #include "YQApplication.h" 52 #include "YQPackageSelectorPluginStub.h" 53 #include "YQGraphPluginStub.h" 54 #include "YQContextMenu.h" 64 static const char * default_font_family =
"Sans Serif";
74 , _qtTranslations( 0 )
76 , _autoNormalFontSize( -1 )
77 , _autoHeadingFontSize( -1 )
78 , _leftHandedMouse( false )
79 , _askedForLeftHandedMouse( false )
80 , _contextMenuPos ( QPoint (0, 0) )
83 yuiDebug() <<
"YQApplication constructor start" << endl;
85 yuiMilestone() <<
"QIcon::themeName = '" << QIcon::themeName() <<
"'" << endl;
89 iconLoader()->addIconSearchPath( ICONDIR
"/icons/" );
93 yuiDebug() <<
"YQApplication constructor end" << endl;
105 static string glob_language =
"";
109 const string & encoding )
111 glob_language = language;
112 YApplication::setLanguage( language, encoding );
115 bool oldReverseLayout = YApplication::reverseLayout();
119 if ( oldReverseLayout != YApplication::reverseLayout() )
121 YDialog * dialog = YDialog::topmostDialog(
false );
124 dialog->recalcLayout();
132 QString path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
135 if (glob_language ==
"")
136 language = QLocale::system().name();
138 language = glob_language.c_str();
140 QString transFile = QString(
"qt_%1.qm").arg( language );
142 yuiMilestone() <<
"Selected language: " << language << endl;
144 if ( path.isEmpty() )
146 yuiWarning() <<
"Qt locale directory not set - " 147 <<
"no translations for predefined Qt dialogs" 157 yuiWarning() <<
"Can't load translations for predefined Qt dialogs for " 162 yuiMilestone() <<
"Loaded translations for predefined Qt dialogs for " 167 if ( qApp->layoutDirection() == Qt::RightToLeft )
168 YApplication::setReverseLayout(
true );
176 QString lang( language.c_str() );
181 if ( lang.startsWith(
"ar" ) ||
182 lang.startsWith(
"fa" ) ||
183 lang.startsWith(
"he" ) )
185 yuiMilestone() <<
"Using reverse layout for " << language << endl;
187 qApp->setLayoutDirection( Qt::RightToLeft );
188 YApplication::setReverseLayout(
true );
192 qApp->setLayoutDirection( Qt::LeftToRight );
193 YApplication::setReverseLayout(
false );
220 _langFonts =
new QSettings( LANG_FONTS_FILE, QSettings::IniFormat );
223 if (
_langFonts->status() != QSettings::NoError )
224 yuiError() <<
"Error reading " <<
_langFonts->fileName() << endl;
226 yuiMilestone() <<
_langFonts->fileName() <<
" read OK" 227 << qPrintable(
_langFonts->allKeys().join(
"-" ) )
231 QString lang = language.c_str();
233 if ( ! encoding.empty() )
234 lang += QString(
"." ) + encoding.c_str();
237 bool reloadFont =
false;
241 lang = language.c_str();
244 lang.replace( QRegExp(
"_.*$" ),
"" );
249 QStringList fontList =
251 for (
int i = 0; i < fontList.size(); ++i )
253 yuiMilestone() <<
fontKey( lang ) <<
" adding " << fontList.at( i ) << endl;
254 QFontDatabase::addApplicationFont( fontList.at( i ) );
268 yuiMilestone() <<
"Reloading fonts" << endl;
273 foreach ( QWidget *widget, QApplication::allWidgets() )
275 QFont wfont( widget->font() );
277 widget->setFont( wfont );
280 QFont font( qApp->font() );
284 yuiMilestone() <<
"Removing the key " << lang << endl;
289 yuiDebug() <<
"No font change" << endl;
298 if ( lang.isEmpty() )
301 return QString(
"font[%1]").arg( lang );
320 if ( ! _currentFont )
327 _currentFont->setPixelSize( _autoNormalFontSize );
328 _currentFont->setWeight( QFont::Normal );
330 yuiMilestone() <<
"Loaded " << _autoNormalFontSize
331 <<
" pixel font: " << _currentFont->toString()
334 qApp->setFont( * _currentFont);
339 _currentFont =
new QFont( qApp->font() );
343 return * _currentFont;
353 _boldFont->setBold(
true );
367 if ( ! _headingFont )
374 _headingFont->setPixelSize( _autoHeadingFontSize );
375 _headingFont->setWeight( QFont::Bold );
377 yuiMilestone() <<
"Loaded " << _autoHeadingFontSize
378 <<
" pixel bold font: " << _headingFont->toString()
383 _headingFont =
new QFont(
_fontFamily, 14, QFont::Bold );
387 return * _headingFont;
407 _autoFonts = useAutoFonts;
414 if ( _autoNormalFontSize >= 0 )
417 int x = defaultWidth();
418 int y = defaultHeight();
423 if ( x >= 800 && y >= 600 )
429 if ( x >= 1024 && y >= 768 )
435 if ( x >= 1280 && y >= 1024 )
459 _autoNormalFontSize = normal;
460 _autoHeadingFontSize = heading;
462 yuiMilestone() <<
"Selecting auto fonts - normal: " << _autoNormalFontSize
463 <<
", heading: " << _autoHeadingFontSize <<
" (bold)" 475 if ( sym == YUIGlyph_ArrowLeft ) unicodeChar = QChar( reverseLayout() ? 0x2192 : 0x2190 );
476 else if ( sym == YUIGlyph_ArrowRight ) unicodeChar = QChar( reverseLayout() ? 0x2190 : 0x2192 );
477 else if ( sym == YUIGlyph_ArrowUp ) unicodeChar = QChar( 0x2191 );
478 else if ( sym == YUIGlyph_ArrowDown ) unicodeChar = QChar( 0x2193 );
479 else if ( sym == YUIGlyph_CheckMark ) unicodeChar = QChar( 0x2714 );
480 else if ( sym == YUIGlyph_BulletArrowRight ) unicodeChar = QChar( 0x279c );
481 else if ( sym == YUIGlyph_BulletCircle ) unicodeChar = QChar( 0x274d );
482 else if ( sym == YUIGlyph_BulletSquare ) unicodeChar = QChar( 0x274f );
485 return toUTF8( QString( unicodeChar ) );
491 const string & headline )
496 QFileDialog::getExistingDirectory( 0,
497 fromUTF8( headline ) ,
498 fromUTF8( startDir ), QFileDialog::DontUseNativeDialog);
502 return toUTF8( dirName );
508 const string & filter,
509 const string & headline )
513 QFileDialog* dialog =
new QFileDialog( 0,
514 fromUTF8( headline ),
515 fromUTF8( startWith ),
517 dialog->setFileMode( QFileDialog::ExistingFile );
518 dialog->setFilter( QDir::System | dialog->filter() );
519 dialog->setOptions( QFileDialog::DontUseNativeDialog );
522 if( dialog->exec() == QDialog::Accepted )
523 fileName = dialog->selectedFiles().value( 0 );
528 return toUTF8( fileName );
534 const string & filter,
535 const string & headline )
541 fromUTF8( headline ) );
544 return toUTF8( fileName );
552 YDialog * currentDialog = YDialog::currentDialog(
false );
555 parent = (QWidget *) currentDialog->widgetRep();
558 menu->addItems(itemCollection);
566 const QString & filter,
567 const QString & headline )
572 YDialog * currentDialog = YDialog::currentDialog(
false );
574 parent = (QWidget *) currentDialog->widgetRep();
580 fileName = QFileDialog::getSaveFileName( parent,
583 filter, 0, QFileDialog::DontUseNativeDialog );
585 if ( fileName.isEmpty() )
593 YQApplication::displayWidth()
595 return qApp->desktop()->width();
600 YQApplication::displayHeight()
602 return qApp->desktop()->height();
607 YQApplication::displayDepth()
609 return qApp->desktop()->depth();
614 YQApplication::displayColors()
616 return 1L << qApp->desktop()->depth();
621 YQApplication::defaultWidth()
628 YQApplication::defaultHeight()
635 YQApplication::leftHandedMouse()
637 return _leftHandedMouse;
644 if ( _askedForLeftHandedMouse )
648 _(
"You clicked the right mouse button " 649 "where a left-click was expected." 651 "Switch left and right mouse buttons?" 655 YDialog * currentDialog = YDialog::currentDialog(
false );
657 parent = (QWidget *) currentDialog->widgetRep();
659 int button = QMessageBox::question( parent,
661 _(
"Unexpected Click" ),
663 QMessageBox::Yes | QMessageBox::Default,
665 QMessageBox::Cancel | QMessageBox::Escape );
667 if ( button == QMessageBox::Yes )
670 const char * command =
672 "xmodmap -e \"pointer = 1 2 3\"":
673 "xmodmap -e \"pointer = 3 2 1\"";
675 _leftHandedMouse = ! _leftHandedMouse;
676 _askedForLeftHandedMouse =
false;
677 yuiMilestone() <<
"Switching mouse buttons: " << command << endl;
679 result = system( command );
681 yuiError() <<
"Calling '" << command <<
"' failed" << endl;
683 yuiError() <<
"Running '" << command <<
"' exited with " << result << endl;
685 else if ( button == 1 )
687 _askedForLeftHandedMouse =
true;
705 if ( dim==YD_HORIZ ) size *= ( 80/640.0 );
706 else size *= ( 25/480.0 );
781 QString qtTitle = fromUTF8( title );
782 YApplication::setApplicationTitle ( title );
784 qApp->setApplicationName(qtTitle);
790 QString qtIcon = fromUTF8( icon );
791 YApplication::setApplicationIcon ( icon );
792 QString icon_name = QFileInfo( qtIcon ).baseName();
794 if ( QIcon::hasThemeIcon( icon_name ) )
796 qApp->setWindowIcon( QIcon::fromTheme ( icon_name ) );
800 QPixmap pixmap( qtIcon );
802 if ( !pixmap.isNull() )
803 qApp->setWindowIcon( QIcon( pixmap ) );
int defaultSize(YUIDimension dim) const
Returns size for opt(defaultsize) dialogs (in one dimension).
virtual void normalCursor()
Change the (mouse) cursor back from busy status to normal.
virtual int deviceUnits(YUIDimension dim, float layoutUnits)
Convert logical layout spacing units into device dependent units.
virtual std::string glyph(const std::string &glyphSymbolName)
Return a std::string for a named glyph.
void setLayoutDirection(const std::string &language)
Set the layout direction (left-to-right or right-to-left) from 'language'.
void maybeLeftHandedUser()
A mouse click with the wrong mouse button was detected - e.g., a right click on a push button...
virtual QPoint contextMenuPos()
Return position of the context menu (in gloabl coordinates)
virtual ~YQApplication()
Destructor.
void setApplicationTitle(const QString &title)
Sets the application name for the window title.
virtual void setApplicationTitle(const std::string &title)
Set the application title.
virtual void busyCursor()
Change the (mouse) cursor to indicate busy status.
QSettings * _langFonts
Language-specific font settings.
void makeScreenShot(std::string filename)
Make a screen shot in .png format and save it to 'filename'.
void setAutoFonts(bool useAutoFonts)
Set whether or not fonts should automatically be picked.
void setLangFonts(const std::string &language, const std::string &encoding=std::string())
Set fonts according to the specified language and encoding.
void deleteFonts()
Delete the fonts so they will be reloaded upon their next usage.
QTranslator * _qtTranslations
Translator for the predefined Qt dialogs.
virtual bool openContextMenu(const YItemCollection &itemCollection)
Open a context menu for a widget.
QString fontKey(const QString &lang)
Constructs a key for the language specific font file: "font[lang]" for font[de_DE] = "Sans Serif" fon...
static YQGraphPluginStub * graphPlugin()
Return the package selector plugin singleton or creates it (including loading the plugin lib) if it d...
virtual void setContextMenuPos(QPoint contextMenuPos)
Sets the position of the context menu (in gloabl coordinates)
virtual void setApplicationIcon(const std::string &icon)
Set the application Icon.
virtual void makeScreenShot(const std::string &fileName)
Make a screen shot and save it to the specified file.
const QFont & headingFont()
Returns the application's heading font.
bool autoFonts() const
Returns 'true' if the UI automatically picks fonts, disregarding Qt standard settings.
QString _fontFamily
Font family or list of font families to use ("Sans Serif" etc.)
YQApplication()
Constructor.
virtual std::string askForExistingDirectory(const std::string &startDir, const std::string &headline)
Open a directory selection box and prompt the user for an existing directory.
virtual std::string askForSaveFileName(const std::string &startWith, const std::string &filter, const std::string &headline)
Open a file selection box and prompt the user for a file to save data to.
void busyCursor()
Show mouse cursor indicating busy state.
virtual void setLanguage(const std::string &language, const std::string &encoding=std::string())
Set language and encoding for the locale environment ($LANG).
virtual float layoutUnits(YUIDimension dim, int deviceUnits)
Convert device dependent units into logical layout spacing units.
const QFont & boldFont()
Returns the application's default bold font.
void loadPredefinedQtTranslations()
Load translations for Qt's predefined dialogs like file selection box etc.
void normalCursor()
Show normal mouse cursor not indicating busy status.
void pickAutoFonts()
Determine good fonts based on defaultsize geometry and set _auto_normal_font_size and _auto_heading_f...
static YQPackageSelectorPluginStub * packageSelectorPlugin()
Return the package selector plugin singleton or creates it (including loading the plugin lib) if it d...
static YQUI * ui()
Access the global Qt-UI.
const QFont & currentFont()
Returns the application's default font.
virtual std::string askForExistingFile(const std::string &startWith, const std::string &filter, const std::string &headline)
Open a file selection box and prompt the user for an existing file.