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 // we first initialize without parent and then set a parent, so we can choose a parent
50 // based on the YDialog constructor
52 , YDialog( dialogType, colorMode )
55 QWidget::setParent( chooseParent( dialogType ),
56 dialogType == YMainDialog ? YQMainDialogWFlags : YQPopupDialogWFlags );
62 setWindowTitle( "YaST2" );
63 setFocusPolicy( Qt::StrongFocus );
65 if ( colorMode != YDialogNormalColor )
67 QColor normalBackground ( 0, 128, 0 );
68 QColor inputFieldBackground ( 0, 96, 0 );
69 QColor text = Qt::white;
71 if ( colorMode == YDialogInfoColor )
73 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
76 QPalette warnPalette( normalBackground );
77 //warnPalette.createColorGroup(Active);
78 warnPalette.setColor( QPalette::Active, QPalette::Text, text );
79 warnPalette.setColor( QPalette::Active, QPalette::Base, inputFieldBackground );
80 warnPalette.setCurrentColorGroup(QPalette::Active);
81 setPalette( warnPalette );
84 if ( dialogType == YMainDialog &&
85 QWidget::parent() != YQMainWinDock::mainWinDock() )
87 setWindowFlags( YQPopupDialogWFlags );
94 if ( dialogType() == YMainDialog )
96 YQMainWinDock::mainWinDock()->remove( (QWidget *) widgetRep() );
102 YQDialog::chooseParent( YDialogType dialogType )
104 QWidget * parent = 0;
106 if ( dialogType == YMainDialog &&
107 YQMainWinDock::mainWinDock()->couldDock() )
109 y2debug( "Adding dialog to mainWinDock" );
110 parent = YQMainWinDock::mainWinDock();
118 YQDialog::preferredWidth()
122 if ( dialogType() == YMainDialog )
125 preferredWidth = _userSize.width();
127 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
131 preferredWidth = YDialog::preferredWidth();
134 int screenWidth = qApp->desktop()->width();
136 if ( preferredWidth > screenWidth )
138 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
139 screenWidth, preferredWidth );
142 return preferredWidth;
147 YQDialog::preferredHeight()
151 if ( dialogType() == YMainDialog )
154 preferredHeight = _userSize.height();
156 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
160 preferredHeight = YDialog::preferredHeight();
163 int screenHeight = qApp->desktop()->height();
165 if ( preferredHeight > screenHeight )
167 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
168 screenHeight, preferredHeight );
171 return preferredHeight;
176 YQDialog::setEnabled( bool enabled )
178 QWidget::setEnabled( enabled );
179 YDialog::setEnabled( enabled );
184 YQDialog::setSize( int newWidth, int newHeight )
186 // y2debug( "Resizing dialog to %d x %d", newWidth, newHeight );
188 if ( newWidth > qApp->desktop()->width() )
189 newWidth = qApp->desktop()->width();
191 if ( newHeight > qApp->desktop()->height() )
192 newHeight = qApp->desktop()->height();
196 firstChild()->setSize( newWidth, newHeight );
199 resize( newWidth, newHeight );
204 YQDialog::activate( bool active )
208 ensureOnlyOneDefaultButton();
214 YQDialog::resizeEvent( QResizeEvent * event )
218 // y2debug( "Resize event: %d x %d", event->size().width(), event->size().height() );
219 setSize ( event->size().width(), event->size().height() );
220 _userSize = event->size();
222 if ( QWidget::parent() )
229 YQDialog::findDefaultButton()
231 if ( _defaultButton )
232 return _defaultButton;
234 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
236 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
237 YDialog::setDefaultButton( _defaultButton );
239 return _defaultButton;
244 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
245 YWidgetListConstIterator end ) const
248 for ( YWidgetListConstIterator it = begin; it != end; ++it )
250 YWidget * widget = *it;
256 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
258 if ( button && button->isDefaultButton() )
265 // Recurse over the children of this widget
268 if ( widget->hasChildren() )
270 button = findDefaultButton( widget->childrenBegin(),
271 widget->childrenEnd() );
282 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
283 YWidgetListConstIterator end )
285 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
286 YQWizard * wizard = 0;
288 for ( YWidgetListConstIterator it = begin; it != end; ++it )
290 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
291 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
294 wizard = dynamic_cast<YQWizard *> (*it);
298 wizardButton->showAsDefault( false );
302 if ( button->isDefaultButton() )
304 if ( _defaultButton && button != _defaultButton )
306 y2error( "Too many default buttons: [%s]",
307 qPrintable(button->text()) );
308 y2error( "Using old default button: [%s]",
309 qPrintable(_defaultButton->text()) );
313 _defaultButton = button;
317 if ( button->isShownAsDefault() && button != def )
318 button->showAsDefault( false );
321 if ( (*it)->hasChildren() )
323 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
324 (*it)->childrenEnd() );
335 YQDialog::ensureOnlyOneDefaultButton()
338 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
340 if ( ! _defaultButton && wizard )
342 _defaultButton = wizardDefaultButton( wizard );
345 if ( _defaultButton )
347 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
348 YDialog::setDefaultButton( _defaultButton );
352 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
355 def->showAsDefault();
360 YQDialog::findWizard() const
362 return findWizard( childrenBegin(), childrenEnd() );
367 YQDialog::findWizard( YWidgetListConstIterator begin,
368 YWidgetListConstIterator end ) const
370 for ( YWidgetListConstIterator it = begin; it != end; ++it )
372 YWidget * widget = *it;
373 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
378 if ( widget->hasChildren() )
380 wizard = findWizard( widget->childrenBegin(),
381 widget->childrenEnd() );
392 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
394 YQGenericButton * def = 0;
397 wizard = findWizard();
401 // Pick one of the wizard buttons
403 if ( wizard->direction() == YQWizard::Backward )
405 if ( wizard->backButton()
406 && wizard->backButton()->isShown()
407 && wizard->backButton()->isEnabled() )
409 def = wizard->backButton();
415 if ( wizard->nextButton()
416 && wizard->nextButton()->isShown()
417 && wizard->nextButton()->isEnabled() )
419 def = wizard->nextButton();
429 YQDialog::setDefaultButton( YPushButton * newDefaultButton )
431 if ( _defaultButton &&
433 newDefaultButton != _defaultButton )
435 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
437 // Let app defined default buttons override wizard buttons
438 _defaultButton->setDefaultButton( false );
442 y2error( "Too many `opt(`default) PushButtons: [%s]",
443 newDefaultButton->label().c_str() );
444 newDefaultButton->setDefaultButton( false );
449 _defaultButton = dynamic_cast<YQGenericButton*>(newDefaultButton);
451 if ( _defaultButton )
453 _defaultButton->setDefaultButton( true );
454 y2debug( "New default button: [%s]", qPrintable(_defaultButton->text()) );
456 if ( _defaultButton && ! _focusButton )
457 _defaultButton->showAsDefault( true );
461 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
462 YDialog::setDefaultButton( _defaultButton );
467 YQDialog::activateDefaultButton( bool warn )
469 // Try the focus button first, if there is any.
472 _focusButton->isEnabled() &&
473 _focusButton->isShownAsDefault() )
475 y2debug( "Activating focus button: [%s]", qPrintable(_focusButton->text()) );
476 _focusButton->activate();
481 // No focus button - try the default button, if there is any.
483 _defaultButton = findDefaultButton();
485 if ( _defaultButton &&
486 _defaultButton->isEnabled() &&
487 _defaultButton->isShownAsDefault() )
489 y2debug( "Activating default button: [%s]", qPrintable(_defaultButton->text()) );
490 _defaultButton->activate();
497 y2warning( "No default button in this dialog - ignoring [Return]" );
506 YQDialog::losingFocus( YQGenericButton * button )
508 if ( button == _focusButton )
510 if ( _focusButton && _focusButton != _defaultButton )
511 _focusButton->showAsDefault( false );
516 if ( ! _focusButton && _defaultButton )
517 _defaultButton->showAsDefault( true );
522 YQDialog::gettingFocus( YQGenericButton * button )
524 if ( _focusButton && _focusButton != button )
525 _focusButton->showAsDefault( false );
527 if ( _defaultButton && _defaultButton != button )
528 _defaultButton->showAsDefault( false );
530 _focusButton = button;
533 _focusButton->showAsDefault( true );
538 YQDialog::keyPressEvent( QKeyEvent * event )
542 if ( event->key() == Qt::Key_Print )
544 YQUI::ui()->makeScreenShot( "" );
547 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
548 event->modifiers() & Qt::ShiftModifier )
550 YQUI::ui()->toggleVisionImpairedPalette();
552 if ( YQUI::ui()->usingVisionImpairedPalette() )
554 y2milestone( "Switched to vision impaired palette" );
555 QMessageBox::information( 0, // parent
556 _("Color switching"), // caption
557 _( "Switching to color palette for vision impaired users -\n"
558 "press Shift-F4 again to switch back to normal colors." ), // text
559 QMessageBox::Ok | QMessageBox::Default, // button0
560 QMessageBox::NoButton, // button1
561 QMessageBox::NoButton ); // button2
565 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
566 event->modifiers() == Qt::ShiftModifier )
568 YQUI::ui()->askConfigureLogging();
571 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
572 event->modifiers() & Qt::ShiftModifier )
574 YQUI::ui()->askSaveLogs();
577 else if ( event->modifiers() & Qt::NoModifier ) // No Ctrl / Alt / Shift etc. pressed
579 if ( event->key() == Qt::Key_Return ||
580 event->key() == Qt::Key_Enter )
582 ( void ) activateDefaultButton();
586 else if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier ) )
588 // Qt-UI special keys - all with Ctrl-Shift-Alt
590 y2milestone( "Caught YaST2 magic key combination" );
592 if ( event->key() == Qt::Key_M )
594 YQUI::ui()->toggleRecordMacro();
597 else if ( event->key() == Qt::Key_P )
599 YQUI::ui()->askPlayMacro();
602 else if ( event->key() == Qt::Key_D )
604 YQUI::ui()->sendEvent( new YDebugEvent() );
607 else if ( event->key() == Qt::Key_X )
609 y2milestone( "Starting xterm" );
610 system( "/usr/bin/xterm &" );
616 QWidget::keyPressEvent( event );
621 YQDialog::closeEvent( QCloseEvent * event )
623 // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
624 // handled just like the user had clicked on the `id`( `cancel ) button in
625 // that dialog. It's up to the YCP application to handle this (if desired).
627 y2milestone( "Caught window manager close event - returning with YCancelEvent" );
629 YQUI::ui()->sendEvent( new YCancelEvent() );
634 YQDialog::focusInEvent( QFocusEvent * event )
637 // The dialog itself doesn't need or want the keyboard focus, but obviously
638 // (since Qt 2.3?) it needs QFocusPolicy::StrongFocus for the default
639 // button mechanism to work. So let's accept the focus and give it to some
642 if ( event->reason() == Qt::TabFocusReason )
644 focusNextPrevChild( true );
648 if ( _defaultButton )
649 _defaultButton->setKeyboardFocus();
651 focusNextPrevChild( true );
657 YQDialog::center( QWidget * dialog, QWidget * parent )
659 if ( ! dialog || ! parent )
662 QPoint pos( ( parent->width() - dialog->width() ) / 2,
663 ( parent->height() - dialog->height() ) / 2 );
665 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
666 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
672 #include "YQDialog.moc"