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 );
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 ( ( QWidget* )firstChild()->widgetRep() )->show();
197 firstChild()->setSize( newWidth, newHeight );
200 resize( newWidth, newHeight );
205 YQDialog::activate( bool active )
209 ensureOnlyOneDefaultButton();
215 YQDialog::resizeEvent( QResizeEvent * event )
219 // y2debug( "Resize event: %d x %d", event->size().width(), event->size().height() );
220 setSize ( event->size().width(), event->size().height() );
221 _userSize = event->size();
223 if ( QWidget::parent() )
230 YQDialog::findDefaultButton()
232 if ( _defaultButton )
233 return _defaultButton;
235 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
237 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
238 YDialog::setDefaultButton( _defaultButton );
240 return _defaultButton;
245 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
246 YWidgetListConstIterator end ) const
249 for ( YWidgetListConstIterator it = begin; it != end; ++it )
251 YWidget * widget = *it;
257 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
259 if ( button && button->isDefaultButton() )
266 // Recurse over the children of this widget
269 if ( widget->hasChildren() )
271 button = findDefaultButton( widget->childrenBegin(),
272 widget->childrenEnd() );
283 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
284 YWidgetListConstIterator end )
286 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
287 YQWizard * wizard = 0;
289 for ( YWidgetListConstIterator it = begin; it != end; ++it )
291 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
292 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
295 wizard = dynamic_cast<YQWizard *> (*it);
299 wizardButton->showAsDefault( false );
303 if ( button->isDefaultButton() )
305 if ( _defaultButton && button != _defaultButton )
307 y2error( "Too many default buttons: [%s]",
308 qPrintable(button->text()) );
309 y2error( "Using old default button: [%s]",
310 qPrintable(_defaultButton->text()) );
314 _defaultButton = button;
318 if ( button->isShownAsDefault() && button != def )
319 button->showAsDefault( false );
322 if ( (*it)->hasChildren() )
324 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
325 (*it)->childrenEnd() );
336 YQDialog::ensureOnlyOneDefaultButton()
339 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
341 if ( ! _defaultButton && wizard )
343 _defaultButton = wizardDefaultButton( wizard );
346 if ( _defaultButton )
348 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
349 YDialog::setDefaultButton( _defaultButton );
353 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
356 def->showAsDefault();
361 YQDialog::findWizard() const
363 return findWizard( childrenBegin(), childrenEnd() );
368 YQDialog::findWizard( YWidgetListConstIterator begin,
369 YWidgetListConstIterator end ) const
371 for ( YWidgetListConstIterator it = begin; it != end; ++it )
373 YWidget * widget = *it;
374 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
379 if ( widget->hasChildren() )
381 wizard = findWizard( widget->childrenBegin(),
382 widget->childrenEnd() );
393 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
395 YQGenericButton * def = 0;
398 wizard = findWizard();
402 // Pick one of the wizard buttons
404 if ( wizard->direction() == YQWizard::Backward )
406 if ( wizard->backButton()
407 && wizard->backButton()->isShown()
408 && wizard->backButton()->isEnabled() )
410 def = wizard->backButton();
416 if ( wizard->nextButton()
417 && wizard->nextButton()->isShown()
418 && wizard->nextButton()->isEnabled() )
420 def = wizard->nextButton();
430 YQDialog::setDefaultButton( YPushButton * newDefaultButton )
432 if ( _defaultButton &&
434 newDefaultButton != _defaultButton )
436 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
438 // Let app defined default buttons override wizard buttons
439 _defaultButton->setDefaultButton( false );
443 y2error( "Too many `opt(`default) PushButtons: [%s]",
444 newDefaultButton->label().c_str() );
445 newDefaultButton->setDefaultButton( false );
450 _defaultButton = dynamic_cast<YQGenericButton*>(newDefaultButton);
452 if ( _defaultButton )
454 _defaultButton->setDefaultButton( true );
455 y2debug( "New default button: [%s]", qPrintable(_defaultButton->text()) );
457 if ( _defaultButton && ! _focusButton )
458 _defaultButton->showAsDefault( true );
462 YDialog::setDefaultButton( 0 ); // prevent complaints about multiple default buttons
463 YDialog::setDefaultButton( _defaultButton );
468 YQDialog::activateDefaultButton( bool warn )
470 // Try the focus button first, if there is any.
473 _focusButton->isEnabled() &&
474 _focusButton->isShownAsDefault() )
476 y2debug( "Activating focus button: [%s]", qPrintable(_focusButton->text()) );
477 _focusButton->activate();
482 // No focus button - try the default button, if there is any.
484 _defaultButton = findDefaultButton();
486 if ( _defaultButton &&
487 _defaultButton->isEnabled() &&
488 _defaultButton->isShownAsDefault() )
490 y2debug( "Activating default button: [%s]", qPrintable(_defaultButton->text()) );
491 _defaultButton->activate();
498 y2warning( "No default button in this dialog - ignoring [Return]" );
507 YQDialog::losingFocus( YQGenericButton * button )
509 if ( button == _focusButton )
511 if ( _focusButton && _focusButton != _defaultButton )
512 _focusButton->showAsDefault( false );
517 if ( ! _focusButton && _defaultButton )
518 _defaultButton->showAsDefault( true );
523 YQDialog::gettingFocus( YQGenericButton * button )
525 if ( _focusButton && _focusButton != button )
526 _focusButton->showAsDefault( false );
528 if ( _defaultButton && _defaultButton != button )
529 _defaultButton->showAsDefault( false );
531 _focusButton = button;
534 _focusButton->showAsDefault( true );
539 YQDialog::keyPressEvent( QKeyEvent * event )
543 if ( event->key() == Qt::Key_Print )
545 YQUI::ui()->makeScreenShot( "" );
548 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
549 event->modifiers() & Qt::ShiftModifier )
551 YQUI::ui()->toggleVisionImpairedPalette();
553 if ( YQUI::ui()->usingVisionImpairedPalette() )
555 y2milestone( "Switched to vision impaired palette" );
556 QMessageBox::information( 0, // parent
557 _("Color switching"), // caption
558 _( "Switching to color palette for vision impaired users -\n"
559 "press Shift-F4 again to switch back to normal colors." ), // text
560 QMessageBox::Ok | QMessageBox::Default, // button0
561 QMessageBox::NoButton, // button1
562 QMessageBox::NoButton ); // button2
566 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
567 event->modifiers() == Qt::ShiftModifier )
569 YQUI::ui()->askConfigureLogging();
572 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
573 event->modifiers() & Qt::ShiftModifier )
575 YQUI::ui()->askSaveLogs();
578 else if ( event->modifiers() & Qt::NoModifier ) // No Ctrl / Alt / Shift etc. pressed
580 if ( event->key() == Qt::Key_Return ||
581 event->key() == Qt::Key_Enter )
583 ( void ) activateDefaultButton();
587 else if ( event->modifiers() & ( Qt::ControlModifier | Qt::ShiftModifier | Qt::AltModifier ) )
589 // Qt-UI special keys - all with Ctrl-Shift-Alt
591 y2milestone( "Caught YaST2 magic key combination" );
593 if ( event->key() == Qt::Key_M )
595 YQUI::ui()->toggleRecordMacro();
598 else if ( event->key() == Qt::Key_P )
600 YQUI::ui()->askPlayMacro();
603 else if ( event->key() == Qt::Key_D )
605 YQUI::ui()->sendEvent( new YDebugEvent() );
608 else if ( event->key() == Qt::Key_X )
610 y2milestone( "Starting xterm" );
611 system( "/usr/bin/xterm &" );
617 QWidget::keyPressEvent( event );
622 YQDialog::closeEvent( QCloseEvent * event )
624 // The window manager "close window" button (and WM menu, e.g. Alt-F4) will be
625 // handled just like the user had clicked on the `id`( `cancel ) button in
626 // that dialog. It's up to the YCP application to handle this (if desired).
628 y2milestone( "Caught window manager close event - returning with YCancelEvent" );
630 YQUI::ui()->sendEvent( new YCancelEvent() );
635 YQDialog::focusInEvent( QFocusEvent * event )
638 // The dialog itself doesn't need or want the keyboard focus, but obviously
639 // (since Qt 2.3?) it needs QFocusPolicy::StrongFocus for the default
640 // button mechanism to work. So let's accept the focus and give it to some
643 if ( event->reason() == Qt::TabFocusReason )
645 focusNextPrevChild( true );
649 if ( _defaultButton )
650 _defaultButton->setKeyboardFocus();
652 focusNextPrevChild( true );
658 YQDialog::center( QWidget * dialog, QWidget * parent )
660 if ( ! dialog || ! parent )
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"