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>
32 #include "YQGenericButton.h"
33 #include "YQWizardButton.h"
36 // Include low-level X headers AFTER Qt headers:
37 // X.h pollutes the global namespace (!!!) with pretty useless #defines
38 // like "Above", "Below" etc. that clash with some Qt headers.
42 YQDialog::YQDialog( const YWidgetOpt & opt,
47 default_size ? 0 : WStyle_Customize | WStyle_DialogBorder | WType_Modal | WStyle_Dialog ) // WFlags
56 setCaption( hasDefaultSize() ? "YaST2" : "" );
57 setFocusPolicy( QWidget::StrongFocus );
59 if ( hasWarnColor() || hasInfoColor() )
61 QColor normalBackground ( 0, 128, 0 );
62 QColor inputFieldBackground ( 0, 96, 0 );
67 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
70 QPalette warnPalette( normalBackground );
71 QColorGroup normalColors = warnPalette.normal();
72 normalColors.setColor( QColorGroup::Text, text );
73 normalColors.setColor( QColorGroup::Base, inputFieldBackground );
74 warnPalette.setNormal( normalColors );
75 setPalette( warnPalette );
78 _qFrame = new QFrame ( this );
79 bool decorate = ! hasDefaultSize() && ! YQUI::ui()->haveWM();
82 if ( hasSmallDecorations() )
84 // None of this works (yet). :-((
86 clearWFlags( getWFlags() );
87 setWFlags( WStyle_Customize | WStyle_DialogBorder | WStyle_StaysOnTop );
94 _qFrame->setFrameStyle ( QFrame::Box | QFrame::Raised );
95 _qFrame->setLineWidth(2);
96 _qFrame->setMidLineWidth(3);
100 _qFrame->setFrameStyle ( QFrame::NoFrame );
105 YQDialog::~YQDialog()
110 int YQDialog::preferredWidth()
114 if ( hasDefaultSize() )
117 preferredWidth = _userSize.width();
119 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
123 preferredWidth = YDialog::preferredWidth() + 2 * decorationWidth();
126 int screenWidth = qApp->desktop()->width();
128 if ( preferredWidth > screenWidth )
130 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
131 screenWidth, preferredWidth );
134 return preferredWidth;
138 int YQDialog::preferredHeight()
142 if ( hasDefaultSize() )
145 preferredHeight = _userSize.height();
147 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
151 preferredHeight = YDialog::preferredHeight() + 2 * decorationWidth();
154 int screenHeight = qApp->desktop()->height();
156 if ( preferredHeight > screenHeight )
158 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
159 screenHeight, preferredHeight );
162 return preferredHeight;
166 int YQDialog::decorationWidth()
168 if ( ! hasDefaultSize() && _qFrame )
169 return _qFrame->frameWidth();
175 void YQDialog::setEnabled( bool enabled )
177 QWidget::setEnabled( enabled );
178 YWidget::setEnabled( enabled );
182 void YQDialog::setSize( int newWidth, int newHeight )
184 if ( newWidth > qApp->desktop()->width() )
185 newWidth = qApp->desktop()->width();
187 if ( newHeight > qApp->desktop()->height() )
188 newHeight = qApp->desktop()->height();
192 firstChild()->setSize( newWidth - 2 * decorationWidth(),
193 newHeight - 2 * decorationWidth() );
195 QWidget * qChild = (QWidget *) firstChild()->widgetRep();
196 qChild->move( decorationWidth(), decorationWidth() );
200 _qFrame->resize( newWidth, newHeight );
202 resize( newWidth, newHeight );
207 void YQDialog::activate( bool active )
211 if ( ! YQUI::ui()->haveWM() )
213 if ( YQUI::ui()->autoActivateDialogs() )
216 y2milestone( "Auto-activating dialog window turned off" );
219 ensureOnlyOneDefaultButton();
225 YQDialog::resizeEvent( QResizeEvent * event )
229 setSize ( event->size().width(), event->size().height() );
230 _userSize = event->size();
237 YQDialog::findDefaultButton()
239 if ( _defaultButton )
240 return _defaultButton;
242 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
244 return _defaultButton;
249 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
250 YWidgetListConstIterator end ) const
253 for ( YWidgetListConstIterator it = begin; it != end; ++it )
255 YWidget * widget = *it;
261 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
263 if ( button && button->isDefaultButton() )
270 // Recurse over the children of this widget
273 if ( widget->hasChildren() )
275 button = findDefaultButton( widget->childrenBegin(),
276 widget->childrenEnd() );
287 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
288 YWidgetListConstIterator end )
290 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
291 YQWizard * wizard = 0;
293 for ( YWidgetListConstIterator it = begin; it != end; ++it )
295 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
296 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
299 wizard = dynamic_cast<YQWizard *> (*it);
303 wizardButton->showAsDefault( false );
307 if ( button->isDefaultButton() )
309 if ( _defaultButton && button != _defaultButton )
311 y2error( "Too many default buttons: [%s]",
312 (const char *) button->text() );
313 y2error( "Using old default button: [%s]",
314 (const char *) _defaultButton->text() );
318 _defaultButton = button;
322 if ( button->isShownAsDefault() && button != def )
323 button->showAsDefault( false );
326 if ( (*it)->hasChildren() )
328 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
329 (*it)->childrenEnd() );
340 YQDialog::ensureOnlyOneDefaultButton()
343 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
345 if ( ! _defaultButton && wizard )
347 _defaultButton = wizardDefaultButton( wizard );
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
369 for ( YWidgetListConstIterator it = begin; it != end; ++it )
371 YWidget * widget = *it;
372 YQWizard * wizard = dynamic_cast<YQWizard *> (widget);
377 if ( widget->hasChildren() )
379 wizard = findWizard( widget->childrenBegin(),
380 widget->childrenEnd() );
391 YQDialog::wizardDefaultButton( YQWizard * wizard ) const
393 YQGenericButton * def = 0;
396 wizard = findWizard();
400 // Pick one of the wizard buttons
402 if ( wizard->direction() == YQWizard::Backward )
404 if ( wizard->backButton()
405 && wizard->backButton()->isShown()
406 && wizard->backButton()->isEnabled() )
408 def = wizard->backButton();
414 if ( wizard->nextButton()
415 && wizard->nextButton()->isShown()
416 && wizard->nextButton()->isEnabled() )
418 def = wizard->nextButton();
428 YQDialog::setDefaultButton( YQGenericButton * newDefaultButton )
430 if ( _defaultButton &&
432 newDefaultButton != _defaultButton )
434 if ( dynamic_cast<YQWizardButton *>( _defaultButton ) )
436 // Let app defined default buttons override wizard buttons
437 _defaultButton->setDefaultButton( false );
441 y2error( "Too many `opt(`default) PushButtons: [%s]",
442 (const char *) newDefaultButton->text() );
443 newDefaultButton->setDefaultButton( false );
448 _defaultButton = newDefaultButton;
450 if ( _defaultButton )
452 _defaultButton->setDefaultButton( true );
453 y2debug( "New default button: [%s]", (const char *) _defaultButton->text() );
455 if ( _defaultButton && ! _focusButton )
456 _defaultButton->showAsDefault( true );
462 YQDialog::activateDefaultButton( bool warn )
464 // Try the focus button first, if there is any.
467 _focusButton->isEnabled() &&
468 _focusButton->isShownAsDefault() )
470 y2debug( "Activating focus button: [%s]", (const char *) _focusButton->text() );
471 _focusButton->activate();
476 // No focus button - try the default button, if there is any.
478 _defaultButton = findDefaultButton();
480 if ( _defaultButton &&
481 _defaultButton->isEnabled() &&
482 _defaultButton->isShownAsDefault() )
484 y2debug( "Activating default button: [%s]", (const char *) _defaultButton->text() );
485 _defaultButton->activate();
492 y2warning( "No default button in this dialog - ignoring [Return]" );
501 YQDialog::losingFocus( YQGenericButton * button )
503 if ( button == _focusButton )
505 if ( _focusButton && _focusButton != _defaultButton )
506 _focusButton->showAsDefault( false );
511 if ( ! _focusButton && _defaultButton )
512 _defaultButton->showAsDefault( true );
517 YQDialog::gettingFocus( YQGenericButton * button )
519 if ( _focusButton && _focusButton != button )
520 _focusButton->showAsDefault( false );
522 if ( _defaultButton && _defaultButton != button )
523 _defaultButton->showAsDefault( false );
525 _focusButton = button;
528 _focusButton->showAsDefault( true );
533 YQDialog::keyPressEvent( QKeyEvent * event )
537 if ( event->key() == Qt::Key_Print )
539 YQUI::ui()->makeScreenShot( "" );
542 else if ( event->key() == Qt::Key_F5 ) // No matter if Ctrl/Alt/Shift pressed
544 YQUI::ui()->easterEgg();
547 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
548 event->state() == Qt::ShiftButton )
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->state() == Qt::ShiftButton )
568 YQUI::ui()->askConfigureLogging();
571 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
572 event->state() == Qt::ShiftButton )
574 YQUI::ui()->askSaveLogs();
577 else if ( event->state() == 0 ) // 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->state() == ( Qt::ControlButton | Qt::ShiftButton | Qt::AltButton ) )
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 );
620 void YQDialog::closeEvent( QCloseEvent * event )
622 // The window manager "close window" button ( and menu, e.g. Alt-F4 ) will be
623 // handled just like the user had clicked on the `id`( `cancel ) button in
624 // that dialog. It's up to the YCP application to handle this ( if desired ).
626 y2debug( "Ignoring window manager close button." );
628 YQUI::ui()->sendEvent( new YCancelEvent() );
632 void 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() == QFocusEvent::Tab )
642 focusNextPrevChild( true );
646 if ( _defaultButton )
647 _defaultButton->setKeyboardFocus();
649 focusNextPrevChild( true );
657 if ( ! hasDefaultSize() && qApp->mainWidget()->isVisible() )
658 center( this, qApp->mainWidget() );
659 else if ( isCentered() )
660 center( this, qApp->desktop() );
667 YQDialog::center( QWidget * dialog, QWidget * parent )
674 if ( dialog == qApp->mainWidget() )
675 parent = qApp->desktop();
677 parent = qApp->mainWidget();
680 QPoint pos( ( parent->width() - dialog->width() ) / 2,
681 ( parent->height() - dialog->height() ) / 2 );
683 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
684 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
690 #include "YQDialog.moc"