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 ( 0xbb, 0xff, 0xbb );
69 QColor text = Qt::white;
71 if ( colorMode == YDialogInfoColor )
73 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
76 QPalette warnPalette( normalBackground );
77 warnPalette.setColor( QPalette::Text, text );
78 warnPalette.setColor( QPalette::Base, inputFieldBackground );
79 setPalette( warnPalette );
82 if ( dialogType == YMainDialog &&
83 QWidget::parent() != YQMainWinDock::mainWinDock() )
85 setWindowFlags( YQPopupDialogWFlags );
92 if ( dialogType() == YMainDialog )
94 YQMainWinDock::mainWinDock()->remove( (QWidget *) widgetRep() );
100 YQDialog::chooseParent( YDialogType dialogType )
102 QWidget * parent = 0;
104 if ( dialogType == YMainDialog &&
105 YQMainWinDock::mainWinDock()->couldDock() )
107 y2debug( "Adding dialog to mainWinDock" );
108 parent = YQMainWinDock::mainWinDock();
116 YQDialog::preferredWidth()
120 if ( dialogType() == YMainDialog )
123 preferredWidth = _userSize.width();
125 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
129 preferredWidth = YDialog::preferredWidth();
132 int screenWidth = qApp->desktop()->width();
134 if ( preferredWidth > screenWidth )
136 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
137 screenWidth, preferredWidth );
140 return preferredWidth;
145 YQDialog::preferredHeight()
149 if ( dialogType() == YMainDialog )
152 preferredHeight = _userSize.height();
154 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
158 preferredHeight = YDialog::preferredHeight();
161 int screenHeight = qApp->desktop()->height();
163 if ( preferredHeight > screenHeight )
165 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
166 screenHeight, preferredHeight );
169 return preferredHeight;
174 YQDialog::setEnabled( bool enabled )
176 QWidget::setEnabled( enabled );
177 YDialog::setEnabled( enabled );
182 YQDialog::setSize( int newWidth, int newHeight )
184 // y2debug( "Resizing dialog to %d x %d", newWidth, newHeight );
186 if ( newWidth > qApp->desktop()->width() )
187 newWidth = qApp->desktop()->width();
189 if ( newHeight > qApp->desktop()->height() )
190 newHeight = qApp->desktop()->height();
194 firstChild()->setSize( newWidth, newHeight );
197 resize( newWidth, newHeight );
202 YQDialog::activate( bool active )
206 ensureOnlyOneDefaultButton();
212 YQDialog::resizeEvent( QResizeEvent * event )
216 // y2debug( "Resize event: %d x %d", event->size().width(), event->size().height() );
217 setSize ( event->size().width(), event->size().height() );
218 _userSize = event->size();
220 if ( QWidget::parent() )
227 YQDialog::findDefaultButton()
229 if ( _defaultButton )
230 return _defaultButton;
232 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
234 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
235 YDialog::setDefaultButton( _defaultButton );
237 return _defaultButton;
242 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
243 YWidgetListConstIterator end ) const
246 for ( YWidgetListConstIterator it = begin; it != end; ++it )
248 YWidget * widget = *it;
254 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
256 if ( button && button->isDefaultButton() )
263 // Recurse over the children of this widget
266 if ( widget->hasChildren() )
268 button = findDefaultButton( widget->childrenBegin(),
269 widget->childrenEnd() );
280 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
281 YWidgetListConstIterator end )
283 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
284 YQWizard * wizard = 0;
286 for ( YWidgetListConstIterator it = begin; it != end; ++it )
288 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
289 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
292 wizard = dynamic_cast<YQWizard *> (*it);
296 wizardButton->showAsDefault( false );
300 if ( button->isDefaultButton() )
302 if ( _defaultButton && button != _defaultButton )
304 y2error( "Too many default buttons: [%s]",
305 qPrintable(button->text()) );
306 y2error( "Using old default button: [%s]",
307 qPrintable(_defaultButton->text()) );
311 _defaultButton = button;
315 if ( button->isShownAsDefault() && button != def )
316 button->showAsDefault( false );
319 if ( (*it)->hasChildren() )
321 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
322 (*it)->childrenEnd() );
333 YQDialog::ensureOnlyOneDefaultButton()
336 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
338 if ( ! _defaultButton && wizard )
340 _defaultButton = wizardDefaultButton( wizard );
343 if ( _defaultButton )
345 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
346 YDialog::setDefaultButton( _defaultButton );
350 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
353 def->showAsDefault();
358 YQDialog::findWizard() const
360 return findWizard( childrenBegin(), childrenEnd() );
365 YQDialog::findWizard( YWidgetListConstIterator begin,
366 YWidgetListConstIterator end ) const
368 for ( YWidgetListConstIterator it = begin; it != end; ++it )
370 YWidget * widget = *it;
371 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
376 if ( widget->hasChildren() )
378 wizard = findWizard( widget->childrenBegin(),
379 widget->childrenEnd() );
390 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
392 YQGenericButton * def = 0;
395 wizard = findWizard();
399 // Pick one of the wizard buttons
401 if ( wizard->direction() == YQWizard::Backward )
403 if ( wizard->backButton()
404 && wizard->backButton()->isShown()
405 && wizard->backButton()->isEnabled() )
407 def = wizard->backButton();
413 if ( wizard->nextButton()
414 && wizard->nextButton()->isShown()
415 && wizard->nextButton()->isEnabled() )
417 def = wizard->nextButton();
427 YQDialog::setDefaultButton( YPushButton * newDefaultButton )
429 if ( _defaultButton &&
431 newDefaultButton != _defaultButton )
433 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
435 // Let app defined default buttons override wizard buttons
436 _defaultButton->setDefaultButton( false );
440 y2error( "Too many `opt(`default) PushButtons: [%s]",
441 newDefaultButton->label().c_str() );
442 newDefaultButton->setDefaultButton( false );
447 _defaultButton = dynamic_cast<YQGenericButton*>(newDefaultButton);
449 if ( _defaultButton )
451 _defaultButton->setDefaultButton( true );
452 y2debug( "New default button: [%s]", qPrintable(_defaultButton->text()) );
454 if ( _defaultButton && ! _focusButton )
455 _defaultButton->showAsDefault( true );
459 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
460 YDialog::setDefaultButton( _defaultButton );
465 YQDialog::activateDefaultButton( bool warn )
467 // Try the focus button first, if there is any.
470 _focusButton->isEnabled() &&
471 _focusButton->isShownAsDefault() )
473 y2debug( "Activating focus button: [%s]", qPrintable(_focusButton->text()) );
474 _focusButton->activate();
479 // No focus button - try the default button, if there is any.
481 _defaultButton = findDefaultButton();
483 if ( _defaultButton &&
484 _defaultButton->isEnabled() &&
485 _defaultButton->isShownAsDefault() )
487 y2debug( "Activating default button: [%s]", qPrintable(_defaultButton->text()) );
488 _defaultButton->activate();
495 y2warning( "No default button in this dialog - ignoring [Return]" );
504 YQDialog::losingFocus( YQGenericButton * button )
506 if ( button == _focusButton )
508 if ( _focusButton && _focusButton != _defaultButton )
509 _focusButton->showAsDefault( false );
514 if ( ! _focusButton && _defaultButton )
515 _defaultButton->showAsDefault( true );
520 YQDialog::gettingFocus( YQGenericButton * button )
522 if ( _focusButton && _focusButton != button )
523 _focusButton->showAsDefault( false );
525 if ( _defaultButton && _defaultButton != button )
526 _defaultButton->showAsDefault( false );
528 _focusButton = button;
531 _focusButton->showAsDefault( true );
536 YQDialog::keyPressEvent( QKeyEvent * event )
540 if ( event->key() == Qt::Key_Print )
542 YQUI::ui()->makeScreenShot( "" );
545 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
546 event->modifiers() & Qt::ShiftModifier )
548 YQUI::ui()->toggleVisionImpairedPalette();
550 if ( YQUI::ui()->usingVisionImpairedPalette() )
552 y2milestone( "Switched to vision impaired palette" );
553 QMessageBox::information( 0, // parent
554 _("Color switching"), // caption
555 _( "Switching to color palette for vision impaired users -\n"
556 "press Shift-F4 again to switch back to normal colors." ), // text
557 QMessageBox::Ok | QMessageBox::Default, // button0
558 QMessageBox::NoButton, // button1
559 QMessageBox::NoButton ); // button2
563 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
564 event->modifiers() == Qt::ShiftModifier )
566 YQUI::ui()->askConfigureLogging();
569 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
570 event->modifiers() & Qt::ShiftModifier )
572 YQUI::ui()->askSaveLogs();
575 else if ( event->modifiers() & Qt::NoModifier ) // No Ctrl / Alt / Shift etc. pressed
577 if ( event->key() == Qt::Key_Return ||
578 event->key() == Qt::Key_Enter )
580 ( void ) activateDefaultButton();
584 else if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier ) )
586 // Qt-UI special keys - all with Ctrl-Shift-Alt
588 y2milestone( "Caught YaST2 magic key combination" );
590 if ( event->key() == Qt::Key_M )
592 YQUI::ui()->toggleRecordMacro();
595 else if ( event->key() == Qt::Key_P )
597 YQUI::ui()->askPlayMacro();
600 else if ( event->key() == Qt::Key_D )
602 YQUI::ui()->sendEvent( new YDebugEvent() );
605 else if ( event->key() == Qt::Key_X )
607 y2milestone( "Starting xterm" );
608 system( "/usr/bin/xterm &" );
614 QWidget::keyPressEvent( event );
619 YQDialog::closeEvent( QCloseEvent * event )
621 // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
622 // handled just like the user had clicked on the `id`( `cancel ) button in
623 // that dialog. It's up to the YCP application to handle this (if desired).
625 y2milestone( "Caught window manager close event - returning with YCancelEvent" );
627 YQUI::ui()->sendEvent( new YCancelEvent() );
632 YQDialog::focusInEvent( QFocusEvent * event )
635 // The dialog itself doesn't need or want the keyboard focus, but obviously
636 // (since Qt 2.3?) it needs QFocusPolicy::StrongFocus for the default
637 // button mechanism to work. So let's accept the focus and give it to some
640 if ( event->reason() == Qt::TabFocusReason )
642 focusNextPrevChild( true );
646 if ( _defaultButton )
647 _defaultButton->setKeyboardFocus();
649 focusNextPrevChild( true );
655 YQDialog::center( QWidget * dialog, QWidget * parent )
657 if ( ! dialog || ! parent )
660 QPoint pos( ( parent->width() - dialog->width() ) / 2,
661 ( parent->height() - dialog->height() ) / 2 );
663 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
664 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
670 #include "YQDialog.moc"