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" 60 static const char * default_font_family =
"Sans Serif";
68 , _qtTranslations( 0 )
70 , _autoNormalFontSize( -1 )
71 , _autoHeadingFontSize( -1 )
72 , _leftHandedMouse( false )
73 , _askedForLeftHandedMouse( false )
74 , _contextMenuPos ( QPoint (0, 0) )
77 yuiDebug() <<
"YQApplication constructor start" << std::endl;
79 yuiMilestone() <<
"QIcon::themeName = '" << QIcon::themeName() <<
"'" << std::endl;
83 iconLoader()->addIconSearchPath( ICONDIR
"/icons/" );
87 yuiDebug() <<
"YQApplication constructor end" << std::endl;
99 static std::string glob_language =
"";
103 const std::string & encoding )
105 glob_language = language;
106 YApplication::setLanguage( language, encoding );
109 bool oldReverseLayout = YApplication::reverseLayout();
113 if ( oldReverseLayout != YApplication::reverseLayout() )
115 YDialog * dialog = YDialog::topmostDialog(
false );
118 dialog->recalcLayout();
126 QString path = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
129 if (glob_language ==
"")
130 language = QLocale::system().name();
132 language = glob_language.c_str();
134 QString transFile = QString(
"qt_%1.qm").arg( language );
136 yuiMilestone() <<
"Selected language: " << language << std::endl;
138 if ( path.isEmpty() )
140 yuiWarning() <<
"Qt locale directory not set - " 141 <<
"no translations for predefined Qt dialogs" 151 yuiWarning() <<
"Can't load translations for predefined Qt dialogs for " 152 << language << std::endl;
156 yuiMilestone() <<
"Loaded translations for predefined Qt dialogs for " 157 << language << std::endl;
161 if ( qApp->layoutDirection() == Qt::RightToLeft )
162 YApplication::setReverseLayout(
true );
170 QString lang( language.c_str() );
174 if ( lang.startsWith(
"ar" ) ||
175 lang.startsWith(
"he" ) )
177 yuiMilestone() <<
"Using reverse layout for " << language << std::endl;
179 qApp->setLayoutDirection( Qt::RightToLeft );
180 YApplication::setReverseLayout(
true );
184 qApp->setLayoutDirection( Qt::LeftToRight );
185 YApplication::setReverseLayout(
false );
212 _langFonts =
new QSettings( LANG_FONTS_FILE, QSettings::IniFormat );
215 if (
_langFonts->status() != QSettings::NoError )
216 yuiError() <<
"Error reading " <<
_langFonts->fileName() << std::endl;
218 yuiMilestone() <<
_langFonts->fileName() <<
" read OK" 219 << qPrintable(
_langFonts->allKeys().join(
"-" ) )
223 QString lang = language.c_str();
225 if ( ! encoding.empty() )
226 lang += QString(
"." ) + encoding.c_str();
229 bool reloadFont =
false;
233 lang = language.c_str();
236 lang.replace( QRegExp(
"_.*$" ),
"" );
241 QStringList fontList =
243 for (
int i = 0; i < fontList.size(); ++i )
245 yuiMilestone() <<
fontKey( lang ) <<
" adding " << fontList.at( i ) << std::endl;
246 QFontDatabase::addApplicationFont( fontList.at( i ) );
259 yuiMilestone() <<
"Reloading fonts" << std::endl;
264 foreach ( QWidget *widget, QApplication::allWidgets() )
266 QFont wfont( widget->font() );
268 widget->setFont( wfont );
270 QFont font( qApp->font() );
274 yuiMilestone() <<
"Removing the key " << lang << std::endl;
279 yuiDebug() <<
"No font change" << std::endl;
288 if ( lang.isEmpty() )
291 return QString(
"font[%1]").arg( lang );
310 if ( ! _currentFont )
317 _currentFont->setPixelSize( _autoNormalFontSize );
318 _currentFont->setWeight( QFont::Normal );
320 yuiMilestone() <<
"Loaded " << _autoNormalFontSize
321 <<
" pixel font: " << _currentFont->toString()
324 qApp->setFont( * _currentFont);
329 _currentFont =
new QFont( qApp->font() );
333 return * _currentFont;
343 _boldFont->setBold(
true );
357 if ( ! _headingFont )
364 _headingFont->setPixelSize( _autoHeadingFontSize );
365 _headingFont->setWeight( QFont::Bold );
367 yuiMilestone() <<
"Loaded " << _autoHeadingFontSize
368 <<
" pixel bold font: " << _headingFont->toString()
373 _headingFont =
new QFont(
_fontFamily, 14, QFont::Bold );
377 return * _headingFont;
397 _autoFonts = useAutoFonts;
404 if ( _autoNormalFontSize >= 0 )
407 int x = defaultWidth();
408 int y = defaultHeight();
413 if ( x >= 800 && y >= 600 )
419 if ( x >= 1024 && y >= 768 )
425 if ( x >= 1280 && y >= 1024 )
449 _autoNormalFontSize = normal;
450 _autoHeadingFontSize = heading;
452 yuiMilestone() <<
"Selecting auto fonts - normal: " << _autoNormalFontSize
453 <<
", heading: " << _autoHeadingFontSize <<
" (bold)" 465 if ( sym == YUIGlyph_ArrowLeft ) unicodeChar = QChar( reverseLayout() ? 0x2192 : 0x2190 );
466 else if ( sym == YUIGlyph_ArrowRight ) unicodeChar = QChar( reverseLayout() ? 0x2190 : 0x2192 );
467 else if ( sym == YUIGlyph_ArrowUp ) unicodeChar = QChar( 0x2191 );
468 else if ( sym == YUIGlyph_ArrowDown ) unicodeChar = QChar( 0x2193 );
469 else if ( sym == YUIGlyph_CheckMark ) unicodeChar = QChar( 0x2714 );
470 else if ( sym == YUIGlyph_BulletArrowRight ) unicodeChar = QChar( 0x279c );
471 else if ( sym == YUIGlyph_BulletCircle ) unicodeChar = QChar( 0x274d );
472 else if ( sym == YUIGlyph_BulletSquare ) unicodeChar = QChar( 0x274f );
475 return toUTF8( QString( unicodeChar ) );
481 const std::string & headline )
486 QFileDialog::getExistingDirectory( 0,
487 fromUTF8( headline ) ,
488 fromUTF8( startDir ), QFileDialog::DontUseNativeDialog);
492 return toUTF8( dirName );
498 const std::string & filter,
499 const std::string & headline )
503 QFileDialog* dialog =
new QFileDialog( 0,
504 fromUTF8( headline ),
505 fromUTF8( startWith ),
507 dialog->setFileMode( QFileDialog::ExistingFile );
508 dialog->setFilter( QDir::System | dialog->filter() );
509 dialog->setOptions( QFileDialog::DontUseNativeDialog );
512 if( dialog->exec() == QDialog::Accepted )
513 fileName = dialog->selectedFiles().value( 0 );
518 return toUTF8( fileName );
524 const std::string & filter,
525 const std::string & headline )
531 fromUTF8( headline ) );
534 return toUTF8( fileName );
542 YDialog * currentDialog = YDialog::currentDialog(
false );
544 parent = (QWidget *) currentDialog->widgetRep();
547 menu->addItems(itemCollection);
555 const QString & filter,
556 const QString & headline )
561 YDialog * currentDialog = YDialog::currentDialog(
false );
563 parent = (QWidget *) currentDialog->widgetRep();
569 fileName = QFileDialog::getSaveFileName( parent,
572 filter, 0, QFileDialog::DontUseNativeDialog );
574 if ( fileName.isEmpty() )
575 return QString::null;
582 YQApplication::displayWidth()
584 return qApp->desktop()->width();
589 YQApplication::displayHeight()
591 return qApp->desktop()->height();
596 YQApplication::displayDepth()
598 return qApp->desktop()->depth();
603 YQApplication::displayColors()
605 return 1L << qApp->desktop()->depth();
610 YQApplication::defaultWidth()
617 YQApplication::defaultHeight()
624 YQApplication::leftHandedMouse()
626 return _leftHandedMouse;
633 if ( _askedForLeftHandedMouse )
637 _(
"You clicked the right mouse button " 638 "where a left-click was expected." 640 "Switch left and right mouse buttons?" 644 YDialog * currentDialog = YDialog::currentDialog(
false );
646 parent = (QWidget *) currentDialog->widgetRep();
648 int button = QMessageBox::question( parent,
650 _(
"Unexpected Click" ),
652 QMessageBox::Yes | QMessageBox::Default,
654 QMessageBox::Cancel | QMessageBox::Escape );
656 if ( button == QMessageBox::Yes )
659 const char * command =
661 "xmodmap -e \"pointer = 1 2 3\"":
662 "xmodmap -e \"pointer = 3 2 1\"";
664 _leftHandedMouse = ! _leftHandedMouse;
665 _askedForLeftHandedMouse =
false;
666 yuiMilestone() <<
"Switching mouse buttons: " << command << std::endl;
668 result = system( command );
670 yuiError() <<
"Calling '" << command <<
"' failed" << std::endl;
672 yuiError() <<
"Running '" << command <<
"' exited with " << result << std::endl;
674 else if ( button == 1 )
676 _askedForLeftHandedMouse =
true;
694 if ( dim==YD_HORIZ ) size *= ( 80/640.0 );
695 else size *= ( 25/480.0 );
769 QString qtTitle = fromUTF8( title );
770 YApplication::setApplicationTitle ( title );
772 qApp->setApplicationName(qtTitle);
778 QString qtIcon = fromUTF8( icon );
779 YApplication::setApplicationIcon ( icon );
780 QString icon_name = QFileInfo( qtIcon ).baseName();
782 if ( QIcon::hasThemeIcon( icon_name ) )
784 qApp->setWindowIcon( QIcon::fromTheme ( icon_name ) );
788 QPixmap pixmap( qtIcon );
790 if ( !pixmap.isNull() )
791 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.