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"
48 #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 )
61 , _wm_close_blocked( false )
62 , _auto_activate_dialogs( true )
64 y2milestone( "YQUI constructor start" );
70 _decorate_toplevel_window = true;
71 _usingVisionImpairedPalette = false;
72 _leftHandedMouse = false;
73 _askedForLeftHandedMouse = false;
74 screenShotNameTemplate = "";
77 qInstallMsgHandler( qMessageHandler );
79 // Copy command line arguments for QApplication
81 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
84 for ( int i=0; i < argc; i++ )
85 _ui_argv[i+1] = strdup( argv[i] );
87 _ui_argv[0] = strdup( "YaST2" );
90 topmostConstructorHasFinished();
99 Qt thinks the first QObject defines the main thread, but
100 the main thread in this case is the ycp thread, not UI
102 //extern void qt_set_current_thread_to_main_thread();
103 //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 qApp->installEventFilter( this );
119 processCommandLineArgs( _ui_argc, _ui_argv );
122 _styler = new QY2Styler( qApp );
123 _styler->setStyleSheet( "style.qss" );
125 // Event loop object. Required since a YaST2 UI needs to react to commands
126 // from the YCP command stream as well as to X11 / Qt events.
127 _eventLoop = new QEventLoop( qApp );
128 _do_exit_loop = false;
130 // Create main window for `opt(`defaultsize) dialogs.
132 // We have to use something else than QWidgetStack since QWidgetStack
133 // doesn't accept a WFlags arg which we badly need here.
135 Qt::WFlags wflags = Qt::Window;
137 if ( ! _decorate_toplevel_window )
139 y2debug( "Suppressing WM decorations for toplevel window" );
140 wflags |= Qt::FramelessWindowHint;
143 _main_win = new QWidget( 0, wflags ); // parent, wflags
144 _main_win->setFocusPolicy( Qt::StrongFocus );
145 _main_win->setObjectName( "main_window" );
146 _styler->registerWidget( _main_win );
148 // Create widget stack for `opt(`defaultsize) dialogs
149 QVBoxLayout *vbox = new QVBoxLayout( _main_win );
150 vbox->setSpacing( 0 );
151 vbox->setMargin( 0 );
153 _widget_stack = new QStackedWidget( _main_win );
154 _widget_stack->setFocusPolicy( Qt::StrongFocus );
155 vbox->addWidget( _widget_stack );
157 _main_win->installEventFilter( this );
160 _main_win->resize( _default_size );
162 if ( _fullscreen || ! _have_wm )
163 _main_win->move( 0, 0 );
168 QString title( "YaST2" );
169 char hostname[ MAXHOSTNAMELEN+1 ];
171 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
173 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
175 if ( strlen( hostname ) > 0 )
177 if ( ( strcmp( hostname, "(none)" ) != 0 &&
178 strcmp( hostname, "linux" ) != 0 ) )
186 _main_win->setWindowTitle( title );
189 // Hide the main window for now. The first call to UI::OpenDialog() on an
190 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
191 // the main window - there is nothing to display yet.
196 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
197 // if no Qt style defined):
199 // Qt does not seem to be designed for use in plugin libs. It loads some
200 // add-on libs dynamically with dlopen() and unloads them at program exit
201 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
202 // lib (libqt-mt) themselves, when they are unloading the last call to
203 // dlclose() for them causes the last reference to libqt-mt to vanish as
204 // well. Since libqt-mt is already in the process of destruction there is
205 // no more reference from the caller of libqt-mt, and the GLIBC decides
206 // that libqt-mt is not needed any more (zero references) and unmaps
207 // libqt-mt. When the static destructor of libqt-mt that triggered the
208 // cleanup in QGPluginManager returns, the code it is to return to is
209 // already unmapped, causing a segfault.
211 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
212 // and make sure there is no corresponding dlclose().
214 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
215 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
216 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
217 qt_lib ? "successful" : "failed" );
221 qApp->setFont( yqApp()->currentFont() );
224 connect( & _user_input_timer, SIGNAL( timeout() ),
225 this, SLOT ( userInputTimeout() ) );
227 connect( _busy_cursor_timer, SIGNAL( timeout() ),
228 this, SLOT ( busyCursor() ) );
232 // playMacro( macro_file );
234 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
235 qApp->processEvents();
242 return static_cast<YQApplication *>( app() );
246 void YQUI::processCommandLineArgs( int argc, char **argv )
250 for( int i=0; i < argc; i++ )
252 QString opt = argv[i];
254 y2milestone ("Qt argument: %s", argv[i]);
256 // Normalize command line option - accept "--xy" as well as "-xy"
258 if ( opt.startsWith( "--" ) )
261 if ( opt == QString( "-no-wm" ) ) _have_wm = false;
262 else if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
263 else if ( opt == QString( "-noborder" ) ) _decorate_toplevel_window = false;
264 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
265 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
266 // --macro is handled by YUI_component
267 else if ( opt == QString( "-help" ) )
270 "Command line options for the YaST2 Qt UI:\n"
272 "--nothreads run without additional UI threads\n"
273 "--no-wm assume no window manager is running\n"
274 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
275 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
276 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
277 "--help this help text\n"
279 "--macro <macro-file> play a macro right on startup\n"
281 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
282 "to maintain backwards compatibility.\n"
291 // Qt handles command line option "-reverse" for Arabic / Hebrew
298 y2debug("Closing down Qt UI.");
302 // Intentionally NOT calling dlclose() to libqt-mt
303 // (see constructor for explanation)
309 YQUI::createWidgetFactory()
311 YQWidgetFactory * factory = new YQWidgetFactory();
312 YUI_CHECK_NEW( factory );
319 YOptionalWidgetFactory *
320 YQUI::createOptionalWidgetFactory()
322 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
323 YUI_CHECK_NEW( factory );
330 YQUI::createApplication()
332 YQApplication * app = new YQApplication();
333 YUI_CHECK_NEW( app );
339 void YQUI::calcDefaultSize()
341 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
342 QSize availableSize = qApp->desktop()->availableGeometry().size();
346 _default_size = availableSize;
348 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
349 _default_size.width(), _default_size.height() );
353 // Get _default_size via -geometry command line option (if set)
355 // NOTE not needed in qt4
356 // QWidget * dummy = new QWidget();
358 // qApp->setMainWidget( dummy );
359 // _default_size = dummy->size();
362 // Set min defaultsize or figure one out if -geometry was not used
364 if ( _default_size.width() < 800 ||
365 _default_size.height() < 600 )
367 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
369 // Scale down to 70% of screen size
371 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
372 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
376 _default_size = availableSize;
381 y2milestone( "Forced size (via -geometry): %dx%d",
382 _default_size.width(), _default_size.height() );
387 _default_size = primaryScreenSize;
391 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
396 void YQUI::internalError( const char * msg )
399 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
400 QMessageBox::Abort | QMessageBox::Default,
404 if ( button == QMessageBox::Abort )
409 // exit() leaves a process running (WFM?), so this really seems to be
410 // the only way to make sure we are really going down.
414 void YQUI::idleLoop( int fd_ycp )
418 // runs in main thread
419 _eventLoop->wakeUp();
424 FD_SET(fd_ycp, &rfds);
428 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
435 void YQUI::sendEvent( YEvent * event )
439 _event_handler.sendEvent( event );
442 _eventLoop->exit( 1 );
447 YEvent * YQUI::userInput( unsigned long timeout_millisec )
451 _event_handler.blockEvents( false );
452 _eventLoop->wakeUp();
455 //y2milestone( "userInput %ld", timeout_millisec );
458 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
460 if ( _user_input_timer.isActive() )
461 _user_input_timer.stop();
465 if ( timeout_millisec > 0 )
466 _user_input_timer.start( timeout_millisec ); // single shot
468 dialog->activate( true );
470 if ( qApp->focusWidget() )
471 qApp->focusWidget()->setFocus();
474 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
476 _do_exit_loop = false;
478 event = _event_handler.consumePendingEvent();
479 dialog->activate( false );
481 // Display a busy cursor, but only if there is no other activity within
482 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
484 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
487 if ( _user_input_timer.isActive() )
488 _user_input_timer.stop();
494 YEvent * YQUI::pollInput()
498 if ( _user_input_timer.isActive() )
499 _user_input_timer.stop();
501 if ( ! pendingEvent() )
503 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
507 dialog->activate( true );
508 //qApp->processEvents();
509 event = _event_handler.consumePendingEvent();
510 dialog->activate( false );
514 if ( pendingEvent() )
515 event = _event_handler.consumePendingEvent();
521 void YQUI::userInputTimeout()
523 if ( ! pendingEvent() )
524 sendEvent( new YTimeoutEvent() );
530 YDialog * YQUI::createDialog( YWidgetOpt & opt )
534 y2milestone( "createDialog %ld", QThread::currentThreadId () );
535 bool has_defaultsize = opt.hasDefaultSize.value();
536 QWidget * qt_parent =
537 has_defaultsize ? _widget_stack : _main_win;
539 // FIXME: Probably obsolete
540 if ( ! has_defaultsize && ! _popup_stack.empty() )
541 qt_parent = _popup_stack.back();
543 YQDialog * dialog = new YQDialog( opt, qt_parent, has_defaultsize );
544 Q_CHECK_PTR( dialog );
546 if ( ! has_defaultsize )
547 _popup_stack.push_back( (QWidget *) dialog->widgetRep() );
554 void YQUI::showDialog( YDialog * dialog )
556 QWidget * qw = (QWidget *) dialog->widgetRep();
560 y2error( "No widgetRep() for dialog" );
564 if ( dialog->dialogType() == YMainDialog )
566 _widget_stack->addWidget ( qw );
567 _widget_stack->setCurrentWidget( qw );
569 if ( ! _main_win->isVisible() )
571 // y2milestone( "Showing main window" );
572 _main_win->resize( _default_size );
575 _main_win->move( 0, 0 );
581 else // non-defaultsize dialog
586 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
588 //qApp->processEvents();
592 void YQUI::closeDialog( YDialog * dialog )
594 QWidget * qw = (QWidget *) dialog->widgetRep();
598 y2error( "No widgetRep() for dialog" );
602 if ( dialog->dialogType() == YMainDialog )
604 _widget_stack->removeWidget( qw );
606 else // non-defaultsize dialog
612 // Clean up the popup stack. libyui guarantees that a dialog will be
613 // deleted after closeDialog() so it is safe to pop that dialog from
614 // the popup stack here.
616 if ( ! _popup_stack.empty() && _popup_stack.back() == qw )
617 _popup_stack.pop_back();
619 y2error( "Popup dialog stack corrupted!" );
625 void YQUI::easterEgg()
627 y2milestone( "Starting easter egg..." );
630 YQEasterBunny::layEgg();
631 y2milestone( "Done." );
634 // desktop()->repaint() has no effect - we need to do it the hard way.
635 system( "/usr/X11R6/bin/xrefresh" );
640 QString YQUI::productName() const
642 return fromUTF8( YUI::productName() );
647 YQUI::setTextdomain( const char * domain )
649 bindtextdomain( domain, LOCALEDIR );
650 bind_textdomain_codeset( domain, "utf8" );
651 textdomain( domain );
653 // Make change known.
655 extern int _nl_msg_cat_cntr;
661 void YQUI::blockEvents( bool block )
665 if ( ++blocked_level == 1 )
667 _event_handler.blockEvents( true );
673 if ( --blocked_level == 0 )
675 _event_handler.blockEvents( false );
676 _eventLoop->wakeUp();
682 bool YQUI::eventsBlocked() const
684 return _event_handler.eventsBlocked();
688 qMessageHandler( QtMsgType type, const char * msg )
693 y2milestone ("qt-debug: %s\n", msg);
696 y2warning ("qt-warning: %s\n", msg);
700 y2warning ("qt-critical: %s\n", msg);
703 y2internal ("qt-fatal: %s\n", msg);
704 exit (1); // qt does the same