File indexing completed on 2024-05-12 04:04:32

0001 // Copyright (C) 2002 Jason Katz-Brown <jason@katzbrown.com>
0002 // Copyright (C) 2002 Neil Stevens <neil@qualityassistant.com>
0003 //
0004 // Permission is hereby granted, free of charge, to any person obtaining a copy
0005 // of this software and associated documentation files (the "Software"), to deal
0006 // in the Software without restriction, including without limitation the rights
0007 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008 // copies of the Software, and to permit persons to whom the Software is
0009 // furnished to do so, subject to the following conditions:
0010 //
0011 // The above copyright notice and this permission notice shall be included in
0012 // all copies or substantial portions of the Software.
0013 //
0014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0017 // THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0018 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0019 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0020 //
0021 // Except as contained in this notice, the name(s) of the author(s) shall not be
0022 // used in advertising or otherwise to promote the sale, use or other dealings
0023 // in this Software without prior written authorization from the author(s).
0024 
0025 #include "kcomboboxdialog.h"
0026 
0027 #include <QCheckBox>
0028 #include <QDialogButtonBox>
0029 #include <QLabel>
0030 #include <QPushButton>
0031 #include <QVBoxLayout>
0032 #include <KConfigGroup>
0033 #include <KHistoryComboBox>
0034 #include <KLocalizedString>
0035 
0036 KComboBoxDialog::KComboBoxDialog( const QString &_text, const QStringList &_items, const QString& _value, bool showDontAskAgain, QWidget *parent )
0037     : QDialog( parent)
0038 {
0039     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
0040     auto mainLayout = new QVBoxLayout(this);
0041     auto okButton = buttonBox->button(QDialogButtonBox::Ok);
0042     okButton->setDefault(true);
0043     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0044     connect(buttonBox, &QDialogButtonBox::accepted, this, &KComboBoxDialog::accept);
0045     connect(buttonBox, &QDialogButtonBox::rejected, this, &KComboBoxDialog::reject);
0046 
0047     setModal(true);
0048     QFrame *frame = new QFrame( this );
0049     QVBoxLayout *topLayout = new QVBoxLayout( frame );
0050     QLabel *label = new QLabel(_text, frame );
0051     topLayout->addWidget( label, 1 );
0052 
0053     combo = new KHistoryComboBox( frame );
0054     combo->setEditable(false);
0055     combo->addItems( _items );
0056     topLayout->addWidget( combo, 1 );
0057 
0058     if (showDontAskAgain)
0059     {
0060         dontAskAgainCheckBox = new QCheckBox( i18n("&Do not ask again"), frame );
0061         topLayout->addWidget( dontAskAgainCheckBox, 1 );
0062     }
0063     else
0064         dontAskAgainCheckBox = nullptr;
0065 
0066     if ( !_value.isNull() )
0067         combo->setCurrentItem( _value );
0068     combo->setFocus();
0069     
0070     QFrame* separationLine = new QFrame();
0071     separationLine->setFrameShape(QFrame::HLine);
0072     separationLine->setFrameShadow(QFrame::Sunken);
0073 
0074     mainLayout->addWidget( frame );
0075     mainLayout->addWidget( separationLine );
0076     mainLayout->addWidget( buttonBox );
0077 }
0078 
0079 KComboBoxDialog::~KComboBoxDialog()
0080 {
0081 }
0082 
0083 QString KComboBoxDialog::text() const
0084 {
0085     return combo->currentText();
0086 }
0087 
0088 bool KComboBoxDialog::dontAskAgainChecked()
0089 {
0090     if (dontAskAgainCheckBox)
0091         return dontAskAgainCheckBox->isChecked();
0092 
0093     return false;
0094 }
0095 
0096 QString KComboBoxDialog::getItem( const QString &_text, const QStringList &_items, const QString& _value, const QString &dontAskAgainName, QWidget *parent )
0097 {
0098     return getItem( _text, QString(), _items, _value, dontAskAgainName, parent );
0099 }
0100 
0101 QString KComboBoxDialog::getItem( const QString &_text, const QString &_caption, const QStringList &_items, const QString& _value, const QString &dontAskAgainName, QWidget *parent )
0102 {
0103     QString prevAnswer;
0104     if ( !dontAskAgainName.isEmpty() )
0105     {
0106         KSharedConfig::Ptr config = KSharedConfig::openConfig();
0107         KConfigGroup *configGroup = new KConfigGroup(config->group(QStringLiteral("Notification Messages")));
0108         prevAnswer = configGroup->readEntry( dontAskAgainName,QString() );
0109         if ( !prevAnswer.isEmpty() )
0110             if ( _items.contains( prevAnswer ) > 0 )
0111                 return prevAnswer;
0112     }
0113 
0114     QPointer<KComboBoxDialog> dlg = new KComboBoxDialog( _text, _items, _value, !dontAskAgainName.isNull(), parent );
0115     if ( !_caption.isNull() )
0116         dlg->setWindowTitle( _caption );
0117 
0118     dlg->exec();
0119 
0120     const QString text = dlg->text();
0121 
0122     if (dlg->dontAskAgainChecked())
0123     {
0124         if ( !dontAskAgainName.isEmpty() && !text.isEmpty() )
0125         {
0126             KSharedConfig::Ptr config = KSharedConfig::openConfig();
0127             KConfigGroup *configGroup = new KConfigGroup(config->group(QStringLiteral("Notification Messages")));
0128             configGroup->writeEntry( dontAskAgainName, text );
0129         }
0130     }
0131     delete dlg;
0132 
0133     return text;
0134 }
0135 
0136 QString KComboBoxDialog::getText(const QString &_caption, const QString &_text, const QString &_value, bool *ok, QWidget *parent, const QString &configName, KSharedConfigPtr config)
0137 {
0138     KConfigGroup *configGroup = nullptr;
0139     QPointer<KComboBoxDialog> dlg = new KComboBoxDialog(_text, QStringList(), _value, false, parent);
0140     if ( !_caption.isNull() )
0141         dlg->setWindowTitle( _caption );
0142 
0143     KHistoryComboBox * const box = dlg->comboBox();
0144     box->setEditable(true);
0145 
0146     const QString historyItem = QStringLiteral("%1History").arg(configName);
0147     const QString completionItem = QStringLiteral("%1Completion").arg(configName);
0148 
0149     if(!configName.isNull())
0150     {
0151         configGroup = new KConfigGroup(config->group(QStringLiteral("KComboBoxDialog")));
0152         box->setHistoryItems(configGroup->readEntry(historyItem,QStringList()));
0153         box->completionObject()->setItems(configGroup->readEntry(completionItem,QStringList()));
0154     }
0155 
0156     bool result = dlg->exec();
0157     if(ok) *ok = result;
0158 
0159     if(!configName.isNull() && result)
0160     {
0161         box->addToHistory(dlg->text());
0162         box->completionObject()->addItem(dlg->text());
0163         configGroup = new KConfigGroup(config->group(QStringLiteral("KComboBoxDialog")));
0164         configGroup->writeEntry(historyItem, box->historyItems());
0165         configGroup->writeEntry(completionItem, box->completionObject()->items());
0166     }
0167 
0168     QString text = dlg->text();
0169     delete dlg;
0170     return text;
0171 }
0172 
0173 #include "moc_kcomboboxdialog.cpp"