28 #include <QApplication> 31 #include <QFileDialog> 32 #include <QDesktopWidget> 33 #include <QMessageBox> 35 #include <QFontDatabase> 38 #include <fontconfig/fontconfig.h> 40 #define YUILogComponent "qt-ui" 41 #include <yui/YUILog.h> 42 #include <yui/YUISymbols.h> 43 #include <yui/Libyui_config.h> 50 #include "YQApplication.h" 51 #include "YQPackageSelectorPluginStub.h" 52 #include "YQGraphPluginStub.h" 53 #include "YQContextMenu.h" 59 static const char * default_font_family =
"Sans Serif";
67 , _qtTranslations( 0 )
69 , _autoNormalFontSize( -1 )
70 , _autoHeadingFontSize( -1 )
71 , _leftHandedMouse( false )
72 , _askedForLeftHandedMouse( false )
73 , _contextMenuPos ( QPoint (0, 0) )
76 yuiDebug() <<
"YQApplication constructor start" << std::endl;
80 iconLoader()->addIconSearchPath( ICONDIR
"/icons/" );
84 yuiDebug() <<
"YQApplication constructor end" << std::endl;
96 static std::string glob_language =
"";
100 const std::string & encoding )
102 glob_language = language;
103 YApplication::setLanguage( language, encoding );
106 bool oldReverseLayout = YApplication::reverseLayout();
110 if ( oldReverseLayout != YApplication::reverseLayout() )
112 YDialog * dialog = YDialog::topmostDialog(
false );
115 dialog->recalcLayout();
123 QString path = QT_LOCALEDIR;
126 if (glob_language ==
"")
127 language = QLocale::system().name();
129 language = glob_language.c_str();
131 QString transFile = QString(
"qt_%1.qm").arg( language );
133 yuiMilestone() <<
"Selected language: " << language << std::endl;
135 if ( path.isEmpty() )
137 yuiWarning() <<
"Qt locale directory not set - " 138 <<
"no translations for predefined Qt dialogs" 151 transFile = QString(
"qt_%1.qm").arg( language.toLower().left(2) );
157 yuiWarning() <<
"Can't load translations for predefined Qt dialogs from " 158 << path <<
"/" << transFile << std::endl;
162 yuiMilestone() <<
"Loaded translations for predefined Qt dialogs from " 163 << path <<
"/" << transFile << std::endl;
167 if ( qApp->layoutDirection() == Qt::RightToLeft )
168 YApplication::setReverseLayout(
true );
176 QString lang( language.c_str() );
180 if ( lang.startsWith(
"ar" ) ||
181 lang.startsWith(
"he" ) )
183 yuiMilestone() <<
"Using reverse layout for " << language << std::endl;
185 qApp->setLayoutDirection( Qt::RightToLeft );
186 YApplication::setReverseLayout(
true );
190 qApp->setLayoutDirection( Qt::LeftToRight );
191 YApplication::setReverseLayout(
false );
218 _langFonts =
new QSettings( LANG_FONTS_FILE, QSettings::IniFormat );
221 if (
_langFonts->status() != QSettings::NoError )
222 yuiError() <<
"Error reading " <<
_langFonts->fileName() << std::endl;
224 yuiMilestone() <<
_langFonts->fileName() <<
" read OK" 225 << qPrintable(
_langFonts->allKeys().join(
"-" ) )
229 QString lang = language.c_str();
231 if ( ! encoding.empty() )
232 lang += QString(
"." ) + encoding.c_str();
235 bool reloadFont =
false;
239 lang = language.c_str();
242 lang.replace( QRegExp(
"_.*$" ),
"" );
247 QStringList fontList =
249 for (
int i = 0; i < fontList.size(); ++i )
251 yuiMilestone() <<
fontKey( lang ) <<
" adding " << fontList.at( i ) << std::endl;
252 QFontDatabase::addApplicationFont( fontList.at( i ) );
265 yuiMilestone() <<
"Reloading fonts" << std::endl;
270 foreach ( QWidget *widget, QApplication::allWidgets() )
272 QFont wfont( widget->font() );
274 widget->setFont( wfont );
276 QFont font( qApp->font() );
280 yuiMilestone() <<
"Removing the key " << lang << std::endl;
285 yuiDebug() <<
"No font change" << std::endl;
294 if ( lang.isEmpty() )
297 return QString(
"font[%1]").arg( lang );
316 if ( ! _currentFont )
323 _currentFont->setPixelSize( _autoNormalFontSize );
324 _currentFont->setWeight( QFont::Normal );
326 yuiMilestone() <<
"Loaded " << _autoNormalFontSize
327 <<
" pixel font: " << _currentFont->toString()
330 qApp->setFont( * _currentFont);
335 _currentFont =
new QFont( qApp->font() );
339 return * _currentFont;
349 _boldFont->setBold(
true );
363 if ( ! _headingFont )
370 _headingFont->setPixelSize( _autoHeadingFontSize );
371 _headingFont->setWeight( QFont::Bold );
373 yuiMilestone() <<
"Loaded " << _autoHeadingFontSize
374 <<
" pixel bold font: " << _headingFont->toString()
379 _headingFont =
new QFont(
_fontFamily, 14, QFont::Bold );
383 return * _headingFont;
403 _autoFonts = useAutoFonts;
410 if ( _autoNormalFontSize >= 0 )
413 int x = defaultWidth();
414 int y = defaultHeight();
419 if ( x >= 800 && y >= 600 )
425 if ( x >= 1024 && y >= 768 )
431 if ( x >= 1280 && y >= 1024 )
455 _autoNormalFontSize = normal;
456 _autoHeadingFontSize = heading;
458 yuiMilestone() <<
"Selecting auto fonts - normal: " << _autoNormalFontSize
459 <<
", heading: " << _autoHeadingFontSize <<
" (bold)" 471 if ( sym == YUIGlyph_ArrowLeft ) unicodeChar = QChar( reverseLayout() ? 0x2192 : 0x2190 );
472 else if ( sym == YUIGlyph_ArrowRight ) unicodeChar = QChar( reverseLayout() ? 0x2190 : 0x2192 );
473 else if ( sym == YUIGlyph_ArrowUp ) unicodeChar = QChar( 0x2191 );
474 else if ( sym == YUIGlyph_ArrowDown ) unicodeChar = QChar( 0x2193 );
475 else if ( sym == YUIGlyph_CheckMark ) unicodeChar = QChar( 0x2714 );
476 else if ( sym == YUIGlyph_BulletArrowRight ) unicodeChar = QChar( 0x279c );
477 else if ( sym == YUIGlyph_BulletCircle ) unicodeChar = QChar( 0x274d );
478 else if ( sym == YUIGlyph_BulletSquare ) unicodeChar = QChar( 0x274f );
481 return toUTF8( QString( unicodeChar ) );
487 const std::string & headline )
492 QFileDialog::getExistingDirectory( 0,
493 fromUTF8( headline ) ,
494 fromUTF8( startDir ), QFileDialog::DontUseNativeDialog);
498 return toUTF8( dirName );
504 const std::string & filter,
505 const std::string & headline )
509 QFileDialog* dialog =
new QFileDialog( 0,
510 fromUTF8( headline ),
511 fromUTF8( startWith ),
513 dialog->setFileMode( QFileDialog::ExistingFile );
514 dialog->setFilter( QDir::System | dialog->filter() );
515 dialog->setOptions( QFileDialog::DontUseNativeDialog );
518 if( dialog->exec() == QDialog::Accepted )
519 fileName = dialog->selectedFiles().value( 0 );
524 return toUTF8( fileName );
530 const std::string & filter,
531 const std::string & headline )
537 fromUTF8( headline ) );
540 return toUTF8( fileName );
548 YDialog * currentDialog = YDialog::currentDialog(
false );
550 parent = (QWidget *) currentDialog->widgetRep();
553 menu->addItems(itemCollection);
561 const QString & filter,
562 const QString & headline )
567 YDialog * currentDialog = YDialog::currentDialog(
false );
569 parent = (QWidget *) currentDialog->widgetRep();
575 fileName = QFileDialog::getSaveFileName( parent,
578 filter, 0, QFileDialog::DontUseNativeDialog );
580 if ( fileName.isEmpty() )
581 return QString::null;
588 YQApplication::displayWidth()
590 return qApp->desktop()->width();
595 YQApplication::displayHeight()
597 return qApp->desktop()->height();
602 YQApplication::displayDepth()
604 return qApp->desktop()->depth();
609 YQApplication::displayColors()
611 return 1L << qApp->desktop()->depth();
616 YQApplication::defaultWidth()
623 YQApplication::defaultHeight()
630 YQApplication::leftHandedMouse()
632 return _leftHandedMouse;
639 if ( _askedForLeftHandedMouse )
643 _(
"You clicked the right mouse button " 644 "where a left-click was expected." 646 "Switch left and right mouse buttons?" 650 YDialog * currentDialog = YDialog::currentDialog(
false );
652 parent = (QWidget *) currentDialog->widgetRep();
654 int button = QMessageBox::question( parent,
656 _(
"Unexpected Click" ),
658 QMessageBox::Yes | QMessageBox::Default,
660 QMessageBox::Cancel | QMessageBox::Escape );
662 if ( button == QMessageBox::Yes )
665 const char * command =
667 "xmodmap -e \"pointer = 1 2 3\"":
668 "xmodmap -e \"pointer = 3 2 1\"";
670 _leftHandedMouse = ! _leftHandedMouse;
671 _askedForLeftHandedMouse =
false;
672 yuiMilestone() <<
"Switching mouse buttons: " << command << std::endl;
674 result = system( command );
676 yuiError() <<
"Calling '" << command <<
"' failed" << std::endl;
678 yuiError() <<
"Running '" << command <<
"' exited with " << result << std::endl;
680 else if ( button == 1 )
682 _askedForLeftHandedMouse =
true;
689 if ( dim==YD_HORIZ ) layoutUnits *= ( 640.0/80 );
690 else layoutUnits *= ( 480.0/25 );
692 return (
int) ( layoutUnits + 0.5 );
698 float size = (float) deviceUnits;
700 if ( dim==YD_HORIZ ) size *= ( 80/640.0 );
701 else size *= ( 25/480.0 );
774 QString qtTitle = fromUTF8( title );
775 YApplication::setApplicationTitle ( title );
777 qApp->setApplicationName(qtTitle);
782 QString qtIcon = fromUTF8( icon );
783 YApplication::setApplicationIcon ( icon );
784 QPixmap pixmap (qtIcon);
785 if ( !pixmap.isNull() )
786 qApp->setWindowIcon ( QIcon ( pixmap ) );
789 #include "YQApplication.moc" 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.
int defaultSize(YUIDimension dim) const
Returns size for opt(defaultsize) dialogs (in one dimension).
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.
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.
bool autoFonts() const
Returns 'true' if the UI automatically picks fonts, disregarding Qt standard settings.
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.