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>
33 #include "YQGenericButton.h"
34 #include "YQWizardButton.h"
36 #include "YQMainWinDock.h"
38 // Include low-level X headers AFTER Qt headers:
39 // X.h pollutes the global namespace (!!!) with pretty useless #defines
40 // like "Above", "Below" etc. that clash with some Qt headers.
43 #define YQMainDialogWFlags Qt::Widget
45 #define YQPopupDialogWFlags Qt::Dialog
48 YQDialog::YQDialog( YDialogType dialogType,
49 YDialogColorMode colorMode )
50 : QWidget( chooseParent( dialogType ),
51 dialogType == YMainDialog ? YQMainDialogWFlags : YQPopupDialogWFlags )
52 , YDialog( dialogType, colorMode )
60 setWindowTitle( "YaST2" );
61 setFocusPolicy( Qt::StrongFocus );
63 if ( colorMode != YDialogNormalColor )
65 QColor normalBackground ( 0, 128, 0 );
66 QColor inputFieldBackground ( 0xbb, 0xff, 0xbb );
67 QColor text = Qt::white;
69 if ( colorMode == YDialogInfoColor )
71 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
74 QPalette warnPalette( normalBackground );
75 warnPalette.setColor( QPalette::Text, text );
76 warnPalette.setColor( QPalette::Base, inputFieldBackground );
77 setPalette( warnPalette );
80 if ( dialogType == YMainDialog &&
81 QWidget::parent() != YQMainWinDock::mainWinDock() )
83 setWindowFlags( YQPopupDialogWFlags );
86 if ( QWidget::parent() == YQMainWinDock::mainWinDock() )
88 YQMainWinDock::mainWinDock()->add( this );
95 if ( dialogType() == YMainDialog )
97 YQMainWinDock::mainWinDock()->remove( this );
100 _defaultButton->forgetDialog();
102 _focusButton->forgetDialog();
107 YQDialog::chooseParent( YDialogType dialogType )
109 QWidget * parent = 0;
111 if ( dialogType == YMainDialog &&
112 YQMainWinDock::mainWinDock()->couldDock() )
114 y2debug( "Adding dialog to mainWinDock" );
115 parent = YQMainWinDock::mainWinDock();
123 YQDialog::preferredWidth()
127 if ( dialogType() == YMainDialog )
130 preferredWidth = _userSize.width();
132 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
136 preferredWidth = YDialog::preferredWidth();
139 int screenWidth = qApp->desktop()->width();
141 if ( preferredWidth > screenWidth )
143 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
144 screenWidth, preferredWidth );
147 return preferredWidth;
152 YQDialog::preferredHeight()
156 if ( dialogType() == YMainDialog )
159 preferredHeight = _userSize.height();
161 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
165 preferredHeight = YDialog::preferredHeight();
168 int screenHeight = qApp->desktop()->height();
170 if ( preferredHeight > screenHeight )
172 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
173 screenHeight, preferredHeight );
176 return preferredHeight;
181 YQDialog::setEnabled( bool enabled )
183 QWidget::setEnabled( enabled );
184 YDialog::setEnabled( enabled );
189 YQDialog::setSize( int newWidth, int newHeight )
191 // y2debug( "Resizing dialog to %d x %d", newWidth, newHeight );
193 if ( newWidth > qApp->desktop()->width() )
194 newWidth = qApp->desktop()->width();
196 if ( newHeight > qApp->desktop()->height() )
197 newHeight = qApp->desktop()->height();
201 ( ( QWidget* )firstChild()->widgetRep() )->show();
202 firstChild()->setSize( newWidth, newHeight );
205 resize( newWidth, newHeight );
210 YQDialog::activate( bool active )
214 ensureOnlyOneDefaultButton();
220 YQDialog::resizeEvent( QResizeEvent * event )
224 // y2debug( "Resize event: %d x %d", event->size().width(), event->size().height() );
225 setSize ( event->size().width(), event->size().height() );
226 _userSize = event->size();
228 if ( QWidget::parent() )
235 YQDialog::findDefaultButton()
237 if ( _defaultButton )
238 return _defaultButton;
240 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
242 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
243 YDialog::setDefaultButton( _defaultButton );
245 return _defaultButton;
250 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
251 YWidgetListConstIterator end ) const
254 for ( YWidgetListConstIterator it = begin; it != end; ++it )
256 YWidget * widget = *it;
262 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
264 if ( button && button->isDefaultButton() )
271 // Recurse over the children of this widget
274 if ( widget->hasChildren() )
276 button = findDefaultButton( widget->childrenBegin(),
277 widget->childrenEnd() );
288 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
289 YWidgetListConstIterator end )
291 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
292 YQWizard * wizard = 0;
294 for ( YWidgetListConstIterator it = begin; it != end; ++it )
296 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
297 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
300 wizard = dynamic_cast<YQWizard *> (*it);
304 wizardButton->showAsDefault( false );
308 if ( button->isDefaultButton() )
310 if ( _defaultButton && button != _defaultButton )
312 y2error( "Too many default buttons: [%s]",
313 qPrintable(button->text()) );
314 y2error( "Using old default button: [%s]",
315 qPrintable(_defaultButton->text()) );
319 _defaultButton = button;
323 if ( button->isShownAsDefault() && button != def )
324 button->showAsDefault( false );
327 if ( (*it)->hasChildren() )
329 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
330 (*it)->childrenEnd() );
341 YQDialog::ensureOnlyOneDefaultButton()
344 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
346 if ( ! _defaultButton && wizard )
348 _defaultButton = wizardDefaultButton( wizard );
351 if ( _defaultButton )
353 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
354 YDialog::setDefaultButton( _defaultButton );
358 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
361 def->showAsDefault();
366 YQDialog::findWizard() const
368 return findWizard( childrenBegin(), childrenEnd() );
373 YQDialog::findWizard( YWidgetListConstIterator begin,
374 YWidgetListConstIterator end ) const
376 for ( YWidgetListConstIterator it = begin; it != end; ++it )
378 YWidget * widget = *it;
379 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
384 if ( widget->hasChildren() )
386 wizard = findWizard( widget->childrenBegin(),
387 widget->childrenEnd() );
398 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
400 YQGenericButton * def = 0;
403 wizard = findWizard();
407 // Pick one of the wizard buttons
409 if ( wizard->direction() == YQWizard::Backward )
411 if ( wizard->backButton()
412 && wizard->backButton()->isShown()
413 && wizard->backButton()->isEnabled() )
415 def = wizard->backButton();
421 if ( wizard->nextButton()
422 && wizard->nextButton()->isShown()
423 && wizard->nextButton()->isEnabled() )
425 def = wizard->nextButton();
435 YQDialog::setDefaultButton( YPushButton * newDefaultButton )
437 if ( _defaultButton &&
439 newDefaultButton != _defaultButton )
441 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
443 // Let app defined default buttons override wizard buttons
444 _defaultButton->setDefaultButton( false );
448 y2error( "Too many `opt(`default) PushButtons: [%s]",
449 newDefaultButton->label().c_str() );
450 newDefaultButton->setDefaultButton( false );
455 _defaultButton = dynamic_cast<YQGenericButton*>(newDefaultButton);
457 if ( _defaultButton )
459 _defaultButton->setDefaultButton( true );
460 y2debug( "New default button: [%s]", qPrintable(_defaultButton->text()) );
462 if ( _defaultButton && ! _focusButton )
463 _defaultButton->showAsDefault( true );
467 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
468 YDialog::setDefaultButton( _defaultButton );
473 YQDialog::activateDefaultButton( bool warn )
475 // Try the focus button first, if there is any.
478 _focusButton->isEnabled() &&
479 _focusButton->isShownAsDefault() )
481 y2debug( "Activating focus button: [%s]", qPrintable(_focusButton->text()) );
482 _focusButton->activate();
487 // No focus button - try the default button, if there is any.
489 _defaultButton = findDefaultButton();
491 if ( _defaultButton &&
492 _defaultButton->isEnabled() &&
493 _defaultButton->isShownAsDefault() )
495 y2debug( "Activating default button: [%s]", qPrintable(_defaultButton->text()) );
496 _defaultButton->activate();
503 y2warning( "No default button in this dialog - ignoring [Return]" );
512 YQDialog::losingFocus( YQGenericButton * button )
514 if ( button == _focusButton )
516 if ( _focusButton && _focusButton != _defaultButton )
517 _focusButton->showAsDefault( false );
522 if ( ! _focusButton && _defaultButton )
523 _defaultButton->showAsDefault( true );
528 YQDialog::gettingFocus( YQGenericButton * button )
530 if ( _focusButton && _focusButton != button )
531 _focusButton->showAsDefault( false );
533 if ( _defaultButton && _defaultButton != button )
534 _defaultButton->showAsDefault( false );
536 _focusButton = button;
539 _focusButton->showAsDefault( true );
544 YQDialog::keyPressEvent( QKeyEvent * event )
548 if ( event->key() == Qt::Key_Print )
550 YQUI::ui()->makeScreenShot( "" );
553 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
554 event->modifiers() & Qt::ShiftModifier )
556 YQUI::ui()->toggleVisionImpairedPalette();
558 if ( YQUI::ui()->usingVisionImpairedPalette() )
560 y2milestone( "Switched to vision impaired palette" );
561 QMessageBox::information( 0, // parent
562 _("Color switching"), // caption
563 _( "Switching to color palette for vision impaired users -\n"
564 "press Shift-F4 again to switch back to normal colors." ), // text
565 QMessageBox::Ok | QMessageBox::Default, // button0
566 QMessageBox::NoButton, // button1
567 QMessageBox::NoButton ); // button2
571 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
572 event->modifiers() == Qt::ShiftModifier )
574 YQUI::ui()->askConfigureLogging();
577 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
578 event->modifiers() & Qt::ShiftModifier )
580 YQUI::ui()->askSaveLogs();
583 else if ( event->modifiers() & Qt::NoModifier ) // No Ctrl / Alt / Shift etc. pressed
585 if ( event->key() == Qt::Key_Return ||
586 event->key() == Qt::Key_Enter )
588 ( void ) activateDefaultButton();
592 else if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier ) )
594 // Qt-UI special keys - all with Ctrl-Shift-Alt
596 y2milestone( "Caught YaST2 magic key combination" );
598 if ( event->key() == Qt::Key_M )
600 YQUI::ui()->toggleRecordMacro();
603 else if ( event->key() == Qt::Key_P )
605 YQUI::ui()->askPlayMacro();
608 else if ( event->key() == Qt::Key_D )
610 YQUI::ui()->sendEvent( new YDebugEvent() );
613 else if ( event->key() == Qt::Key_X )
615 y2milestone( "Starting xterm" );
616 system( "/usr/bin/xterm &" );
622 QWidget::keyPressEvent( event );
627 YQDialog::closeEvent( QCloseEvent * event )
629 // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
630 // handled just like the user had clicked on the `id`( `cancel ) button in
631 // that dialog. It's up to the YCP application to handle this (if desired).
633 y2milestone( "Caught window manager close event - returning with YCancelEvent" );
635 YQUI::ui()->sendEvent( new YCancelEvent() );
640 YQDialog::focusInEvent( QFocusEvent * event )
643 // The dialog itself doesn't need or want the keyboard focus, but obviously
644 // (since Qt 2.3?) it needs QFocusPolicy::StrongFocus for the default
645 // button mechanism to work. So let's accept the focus and give it to some
648 if ( event->reason() == Qt::TabFocusReason )
650 focusNextPrevChild( true );
654 if ( _defaultButton )
655 _defaultButton->setKeyboardFocus();
657 focusNextPrevChild( true );
663 YQDialog::center( QWidget * dialog, QWidget * parent )
665 if ( ! dialog || ! parent )
668 qDebug() << "center" << parent->rect() << dialog->rect();
670 QPoint pos( ( parent->width() - dialog->width() ) / 2,
671 ( parent->height() - dialog->height() ) / 2 );
673 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
674 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
681 #include "YQDialog.moc"