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>
26 #include <qmessagebox.h>
27 #include <QDesktopWidget>
33 #include "YQGenericButton.h"
34 #include "YQWizardButton.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::Window
44 #define YQPopupDialogWFlags Qt::Widget
46 YQDialog::YQDialog( QWidget * qParent,
47 YDialogType dialogType,
48 YDialogColorMode colorMode )
50 dialogType == YMainDialog ? YQMainDialogWFlags : YQPopupDialogWFlags )
51 , YDialog( dialogType, colorMode )
58 setWindowTitle( dialogType == YMainDialog ? "YaST2" : "" );
59 setFocusPolicy( Qt::StrongFocus );
61 if ( colorMode != YDialogNormalColor )
63 QColor normalBackground ( 0, 128, 0 );
64 QColor inputFieldBackground ( 0, 96, 0 );
65 QColor text = Qt::white;
67 if ( colorMode == YDialogInfoColor )
69 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
72 QPalette warnPalette( normalBackground );
73 //warnPalette.createColorGroup(Active);
74 warnPalette.setColor( QPalette::Active, QPalette::Text, text );
75 warnPalette.setColor( QPalette::Active, QPalette::Base, inputFieldBackground );
76 warnPalette.setCurrentColorGroup(QPalette::Active);
77 setPalette( warnPalette );
87 int YQDialog::preferredWidth()
91 if ( dialogType() == YMainDialog )
94 preferredWidth = _userSize.width();
96 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
100 preferredWidth = YDialog::preferredWidth();
103 int screenWidth = qApp->desktop()->width();
105 if ( preferredWidth > screenWidth )
107 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
108 screenWidth, preferredWidth );
111 return preferredWidth;
115 int YQDialog::preferredHeight()
119 if ( dialogType() == YMainDialog )
122 preferredHeight = _userSize.height();
124 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
128 preferredHeight = YDialog::preferredHeight();
131 int screenHeight = qApp->desktop()->height();
133 if ( preferredHeight > screenHeight )
135 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
136 screenHeight, preferredHeight );
139 return preferredHeight;
143 void YQDialog::setEnabled( bool enabled )
145 QWidget::setEnabled( enabled );
146 YDialog::setEnabled( enabled );
150 void YQDialog::setSize( int newWidth, int newHeight )
152 if ( newWidth > qApp->desktop()->width() )
153 newWidth = qApp->desktop()->width();
155 if ( newHeight > qApp->desktop()->height() )
156 newHeight = qApp->desktop()->height();
160 firstChild()->setSize( newWidth, newHeight );
163 resize( newWidth, newHeight );
168 void YQDialog::activate( bool active )
172 if ( ! YQUI::ui()->haveWM() )
174 if ( YQUI::ui()->autoActivateDialogs() )
177 y2milestone( "Auto-activating dialog window turned off" );
180 ensureOnlyOneDefaultButton();
186 YQDialog::resizeEvent( QResizeEvent * event )
190 setSize ( event->size().width(), event->size().height() );
191 _userSize = event->size();
198 YQDialog::findDefaultButton()
200 if ( _defaultButton )
201 return _defaultButton;
203 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
205 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
206 YDialog::setDefaultButton( _defaultButton );
208 return _defaultButton;
213 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
214 YWidgetListConstIterator end ) const
217 for ( YWidgetListConstIterator it = begin; it != end; ++it )
219 YWidget * widget = *it;
225 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
227 if ( button && button->isDefaultButton() )
234 // Recurse over the children of this widget
237 if ( widget->hasChildren() )
239 button = findDefaultButton( widget->childrenBegin(),
240 widget->childrenEnd() );
251 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
252 YWidgetListConstIterator end )
254 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
255 YQWizard * wizard = 0;
257 for ( YWidgetListConstIterator it = begin; it != end; ++it )
259 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
260 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
263 wizard = dynamic_cast<YQWizard *> (*it);
267 wizardButton->showAsDefault( false );
271 if ( button->isDefaultButton() )
273 if ( _defaultButton && button != _defaultButton )
275 y2error( "Too many default buttons: [%s]",
276 qPrintable(button->text()) );
277 y2error( "Using old default button: [%s]",
278 qPrintable(_defaultButton->text()) );
282 _defaultButton = button;
286 if ( button->isShownAsDefault() && button != def )
287 button->showAsDefault( false );
290 if ( (*it)->hasChildren() )
292 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
293 (*it)->childrenEnd() );
304 YQDialog::ensureOnlyOneDefaultButton()
307 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
309 if ( ! _defaultButton && wizard )
311 _defaultButton = wizardDefaultButton( wizard );
314 if ( _defaultButton )
316 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
317 YDialog::setDefaultButton( _defaultButton );
321 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
324 def->showAsDefault();
329 YQDialog::findWizard() const
331 return findWizard( childrenBegin(), childrenEnd() );
336 YQDialog::findWizard( YWidgetListConstIterator begin,
337 YWidgetListConstIterator end ) const
340 for ( YWidgetListConstIterator it = begin; it != end; ++it )
342 YWidget * widget = *it;
343 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
348 if ( widget->hasChildren() )
350 wizard = findWizard( widget->childrenBegin(),
351 widget->childrenEnd() );
362 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
364 YQGenericButton * def = 0;
367 wizard = findWizard();
371 // Pick one of the wizard buttons
373 if ( wizard->direction() == YQWizard::Backward )
375 if ( wizard->backButton()
376 && wizard->backButton()->isShown()
377 && wizard->backButton()->isEnabled() )
379 def = wizard->backButton();
385 if ( wizard->nextButton()
386 && wizard->nextButton()->isShown()
387 && wizard->nextButton()->isEnabled() )
389 def = wizard->nextButton();
399 YQDialog::setDefaultButton( YPushButton * newDefaultButton )
401 if ( _defaultButton &&
403 newDefaultButton != _defaultButton )
405 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
407 // Let app defined default buttons override wizard buttons
408 _defaultButton->setDefaultButton( false );
412 y2error( "Too many `opt(`default) PushButtons: [%s]",
413 newDefaultButton->label().c_str() );
414 newDefaultButton->setDefaultButton( false );
419 _defaultButton = dynamic_cast<YQGenericButton*>(newDefaultButton);
421 if ( _defaultButton )
423 _defaultButton->setDefaultButton( true );
424 y2debug( "New default button: [%s]", qPrintable(_defaultButton->text()) );
426 if ( _defaultButton && ! _focusButton )
427 _defaultButton->showAsDefault( true );
431 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
432 YDialog::setDefaultButton( _defaultButton );
437 YQDialog::activateDefaultButton( bool warn )
439 // Try the focus button first, if there is any.
442 _focusButton->isEnabled() &&
443 _focusButton->isShownAsDefault() )
445 y2debug( "Activating focus button: [%s]", qPrintable(_focusButton->text()) );
446 _focusButton->activate();
451 // No focus button - try the default button, if there is any.
453 _defaultButton = findDefaultButton();
455 if ( _defaultButton &&
456 _defaultButton->isEnabled() &&
457 _defaultButton->isShownAsDefault() )
459 y2debug( "Activating default button: [%s]", qPrintable(_defaultButton->text()) );
460 _defaultButton->activate();
467 y2warning( "No default button in this dialog - ignoring [Return]" );
476 YQDialog::losingFocus( YQGenericButton * button )
478 if ( button == _focusButton )
480 if ( _focusButton && _focusButton != _defaultButton )
481 _focusButton->showAsDefault( false );
486 if ( ! _focusButton && _defaultButton )
487 _defaultButton->showAsDefault( true );
492 YQDialog::gettingFocus( YQGenericButton * button )
494 if ( _focusButton && _focusButton != button )
495 _focusButton->showAsDefault( false );
497 if ( _defaultButton && _defaultButton != button )
498 _defaultButton->showAsDefault( false );
500 _focusButton = button;
503 _focusButton->showAsDefault( true );
508 YQDialog::keyPressEvent( QKeyEvent * event )
512 if ( event->key() == Qt::Key_Print )
514 YQUI::ui()->makeScreenShot( "" );
517 else if ( event->key() == Qt::Key_F5 ) // No matter if Ctrl/Alt/Shift pressed
519 YQUI::ui()->easterEgg();
522 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
523 event->modifiers() & Qt::ShiftModifier )
525 YQUI::ui()->toggleVisionImpairedPalette();
527 if ( YQUI::ui()->usingVisionImpairedPalette() )
529 y2milestone( "Switched to vision impaired palette" );
530 QMessageBox::information( 0, // parent
531 _("Color switching"), // caption
532 _( "Switching to color palette for vision impaired users -\n"
533 "press Shift-F4 again to switch back to normal colors." ), // text
534 QMessageBox::Ok | QMessageBox::Default, // button0
535 QMessageBox::NoButton, // button1
536 QMessageBox::NoButton ); // button2
540 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
541 event->modifiers() == Qt::ShiftModifier )
543 YQUI::ui()->askConfigureLogging();
546 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
547 event->modifiers() & Qt::ShiftModifier )
549 YQUI::ui()->askSaveLogs();
552 else if ( event->modifiers() & Qt::NoModifier ) // No Ctrl / Alt / Shift etc. pressed
554 if ( event->key() == Qt::Key_Return ||
555 event->key() == Qt::Key_Enter )
557 ( void ) activateDefaultButton();
561 else if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier ) )
563 // Qt-UI special keys - all with Ctrl-Shift-Alt
565 y2milestone( "Caught YaST2 magic key combination" );
567 if ( event->key() == Qt::Key_M )
569 YQUI::ui()->toggleRecordMacro();
572 else if ( event->key() == Qt::Key_P )
574 YQUI::ui()->askPlayMacro();
577 else if ( event->key() == Qt::Key_D )
579 YQUI::ui()->sendEvent( new YDebugEvent() );
582 else if ( event->key() == Qt::Key_X )
584 y2milestone( "Starting xterm" );
585 system( "/usr/bin/xterm &" );
591 QWidget::keyPressEvent( event );
595 void YQDialog::closeEvent( QCloseEvent * event )
597 // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
598 // handled just like the user had clicked on the `id`( `cancel ) button in
599 // that dialog. It's up to the YCP application to handle this (if desired).
601 y2debug( "Ignoring window manager close button." );
603 YQUI::ui()->sendEvent( new YCancelEvent() );
607 void YQDialog::focusInEvent( QFocusEvent * event )
610 // The dialog itself doesn't need or want the keyboard focus, but obviously
611 // ( since Qt 2.3? ) it needs QFocusPolicy::StrongFocus for the default
612 // button mechanism to work. So let's accept the focus and give it to some
615 if ( event->reason() == Qt::TabFocusReason )
617 focusNextPrevChild( true );
621 if ( _defaultButton )
622 _defaultButton->setKeyboardFocus();
624 focusNextPrevChild( true );
633 if ( ! dialogType() == YMainDialog && qApp->mainWidget()->isVisible() )
634 center( this, qApp->mainWidget() );
642 YQDialog::center( QWidget * dialog, QWidget * parent )
649 //FIXME if not toplevel window
650 // align with top level widget
651 foreach (QWidget *widget, QApplication::topLevelWidgets())
653 // if it is a top level widget, center with desktop
654 if (dialog == widget) parent = qApp->desktop(); break;
657 if ( QApplication::topLevelWidgets().empty() )
660 parent = QApplication::topLevelWidgets().first();
663 QPoint pos( ( parent->width() - dialog->width() ) / 2,
664 ( parent->height() - dialog->height() ) / 2 );
666 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
667 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
673 #include "YQDialog.moc"