1// Copyright (C) 2002 Jason Katz-Brown <jason@katzbrown.com>
2// Copyright (C) 2002 Neil Stevens <neil@qualityassistant.com>
3//
4// Permission is hereby granted, free of charge, to any person obtaining a copy
5// of this software and associated documentation files (the "Software"), to deal
6// in the Software without restriction, including without limitation the rights
7// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8// copies of the Software, and to permit persons to whom the Software is
9// furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// THE AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20//
21// Except as contained in this notice, the name(s) of the author(s) shall not be
22// used in advertising or otherwise to promote the sale, use or other dealings
23// in this Software without prior written authorization from the author(s).
24
25#include "kcomboboxdialog.h"
26
27#include <QCheckBox>
28#include <QLabel>
29#include <QVBoxLayout>
30#include <KHistoryComboBox>
31#include <KLocale>
32
33KComboBoxDialog::KComboBoxDialog( const QString &_text, const QStringList &_items, const QString& _value, bool showDontAskAgain, QWidget *parent )
34 : KDialog( parent)
35{
36 setButtons(Ok);
37 setDefaultButton(Ok);
38 setModal(true);
39 showButtonSeparator(true);
40 QFrame *frame = new QFrame(this);
41 setMainWidget(frame);
42 QVBoxLayout *topLayout = new QVBoxLayout( frame );
43 topLayout->setMargin( marginHint() );
44 topLayout->setSpacing( spacingHint() );
45 QLabel *label = new QLabel(_text, frame );
46 topLayout->addWidget( label, 1 );
47
48 combo = new KHistoryComboBox( frame);
49 combo->setEditable(false);
50 combo->addItems( _items );
51 topLayout->addWidget( combo, 1 );
52
53 if (showDontAskAgain)
54 {
55 dontAskAgainCheckBox = new QCheckBox( i18n("&Do not ask again"), frame );
56 topLayout->addWidget( dontAskAgainCheckBox, 1 );
57 }
58 else
59 dontAskAgainCheckBox = 0;
60
61 if ( !_value.isNull() )
62 combo->setCurrentItem( _value );
63 combo->setFocus();
64}
65
66KComboBoxDialog::~KComboBoxDialog()
67{
68}
69
70QString KComboBoxDialog::text() const
71{
72 return combo->currentText();
73}
74
75bool KComboBoxDialog::dontAskAgainChecked()
76{
77 if (dontAskAgainCheckBox)
78 return dontAskAgainCheckBox->isChecked();
79
80 return false;
81}
82
83QString KComboBoxDialog::getItem( const QString &_text, const QStringList &_items, const QString& _value, const QString &dontAskAgainName, QWidget *parent )
84{
85 return getItem( _text, QString(), _items, _value, dontAskAgainName, parent );
86}
87
88QString KComboBoxDialog::getItem( const QString &_text, const QString &_caption, const QStringList &_items, const QString& _value, const QString &dontAskAgainName, QWidget *parent )
89{
90 QString prevAnswer;
91 if ( !dontAskAgainName.isEmpty() )
92 {
93 KSharedConfig::Ptr config = KGlobal::config();
94 KConfigGroup *configGroup = new KConfigGroup(config->group("Notification Messages"));
95 prevAnswer = configGroup->readEntry( dontAskAgainName,QString() );
96 if ( !prevAnswer.isEmpty() )
97 if ( _items.contains( prevAnswer ) > 0 )
98 return prevAnswer;
99 }
100
101 KComboBoxDialog dlg( _text, _items, _value, !dontAskAgainName.isNull(), parent );
102 if ( !_caption.isNull() )
103 dlg.setCaption( _caption );
104
105 dlg.exec();
106
107 const QString text = dlg.text();
108
109 if (dlg.dontAskAgainChecked())
110 {
111 if ( !dontAskAgainName.isEmpty() && !text.isEmpty() )
112 {
113 KSharedConfig::Ptr config = KGlobal::config();
114 KConfigGroup *configGroup = new KConfigGroup(config->group("Notification Messages"));
115 configGroup->writeEntry( dontAskAgainName, text );
116 }
117 }
118
119 return text;
120}
121
122QString KComboBoxDialog::getText(const QString &_caption, const QString &_text, const QString &_value, bool *ok, QWidget *parent, const QString &configName, KSharedConfigPtr config)
123{
124 KConfigGroup *configGroup = 0;
125 KComboBoxDialog dlg(_text, QStringList(), _value, false, parent);
126 if ( !_caption.isNull() )
127 dlg.setCaption( _caption );
128
129 KHistoryComboBox * const box = dlg.comboBox();
130 box->setEditable(true);
131
132 const QString historyItem = QString("%1History").arg(configName);
133 const QString completionItem = QString("%1Completion").arg(configName);
134
135 if(!configName.isNull())
136 {
137 configGroup = new KConfigGroup(config->group("KComboBoxDialog"));
138 box->setHistoryItems(configGroup->readEntry(historyItem,QStringList()));
139 box->completionObject()->setItems(configGroup->readEntry(completionItem,QStringList()));
140 }
141
142 bool result = dlg.exec();
143 if(ok) *ok = result;
144
145 if(!configName.isNull() && result)
146 {
147 box->addToHistory(dlg.text());
148 box->completionObject()->addItem(dlg.text());
149 configGroup = new KConfigGroup(config->group("KComboBoxDialog"));
150 configGroup->writeEntry(historyItem, box->historyItems());
151 configGroup->writeEntry(completionItem, box->completionObject()->items());
152 }
153
154 return dlg.text();
155}
156
157#include "kcomboboxdialog.moc"
158