File indexing completed on 2025-02-16 07:39:53
0001 /* 0002 SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "k3bmultichoicedialog.h" 0008 #include "k3bstdguiitems.h" 0009 0010 #include <KIconLoader> 0011 0012 #include <QCloseEvent> 0013 #include <QGridLayout> 0014 #include <QHBoxLayout> 0015 #include <QLabel> 0016 #include <QLayout> 0017 #include <QMessageBox> 0018 #include <QPushButton> 0019 0020 0021 class K3b::MultiChoiceDialog::Private 0022 { 0023 public: 0024 Private() 0025 : buttonLayout(0) { 0026 } 0027 0028 QList<QPushButton*> buttons; 0029 QHBoxLayout* buttonLayout; 0030 0031 bool buttonClicked; 0032 }; 0033 0034 0035 // from kmessagebox.cpp 0036 static QIcon themedMessageBoxIcon(QMessageBox::Icon icon) 0037 { 0038 QString icon_name; 0039 0040 switch (icon) { 0041 case QMessageBox::NoIcon: 0042 return QIcon(); 0043 break; 0044 case QMessageBox::Information: 0045 icon_name = "dialog-information"; 0046 break; 0047 case QMessageBox::Warning: 0048 icon_name = "dialog-warning"; 0049 break; 0050 case QMessageBox::Critical: 0051 icon_name = "dialog-error"; 0052 break; 0053 default: 0054 break; 0055 } 0056 0057 QIcon ret = KIconLoader::global()->loadIcon(icon_name, KIconLoader::NoGroup, KIconLoader::SizeLarge, KIconLoader::DefaultState, QStringList(), 0, true); 0058 0059 if (ret.isNull()) { 0060 return QMessageBox::standardIcon(icon); 0061 } else { 0062 return ret; 0063 } 0064 } 0065 0066 0067 K3b::MultiChoiceDialog::MultiChoiceDialog( const QString& caption, 0068 const QString& text, 0069 QMessageBox::Icon icon, 0070 QWidget* parent ) 0071 : QDialog( parent ) 0072 { 0073 d = new Private(); 0074 0075 setWindowTitle( caption ); 0076 0077 QGridLayout* mainGrid = new QGridLayout( this ); 0078 0079 QHBoxLayout* contents = new QHBoxLayout; 0080 contents->setSpacing( contents->spacing()*2 ); 0081 contents->setContentsMargins( 0, 0, 0, 0 ); 0082 0083 QLabel* pixLabel = new QLabel( this ); 0084 int size = KIconLoader::global()->currentSize(KIconLoader::Dialog); 0085 pixLabel->setPixmap( themedMessageBoxIcon( icon ).pixmap( size, size ) ); 0086 pixLabel->setScaledContents( false ); 0087 QLabel* label = new QLabel( text, this ); 0088 label->setWordWrap( true ); 0089 contents->addWidget( pixLabel, 0 ); 0090 contents->addWidget( label, 1 ); 0091 0092 d->buttonLayout = new QHBoxLayout; 0093 d->buttonLayout->setContentsMargins( 0, 0, 0, 0 ); 0094 0095 mainGrid->addLayout( contents, 0, 0, 1, 3 ); 0096 mainGrid->addWidget( K3b::StdGuiItems::horizontalLine( this ), 1, 0, 1, 3 ); 0097 mainGrid->addLayout( d->buttonLayout, 2, 1 ); 0098 0099 mainGrid->setColumnStretch( 0, 1 ); 0100 mainGrid->setColumnStretch( 2, 1 ); 0101 mainGrid->setRowStretch( 0, 1 ); 0102 } 0103 0104 0105 K3b::MultiChoiceDialog::~MultiChoiceDialog() 0106 { 0107 delete d; 0108 } 0109 0110 0111 int K3b::MultiChoiceDialog::addButton( const KGuiItem& b ) 0112 { 0113 QPushButton* button = new QPushButton( this ); 0114 KGuiItem::assign( button, b ); 0115 d->buttonLayout->addWidget( button ); 0116 d->buttons.append(button); 0117 const auto buttonId = d->buttons.count(); 0118 connect( button, &QAbstractButton::clicked, this, [this, buttonId]() { done(buttonId); } ); 0119 return buttonId; 0120 } 0121 0122 0123 void K3b::MultiChoiceDialog::slotButtonClicked( int code ) 0124 { 0125 d->buttonClicked = true; 0126 done( code ); 0127 } 0128 0129 0130 int K3b::MultiChoiceDialog::exec() 0131 { 0132 d->buttonClicked = false; 0133 return QDialog::exec(); 0134 } 0135 0136 0137 void K3b::MultiChoiceDialog::closeEvent( QCloseEvent* e ) 0138 { 0139 // make sure the dialog can only be closed by the buttons 0140 // otherwise we may get an undefined return value in exec 0141 0142 if( d->buttonClicked ) 0143 QDialog::closeEvent( e ); 0144 else 0145 e->ignore(); 0146 } 0147 0148 0149 int K3b::MultiChoiceDialog::choose( const QString& caption, 0150 const QString& text, 0151 QMessageBox::Icon icon, 0152 QWidget* parent, 0153 int buttonCount, 0154 const KGuiItem& b1, 0155 const KGuiItem& b2, 0156 const KGuiItem& b3, 0157 const KGuiItem& b4, 0158 const KGuiItem& b5, 0159 const KGuiItem& b6 ) 0160 { 0161 K3b::MultiChoiceDialog dlg( caption, text, icon, parent ); 0162 dlg.addButton( b1 ); 0163 if( buttonCount > 1 ) 0164 dlg.addButton( b2 ); 0165 if( buttonCount > 2 ) 0166 dlg.addButton( b3 ); 0167 if( buttonCount > 3 ) 0168 dlg.addButton( b4 ); 0169 if( buttonCount > 4 ) 0170 dlg.addButton( b5 ); 0171 if( buttonCount > 5 ) 0172 dlg.addButton( b6 ); 0173 0174 return dlg.exec(); 0175 } 0176 0177 #include "moc_k3bmultichoicedialog.cpp"