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.
43 YQDialog::YQDialog( const YWidgetOpt & opt,
47 default_size ? Qt::Widget : Qt::Window ) // WFlags
55 setCaption( hasDefaultSize() ? "YaST2" : "" );
56 setFocusPolicy( Qt::StrongFocus );
59 setWindowModality( Qt::WindowModal );
61 if ( hasWarnColor() || hasInfoColor() )
63 QColor normalBackground ( 0, 128, 0 );
64 QColor inputFieldBackground ( 0, 96, 0 );
65 QColor text = Qt::white;
69 normalBackground = QColor ( 238, 232, 170 ); // PaleGoldenrod
72 QPalette warnPalette( normalBackground );
73 QColorGroup normalColors = warnPalette.normal();
74 normalColors.setColor( QColorGroup::Text, text );
75 normalColors.setColor( QColorGroup::Base, inputFieldBackground );
76 warnPalette.setNormal( normalColors );
77 setPalette( warnPalette );
80 _qFrame = new Q3Frame ( this );
81 bool decorate = ! hasDefaultSize() && ! YQUI::ui()->haveWM();
84 if ( hasSmallDecorations() )
86 // None of this works (yet). :-((
88 clearWFlags( getWFlags() );
89 setWFlags( Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_StaysOnTop );
96 _qFrame->setFrameStyle ( Q3Frame::Box | Q3Frame::Raised );
97 _qFrame->setLineWidth(2);
98 _qFrame->setMidLineWidth(3);
102 _qFrame->setFrameStyle ( Q3Frame::NoFrame );
107 YQDialog::~YQDialog()
112 int YQDialog::preferredWidth()
116 if ( hasDefaultSize() )
119 preferredWidth = _userSize.width();
121 preferredWidth = YQUI::ui()->defaultSize( YD_HORIZ );
125 preferredWidth = YDialog::preferredWidth() + 2 * decorationWidth();
128 int screenWidth = qApp->desktop()->width();
130 if ( preferredWidth > screenWidth )
132 y2warning( "Limiting dialog width to screen width (%d) instead of %d - check the layout!",
133 screenWidth, preferredWidth );
136 return preferredWidth;
140 int YQDialog::preferredHeight()
144 if ( hasDefaultSize() )
147 preferredHeight = _userSize.height();
149 preferredHeight = YQUI::ui()->defaultSize( YD_VERT );
153 preferredHeight = YDialog::preferredHeight() + 2 * decorationWidth();
156 int screenHeight = qApp->desktop()->height();
158 if ( preferredHeight > screenHeight )
160 y2warning( "Limiting dialog height to screen height (%d) instead of %d - check the layout!",
161 screenHeight, preferredHeight );
164 return preferredHeight;
168 int YQDialog::decorationWidth()
170 if ( ! hasDefaultSize() && _qFrame )
171 return _qFrame->frameWidth();
177 void YQDialog::setEnabled( bool enabled )
179 QWidget::setEnabled( enabled );
180 YWidget::setEnabled( enabled );
184 void YQDialog::setSize( int newWidth, int 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 - 2 * decorationWidth(),
195 newHeight - 2 * decorationWidth() );
197 QWidget * qChild = (QWidget *) firstChild()->widgetRep();
198 qChild->move( decorationWidth(), decorationWidth() );
202 _qFrame->resize( newWidth, newHeight );
204 resize( newWidth, newHeight );
209 void YQDialog::activate( bool active )
213 if ( ! YQUI::ui()->haveWM() )
215 if ( YQUI::ui()->autoActivateDialogs() )
218 y2milestone( "Auto-activating dialog window turned off" );
221 ensureOnlyOneDefaultButton();
227 YQDialog::resizeEvent( QResizeEvent * event )
231 setSize ( event->size().width(), event->size().height() );
232 _userSize = event->size();
239 YQDialog::findDefaultButton()
241 if ( _defaultButton )
242 return _defaultButton;
244 _defaultButton = findDefaultButton( childrenBegin(), childrenEnd() );
246 return _defaultButton;
251 YQDialog::findDefaultButton( YWidgetListConstIterator begin,
252 YWidgetListConstIterator end ) const
255 for ( YWidgetListConstIterator it = begin; it != end; ++it )
257 YWidget * widget = *it;
263 YQGenericButton * button = dynamic_cast<YQGenericButton *> (widget);
265 if ( button && button->isDefaultButton() )
272 // Recurse over the children of this widget
275 if ( widget->hasChildren() )
277 button = findDefaultButton( widget->childrenBegin(),
278 widget->childrenEnd() );
289 YQDialog::ensureOnlyOneDefaultButton( YWidgetListConstIterator begin,
290 YWidgetListConstIterator end )
292 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
293 YQWizard * wizard = 0;
295 for ( YWidgetListConstIterator it = begin; it != end; ++it )
297 YQGenericButton * button = dynamic_cast<YQGenericButton *> (*it);
298 YQWizardButton * wizardButton = dynamic_cast<YQWizardButton * > (*it);
301 wizard = dynamic_cast<YQWizard *> (*it);
305 wizardButton->showAsDefault( false );
309 if ( button->isDefaultButton() )
311 if ( _defaultButton && button != _defaultButton )
313 y2error( "Too many default buttons: [%s]",
314 (const char *) button->text() );
315 y2error( "Using old default button: [%s]",
316 (const char *) _defaultButton->text() );
320 _defaultButton = button;
324 if ( button->isShownAsDefault() && button != def )
325 button->showAsDefault( false );
328 if ( (*it)->hasChildren() )
330 YQWizard * wiz = ensureOnlyOneDefaultButton( (*it)->childrenBegin(),
331 (*it)->childrenEnd() );
342 YQDialog::ensureOnlyOneDefaultButton()
345 YQWizard * wizard = ensureOnlyOneDefaultButton( childrenBegin(), childrenEnd() );
347 if ( ! _defaultButton && wizard )
349 _defaultButton = wizardDefaultButton( wizard );
352 YQGenericButton * def = _focusButton ? _focusButton : _defaultButton;
355 def->showAsDefault();
360 YQDialog::findWizard() const
362 return findWizard( childrenBegin(), childrenEnd() );
367 YQDialog::findWizard( YWidgetListConstIterator begin,
368 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( YQGenericButton * 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 (const char *) newDefaultButton->text() );
445 newDefaultButton->setDefaultButton( false );
450 _defaultButton = newDefaultButton;
452 if ( _defaultButton )
454 _defaultButton->setDefaultButton( true );
455 y2debug( "New default button: [%s]", (const char *) _defaultButton->text() );
457 if ( _defaultButton && ! _focusButton )
458 _defaultButton->showAsDefault( true );
464 YQDialog::activateDefaultButton( bool warn )
466 // Try the focus button first, if there is any.
469 _focusButton->isEnabled() &&
470 _focusButton->isShownAsDefault() )
472 y2debug( "Activating focus button: [%s]", (const char *) _focusButton->text() );
473 _focusButton->activate();
478 // No focus button - try the default button, if there is any.
480 _defaultButton = findDefaultButton();
482 if ( _defaultButton &&
483 _defaultButton->isEnabled() &&
484 _defaultButton->isShownAsDefault() )
486 y2debug( "Activating default button: [%s]", (const char *) _defaultButton->text() );
487 _defaultButton->activate();
494 y2warning( "No default button in this dialog - ignoring [Return]" );
503 YQDialog::losingFocus( YQGenericButton * button )
505 if ( button == _focusButton )
507 if ( _focusButton && _focusButton != _defaultButton )
508 _focusButton->showAsDefault( false );
513 if ( ! _focusButton && _defaultButton )
514 _defaultButton->showAsDefault( true );
519 YQDialog::gettingFocus( YQGenericButton * button )
521 if ( _focusButton && _focusButton != button )
522 _focusButton->showAsDefault( false );
524 if ( _defaultButton && _defaultButton != button )
525 _defaultButton->showAsDefault( false );
527 _focusButton = button;
530 _focusButton->showAsDefault( true );
535 YQDialog::keyPressEvent( QKeyEvent * event )
539 if ( event->key() == Qt::Key_Print )
541 YQUI::ui()->makeScreenShot( "" );
544 else if ( event->key() == Qt::Key_F5 ) // No matter if Ctrl/Alt/Shift pressed
546 YQUI::ui()->easterEgg();
549 else if ( event->key() == Qt::Key_F4 && // Shift-F4: toggle colors for vision impaired users
550 event->state() == Qt::ShiftButton )
552 YQUI::ui()->toggleVisionImpairedPalette();
554 if ( YQUI::ui()->usingVisionImpairedPalette() )
556 y2milestone( "Switched to vision impaired palette" );
557 QMessageBox::information( 0, // parent
558 _("Color switching"), // caption
559 _( "Switching to color palette for vision impaired users -\n"
560 "press Shift-F4 again to switch back to normal colors." ), // text
561 QMessageBox::Ok | QMessageBox::Default, // button0
562 QMessageBox::NoButton, // button1
563 QMessageBox::NoButton ); // button2
567 else if ( event->key() == Qt::Key_F7 && // Shift-F7: toggle y2debug logging
568 event->state() == Qt::ShiftButton )
570 YQUI::ui()->askConfigureLogging();
573 else if ( event->key() == Qt::Key_F8 && // Shift-F8: save y2logs
574 event->state() == Qt::ShiftButton )
576 YQUI::ui()->askSaveLogs();
579 else if ( event->state() == 0 ) // No Ctrl / Alt / Shift etc. pressed
581 if ( event->key() == Qt::Key_Return ||
582 event->key() == Qt::Key_Enter )
584 ( void ) activateDefaultButton();
588 else if ( event->state() == ( Qt::ControlButton | Qt::ShiftButton | Qt::AltButton ) )
590 // Qt-UI special keys - all with Ctrl-Shift-Alt
592 y2milestone( "Caught YaST2 magic key combination" );
594 if ( event->key() == Qt::Key_M )
596 YQUI::ui()->toggleRecordMacro();
599 else if ( event->key() == Qt::Key_P )
601 YQUI::ui()->askPlayMacro();
604 else if ( event->key() == Qt::Key_D )
606 YQUI::ui()->sendEvent( new YDebugEvent() );
609 else if ( event->key() == Qt::Key_X )
611 y2milestone( "Starting xterm" );
612 system( "/usr/bin/xterm &" );
618 QWidget::keyPressEvent( event );
622 void YQDialog::closeEvent( QCloseEvent * event )
624 // The window manager "close window" button ( and 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 y2debug( "Ignoring window manager close button." );
630 YQUI::ui()->sendEvent( new YCancelEvent() );
634 void YQDialog::focusInEvent( QFocusEvent * event )
637 // The dialog itself doesn't need or want the keyboard focus, but obviously
638 // ( since Qt 2.3? ) it needs QFocusPolicy::StrongFocus for the default
639 // button mechanism to work. So let's accept the focus and give it to some
642 if ( event->reason() == Qt::TabFocusReason )
644 focusNextPrevChild( true );
648 if ( _defaultButton )
649 _defaultButton->setKeyboardFocus();
651 focusNextPrevChild( true );
659 if ( ! hasDefaultSize() && qApp->mainWidget()->isVisible() )
660 center( this, qApp->mainWidget() );
661 else if ( isCentered() )
662 center( this, qApp->desktop() );
669 YQDialog::center( QWidget * dialog, QWidget * parent )
676 if ( dialog == qApp->mainWidget() )
677 parent = qApp->desktop();
679 parent = qApp->mainWidget();
682 QPoint pos( ( parent->width() - dialog->width() ) / 2,
683 ( parent->height() - dialog->height() ) / 2 );
685 pos += parent->mapToGlobal( QPoint( 0, 0 ) );
686 pos = dialog->mapToParent( dialog->mapFromGlobal( pos ) );
692 #include "YQDialog.moc"