1 #include "kcomboboxtest.h"
2
3 #include <assert.h>
4
5 #include <KConfig>
6 #include <KConfigGroup>
7
8 #include <QApplication>
9 #include <QBoxLayout>
10 #include <QDebug>
11 #include <QLabel>
12 #include <QLayout>
13 #include <QPixmap>
14 #include <QPushButton>
15 #include <QTimer>
16
17 #include <khistorycombobox.h>
18
KComboBoxTest(QWidget * widget)19 KComboBoxTest::KComboBoxTest(QWidget *widget)
20 : QWidget(widget)
21 {
22 QVBoxLayout *vbox = new QVBoxLayout(this);
23
24 // Qt combobox
25 QHBoxLayout *hbox = new QHBoxLayout();
26 QLabel *lbl = new QLabel(QStringLiteral("&QCombobox:"), this);
27 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
28 hbox->addWidget(lbl);
29
30 m_qc = new QComboBox(this);
31 m_qc->setObjectName(QStringLiteral("QtReadOnlyCombo"));
32 lbl->setBuddy(m_qc);
33 connectComboSignals(m_qc);
34 hbox->addWidget(m_qc);
35
36 vbox->addLayout(hbox);
37
38 // Read-only combobox
39 hbox = new QHBoxLayout();
40 lbl = new QLabel(QStringLiteral("&Read-Only Combo:"), this);
41 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
42 hbox->addWidget(lbl);
43
44 m_ro = new KComboBox(this);
45 m_ro->setObjectName(QStringLiteral("ReadOnlyCombo"));
46 lbl->setBuddy(m_ro);
47 m_ro->setCompletionMode(KCompletion::CompletionAuto);
48 connectComboSignals(m_ro);
49 hbox->addWidget(m_ro);
50
51 vbox->addLayout(hbox);
52
53 // Read-write combobox
54 hbox = new QHBoxLayout();
55 lbl = new QLabel(QStringLiteral("&Editable Combo:"), this);
56 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
57 hbox->addWidget(lbl);
58
59 m_rw = new KComboBox(true, this);
60 m_rw->setObjectName(QStringLiteral("ReadWriteCombo"));
61 lbl->setBuddy(m_rw);
62 m_rw->setDuplicatesEnabled(true);
63 m_rw->setInsertPolicy(QComboBox::NoInsert);
64 m_rw->setTrapReturnKey(true);
65 connectComboSignals(m_rw);
66 hbox->addWidget(m_rw);
67 vbox->addLayout(hbox);
68
69 // History combobox...
70 hbox = new QHBoxLayout();
71 lbl = new QLabel(QStringLiteral("&History Combo:"), this);
72 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
73 hbox->addWidget(lbl);
74
75 m_hc = new KHistoryComboBox(this);
76 m_hc->setObjectName(QStringLiteral("HistoryCombo"));
77 lbl->setBuddy(m_hc);
78 m_hc->setDuplicatesEnabled(true);
79 m_hc->setInsertPolicy(QComboBox::NoInsert);
80 connectComboSignals(m_hc);
81 m_hc->setTrapReturnKey(true);
82 hbox->addWidget(m_hc);
83 vbox->addLayout(hbox);
84
85 // Read-write combobox that is a replica of code in konqueror...
86 hbox = new QHBoxLayout();
87 lbl = new QLabel(QStringLiteral("&Konq's Combo:"), this);
88 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
89 hbox->addWidget(lbl);
90
91 m_konqc = new KComboBox(true, this);
92 m_konqc->setObjectName(QStringLiteral("KonqyCombo"));
93 lbl->setBuddy(m_konqc);
94 m_konqc->setMaxCount(10);
95 connectComboSignals(m_konqc);
96 hbox->addWidget(m_konqc);
97 vbox->addLayout(hbox);
98
99 // Create an exit button
100 hbox = new QHBoxLayout();
101 m_btnExit = new QPushButton(QStringLiteral("E&xit"), this);
102 QObject::connect(m_btnExit, &QAbstractButton::clicked, this, &KComboBoxTest::quitApp);
103 hbox->addWidget(m_btnExit);
104
105 // Create a disable button...
106 m_btnEnable = new QPushButton(QStringLiteral("Disa&ble"), this);
107 QObject::connect(m_btnEnable, &QAbstractButton::clicked, this, &KComboBoxTest::slotDisable);
108 hbox->addWidget(m_btnEnable);
109
110 vbox->addLayout(hbox);
111
112 // Populate the select-only list box
113 QStringList list;
114 list << QStringLiteral("Stone") << QStringLiteral("Tree") << QStringLiteral("Peables") << QStringLiteral("Ocean") << QStringLiteral("Sand")
115 << QStringLiteral("Chips") << QStringLiteral("Computer") << QStringLiteral("Mankind");
116 list.sort();
117
118 // Setup the qcombobox
119 m_qc->addItems(list);
120
121 // Setup read-only combo
122 m_ro->addItems(list);
123 m_ro->completionObject()->setItems(list);
124
125 // Setup read-write combo
126 m_rw->addItems(list);
127 m_rw->completionObject()->setItems(list);
128
129 // Setup history combo
130 m_hc->addItems(list);
131 m_hc->completionObject()->setItems(list + QStringList() << QStringLiteral("One") << QStringLiteral("Two") << QStringLiteral("Three"));
132
133 // Setup konq's combobox
134 KConfig historyConfig(QStringLiteral("konq_history"), KConfig::SimpleConfig);
135 KConfigGroup cg(&historyConfig, "Location Bar");
136 KCompletion *s_pCompletion = new KCompletion;
137 s_pCompletion->setOrder(KCompletion::Weighted);
138 s_pCompletion->setItems(cg.readEntry("ComboContents", QStringList()));
139 s_pCompletion->setCompletionMode(KCompletion::CompletionPopup);
140 m_konqc->setCompletionObject(s_pCompletion);
141
142 QPixmap pix(16, 16);
143 pix.fill(Qt::blue);
144 m_konqc->addItem(pix, QStringLiteral("http://www.kde.org"));
145 m_konqc->setCurrentIndex(m_konqc->count() - 1);
146
147 m_timer = new QTimer(this);
148 connect(m_timer, &QTimer::timeout, this, &KComboBoxTest::slotTimeout);
149 }
150
~KComboBoxTest()151 KComboBoxTest::~KComboBoxTest()
152 {
153 delete m_timer;
154 m_timer = nullptr;
155 }
156
connectComboSignals(QComboBox * combo)157 void KComboBoxTest::connectComboSignals(QComboBox *combo)
158 {
159 QObject::connect(combo, SIGNAL(activated(int)), SLOT(slotActivated(int)));
160 QObject::connect(combo, SIGNAL(activated(QString)), SLOT(slotActivated(QString)));
161 QObject::connect(combo, SIGNAL(currentIndexChanged(int)), SLOT(slotCurrentIndexChanged(int)));
162 QObject::connect(combo, SIGNAL(currentIndexChanged(QString)), SLOT(slotCurrentIndexChanged(QString)));
163 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 81)
164 QObject::connect(combo, SIGNAL(returnPressed()), SLOT(slotReturnPressed()));
165 #endif
166 QObject::connect(combo, SIGNAL(returnPressed(QString)), SLOT(slotReturnPressed(QString)));
167 }
168
slotDisable()169 void KComboBoxTest::slotDisable()
170 {
171 if (m_timer->isActive()) {
172 return;
173 }
174
175 m_btnEnable->setEnabled(!m_btnEnable->isEnabled());
176
177 m_timer->setSingleShot(true);
178 m_timer->start(5000);
179 }
180
slotTimeout()181 void KComboBoxTest::slotTimeout()
182 {
183 bool enabled = m_ro->isEnabled();
184
185 if (enabled) {
186 m_btnEnable->setText(QStringLiteral("Ena&ble"));
187 } else {
188 m_btnEnable->setText(QStringLiteral("Disa&ble"));
189 }
190
191 m_qc->setEnabled(!enabled);
192 m_ro->setEnabled(!enabled);
193 m_rw->setEnabled(!enabled);
194 m_hc->setEnabled(!enabled);
195 m_konqc->setEnabled(!enabled);
196
197 m_btnEnable->setEnabled(!m_btnEnable->isEnabled());
198 }
199
slotCurrentIndexChanged(int index)200 void KComboBoxTest::slotCurrentIndexChanged(int index)
201 {
202 qDebug() << qPrintable(sender()->objectName()) << ", index:" << index;
203 }
204
slotCurrentIndexChanged(const QString & item)205 void KComboBoxTest::slotCurrentIndexChanged(const QString &item)
206 {
207 qDebug() << qPrintable(sender()->objectName()) << ", item:" << item;
208 }
209
slotActivated(int index)210 void KComboBoxTest::slotActivated(int index)
211 {
212 qDebug() << "Activated Combo:" << qPrintable(sender()->objectName()) << ", index:" << index;
213 }
214
slotActivated(const QString & item)215 void KComboBoxTest::slotActivated(const QString &item)
216 {
217 qDebug() << "Activated Combo:" << qPrintable(sender()->objectName()) << ", item:" << item;
218 }
219
220 #if KCOMPLETION_BUILD_DEPRECATED_SINCE(5, 81)
slotReturnPressed()221 void KComboBoxTest::slotReturnPressed()
222 {
223 qDebug() << "Return Pressed:" << qPrintable(sender()->objectName());
224 }
225 #endif
226
slotReturnPressed(const QString & item)227 void KComboBoxTest::slotReturnPressed(const QString &item)
228 {
229 qDebug() << "Return Pressed:" << qPrintable(sender()->objectName()) << ", value =" << item;
230 }
231
quitApp()232 void KComboBoxTest::quitApp()
233 {
234 qApp->closeAllWindows();
235 }
236
main(int argc,char ** argv)237 int main(int argc, char **argv)
238 {
239 QApplication::setApplicationName(QStringLiteral("kcomboboxtest"));
240
241 QApplication a(argc, argv);
242
243 KComboBoxTest *t = new KComboBoxTest;
244 t->show();
245 return a.exec();
246 }
247