1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
19 #include <rpc/types.h> // MAXHOSTNAMELEN
24 #include <QMessageBox>
25 #include <QSocketNotifier>
26 #include <QStackedWidget>
27 #include <QDesktopWidget>
29 #include <QVBoxLayout>
31 #include <ycp/YCPTerm.h>
32 #include <ycp/YCPCode.h>
34 #define y2log_component "qt-ui"
35 #include <ycp/y2log.h>
38 #include "QY2Styler.h"
39 #include "YQApplication.h"
40 #include "YQWidgetFactory.h"
41 #include "YQOptionalWidgetFactory.h"
43 #include "YUISymbols.h"
47 #include "QY2Settings.h"
50 #define BUSY_CURSOR_TIMEOUT 200 // milliseconds
54 static void qMessageHandler( QtMsgType type, const char * msg );
56 YQUI::YQUI( int argc, char **argv, bool with_threads, const char * macro_file )
60 , _do_exit_loop( false )
63 y2milestone( "YQUI constructor start" );
68 _usingVisionImpairedPalette = false;
69 _leftHandedMouse = false;
70 _askedForLeftHandedMouse = false;
72 screenShotNameTemplate = "";
75 qInstallMsgHandler( qMessageHandler );
77 // Copy command line arguments for QApplication
79 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
82 for ( int i=0; i < argc; i++ )
83 _ui_argv[i+1] = strdup( argv[i] );
85 _ui_argv[0] = strdup( "YaST2" );
88 topmostConstructorHasFinished();
98 Qt thinks the first QObject defines the main thread, but
99 the main thread in this case is the ycp thread, not UI
101 extern void qt_set_current_thread_to_main_thread();
102 qt_set_current_thread_to_main_thread();
107 new QApplication( _ui_argc, _ui_argv);
108 _busy_cursor_timer = new QTimer( qApp );
109 _busy_cursor_timer->setSingleShot( true );
111 _user_input_timer.setSingleShot( true );
113 _normalPalette = qApp->palette();
115 // Qt keeps track to a global QApplication in qApp.
118 processCommandLineArgs( _ui_argc, _ui_argv );
121 _styler = new QY2Styler( qApp );
122 QString style = getenv("Y2STYLE");
123 if ( !style.isNull() )
124 _styler->setStyleSheet( style );
126 // Event loop object. Required since a YaST2 UI needs to react to commands
127 // from the YCP command stream as well as to X11 / Qt events.
128 _eventLoop = new QEventLoop( qApp );
129 _do_exit_loop = false;
131 // Create main window for `opt(`defaultsize) dialogs.
133 // We have to use something else than QWidgetStack since QWidgetStack
134 // doesn't accept a WFlags arg which we badly need here.
136 _main_win = new QWidget( 0, Qt::Window ); // parent, wflags
137 _main_win->setFocusPolicy( Qt::StrongFocus );
138 _main_win->setObjectName( "main_window" );
140 _main_win->resize( _default_size );
143 _main_win->move( 0, 0 );
148 QString title( "YaST2" );
149 char hostname[ MAXHOSTNAMELEN+1 ];
151 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
153 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
155 if ( strlen( hostname ) > 0 )
157 if ( ( strcmp( hostname, "(none)" ) != 0 &&
158 strcmp( hostname, "linux" ) != 0 ) )
166 _main_win->setWindowTitle( title );
169 // Hide the main window for now. The first call to UI::OpenDialog() on an
170 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
171 // the main window - there is nothing to display yet.
176 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
177 // if no Qt style defined):
179 // Qt does not seem to be designed for use in plugin libs. It loads some
180 // add-on libs dynamically with dlopen() and unloads them at program exit
181 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
182 // lib (libqt-mt) themselves, when they are unloading the last call to
183 // dlclose() for them causes the last reference to libqt-mt to vanish as
184 // well. Since libqt-mt is already in the process of destruction there is
185 // no more reference from the caller of libqt-mt, and the GLIBC decides
186 // that libqt-mt is not needed any more (zero references) and unmaps
187 // libqt-mt. When the static destructor of libqt-mt that triggered the
188 // cleanup in QGPluginManager returns, the code it is to return to is
189 // already unmapped, causing a segfault.
191 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
192 // and make sure there is no corresponding dlclose().
194 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
195 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
196 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
197 qt_lib ? "successful" : "failed" );
201 qApp->setFont( yqApp()->currentFont() );
204 connect( & _user_input_timer, SIGNAL( timeout() ),
205 this, SLOT ( userInputTimeout() ) );
207 connect( _busy_cursor_timer, SIGNAL( timeout() ),
208 this, SLOT ( busyCursor() ) );
212 // playMacro( macro_file );
214 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
215 qApp->processEvents();
222 return static_cast<YQApplication *>( app() );
226 void YQUI::processCommandLineArgs( int argc, char **argv )
230 for( int i=0; i < argc; i++ )
232 QString opt = argv[i];
234 y2milestone ("Qt argument: %s", argv[i]);
236 // Normalize command line option - accept "--xy" as well as "-xy"
238 if ( opt.startsWith( "--" ) )
241 if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
242 else if ( opt == QString( "-noborder" ) ) _noborder = true;
243 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
244 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
245 // --macro is handled by YUI_component
246 else if ( opt == QString( "-help" ) )
249 "Command line options for the YaST2 Qt UI:\n"
251 "--nothreads run without additional UI threads\n"
252 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
253 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
254 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
255 "--help this help text\n"
257 "--macro <macro-file> play a macro right on startup\n"
259 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
260 "to maintain backwards compatibility.\n"
269 // Qt handles command line option "-reverse" for Arabic / Hebrew
276 y2debug("Closing down Qt UI.");
280 // Intentionally NOT calling dlclose() to libqt-mt
281 // (see constructor for explanation)
287 YQUI::createWidgetFactory()
289 YQWidgetFactory * factory = new YQWidgetFactory();
290 YUI_CHECK_NEW( factory );
297 YOptionalWidgetFactory *
298 YQUI::createOptionalWidgetFactory()
300 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
301 YUI_CHECK_NEW( factory );
308 YQUI::createApplication()
310 YQApplication * app = new YQApplication();
311 YUI_CHECK_NEW( app );
317 void YQUI::calcDefaultSize()
319 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
320 QSize availableSize = qApp->desktop()->availableGeometry().size();
324 _default_size = availableSize;
326 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
327 _default_size.width(), _default_size.height() );
331 // Get _default_size via -geometry command line option (if set)
333 // NOTE not needed in qt4
334 // QWidget * dummy = new QWidget();
336 // qApp->setMainWidget( dummy );
337 // _default_size = dummy->size();
339 // Set min defaultsize or figure one out if -geometry was not used
341 if ( _default_size.width() < 800 ||
342 _default_size.height() < 600 )
344 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
346 // Scale down to 70% of screen size
348 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
349 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
353 _default_size = availableSize;
358 y2milestone( "Forced size (via -geometry): %dx%d",
359 _default_size.width(), _default_size.height() );
363 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
368 void YQUI::internalError( const char * msg )
371 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
372 QMessageBox::Abort | QMessageBox::Default,
376 if ( button == QMessageBox::Abort )
381 // exit() leaves a process running (WFM?), so this really seems to be
382 // the only way to make sure we are really going down.
386 void YQUI::idleLoop( int fd_ycp )
390 // runs in main thread
391 _eventLoop->wakeUp();
396 FD_SET(fd_ycp, &rfds);
400 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
407 void YQUI::sendEvent( YEvent * event )
411 _event_handler.sendEvent( event );
414 _eventLoop->exit( 1 );
419 YEvent * YQUI::userInput( unsigned long timeout_millisec )
423 _event_handler.blockEvents( false );
424 _eventLoop->wakeUp();
427 //y2milestone( "userInput %ld", timeout_millisec );
430 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
432 if ( _user_input_timer.isActive() )
433 _user_input_timer.stop();
437 if ( timeout_millisec > 0 )
438 _user_input_timer.start( timeout_millisec ); // single shot
440 if ( qApp->focusWidget() )
441 qApp->focusWidget()->setFocus();
444 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
446 _do_exit_loop = false;
448 event = _event_handler.consumePendingEvent();
450 // Display a busy cursor, but only if there is no other activity within
451 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
453 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
456 if ( _user_input_timer.isActive() )
457 _user_input_timer.stop();
463 YEvent * YQUI::pollInput()
467 if ( _user_input_timer.isActive() )
468 _user_input_timer.stop();
470 if ( ! pendingEvent() )
472 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
476 //qApp->processEvents();
477 event = _event_handler.consumePendingEvent();
481 if ( pendingEvent() )
482 event = _event_handler.consumePendingEvent();
488 void YQUI::userInputTimeout()
490 if ( ! pendingEvent() )
491 sendEvent( new YTimeoutEvent() );
495 #warning FIXME Move this to Y(Q)Dialog (and rename it to ::finalize()?)
496 void YQUI::showDialog( YDialog * dialog )
498 QWidget * qw = (QWidget *) dialog->widgetRep();
507 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
509 //qApp->processEvents();
513 void YQUI::closeDialog( YDialog * dialog )
518 QString YQUI::productName() const
520 return fromUTF8( YUI::productName() );
525 YQUI::setTextdomain( const char * domain )
527 bindtextdomain( domain, LOCALEDIR );
528 bind_textdomain_codeset( domain, "utf8" );
529 textdomain( domain );
531 // Make change known.
533 extern int _nl_msg_cat_cntr;
539 void YQUI::blockEvents( bool block )
543 if ( ++blocked_level == 1 )
545 _event_handler.blockEvents( true );
551 if ( --blocked_level == 0 )
553 _event_handler.blockEvents( false );
554 _eventLoop->wakeUp();
560 bool YQUI::eventsBlocked() const
562 return _event_handler.eventsBlocked();
566 qMessageHandler( QtMsgType type, const char * msg )
571 y2milestone ("qt-debug: %s\n", msg);
574 y2warning ("qt-warning: %s\n", msg);
580 y2warning ("qt-critical: %s\n", msg);
583 y2internal ("qt-fatal: %s\n", msg);
584 exit (1); // qt does the same