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;
71 screenShotNameTemplate = "";
74 qInstallMsgHandler( qMessageHandler );
76 // Copy command line arguments for QApplication
78 _ui_argv = (char **) malloc( (argc+1) * sizeof( const char * ) );
81 for ( int i=0; i < argc; i++ )
82 _ui_argv[i+1] = strdup( argv[i] );
84 _ui_argv[0] = strdup( "YaST2" );
87 topmostConstructorHasFinished();
96 Qt thinks the first QObject defines the main thread, but
97 the main thread in this case is the ycp thread, not UI
99 //extern void qt_set_current_thread_to_main_thread();
100 //qt_set_current_thread_to_main_thread();
104 new QApplication( _ui_argc, _ui_argv);
105 _busy_cursor_timer = new QTimer( qApp );
106 _busy_cursor_timer->setSingleShot( true );
108 _user_input_timer.setSingleShot( true );
110 _normalPalette = qApp->palette();
112 // Qt keeps track to a global QApplication in qApp.
115 processCommandLineArgs( _ui_argc, _ui_argv );
118 _styler = new QY2Styler( qApp );
119 QString style = getenv("Y2STYLE");
120 if ( !style.isNull() )
121 _styler->setStyleSheet( style );
123 // Event loop object. Required since a YaST2 UI needs to react to commands
124 // from the YCP command stream as well as to X11 / Qt events.
125 _eventLoop = new QEventLoop( qApp );
126 _do_exit_loop = false;
128 // Create main window for `opt(`defaultsize) dialogs.
130 // We have to use something else than QWidgetStack since QWidgetStack
131 // doesn't accept a WFlags arg which we badly need here.
133 _main_win = new QWidget( 0, Qt::Window ); // parent, wflags
134 _main_win->setFocusPolicy( Qt::StrongFocus );
135 _main_win->setObjectName( "main_window" );
137 _main_win->resize( _default_size );
140 _main_win->move( 0, 0 );
145 QString title( "YaST2" );
146 char hostname[ MAXHOSTNAMELEN+1 ];
148 if ( gethostname( hostname, sizeof( hostname )-1 ) == 0 )
150 hostname[ sizeof( hostname ) -1 ] = '\0'; // make sure it's terminated
152 if ( strlen( hostname ) > 0 )
154 if ( ( strcmp( hostname, "(none)" ) != 0 &&
155 strcmp( hostname, "linux" ) != 0 ) )
163 _main_win->setWindowTitle( title );
166 // Hide the main window for now. The first call to UI::OpenDialog() on an
167 // `opt(`defaultSize) dialog will trigger a showDialog() call that shows
168 // the main window - there is nothing to display yet.
173 // Ugly hack as a workaround of bug #121872 (Segfault at program exit
174 // if no Qt style defined):
176 // Qt does not seem to be designed for use in plugin libs. It loads some
177 // add-on libs dynamically with dlopen() and unloads them at program exit
178 // (QGPluginManager). Unfortunately, since they all depend on the Qt master
179 // lib (libqt-mt) themselves, when they are unloading the last call to
180 // dlclose() for them causes the last reference to libqt-mt to vanish as
181 // well. Since libqt-mt is already in the process of destruction there is
182 // no more reference from the caller of libqt-mt, and the GLIBC decides
183 // that libqt-mt is not needed any more (zero references) and unmaps
184 // libqt-mt. When the static destructor of libqt-mt that triggered the
185 // cleanup in QGPluginManager returns, the code it is to return to is
186 // already unmapped, causing a segfault.
188 // Workaround: Keep one more reference to libqt-mt open - dlopen() it here
189 // and make sure there is no corresponding dlclose().
191 QString qt_lib_name = QString( QTLIBDIR "/libQtGui.so.%1" ).arg( QT_VERSION >> 16 );;
192 void * qt_lib = dlopen( qt_lib_name.toUtf8().constData(), RTLD_GLOBAL );
193 y2milestone( "Forcing %s open %s", qt_lib_name.toUtf8().constData(),
194 qt_lib ? "successful" : "failed" );
198 qApp->setFont( yqApp()->currentFont() );
201 connect( & _user_input_timer, SIGNAL( timeout() ),
202 this, SLOT ( userInputTimeout() ) );
204 connect( _busy_cursor_timer, SIGNAL( timeout() ),
205 this, SLOT ( busyCursor() ) );
209 // playMacro( macro_file );
211 y2milestone( "YQUI constructor end %ld", QThread::currentThreadId () );
212 qApp->processEvents();
219 return static_cast<YQApplication *>( app() );
223 void YQUI::processCommandLineArgs( int argc, char **argv )
227 for( int i=0; i < argc; i++ )
229 QString opt = argv[i];
231 y2milestone ("Qt argument: %s", argv[i]);
233 // Normalize command line option - accept "--xy" as well as "-xy"
235 if ( opt.startsWith( "--" ) )
238 if ( opt == QString( "-fullscreen" ) ) _fullscreen = true;
239 else if ( opt == QString( "-noborder" ) ) _noborder = true;
240 else if ( opt == QString( "-auto-font" ) ) yqApp()->setAutoFonts( true );
241 else if ( opt == QString( "-auto-fonts" ) ) yqApp()->setAutoFonts( true );
242 // --macro is handled by YUI_component
243 else if ( opt == QString( "-help" ) )
246 "Command line options for the YaST2 Qt UI:\n"
248 "--nothreads run without additional UI threads\n"
249 "--fullscreen use full screen for `opt(`defaultsize) dialogs\n"
250 "--noborder no window manager border for `opt(`defaultsize) dialogs\n"
251 "--auto-fonts automatically pick fonts, disregard Qt standard settings\n"
252 "--help this help text\n"
254 "--macro <macro-file> play a macro right on startup\n"
256 "-no-wm, -noborder etc. are accepted as well as --no-wm, --noborder\n"
257 "to maintain backwards compatibility.\n"
266 // Qt handles command line option "-reverse" for Arabic / Hebrew
273 y2debug("Closing down Qt UI.");
277 // Intentionally NOT calling dlclose() to libqt-mt
278 // (see constructor for explanation)
284 YQUI::createWidgetFactory()
286 YQWidgetFactory * factory = new YQWidgetFactory();
287 YUI_CHECK_NEW( factory );
294 YOptionalWidgetFactory *
295 YQUI::createOptionalWidgetFactory()
297 YQOptionalWidgetFactory * factory = new YQOptionalWidgetFactory();
298 YUI_CHECK_NEW( factory );
305 YQUI::createApplication()
307 YQApplication * app = new YQApplication();
308 YUI_CHECK_NEW( app );
314 void YQUI::calcDefaultSize()
316 QSize primaryScreenSize = qApp->desktop()->screenGeometry( qApp->desktop()->primaryScreen() ).size();
317 QSize availableSize = qApp->desktop()->availableGeometry().size();
321 _default_size = availableSize;
323 y2milestone( "-fullscreen: using %dx%d for `opt(`defaultsize)",
324 _default_size.width(), _default_size.height() );
328 // Get _default_size via -geometry command line option (if set)
330 // NOTE not needed in qt4
331 // QWidget * dummy = new QWidget();
333 // qApp->setMainWidget( dummy );
334 // _default_size = dummy->size();
336 // Set min defaultsize or figure one out if -geometry was not used
338 if ( _default_size.width() < 800 ||
339 _default_size.height() < 600 )
341 if ( primaryScreenSize.width() >= 1024 && primaryScreenSize.height() >= 768 )
343 // Scale down to 70% of screen size
345 _default_size.setWidth ( max( (int) (availableSize.width() * 0.7), 800 ) );
346 _default_size.setHeight( max( (int) (availableSize.height() * 0.7), 600 ) );
350 _default_size = availableSize;
355 y2milestone( "Forced size (via -geometry): %dx%d",
356 _default_size.width(), _default_size.height() );
360 y2milestone( "Default size: %dx%d", _default_size.width(), _default_size.height() );
365 void YQUI::internalError( const char * msg )
368 int button = QMessageBox::critical( 0, "YaST2 Internal Error", msg,
369 QMessageBox::Abort | QMessageBox::Default,
373 if ( button == QMessageBox::Abort )
378 // exit() leaves a process running (WFM?), so this really seems to be
379 // the only way to make sure we are really going down.
383 void YQUI::idleLoop( int fd_ycp )
387 // runs in main thread
388 _eventLoop->wakeUp();
393 FD_SET(fd_ycp, &rfds);
397 int retval = select(fd_ycp+1, &rfds, NULL, NULL, NULL);
404 void YQUI::sendEvent( YEvent * event )
408 _event_handler.sendEvent( event );
411 _eventLoop->exit( 1 );
416 YEvent * YQUI::userInput( unsigned long timeout_millisec )
420 _event_handler.blockEvents( false );
421 _eventLoop->wakeUp();
424 //y2milestone( "userInput %ld", timeout_millisec );
427 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
429 if ( _user_input_timer.isActive() )
430 _user_input_timer.stop();
434 if ( timeout_millisec > 0 )
435 _user_input_timer.start( timeout_millisec ); // single shot
437 if ( qApp->focusWidget() )
438 qApp->focusWidget()->setFocus();
441 _do_exit_loop = true; // should exit_loop() be called in sendEvent()?
443 _do_exit_loop = false;
445 event = _event_handler.consumePendingEvent();
447 // Display a busy cursor, but only if there is no other activity within
448 // BUSY_CURSOR_TIMEOUT milliseconds (avoid cursor flicker)
450 _busy_cursor_timer->start( BUSY_CURSOR_TIMEOUT ); // single shot
453 if ( _user_input_timer.isActive() )
454 _user_input_timer.stop();
460 YEvent * YQUI::pollInput()
464 if ( _user_input_timer.isActive() )
465 _user_input_timer.stop();
467 if ( ! pendingEvent() )
469 YQDialog * dialog = dynamic_cast<YQDialog *> ( YDialog::currentDialog( false ) );
473 //qApp->processEvents();
474 event = _event_handler.consumePendingEvent();
478 if ( pendingEvent() )
479 event = _event_handler.consumePendingEvent();
485 void YQUI::userInputTimeout()
487 if ( ! pendingEvent() )
488 sendEvent( new YTimeoutEvent() );
492 #warning FIXME Move this to Y(Q)Dialog (and rename it to ::finalize()?)
493 void YQUI::showDialog( YDialog * dialog )
495 QWidget * qw = (QWidget *) dialog->widgetRep();
504 ( (YQDialog *) dialog)->ensureOnlyOneDefaultButton();
506 //qApp->processEvents();
510 void YQUI::closeDialog( YDialog * dialog )
515 QString YQUI::productName() const
517 return fromUTF8( YUI::productName() );
522 YQUI::setTextdomain( const char * domain )
524 bindtextdomain( domain, LOCALEDIR );
525 bind_textdomain_codeset( domain, "utf8" );
526 textdomain( domain );
528 // Make change known.
530 extern int _nl_msg_cat_cntr;
536 void YQUI::blockEvents( bool block )
540 if ( ++blocked_level == 1 )
542 _event_handler.blockEvents( true );
548 if ( --blocked_level == 0 )
550 _event_handler.blockEvents( false );
551 _eventLoop->wakeUp();
557 bool YQUI::eventsBlocked() const
559 return _event_handler.eventsBlocked();
563 qMessageHandler( QtMsgType type, const char * msg )
568 y2milestone ("qt-debug: %s\n", msg);
571 y2warning ("qt-warning: %s\n", msg);
575 y2warning ("qt-critical: %s\n", msg);
578 y2internal ("qt-fatal: %s\n", msg);
579 exit (1); // qt does the same