File indexing completed on 2024-10-06 04:26:00
0001 /* 0002 SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "k3binteractiondialog.h" 0007 #include "k3btitlelabel.h" 0008 #include "k3bstdguiitems.h" 0009 #include "k3bthemedheader.h" 0010 #include "k3bthememanager.h" 0011 #include "k3bapplication.h" 0012 0013 #include <KConfig> 0014 #include <KSharedConfig> 0015 #include <KIconLoader> 0016 #include <KLocalizedString> 0017 #include <KStandardGuiItem> 0018 0019 #include <QDebug> 0020 #include <QEvent> 0021 #include <QPoint> 0022 #include <QString> 0023 #include <QTimer> 0024 #include <QFont> 0025 #include <QIcon> 0026 #include <QKeyEvent> 0027 #include <QDialogButtonBox> 0028 #include <QGridLayout> 0029 #include <QHBoxLayout> 0030 #include <QApplication> 0031 #include <QLabel> 0032 #include <QLayout> 0033 #include <QMenu> 0034 #include <QPushButton> 0035 #include <QToolButton> 0036 #include <QToolTip> 0037 0038 0039 K3b::InteractionDialog::InteractionDialog( QWidget* parent, 0040 const QString& title, 0041 const QString& subTitle, 0042 int buttonMask, 0043 int defaultButton, 0044 const QString& configGroup ) 0045 : QDialog( parent ), 0046 m_mainWidget(0), 0047 m_defaultButton(defaultButton), 0048 m_configGroup(configGroup), 0049 m_inToggleMode(false), 0050 m_delayedInit(false) 0051 { 0052 installEventFilter( this ); 0053 0054 mainGrid = new QGridLayout( this ); 0055 0056 // header 0057 // --------------------------------------------------------------------------------------------------- 0058 m_dialogHeader = new K3b::ThemedHeader( this ); 0059 mainGrid->addWidget( m_dialogHeader, 0, 0, 1, 3 ); 0060 0061 0062 // settings buttons 0063 // --------------------------------------------------------------------------------------------------- 0064 if( !m_configGroup.isEmpty() ) { 0065 QHBoxLayout* layout2 = new QHBoxLayout; 0066 m_buttonLoadSettings = new QToolButton( this ); 0067 m_buttonLoadSettings->setIcon( QIcon::fromTheme( "document-revert" ) ); 0068 m_buttonLoadSettings->setPopupMode( QToolButton::InstantPopup ); 0069 QMenu* userDefaultsPopup = new QMenu( m_buttonLoadSettings ); 0070 userDefaultsPopup->addAction( i18n("Load default settings"), this, SLOT(slotLoadK3bDefaults()) ); 0071 userDefaultsPopup->addAction( i18n("Load saved settings"), this, SLOT(slotLoadUserDefaults()) ); 0072 userDefaultsPopup->addAction( i18n("Load last used settings"), this, SLOT(slotLoadLastSettings()) ); 0073 m_buttonLoadSettings->setMenu( userDefaultsPopup ); 0074 layout2->addWidget( m_buttonLoadSettings ); 0075 0076 m_buttonSaveSettings = new QToolButton( this ); 0077 m_buttonSaveSettings->setIcon( QIcon::fromTheme( "document-save" ) ); 0078 layout2->addWidget( m_buttonSaveSettings ); 0079 0080 mainGrid->addLayout( layout2, 2, 0 ); 0081 } 0082 0083 QSpacerItem* spacer = new QSpacerItem( 10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum ); 0084 mainGrid->addItem( spacer, 2, 1 ); 0085 0086 // action buttons 0087 // --------------------------------------------------------------------------------------------------- 0088 QDialogButtonBox *buttonBox = new QDialogButtonBox( this ); 0089 connect( buttonBox, SIGNAL(accepted()), this, SLOT(accept()) ); 0090 connect( buttonBox, SIGNAL(rejected()), this, SLOT(reject()) ); 0091 0092 if( buttonMask & START_BUTTON ) { 0093 m_buttonStart = new QPushButton( buttonBox ); 0094 KGuiItem::assign( m_buttonStart, KStandardGuiItem::ok() ); 0095 // refine the button text 0096 setButtonText( START_BUTTON, 0097 i18n("Start"), 0098 i18n("Start the task") ); 0099 QFont fnt( m_buttonStart->font() ); 0100 fnt.setBold(true); 0101 m_buttonStart->setFont( fnt ); 0102 buttonBox->addButton( m_buttonStart, QDialogButtonBox::AcceptRole ); 0103 } 0104 if( buttonMask & SAVE_BUTTON ) { 0105 m_buttonSave = new QPushButton( buttonBox ); 0106 KGuiItem::assign( m_buttonSave, KStandardGuiItem::save() ); 0107 buttonBox->addButton( m_buttonSave, QDialogButtonBox::ApplyRole ); 0108 } 0109 else { 0110 m_buttonSave = 0; 0111 } 0112 if( buttonMask & CANCEL_BUTTON ) { 0113 m_buttonCancel = new QPushButton( buttonBox ); 0114 KGuiItem::assign( m_buttonCancel, KConfigGroup( KSharedConfig::openConfig(), QStringLiteral("General Options") ) 0115 .readEntry( "keep action dialogs open", false ) 0116 ? KStandardGuiItem::close() 0117 : KStandardGuiItem::cancel() ); 0118 buttonBox->addButton( m_buttonCancel, QDialogButtonBox::RejectRole ); 0119 } 0120 else { 0121 m_buttonCancel = 0; 0122 } 0123 0124 mainGrid->addWidget( buttonBox, 2, 2 ); 0125 mainGrid->setRowStretch( 1, 1 ); 0126 0127 setTitle( title, subTitle ); 0128 0129 initConnections(); 0130 initToolTipsAndWhatsThis(); 0131 0132 setDefaultButton( START_BUTTON ); 0133 } 0134 0135 K3b::InteractionDialog::~InteractionDialog() 0136 { 0137 qDebug() << this; 0138 } 0139 0140 0141 void K3b::InteractionDialog::show() 0142 { 0143 QDialog::show(); 0144 if( QPushButton* b = getButton( m_defaultButton ) ) 0145 b->setFocus(); 0146 } 0147 0148 0149 QSize K3b::InteractionDialog::sizeHint() const 0150 { 0151 QSize s = QDialog::sizeHint(); 0152 // I want the dialogs to look good. 0153 // That means their height should never outgrow their width 0154 if( s.height() > s.width() ) 0155 s.setWidth( s.height() ); 0156 0157 return s; 0158 } 0159 0160 0161 void K3b::InteractionDialog::initConnections() 0162 { 0163 if( m_buttonStart ) { 0164 connect( m_buttonStart, SIGNAL(clicked()), 0165 this, SLOT(slotStartClickedInternal()) ); 0166 } 0167 if( m_buttonSave ) { 0168 // connect( m_buttonSave, SIGNAL(clicked()), 0169 // this, SLOT(slotSaveLastSettings()) ); 0170 connect( m_buttonSave, SIGNAL(clicked()), 0171 this, SLOT(slotSaveClicked()) ); 0172 } 0173 if( m_buttonCancel ) 0174 connect( m_buttonCancel, SIGNAL(clicked()), 0175 this, SLOT(slotCancelClicked()) ); 0176 0177 if( !m_configGroup.isEmpty() ) { 0178 connect( m_buttonSaveSettings, SIGNAL(clicked()), 0179 this, SLOT(slotSaveUserDefaults()) ); 0180 } 0181 } 0182 0183 0184 void K3b::InteractionDialog::initToolTipsAndWhatsThis() 0185 { 0186 if( !m_configGroup.isEmpty() ) { 0187 // ToolTips 0188 // ------------------------------------------------------------------------- 0189 m_buttonLoadSettings->setToolTip( i18n("Load default or saved settings") ); 0190 m_buttonSaveSettings->setToolTip( i18n("Save current settings to reuse them later") ); 0191 0192 // What's This info 0193 // ------------------------------------------------------------------------- 0194 m_buttonLoadSettings->setWhatsThis( i18n("<p>Load a set of settings either from the default K3b settings, " 0195 "settings saved before, or the last used ones.") ); 0196 m_buttonSaveSettings->setWhatsThis( i18n("<p>Saves the current settings of the action dialog." 0197 "<p>These settings can be loaded with the <em>Load saved settings</em> " 0198 "button." 0199 "<p><b>The K3b defaults are not overwritten by this.</b>") ); 0200 } 0201 } 0202 0203 0204 void K3b::InteractionDialog::setTitle( const QString& title, const QString& subTitle ) 0205 { 0206 m_dialogHeader->setTitle( title, subTitle ); 0207 0208 setWindowTitle( title ); 0209 } 0210 0211 0212 void K3b::InteractionDialog::setMainWidget( QWidget* w ) 0213 { 0214 w->setParent( this ); 0215 mainGrid->addWidget( w, 1, 0, 1, 3 ); 0216 m_mainWidget = w; 0217 } 0218 0219 0220 QWidget* K3b::InteractionDialog::mainWidget() 0221 { 0222 if( !m_mainWidget ) { 0223 QWidget *widget = new QWidget(this); 0224 setMainWidget( widget ); 0225 } 0226 return m_mainWidget; 0227 } 0228 0229 0230 void K3b::InteractionDialog::slotLoadK3bDefaults() 0231 { 0232 KSharedConfig::Ptr c = KSharedConfig::openConfig(); 0233 c->setReadDefaults( true ); 0234 loadSettings( c->group( m_configGroup ) ); 0235 c->setReadDefaults( false ); 0236 } 0237 0238 0239 void K3b::InteractionDialog::slotLoadUserDefaults() 0240 { 0241 KConfigGroup c( KSharedConfig::openConfig(), m_configGroup ); 0242 loadSettings( c ); 0243 } 0244 0245 0246 void K3b::InteractionDialog::slotSaveUserDefaults() 0247 { 0248 KConfigGroup c( KSharedConfig::openConfig(), m_configGroup ); 0249 saveSettings( c ); 0250 } 0251 0252 0253 void K3b::InteractionDialog::slotLoadLastSettings() 0254 { 0255 KConfigGroup c( KSharedConfig::openConfig(), "last used " + m_configGroup ); 0256 loadSettings( c ); 0257 } 0258 0259 0260 void K3b::InteractionDialog::saveLastSettings() 0261 { 0262 KConfigGroup c( KSharedConfig::openConfig(), "last used " + m_configGroup ); 0263 saveSettings( c ); 0264 } 0265 0266 0267 void K3b::InteractionDialog::slotStartClickedInternal() 0268 { 0269 saveLastSettings(); 0270 0271 slotStartClicked(); 0272 } 0273 0274 0275 void K3b::InteractionDialog::slotStartClicked() 0276 { 0277 emit started(); 0278 } 0279 0280 0281 void K3b::InteractionDialog::slotCancelClicked() 0282 { 0283 emit canceled(); 0284 close(); 0285 } 0286 0287 0288 void K3b::InteractionDialog::slotSaveClicked() 0289 { 0290 emit saved(); 0291 } 0292 0293 0294 void K3b::InteractionDialog::setDefaultButton( int button ) 0295 { 0296 m_defaultButton = button; 0297 0298 // reset all other default buttons 0299 if( QPushButton* b = getButton( START_BUTTON ) ) 0300 b->setDefault( true ); 0301 if( QPushButton* b = getButton( SAVE_BUTTON ) ) 0302 b->setDefault( true ); 0303 if( QPushButton* b = getButton( CANCEL_BUTTON ) ) 0304 b->setDefault( true ); 0305 0306 // set the selected default 0307 if( QPushButton* b = getButton( button ) ) 0308 b->setDefault( true ); 0309 } 0310 0311 0312 bool K3b::InteractionDialog::eventFilter( QObject* o, QEvent* ev ) 0313 { 0314 if( dynamic_cast<K3b::InteractionDialog*>(o) == this && 0315 ev->type() == QEvent::KeyPress ) { 0316 0317 QKeyEvent* kev = dynamic_cast<QKeyEvent*>(ev); 0318 0319 switch ( kev->key() ) { 0320 case Qt::Key_Enter: 0321 case Qt::Key_Return: 0322 // if the process finished this closes the dialog 0323 if( m_defaultButton == START_BUTTON ) { 0324 if( m_buttonStart->isEnabled() ) 0325 slotStartClickedInternal(); 0326 } 0327 else if( m_defaultButton == CANCEL_BUTTON ) { 0328 if( m_buttonCancel->isEnabled() ) 0329 slotCancelClicked(); 0330 } 0331 else if( m_defaultButton == SAVE_BUTTON ) { 0332 if( m_buttonSave->isEnabled() ) 0333 slotSaveClicked(); 0334 } 0335 return true; 0336 0337 case Qt::Key_Escape: 0338 // simulate button clicks 0339 if( m_buttonCancel ) { 0340 if( m_buttonCancel->isEnabled() ) 0341 slotCancelClicked(); 0342 } 0343 return true; 0344 } 0345 } 0346 0347 return QDialog::eventFilter( o, ev ); 0348 } 0349 0350 0351 QPushButton* K3b::InteractionDialog::getButton( int button ) 0352 { 0353 switch( button ) { 0354 case START_BUTTON: 0355 return m_buttonStart; 0356 case SAVE_BUTTON: 0357 return m_buttonSave; 0358 case CANCEL_BUTTON: 0359 return m_buttonCancel; 0360 default: 0361 return 0; 0362 } 0363 } 0364 0365 0366 void K3b::InteractionDialog::setButtonGui( int button, 0367 const KGuiItem& item ) 0368 { 0369 if( QPushButton* b = getButton( button ) ) 0370 KGuiItem::assign( b, item ); 0371 } 0372 0373 0374 void K3b::InteractionDialog::setButtonText( int button, 0375 const QString& text, 0376 const QString& tooltip, 0377 const QString& whatsthis ) 0378 { 0379 if( QPushButton* b = getButton( button ) ) { 0380 b->setText( text ); 0381 b->setToolTip( tooltip ); 0382 b->setWhatsThis( whatsthis ); 0383 } 0384 } 0385 0386 0387 void K3b::InteractionDialog::setButtonEnabled( int button, bool enabled ) 0388 { 0389 if( QPushButton* b = getButton( button ) ) { 0390 b->setEnabled( enabled ); 0391 // make sure the correct button is selected as default again 0392 setDefaultButton( m_defaultButton ); 0393 } 0394 } 0395 0396 0397 void K3b::InteractionDialog::setButtonShown( int button, bool shown ) 0398 { 0399 if( QPushButton* b = getButton( button ) ) { 0400 b->setVisible( shown ); 0401 // make sure the correct button is selected as default again 0402 setDefaultButton( m_defaultButton ); 0403 } 0404 } 0405 0406 0407 void K3b::InteractionDialog::setStartButtonText( const QString& text, 0408 const QString& tooltip, 0409 const QString& whatsthis ) 0410 { 0411 if( m_buttonStart ) { 0412 m_buttonStart->setText( text ); 0413 m_buttonStart->setToolTip( tooltip ); 0414 m_buttonStart->setWhatsThis( whatsthis ); 0415 } 0416 } 0417 0418 0419 void K3b::InteractionDialog::setCancelButtonText( const QString& text, 0420 const QString& tooltip, 0421 const QString& whatsthis ) 0422 { 0423 if( m_buttonCancel ) { 0424 m_buttonCancel->setText( text ); 0425 m_buttonCancel->setToolTip( tooltip ); 0426 m_buttonCancel->setWhatsThis( whatsthis ); 0427 } 0428 } 0429 0430 0431 void K3b::InteractionDialog::setSaveButtonText( const QString& text, 0432 const QString& tooltip, 0433 const QString& whatsthis ) 0434 { 0435 if( m_buttonSave ) { 0436 m_buttonSave->setText( text ); 0437 m_buttonSave->setToolTip( tooltip ); 0438 m_buttonSave->setWhatsThis( whatsthis ); 0439 } 0440 } 0441 0442 0443 void K3b::InteractionDialog::saveSettings( KConfigGroup ) 0444 { 0445 } 0446 0447 0448 void K3b::InteractionDialog::loadSettings( const KConfigGroup& ) 0449 { 0450 } 0451 0452 0453 void K3b::InteractionDialog::loadStartupSettings() 0454 { 0455 KConfigGroup c( KSharedConfig::openConfig(), QStringLiteral("General Options") ); 0456 0457 // earlier K3b versions loaded the saved settings 0458 // so that is what we do as a default 0459 int i = c.readEntry( "action dialog startup settings", int(LOAD_SAVED_SETTINGS) ); 0460 switch( i ) { 0461 case LOAD_K3B_DEFAULTS: 0462 slotLoadK3bDefaults(); 0463 break; 0464 case LOAD_SAVED_SETTINGS: 0465 slotLoadUserDefaults(); 0466 break; 0467 case LOAD_LAST_SETTINGS: 0468 slotLoadLastSettings(); 0469 break; 0470 } 0471 } 0472 0473 0474 int K3b::InteractionDialog::exec() 0475 { 0476 qDebug() << this; 0477 0478 loadStartupSettings(); 0479 0480 if( m_delayedInit ) 0481 QMetaObject::invokeMethod( this, "slotInternalInit", Qt::QueuedConnection ); 0482 else 0483 slotInternalInit(); 0484 0485 return QDialog::exec(); 0486 } 0487 0488 0489 void K3b::InteractionDialog::hideTemporarily() 0490 { 0491 hide(); 0492 } 0493 0494 0495 void K3b::InteractionDialog::close() 0496 { 0497 QDialog::close(); 0498 } 0499 0500 0501 void K3b::InteractionDialog::done( int r ) 0502 { 0503 QDialog::done( r ); 0504 } 0505 0506 0507 void K3b::InteractionDialog::hideEvent( QHideEvent* e ) 0508 { 0509 qDebug() << this; 0510 QDialog::hideEvent( e ); 0511 } 0512 0513 0514 void K3b::InteractionDialog::slotToggleAll() 0515 { 0516 if( !m_inToggleMode ) { 0517 m_inToggleMode = true; 0518 toggleAll(); 0519 m_inToggleMode = false; 0520 } 0521 } 0522 0523 0524 void K3b::InteractionDialog::slotInternalInit() 0525 { 0526 init(); 0527 slotToggleAll(); 0528 } 0529 0530 0531 void K3b::InteractionDialog::toggleAll() 0532 { 0533 } 0534 0535 #include "moc_k3binteractiondialog.cpp"