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"
62 , _qtTranslations( 0 )
64 , _autoNormalFontSize( -1 )
65 , _autoHeadingFontSize( -1 )
66 , _leftHandedMouse( false )
67 , _askedForLeftHandedMouse( false )
68 , _contextMenuPos ( QPoint (0, 0) )
71 yuiDebug() <<
"YQApplication constructor start" << std::endl;
75 iconLoader()->addIconSearchPath( ICONDIR
"/icons/" );
78 yuiDebug() <<
"YQApplication constructor end" << std::endl;
90 static std::string glob_language =
"";
94 const std::string & encoding )
96 glob_language = language;
97 YApplication::setLanguage( language, encoding );
100 bool oldReverseLayout = YApplication::reverseLayout();
104 if ( oldReverseLayout != YApplication::reverseLayout() )
106 YDialog * dialog = YDialog::topmostDialog(
false );
109 dialog->recalcLayout();
117 QString path = QT_LOCALEDIR;
120 if (glob_language ==
"")
121 language = QLocale::system().name();
123 language = glob_language.c_str();
125 QString transFile = QString(
"qt_%1.qm").arg( language );
127 yuiMilestone() <<
"Selected language: " << language << std::endl;
129 if ( path.isEmpty() )
131 yuiWarning() <<
"Qt locale directory not set - "
132 <<
"no translations for predefined Qt dialogs"
145 transFile = QString(
"qt_%1.qm").arg( language.toLower().left(2) );
151 yuiWarning() <<
"Can't load translations for predefined Qt dialogs from "
152 << path <<
"/" << transFile << std::endl;
156 yuiMilestone() <<
"Loaded translations for predefined Qt dialogs from "
157 << path <<
"/" << transFile << 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 );
215 _langFonts =
new QSettings( LANG_FONTS_FILE, QSettings::IniFormat );
218 if (
_langFonts->status() != QSettings::NoError )
219 yuiError() <<
"Error reading " <<
_langFonts->fileName() << std::endl;
221 yuiMilestone() <<
_langFonts->fileName() <<
" read OK"
222 << qPrintable(
_langFonts->allKeys().join(
"-" ) )
226 QString lang = language.c_str();
228 if ( ! encoding.empty() )
229 lang += QString(
"." ) + encoding.c_str();
235 lang = language.c_str();
238 lang.replace( QRegExp(
"_.*$" ),
"" );
249 yuiMilestone() <<
"Using fallback for " << lang
260 yuiMilestone() <<
"New font family: " <<
_fontFamily << std::endl;
263 FcInitReinitialize();
265 foreach ( QWidget *widget, QApplication::allWidgets() )
267 if ( widget->font().family() != oldFontFamily )
270 QFont wfont( widget->font() );
272 widget->setFont( wfont );
274 QFont font( qApp->font() );
278 yuiMilestone() <<
"Reloading fonts - now using \"" << font.toString() <<
"\"" << std::endl;
282 yuiDebug() <<
"No font change" << std::endl;
291 if ( lang.isEmpty() )
294 return QString(
"font[%1]").arg( lang );
313 if ( ! _currentFont )
320 _currentFont->setPixelSize( _autoNormalFontSize );
321 _currentFont->setWeight( QFont::Normal );
323 yuiMilestone() <<
"Loaded " << _autoNormalFontSize
324 <<
" pixel font: " << _currentFont->toString()
327 qApp->setFont( * _currentFont);
332 _currentFont =
new QFont( qApp->font() );
336 return * _currentFont;
346 _boldFont->setBold(
true );
360 if ( ! _headingFont )
367 _headingFont->setPixelSize( _autoHeadingFontSize );
368 _headingFont->setWeight( QFont::Bold );
370 yuiMilestone() <<
"Loaded " << _autoHeadingFontSize
371 <<
" pixel bold font: " << _headingFont->toString()
376 _headingFont =
new QFont(
_fontFamily, 14, QFont::Bold );
380 return * _headingFont;
400 _autoFonts = useAutoFonts;
407 if ( _autoNormalFontSize >= 0 )
410 int x = defaultWidth();
411 int y = defaultHeight();
416 if ( x >= 800 && y >= 600 )
422 if ( x >= 1024 && y >= 768 )
428 if ( x >= 1280 && y >= 1024 )
452 _autoNormalFontSize = normal;
453 _autoHeadingFontSize = heading;
455 yuiMilestone() <<
"Selecting auto fonts - normal: " << _autoNormalFontSize
456 <<
", heading: " << _autoHeadingFontSize <<
" (bold)"
468 if ( sym == YUIGlyph_ArrowLeft ) unicodeChar = QChar( reverseLayout() ? 0x2192 : 0x2190 );
469 else if ( sym == YUIGlyph_ArrowRight ) unicodeChar = QChar( reverseLayout() ? 0x2190 : 0x2192 );
470 else if ( sym == YUIGlyph_ArrowUp ) unicodeChar = QChar( 0x2191 );
471 else if ( sym == YUIGlyph_ArrowDown ) unicodeChar = QChar( 0x2193 );
472 else if ( sym == YUIGlyph_CheckMark ) unicodeChar = QChar( 0x2714 );
473 else if ( sym == YUIGlyph_BulletArrowRight ) unicodeChar = QChar( 0x279c );
474 else if ( sym == YUIGlyph_BulletCircle ) unicodeChar = QChar( 0x274d );
475 else if ( sym == YUIGlyph_BulletSquare ) unicodeChar = QChar( 0x274f );
478 return toUTF8( QString( unicodeChar ) );
484 const std::string & headline )
489 QFileDialog::getExistingDirectory( 0,
490 fromUTF8( headline ) ,
491 fromUTF8( startDir ), QFileDialog::DontUseNativeDialog);
495 return toUTF8( dirName );
501 const std::string & filter,
502 const std::string & headline )
506 QFileDialog* dialog =
new QFileDialog( 0,
507 fromUTF8( headline ),
508 fromUTF8( startWith ),
510 dialog->setFileMode( QFileDialog::ExistingFile );
511 dialog->setFilter( QDir::System | dialog->filter() );
512 dialog->setOptions( QFileDialog::DontUseNativeDialog );
515 if( dialog->exec() == QDialog::Accepted )
516 fileName = dialog->selectedFiles().value( 0 );
521 return toUTF8( fileName );
527 const std::string & filter,
528 const std::string & headline )
534 fromUTF8( headline ) );
537 return toUTF8( fileName );
545 menu->addItems(itemCollection);
553 const QString & filter,
554 const QString & headline )
559 YDialog * currentDialog = YDialog::currentDialog(
false );
561 parent = (QWidget *) currentDialog->widgetRep();
567 fileName = QFileDialog::getSaveFileName( parent,
570 filter, 0, QFileDialog::DontUseNativeDialog );
572 if ( fileName.isEmpty() )
573 return QString::null;
580 YQApplication::displayWidth()
582 return qApp->desktop()->width();
587 YQApplication::displayHeight()
589 return qApp->desktop()->height();
594 YQApplication::displayDepth()
596 return qApp->desktop()->depth();
601 YQApplication::displayColors()
603 return 1L << qApp->desktop()->depth();
608 YQApplication::defaultWidth()
615 YQApplication::defaultHeight()
622 YQApplication::leftHandedMouse()
624 return _leftHandedMouse;
631 if ( _askedForLeftHandedMouse )
635 _(
"You clicked the right mouse button "
636 "where a left-click was expected."
638 "Switch left and right mouse buttons?"
642 YDialog * currentDialog = YDialog::currentDialog(
false );
644 parent = (QWidget *) currentDialog->widgetRep();
646 int button = QMessageBox::question( parent,
648 _(
"Unexpected Click" ),
650 QMessageBox::Yes | QMessageBox::Default,
652 QMessageBox::Cancel | QMessageBox::Escape );
654 if ( button == QMessageBox::Yes )
657 const char * command =
659 "xmodmap -e \"pointer = 1 2 3\"":
660 "xmodmap -e \"pointer = 3 2 1\"";
662 _leftHandedMouse = ! _leftHandedMouse;
663 _askedForLeftHandedMouse =
false;
664 yuiMilestone() <<
"Switching mouse buttons: " << command << std::endl;
666 result = system( command );
668 yuiError() <<
"Calling '" << command <<
"' failed" << std::endl;
670 yuiError() <<
"Running '" << command <<
"' exited with " << result << std::endl;
672 else if ( button == 1 )
674 _askedForLeftHandedMouse =
true;
681 if ( dim==YD_HORIZ ) layoutUnits *= ( 640.0/80 );
682 else layoutUnits *= ( 480.0/25 );
684 return (
int) ( layoutUnits + 0.5 );
690 float size = (float) deviceUnits;
692 if ( dim==YD_HORIZ ) size *= ( 80/640.0 );
693 else size *= ( 25/480.0 );
766 QString qtTitle = fromUTF8( title );
767 YApplication::setApplicationTitle ( title );
769 qApp->setApplicationName(qtTitle);
774 QString qtIcon = fromUTF8( icon );
775 YApplication::setApplicationIcon ( icon );
776 QPixmap pixmap (qtIcon);
777 if ( !pixmap.isNull() )
778 qApp->setWindowIcon ( QIcon ( pixmap ) );
781 #include "YQApplication.moc"
virtual void normalCursor()
virtual int deviceUnits(YUIDimension dim, float layoutUnits)
virtual std::string glyph(const std::string &glyphSymbolName)
void setLayoutDirection(const std::string &language)
void maybeLeftHandedUser()
virtual QPoint contextMenuPos()
void setApplicationTitle(const QString &title)
virtual void setApplicationTitle(const std::string &title)
virtual void busyCursor()
int defaultSize(YUIDimension dim) const
void makeScreenShot(std::string filename)
void setAutoFonts(bool useAutoFonts)
void setLangFonts(const std::string &language, const std::string &encoding=std::string())
QTranslator * _qtTranslations
virtual bool openContextMenu(const YItemCollection &itemCollection)
QString fontKey(const QString &lang)
static YQGraphPluginStub * graphPlugin()
virtual void setContextMenuPos(QPoint contextMenuPos)
virtual void setApplicationIcon(const std::string &icon)
virtual void makeScreenShot(const std::string &fileName)
const QFont & headingFont()
virtual std::string askForExistingDirectory(const std::string &startDir, const std::string &headline)
virtual std::string askForSaveFileName(const std::string &startWith, const std::string &filter, const std::string &headline)
virtual void setLanguage(const std::string &language, const std::string &encoding=std::string())
virtual float layoutUnits(YUIDimension dim, int deviceUnits)
void loadPredefinedQtTranslations()
static YQPackageSelectorPluginStub * packageSelectorPlugin()
const QFont & currentFont()
virtual std::string askForExistingFile(const std::string &startWith, const std::string &filter, const std::string &headline)