File indexing completed on 2024-10-06 04:26:01

0001 /*
0002     SPDX-FileCopyrightText: 2005-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bmediaselectiondialog.h"
0009 #include "k3bmediaselectioncombobox.h"
0010 #include "k3bmediacache.h"
0011 #include "k3bapplication.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QDialogButtonBox>
0016 #include <QLabel>
0017 #include <QVBoxLayout>
0018 #include <QPushButton>
0019 
0020 
0021 K3b::MediaSelectionDialog::MediaSelectionDialog( QWidget* parent,
0022                                                  const QString& title,
0023                                                  const QString& text,
0024                                                  bool modal )
0025     : QDialog( parent)
0026 {
0027     setWindowTitle(title.isEmpty() ? i18n("Medium Selection") : title);
0028     setModal(modal);
0029 
0030     QLabel* label = new QLabel( text.isEmpty() ? i18n("Please select a medium:") : text, this );
0031     m_combo = new K3b::MediaSelectionComboBox( this );
0032 
0033     QDialogButtonBox* buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this );
0034     m_okButton = buttonBox->button( QDialogButtonBox::Ok );
0035     connect( buttonBox, SIGNAL(accepted()), SLOT(accept()) );
0036     connect( buttonBox, SIGNAL(rejected()), SLOT(reject()) );
0037 
0038     QVBoxLayout* lay = new QVBoxLayout( this );
0039     lay->addWidget( label );
0040     lay->addWidget( m_combo );
0041     lay->addWidget( buttonBox );
0042 
0043     connect( m_combo, SIGNAL(selectionChanged(K3b::Device::Device*)),
0044              this, SLOT(slotSelectionChanged(K3b::Device::Device*)) );
0045 
0046     slotSelectionChanged( m_combo->selectedDevice() );
0047 }
0048 
0049 
0050 K3b::MediaSelectionDialog::~MediaSelectionDialog()
0051 {
0052 }
0053 
0054 
0055 void K3b::MediaSelectionDialog::setWantedMediumType( Device::MediaTypes type )
0056 {
0057     m_combo->setWantedMediumType( type );
0058 }
0059 
0060 
0061 void K3b::MediaSelectionDialog::setWantedMediumState( Device::MediaStates state )
0062 {
0063     m_combo->setWantedMediumState( state );
0064 }
0065 
0066 
0067 void K3b::MediaSelectionDialog::setWantedMediumContent( Medium::MediumContents content )
0068 {
0069     m_combo->setWantedMediumContent( content );
0070 }
0071 
0072 
0073 K3b::Device::Device* K3b::MediaSelectionDialog::selectedDevice() const
0074 {
0075     return m_combo->selectedDevice();
0076 }
0077 
0078 
0079 void K3b::MediaSelectionDialog::slotSelectionChanged( K3b::Device::Device* dev )
0080 {
0081     m_okButton->setEnabled( dev != 0 );
0082 }
0083 
0084 
0085 K3b::Device::Device* K3b::MediaSelectionDialog::selectMedium( Device::MediaTypes type,
0086                                                               Device::MediaStates state,
0087                                                               Medium::MediumContents content,
0088                                                               QWidget* parent,
0089                                                               const QString& title, const QString& text,
0090                                                               bool* canceled )
0091 {
0092     K3b::MediaSelectionDialog dlg( parent, title, text );
0093     dlg.setWantedMediumType( type );
0094     dlg.setWantedMediumState( state );
0095     dlg.setWantedMediumContent( content );
0096 
0097     // even if no usable medium is inserted the combobox shows the "insert one" message
0098     // so it's not sufficient to check for just one entry to check if there only is a
0099     // single usable medium
0100     if( ( dlg.selectedDevice() && dlg.m_combo->count() == 1 )
0101         || dlg.exec() == Accepted ) {
0102         if( canceled )
0103             *canceled = false;
0104         return dlg.selectedDevice();
0105     }
0106     else {
0107         if( canceled )
0108             *canceled = true;
0109         return 0;
0110     }
0111 }
0112 
0113 #include "moc_k3bmediaselectiondialog.cpp"