1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
22 #define y2log_component "qt-ui"
23 #include <ycp/y2log.h>
24 #include <qpushbutton.h>
25 #include <qmessagebox.h>
26 #include <QDesktopWidget>
32 #include "YQGenericButton.h"
33 #include "YQWizardButton.h"
35 #include "YQMainWinDock.h"
37 // Include low-level X headers AFTER Qt headers:
38 // X.h pollutes the global namespace (!!!) with pretty useless #defines
39 // like "Above", "Below" etc. that clash with some Qt headers.
42 #define YQMainDialogWFlags Qt::Widget
44 #define YQPopupDialogWFlags Qt::Dialog
47 YQDialog::YQDialog( YDialogType dialogType,
48 YDialogColorMode colorMode )
49 : QWidget( chooseParent( dialogType ),
50 dialogType == YMainDialog ? YQMainDialogWFlags : YQPopupDialogWFlags )
51 , YDialog( dialogType, colorMode )
59 setWindowTitle( "YaST2" );
60 setFocusPolicy( Qt::StrongFocus );
62 if ( colorMode != YDialogNormalColor )
64 QColor normalBackground ( 0, 128, 0 );
65 QColor inputFieldBackground ( 0xbb, 0xff, 0xbb );
66 QColor text = Qt::white;
68 if ( colorMode == YDialogInfoColor )
70 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
73 QPalette warnPalette( normalBackground );
74 warnPalette.setColor( QPalette::Text, text );
75 warnPalette.setColor( QPalette::Base, inputFieldBackground );
76 setPalette( warnPalette );
79 if ( dialogType == YMainDialog &&
80 QWidget::parent() != YQMainWinDock::mainWinDock() )
82 setWindowFlags( YQPopupDialogWFlags );
85 if ( QWidget::parent() == YQMainWinDock::mainWinDock() )
87 YQMainWinDock::mainWinDock()->add( this );
94 if ( dialogType() == YMainDialog )
96 YQMainWinDock::mainWinDock()->remove( this );
99 _defaultButton->forgetDialog();
101 _focusButton->forgetDialog();
106 YQDialog::chooseParent( YDialogType dialogType )
108 QWidget * parent = 0;
110 if ( dialogType == YMainDialog &&
111 YQMainWinDock::mainWinDock()->couldDock() )
113 y2debug( "Adding dialog to mainWinDock" );
114 parent = YQMainWinDock::mainWinDock();
122 YQDialog::preferredWidth()
126 if ( dialogType() == YMainDialog )
129 preferredWidth = _userSize.width();
131 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
135 preferredWidth = YDialog::preferredWidth();
138 int screenWidth = qApp->desktop()->width();
140 if ( preferredWidth > screenWidth )
142 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
143 screenWidth, preferredWidth );
146 return preferredWidth;
151 YQDialog::preferredHeight()
155 if ( dialogType() == YMainDialog )
158 preferredHeight = _userSize.height();
160 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
164 preferredHeight = YDialog::preferredHeight();
167 int screenHeight = qApp->desktop()->height();
169 if ( preferredHeight > screenHeight )
171 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
172 screenHeight, preferredHeight );
175 return preferredHeight;
180 YQDialog::setEnabled( bool enabled )
182 QWidget::setEnabled( enabled );
183 YDialog::setEnabled( enabled );
188 YQDialog::setSize( int newWidth, int newHeight )
190 // y2debug( "Resizing dialog to %d x %d", newWidth, newHeight );
192 if ( newWidth > qApp->desktop()->width() )
193 newWidth = qApp->desktop()->width();
195 if ( newHeight > qApp->desktop()->height() )
196 newHeight = qApp->desktop()->height();
200 ( ( QWidget* )firstChild()->widgetRep() )->show();
201 firstChild()->setSize( newWidth, newHeight );
204 resize( newWidth, newHeight );
209 YQDialog::activate( bool active )
213 ensureOnlyOneDefaultButton();
219 YQDialog::resizeEvent( QResizeEvent * event )
223 // y2debug( "Resize event: %d x %d", event->size().width(), event->size().height() );
224 setSize ( event->size().width(), event->size().height() );
225 _userSize = event->size();
227 if ( QWidget::parent() )
234 YQDialog::findDefaultButton()
236 if ( _defaultButton )
237 return _defaultButton;
239 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
241 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
242 YDialog::setDefaultButton( _defaultButton );
244 return _defaultButton;
249 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
250 YWidgetListConstIterator end ) const
253 for ( YWidgetListConstIterator it = begin; it != end; ++it )
255 YWidget * widget = *it;
261 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
263 if ( button && button->isDefaultButton() )
270 // Recurse over the children of this widget
273 if ( widget->hasChildren() )
275 button = findDefaultButton( widget->childrenBegin(),
276 widget->childrenEnd() );
287 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
288 YWidgetListConstIterator end )
290 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
291 YQWizard * wizard = 0;
293 for ( YWidgetListConstIterator it = begin; it != end; ++it )
295 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
296 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
299 wizard = dynamic_cast<YQWizard *> (*it);
303 wizardButton->showAsDefault( false );
307 if ( button->isDefaultButton() )
309 if ( _defaultButton && button != _defaultButton )
311 y2error( "Too many default buttons: [%s]",
312 qPrintable(button->text()) );
313 y2error( "Using old default button: [%s]",
314 qPrintable(_defaultButton->text()) );
318 _defaultButton = button;
322 if ( button->isShownAsDefault() && button != def )
323 button->showAsDefault( false );
326 if ( (*it)->hasChildren() )
328 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
329 (*it)->childrenEnd() );
340 YQDialog::ensureOnlyOneDefaultButton()
343 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
345 if ( ! _defaultButton && wizard )
347 _defaultButton = wizardDefaultButton( wizard );
350 if ( _defaultButton )
352 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
353 YDialog::setDefaultButton( _defaultButton );
357 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
360 def->showAsDefault();
365 YQDialog::findWizard() const
367 return findWizard( childrenBegin(), childrenEnd() );
372 YQDialog::findWizard( YWidgetListConstIterator begin,
373 YWidgetListConstIterator end ) const
375 for ( YWidgetListConstIterator it = begin; it != end; ++it )
377 YWidget * widget = *it;
378 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
383 if ( widget->hasChildren() )
385 wizard = findWizard( widget->childrenBegin(),
386 widget->childrenEnd() );
397 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
399 YQGenericButton * def = 0;
402 wizard = findWizard();
406 // Pick one of the wizard buttons
408 if ( wizard->direction() == YQWizard::Backward )
410 if ( wizard->backButton()
411 && wizard->backButton()->isShown()
412 && wizard->backButton()->isEnabled() )
414 def = wizard->backButton();
420 if ( wizard->nextButton()
421 && wizard->nextButton()->isShown()
422 && wizard->nextButton()->isEnabled() )
424 def = wizard->nextButton();
434 YQDialog::setDefaultButton( YPushButton * newDefaultButton )
436 if ( _defaultButton &&
438 newDefaultButton != _defaultButton )
440 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
442 // Let app defined default buttons override wizard buttons
443 _defaultButton->setDefaultButton( false );
447 y2error( "Too many `opt(`default) PushButtons: [%s]",
448 newDefaultButton->label().c_str() );
449 newDefaultButton->setDefaultButton( false );
454 _defaultButton = dynamic_cast<YQGenericButton*>(newDefaultButton);
456 if ( _defaultButton )
458 _defaultButton->setDefaultButton( true );
459 y2debug( "New default button: [%s]", qPrintable(_defaultButton->text()) );
461 if ( _defaultButton && ! _focusButton )
462 _defaultButton->showAsDefault( true );
466 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
467 YDialog::setDefaultButton( _defaultButton );
472 YQDialog::activateDefaultButton( bool warn )
474 // Try the focus button first, if there is any.
477 _focusButton->isEnabled() &&
478 _focusButton->isShownAsDefault() )
480 y2debug( "Activating focus button: [%s]", qPrintable(_focusButton->text()) );
481 _focusButton->activate();
486 // No focus button - try the default button, if there is any.
488 _defaultButton = findDefaultButton();
490 if ( _defaultButton &&
491 _defaultButton->isEnabled() &&
492 _defaultButton->isShownAsDefault() )
494 y2debug( "Activating default button: [%s]", qPrintable(_defaultButton->text()) );
495 _defaultButton->activate();
502 y2warning( "No default button in this dialog - ignoring [Return]" );
511 YQDialog::losingFocus( YQGenericButton * button )
513 if ( button == _focusButton )
515 if ( _focusButton && _focusButton != _defaultButton )
516 _focusButton->showAsDefault( false );
521 if ( ! _focusButton && _defaultButton )
522 _defaultButton->showAsDefault( true );
527 YQDialog::gettingFocus( YQGenericButton * button )
529 if ( _focusButton && _focusButton != button )
530 _focusButton->showAsDefault( false );
532 if ( _defaultButton && _defaultButton != button )
533 _defaultButton->showAsDefault( false );
535 _focusButton = button;
538 _focusButton->showAsDefault( true );
543 YQDialog::keyPressEvent( QKeyEvent * event )
547 if ( event->key() == Qt::Key_Print )
549 YQUI::ui()->makeScreenShot( "" );
552 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
553 event->modifiers() & Qt::ShiftModifier )
555 YQUI::ui()->toggleVisionImpairedPalette();
557 if ( YQUI::ui()->usingVisionImpairedPalette() )
559 y2milestone( "Switched to vision impaired palette" );
560 QMessageBox::information( 0, // parent
561 _("Color switching"), // caption
562 _( "Switching to color palette for vision impaired users -\n"
563 "press Shift-F4 again to switch back to normal colors." ), // text
564 QMessageBox::Ok | QMessageBox::Default, // button0
565 QMessageBox::NoButton, // button1
566 QMessageBox::NoButton ); // button2
570 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
571 event->modifiers() == Qt::ShiftModifier )
573 YQUI::ui()->askConfigureLogging();
576 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
577 event->modifiers() & Qt::ShiftModifier )
579 YQUI::ui()->askSaveLogs();
582 else if ( event->modifiers() & Qt::NoModifier ) // No Ctrl / Alt / Shift etc. pressed
584 if ( event->key() == Qt::Key_Return ||
585 event->key() == Qt::Key_Enter )
587 ( void ) activateDefaultButton();
591 else if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier ) )
593 // Qt-UI special keys - all with Ctrl-Shift-Alt
595 y2milestone( "Caught YaST2 magic key combination" );
597 if ( event->key() == Qt::Key_M )
599 YQUI::ui()->toggleRecordMacro();
602 else if ( event->key() == Qt::Key_P )
604 YQUI::ui()->askPlayMacro();
607 else if ( event->key() == Qt::Key_D )
609 YQUI::ui()->sendEvent( new YDebugEvent() );
612 else if ( event->key() == Qt::Key_X )
614 y2milestone( "Starting xterm" );
615 system( "/usr/bin/xterm &" );
621 QWidget::keyPressEvent( event );
626 YQDialog::closeEvent( QCloseEvent * event )
628 // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
629 // handled just like the user had clicked on the `id`( `cancel ) button in
630 // that dialog. It's up to the YCP application to handle this (if desired).
632 y2milestone( "Caught window manager close event - returning with YCancelEvent" );
634 YQUI::ui()->sendEvent( new YCancelEvent() );
639 YQDialog::focusInEvent( QFocusEvent * event )
642 // The dialog itself doesn't need or want the keyboard focus, but obviously
643 // (since Qt 2.3?) it needs QFocusPolicy::StrongFocus for the default
644 // button mechanism to work. So let's accept the focus and give it to some
647 if ( event->reason() == Qt::TabFocusReason )
649 focusNextPrevChild( true );
653 if ( _defaultButton )
654 _defaultButton->setKeyboardFocus();
656 focusNextPrevChild( true );
662 YQDialog::center( QWidget * dialog, QWidget * parent )
664 if ( ! dialog || ! parent )
667 QPoint pos( ( parent->width() - dialog->width() ) / 2,
668 ( parent->height() - dialog->height() ) / 2 );
670 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
671 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
677 #include "YQDialog.moc"