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"
49 #define BUSY_CURSOR_TIMEOUT 200 // milliseconds
53 static void qMessageHandler( QtMsgType type, const char * msg );
55 YQUI::YQUI( int argc, char **argv, bool with_threads, const char * macro_file )
59 , _do_exit_loop( false )
62 y2milestone( "YQUI constructor start" );
67 _usingVisionImpairedPalette = false;
68 _leftHandedMouse = false;
69 _askedForLeftHandedMouse = false;
70 screenShotNameTemplate = "";
73 qInstallMsgHandler( qMessageHandler );
75 // Copy command line arguments for QApplication
77 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
80 for ( int i=0; i < argc; i++ )
81 _ui_argv[i+1] = strdup( argv[i] );
83 _ui_argv[0] = strdup( "YaST2" );
86 topmostConstructorHasFinished();
95 Qt thinks the first QObject defines the main thread, but
96 the main thread in this case is the ycp thread, not UI
98 //extern void qt_set_current_thread_to_main_thread();
99 //qt_set_current_thread_to_main_thread();
103 new QApplication( _ui_argc, _ui_argv);
104 _busy_cursor_timer = new QTimer( qApp );
105 _busy_cursor_timer->setSingleShot( true );
107 _user_input_timer.setSingleShot( true );
109 _normalPalette = qApp->palette();
111 // Qt keeps track to a global QApplication in qApp.
114 processCommandLineArgs( _ui_argc, _ui_argv );
117 _styler = new QY2Styler( qApp );
118 // _styler->setStyleSheet( "style.qss" );
120 // Event loop object. Required since a YaST2 UI needs to react to commands
121 // from the YCP command stream as well as to X11 / Qt events.
122 _eventLoop = new QEventLoop( qApp );
123 _do_exit_loop = false;
125 // Create main window for `opt(`defaultsize) dialogs.
127 // We have to use something else than QWidgetStack since QWidgetStack
128 // doesn't accept a WFlags arg which we badly need here.
130 _main_win = new QWidget( 0, Qt::Window ); // parent, wflags
131 _main_win->setFocusPolicy( Qt::StrongFocus );
132 _main_win->setObjectName( "main_window" );
134 _main_win->resize( _default_size );
137 _main_win->move( 0, 0 );
142 QString title( "YaST2" );
143 char hostname[ MAXHOSTNAMELEN+1 ];
145 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
147 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
149 if ( strlen( hostname ) > 0 )
151 if ( ( strcmp( hostname, "(none)" ) != 0 &&
152 strcmp( hostname, "linux" ) != 0 ) )
160 _main_win->setWindowTitle( title );
163 // Hide the main window for now. The first call to UI::OpenDialog() on an
164 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
165 // the main window - there is nothing to display yet.
170 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
171 // if no Qt style defined):
173 // Qt does not seem to be designed for use in plugin libs. It loads some
174 // add-on libs dynamically with dlopen() and unloads them at program exit
175 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
176 // lib (libqt-mt) themselves, when they are unloading the last call to
177 // dlclose() for them causes the last reference to libqt-mt to vanish as
178 // well. Since libqt-mt is already in the process of destruction there is
179 // no more reference from the caller of libqt-mt, and the GLIBC decides
180 // that libqt-mt is not needed any more (zero references) and unmaps
181 // libqt-mt. When the static destructor of libqt-mt that triggered the
182 // cleanup in QGPluginManager returns, the code it is to return to is
183 // already unmapped, causing a segfault.
185 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
186 // and make sure there is no corresponding dlclose().
188 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
189 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
190 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
191 qt_lib ? "successful" : "failed" );
195 qApp->setFont( yqApp()->currentFont() );
198 connect( & _user_input_timer, SIGNAL( timeout() ),
199 this, SLOT ( userInputTimeout() ) );
201 connect( _busy_cursor_timer, SIGNAL( timeout() ),
202 this, SLOT ( busyCursor() ) );
206 // playMacro( macro_file );
208 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
209 qApp->processEvents();
216 return static_cast<YQApplication *>( app() );
220 void YQUI::processCommandLineArgs( int argc, char **argv )
224 for( int i=0; i < argc; i++ )
226 QString opt = argv[i];
228 y2milestone ("Qt argument: %s", argv[i]);
230 // Normalize command line option - accept "--xy" as well as "-xy"
232 if ( opt.startsWith( "--" ) )
235 if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
236 else if ( opt == QString( "-noborder" ) ) _noborder = true;
237 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
238 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
239 // --macro is handled by YUI_component
240 else if ( opt == QString( "-help" ) )
243 "Command line options for the YaST2 Qt UI:\n"
245 "--nothreads run without additional UI threads\n"
246 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
247 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
248 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
249 "--help this help text\n"
251 "--macro <macro-file> play a macro right on startup\n"
253 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
254 "to maintain backwards compatibility.\n"
263 // Qt handles command line option "-reverse" for Arabic / Hebrew
270 y2debug("Closing down Qt UI.");
274 // Intentionally NOT calling dlclose() to libqt-mt
275 // (see constructor for explanation)
281 YQUI::createWidgetFactory()
283 YQWidgetFactory * factory = new YQWidgetFactory();
284 YUI_CHECK_NEW( factory );
291 YOptionalWidgetFactory *
292 YQUI::createOptionalWidgetFactory()
294 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
295 YUI_CHECK_NEW( factory );
302 YQUI::createApplication()
304 YQApplication * app = new YQApplication();
305 YUI_CHECK_NEW( app );
311 void YQUI::calcDefaultSize()
313 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
314 QSize availableSize = qApp->desktop()->availableGeometry().size();
318 _default_size = availableSize;
320 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
321 _default_size.width(), _default_size.height() );
325 // Get _default_size via -geometry command line option (if set)
327 // NOTE not needed in qt4
328 // QWidget * dummy = new QWidget();
330 // qApp->setMainWidget( dummy );
331 // _default_size = dummy->size();
333 // Set min defaultsize or figure one out if -geometry was not used
335 if ( _default_size.width() < 800 ||
336 _default_size.height() < 600 )
338 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
340 // Scale down to 70% of screen size
342 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
343 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
347 _default_size = availableSize;
352 y2milestone( "Forced size (via -geometry): %dx%d",
353 _default_size.width(), _default_size.height() );
357 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
362 void YQUI::internalError( const char * msg )
365 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
366 QMessageBox::Abort | QMessageBox::Default,
370 if ( button == QMessageBox::Abort )
375 // exit() leaves a process running (WFM?), so this really seems to be
376 // the only way to make sure we are really going down.
380 void YQUI::idleLoop( int fd_ycp )
384 // runs in main thread
385 _eventLoop->wakeUp();
390 FD_SET(fd_ycp, &rfds);
394 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
401 void YQUI::sendEvent( YEvent * event )
405 _event_handler.sendEvent( event );
408 _eventLoop->exit( 1 );
413 YEvent * YQUI::userInput( unsigned long timeout_millisec )
417 _event_handler.blockEvents( false );
418 _eventLoop->wakeUp();
421 //y2milestone( "userInput %ld", timeout_millisec );
424 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
426 if ( _user_input_timer.isActive() )
427 _user_input_timer.stop();
431 if ( timeout_millisec > 0 )
432 _user_input_timer.start( timeout_millisec ); // single shot
434 if ( qApp->focusWidget() )
435 qApp->focusWidget()->setFocus();
438 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
440 _do_exit_loop = false;
442 event = _event_handler.consumePendingEvent();
444 // Display a busy cursor, but only if there is no other activity within
445 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
447 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
450 if ( _user_input_timer.isActive() )
451 _user_input_timer.stop();
457 YEvent * YQUI::pollInput()
461 if ( _user_input_timer.isActive() )
462 _user_input_timer.stop();
464 if ( ! pendingEvent() )
466 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
470 //qApp->processEvents();
471 event = _event_handler.consumePendingEvent();
475 if ( pendingEvent() )
476 event = _event_handler.consumePendingEvent();
482 void YQUI::userInputTimeout()
484 if ( ! pendingEvent() )
485 sendEvent( new YTimeoutEvent() );
489 #warning FIXME Move this to Y(Q)Dialog (and rename it to ::finalize()?)
490 void YQUI::showDialog( YDialog * dialog )
492 QWidget * qw = (QWidget *) dialog->widgetRep();
501 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
503 //qApp->processEvents();
507 void YQUI::closeDialog( YDialog * dialog )
512 QString YQUI::productName() const
514 return fromUTF8( YUI::productName() );
519 YQUI::setTextdomain( const char * domain )
521 bindtextdomain( domain, LOCALEDIR );
522 bind_textdomain_codeset( domain, "utf8" );
523 textdomain( domain );
525 // Make change known.
527 extern int _nl_msg_cat_cntr;
533 void YQUI::blockEvents( bool block )
537 if ( ++blocked_level == 1 )
539 _event_handler.blockEvents( true );
545 if ( --blocked_level == 0 )
547 _event_handler.blockEvents( false );
548 _eventLoop->wakeUp();
554 bool YQUI::eventsBlocked() const
556 return _event_handler.eventsBlocked();
560 qMessageHandler( QtMsgType type, const char * msg )
565 y2milestone ("qt-debug: %s\n", msg);
568 y2warning ("qt-warning: %s\n", msg);
572 y2warning ("qt-critical: %s\n", msg);
575 y2internal ("qt-fatal: %s\n", msg);
576 exit (1); // qt does the same