25 #include <rpc/types.h>
33 #include <QSocketNotifier>
34 #include <QDesktopWidget>
38 #include <QMessageLogContext>
41 #define YUILogComponent "qt-ui"
42 #include <yui/YUILog.h>
43 #include <yui/Libyui_config.h>
47 #include <yui/YEvent.h>
48 #include <yui/YCommandLine.h>
49 #include <yui/YButtonBox.h>
50 #include <yui/YUISymbols.h>
52 #include "QY2Styler.h"
53 #include "YQApplication.h"
55 #include "YQWidgetFactory.h"
56 #include "YQOptionalWidgetFactory.h"
69 #define BUSY_CURSOR_TIMEOUT 200 // milliseconds
70 #define VERBOSE_EVENT_LOOP 0
74 static void qMessageHandler( QtMsgType type,
const QMessageLogContext &,
const QString & msg );
78 YUI * createUI(
bool withThreads )
84 if ( ui && ! withThreads )
97 , _do_exit_loop( false )
99 yuiDebug() <<
"YQUI constructor start" << std::endl;
100 yuiMilestone() <<
"This is libyui-qt " << VERSION << std::endl;
103 _uiInitialized =
false;
107 screenShotNameTemplate =
"";
110 qInstallMessageHandler( qMessageHandler );
112 yuiDebug() <<
"YQUI constructor finished" << std::endl;
114 topmostConstructorHasFinished();
120 if ( _uiInitialized )
123 _uiInitialized =
true;
124 yuiDebug() <<
"Initializing Qt part" << std::endl;
126 YCommandLine cmdLine;
127 std::string progName;
129 if ( cmdLine.argc() > 0 )
131 progName = cmdLine[0];
132 std::size_t lastSlashPos = progName.find_last_of(
'/' );
134 if ( lastSlashPos != std::string::npos )
135 progName = progName.substr( lastSlashPos+1 );
141 if ( progName ==
"y2base" )
142 cmdLine.replace( 0,
"YaST2" );
145 _ui_argc = cmdLine.argc();
146 char ** argv = cmdLine.argv();
151 yuiDebug() <<
"Creating QApplication" << std::endl;
152 new QApplication( _ui_argc, argv );
157 _busyCursorTimer =
new QTimer( _signalReceiver );
158 _busyCursorTimer->setSingleShot(
true );
160 (void) QY2Styler::styler();
162 setButtonOrderFromEnvironment();
166 _do_exit_loop =
false;
174 _main_win =
new QWidget( 0, Qt::Window );
175 _main_win->setFocusPolicy( Qt::StrongFocus );
176 _main_win->setObjectName(
"main_window" );
178 _main_win->resize( _defaultSize );
181 _main_win->move( 0, 0 );
190 if ( progName ==
"y2base" )
191 _applicationTitle = QString(
"YaST2" );
193 _applicationTitle = fromUTF8( progName );
196 int displayArgPos = cmdLine.find(
"-display" );
199 if ( displayArgPos > 0 && displayArgPos+1 < cmdLine.argc() )
200 displayName = cmdLine[ displayArgPos+1 ].c_str();
202 displayName = getenv(
"DISPLAY" );
205 char hostname[ MAXHOSTNAMELEN+1 ];
206 if ( gethostname( hostname,
sizeof( hostname )-1 ) == 0 )
207 hostname[
sizeof( hostname ) -1 ] =
'\0';
212 if ( !displayName.startsWith(
":" ) && strlen( hostname ) > 0 )
214 _applicationTitle += QString(
"@" );
215 _applicationTitle += fromUTF8( hostname );
227 YButtonBoxMargins buttonBoxMargins;
228 buttonBoxMargins.left = 8;
229 buttonBoxMargins.right = 8;
230 buttonBoxMargins.top = 6;
231 buttonBoxMargins.bottom = 6;
233 buttonBoxMargins.spacing = 4;
234 buttonBoxMargins.helpButtonExtraSpacing = 16;
235 YButtonBox::setDefaultMargins( buttonBoxMargins );
257 QString qt_lib_name = QString( QTLIBDIR
"/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
258 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_LAZY | RTLD_GLOBAL );
260 yuiMilestone() <<
"Forcing " << qt_lib_name.toUtf8().constData() <<
" open successful" << std::endl;
262 yuiError() <<
"Forcing " << qt_lib_name.toUtf8().constData() <<
" open failed" << std::endl;
266 qApp->setFont(
yqApp()->currentFont() );
270 QObject::connect( _busyCursorTimer, &pclass(_busyCursorTimer)::timeout,
271 _signalReceiver, &pclass(_signalReceiver)::slotBusyCursor );
273 yuiMilestone() <<
"YQUI initialized. Thread ID: 0x"
274 << hex << QThread::currentThreadId () << dec
277 qApp->processEvents();
292 for(
int i=0; i < argc; i++ )
294 QString opt = argv[i];
296 yuiMilestone() <<
"Qt argument: " << argv[i] << std::endl;
300 if ( opt.startsWith(
"--" ) )
303 if ( opt == QString(
"-fullscreen" ) ) _fullscreen =
true;
304 else if ( opt == QString(
"-noborder" ) ) _noborder =
true;
307 else if ( opt == QString(
"-gnome-button-order" ) ) YButtonBox::setLayoutPolicy( YButtonBox::gnomeLayoutPolicy() );
308 else if ( opt == QString(
"-kde-button-order" ) ) YButtonBox::setLayoutPolicy( YButtonBox::kdeLayoutPolicy() );
310 else if ( opt == QString(
"-help" ) )
313 "Command line options for the YaST2 Qt UI:\n"
315 "--nothreads run without additional UI threads\n"
316 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
317 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
318 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
319 "--help this help text\n"
321 "--macro <macro-file> play a macro right on startup\n"
323 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
324 "to maintain backwards compatibility.\n"
340 yuiDebug() <<
"Closing down Qt UI." << std::endl;
351 delete _signalReceiver;
369 YUI_CHECK_NEW( factory );
376 YOptionalWidgetFactory *
380 YUI_CHECK_NEW( factory );
387 YQUI::createApplication()
390 YUI_CHECK_NEW( app );
398 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
399 QSize availableSize = qApp->desktop()->availableGeometry( qApp->desktop()->primaryScreen() ).size();
403 _defaultSize = availableSize;
405 yuiMilestone() <<
"-fullscreen: using "
406 << _defaultSize.width() <<
" x " << _defaultSize.height()
407 <<
"for `opt(`defaultsize)"
416 if ( _defaultSize.width() < 800 ||
417 _defaultSize.height() < 600 )
419 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
423 _defaultSize.setWidth ( max( (
int) (availableSize.width() * 0.7), 800 ) );
424 _defaultSize.setHeight( max( (
int) (availableSize.height() * 0.7), 600 ) );
428 _defaultSize = availableSize;
433 yuiMilestone() <<
"Forced size (via -geometry): "
434 << _defaultSize.width() <<
" x " << _defaultSize.height()
439 yuiMilestone() <<
"Default size: "
440 << _defaultSize.width() <<
" x " << _defaultSize.height()
449 _received_ycp_command =
false;
450 QSocketNotifier * notifier =
new QSocketNotifier( fd_ycp, QSocketNotifier::Read );
451 QObject::connect( notifier, &pclass(notifier)::activated,
452 _signalReceiver, &pclass(_signalReceiver)::slotReceivedYCPCommand );
454 notifier->setEnabled(
true );
461 #if VERBOSE_EVENT_LOOP
462 yuiDebug() <<
"Entering idle loop" << std::endl;
465 QEventLoop eventLoop( qApp );
467 while ( !_received_ycp_command )
468 eventLoop.processEvents( QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents );
470 #if VERBOSE_EVENT_LOOP
471 yuiDebug() <<
"Leaving idle loop" << std::endl;
480 _received_ycp_command =
true;
488 _eventHandler.sendEvent( event );
498 yuiError() <<
"No dialog" << std::endl;
506 bindtextdomain( domain, YSettings::localeDir().c_str() );
507 bind_textdomain_codeset( domain,
"utf8" );
508 textdomain( domain );
512 extern int _nl_msg_cat_cntr;
524 if ( ++_blockedLevel == 1 )
526 _eventHandler.blockEvents(
true );
530 if ( dialog && dialog->
eventLoop()->isRunning() )
532 yuiWarning() <<
"blocking events in active event loop of " << dialog << std::endl;
539 if ( --_blockedLevel == 0 )
541 _eventHandler.blockEvents(
false );
556 _eventHandler.blockEvents(
false );
562 return _eventHandler.eventsBlocked();
568 qApp->setOverrideCursor( Qt::BusyCursor );
574 if ( _busyCursorTimer->isActive() )
575 _busyCursorTimer->stop();
577 while ( qApp->overrideCursor() )
578 qApp->restoreOverrideCursor();
587 _busyCursorTimer->start( BUSY_CURSOR_TIMEOUT );
593 return dim == YD_HORIZ ? _defaultSize.width() : _defaultSize.height();
599 int displayArgPos = cmdLine.find(
"-display" );
600 std::string displayNameStr;
602 if ( displayArgPos > 0 && displayArgPos+1 < cmdLine.argc() )
604 displayNameStr = cmdLine[ displayArgPos+1 ];
605 yuiMilestone() <<
"Using X11 display \"" << displayNameStr <<
"\"" << std::endl;
608 const char * displayName = ( displayNameStr.empty() ? 0 : displayNameStr.c_str() );
609 Display * display = XOpenDisplay( displayName );
613 yuiDebug() <<
"Probing X11 display successful" << std::endl;
614 XCloseDisplay( display );
618 string msg =
"Can't open display " + displayNameStr;
619 YUI_THROW( YUIException( msg ) );
626 _eventHandler.deletePendingEventsFor( widget );
632 yuiMilestone() <<
"Closing application" << std::endl;
640 YQUISignalReceiver::YQUISignalReceiver()
646 void YQUISignalReceiver::slotBusyCursor()
652 void YQUISignalReceiver::slotReceivedYCPCommand()
660 qMessageHandler( QtMsgType type,
const QMessageLogContext &,
const QString & msg )
665 yuiMilestone() <<
"<libqt-debug> " << msg << std::endl;
668 #if QT_VERSION >= 0x050500
670 yuiMilestone() <<
"<libqt-info> " << msg << std::endl;
675 yuiWarning() <<
"<libqt-warning> " << msg << std::endl;
679 yuiError() <<
"<libqt-critical>" << msg << std::endl;
683 yuiError() <<
"<libqt-fatal> " << msg << std::endl;
688 if ( QString( msg ).contains(
"Fatal IO error", Qt::CaseInsensitive ) &&
689 QString( msg ).contains(
"client killed", Qt::CaseInsensitive ) )
690 yuiError() <<
"Client killed. Possibly caused by X server shutdown or crash." << std::endl;
void receivedYCPCommand()
Notification that a YCP command has been received on fd_ycp to leave idleLoop()
static YQApplication * yqApp()
Return the global YApplication object as YQApplication.
void forceUnblockEvents()
Force unblocking all events, no matter how many times blockEvents() has This returns 0 if there is no...
int defaultSize(YUIDimension dim) const
Returns size for opt(defaultsize) dialogs (in one dimension).
void setAutoFonts(bool useAutoFonts)
Set whether or not fonts should automatically be picked.
virtual YOptionalWidgetFactory * createOptionalWidgetFactory()
Create the widget factory that provides all the createXY() methods for optional ("special") widgets a...
void calcDefaultSize()
Calculate size of opt(defaultsize) dialogs.
QEventLoop * eventLoop()
Access to this dialog's event loop.
Helper class that acts as a Qt signal receiver for YQUI.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
YQUI(bool withThreads)
Constructor.
virtual void idleLoop(int fd_ycp)
Idle around until fd_ycp is readable and handle repaints.
void probeX11Display(const YCommandLine &cmdLine)
Probe the X11 display.
virtual bool eventsBlocked() const
Returns 'true' if events are currently blocked.
virtual void deleteNotify(YWidget *widget)
Notification that a widget is being deleted.
void busyCursor()
Show mouse cursor indicating busy state.
void processCommandLineArgs(int argc, char **argv)
Handle command line args.
virtual void uiThreadDestructor()
Destroy whatever needs to be destroyed within the UI thread.
virtual void blockEvents(bool block=true)
Block (or unblock) events.
void timeoutBusyCursor()
Show mouse cursor indicating busy state if the UI is unable to respond to user input for more than a ...
bool close()
Application shutdown.
void normalCursor()
Show normal mouse cursor not indicating busy status.
void initUI()
Post-constructor initialization.
virtual ~YQUI()
Destructor.
void raiseFatalError()
Raise a fatal UI error.
static void setTextdomain(const char *domain)
Initialize and set a textdomain for gettext()
static YQUI * ui()
Access the global Qt-UI.
virtual YWidgetFactory * createWidgetFactory()
Create the widget factory that provides all the createXY() methods for standard (mandatory, i.e.