File indexing completed on 2024-04-28 03:53:11
0001 #include "kcomboboxtest.h" 0002 0003 #include <assert.h> 0004 0005 #include <KConfig> 0006 #include <KConfigGroup> 0007 0008 #include <QApplication> 0009 #include <QBoxLayout> 0010 #include <QDebug> 0011 #include <QLabel> 0012 #include <QLayout> 0013 #include <QPixmap> 0014 #include <QPushButton> 0015 #include <QTimer> 0016 0017 #include <khistorycombobox.h> 0018 0019 KComboBoxTest::KComboBoxTest(QWidget *widget) 0020 : QWidget(widget) 0021 { 0022 QVBoxLayout *vbox = new QVBoxLayout(this); 0023 0024 // Qt combobox 0025 QHBoxLayout *hbox = new QHBoxLayout(); 0026 QLabel *lbl = new QLabel(QStringLiteral("&QCombobox:"), this); 0027 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); 0028 hbox->addWidget(lbl); 0029 0030 m_qc = new QComboBox(this); 0031 m_qc->setObjectName(QStringLiteral("QtReadOnlyCombo")); 0032 lbl->setBuddy(m_qc); 0033 connectComboSignals(m_qc); 0034 hbox->addWidget(m_qc); 0035 0036 vbox->addLayout(hbox); 0037 0038 // Read-only combobox 0039 hbox = new QHBoxLayout(); 0040 lbl = new QLabel(QStringLiteral("&Read-Only Combo:"), this); 0041 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); 0042 hbox->addWidget(lbl); 0043 0044 m_ro = new KComboBox(this); 0045 m_ro->setObjectName(QStringLiteral("ReadOnlyCombo")); 0046 lbl->setBuddy(m_ro); 0047 m_ro->setCompletionMode(KCompletion::CompletionAuto); 0048 connectComboSignals(m_ro); 0049 hbox->addWidget(m_ro); 0050 0051 vbox->addLayout(hbox); 0052 0053 // Read-write combobox 0054 hbox = new QHBoxLayout(); 0055 lbl = new QLabel(QStringLiteral("&Editable Combo:"), this); 0056 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); 0057 hbox->addWidget(lbl); 0058 0059 m_rw = new KComboBox(true, this); 0060 m_rw->setObjectName(QStringLiteral("ReadWriteCombo")); 0061 lbl->setBuddy(m_rw); 0062 m_rw->setDuplicatesEnabled(true); 0063 m_rw->setInsertPolicy(QComboBox::NoInsert); 0064 m_rw->setTrapReturnKey(true); 0065 connectComboSignals(m_rw); 0066 hbox->addWidget(m_rw); 0067 vbox->addLayout(hbox); 0068 0069 // History combobox... 0070 hbox = new QHBoxLayout(); 0071 lbl = new QLabel(QStringLiteral("&History Combo:"), this); 0072 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); 0073 hbox->addWidget(lbl); 0074 0075 m_hc = new KHistoryComboBox(this); 0076 m_hc->setObjectName(QStringLiteral("HistoryCombo")); 0077 lbl->setBuddy(m_hc); 0078 m_hc->setDuplicatesEnabled(true); 0079 m_hc->setInsertPolicy(QComboBox::NoInsert); 0080 connectComboSignals(m_hc); 0081 m_hc->setTrapReturnKey(true); 0082 hbox->addWidget(m_hc); 0083 vbox->addLayout(hbox); 0084 0085 // Read-write combobox that is a replica of code in konqueror... 0086 hbox = new QHBoxLayout(); 0087 lbl = new QLabel(QStringLiteral("&Konq's Combo:"), this); 0088 lbl->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred); 0089 hbox->addWidget(lbl); 0090 0091 m_konqc = new KComboBox(true, this); 0092 m_konqc->setObjectName(QStringLiteral("KonqyCombo")); 0093 lbl->setBuddy(m_konqc); 0094 m_konqc->setMaxCount(10); 0095 connectComboSignals(m_konqc); 0096 hbox->addWidget(m_konqc); 0097 vbox->addLayout(hbox); 0098 0099 // Create an exit button 0100 hbox = new QHBoxLayout(); 0101 m_btnExit = new QPushButton(QStringLiteral("E&xit"), this); 0102 QObject::connect(m_btnExit, &QAbstractButton::clicked, this, &KComboBoxTest::quitApp); 0103 hbox->addWidget(m_btnExit); 0104 0105 // Create a disable button... 0106 m_btnEnable = new QPushButton(QStringLiteral("Disa&ble"), this); 0107 QObject::connect(m_btnEnable, &QAbstractButton::clicked, this, &KComboBoxTest::slotDisable); 0108 hbox->addWidget(m_btnEnable); 0109 0110 vbox->addLayout(hbox); 0111 0112 // Populate the select-only list box 0113 QStringList list; 0114 list << QStringLiteral("Stone") << QStringLiteral("Tree") << QStringLiteral("Peables") << QStringLiteral("Ocean") << QStringLiteral("Sand") 0115 << QStringLiteral("Chips") << QStringLiteral("Computer") << QStringLiteral("Mankind"); 0116 list.sort(); 0117 0118 // Setup the qcombobox 0119 m_qc->addItems(list); 0120 0121 // Setup read-only combo 0122 m_ro->addItems(list); 0123 m_ro->completionObject()->setItems(list); 0124 0125 // Setup read-write combo 0126 m_rw->addItems(list); 0127 m_rw->completionObject()->setItems(list); 0128 0129 // Setup history combo 0130 m_hc->addItems(list); 0131 m_hc->completionObject()->setItems(list + QStringList() << QStringLiteral("One") << QStringLiteral("Two") << QStringLiteral("Three")); 0132 0133 // Setup konq's combobox 0134 KConfig historyConfig(QStringLiteral("konq_history"), KConfig::SimpleConfig); 0135 KConfigGroup cg(&historyConfig, QStringLiteral("Location Bar")); 0136 KCompletion *s_pCompletion = new KCompletion; 0137 s_pCompletion->setOrder(KCompletion::Weighted); 0138 s_pCompletion->setItems(cg.readEntry("ComboContents", QStringList())); 0139 s_pCompletion->setCompletionMode(KCompletion::CompletionPopup); 0140 m_konqc->setCompletionObject(s_pCompletion); 0141 0142 QPixmap pix(16, 16); 0143 pix.fill(Qt::blue); 0144 m_konqc->addItem(pix, QStringLiteral("http://www.kde.org")); 0145 m_konqc->setCurrentIndex(m_konqc->count() - 1); 0146 0147 m_timer = new QTimer(this); 0148 connect(m_timer, &QTimer::timeout, this, &KComboBoxTest::slotTimeout); 0149 } 0150 0151 KComboBoxTest::~KComboBoxTest() 0152 { 0153 delete m_timer; 0154 m_timer = nullptr; 0155 } 0156 0157 void KComboBoxTest::connectComboSignals(QComboBox *combo) 0158 { 0159 QObject::connect(combo, qOverload<int>(&QComboBox::activated), this, &KComboBoxTest::slotActivated); 0160 QObject::connect(combo, &QComboBox::textActivated, this, &KComboBoxTest::slotTextActivated); 0161 0162 QObject::connect(combo, qOverload<int>(&QComboBox::currentIndexChanged), this, &KComboBoxTest::slotCurrentIndexChanged); 0163 QObject::connect(combo, qOverload<int>(&QComboBox::currentIndexChanged), this, [this, combo](const int index) { 0164 slotCurrentTextChanged(combo->itemText(index)); 0165 }); 0166 0167 QObject::connect(combo, &QComboBox::textActivated, this, qOverload<const QString &>(&KComboBoxTest::slotReturnPressed)); 0168 } 0169 0170 void KComboBoxTest::slotDisable() 0171 { 0172 if (m_timer->isActive()) { 0173 return; 0174 } 0175 0176 m_btnEnable->setEnabled(!m_btnEnable->isEnabled()); 0177 0178 m_timer->setSingleShot(true); 0179 m_timer->start(5000); 0180 } 0181 0182 void KComboBoxTest::slotTimeout() 0183 { 0184 bool enabled = m_ro->isEnabled(); 0185 0186 if (enabled) { 0187 m_btnEnable->setText(QStringLiteral("Ena&ble")); 0188 } else { 0189 m_btnEnable->setText(QStringLiteral("Disa&ble")); 0190 } 0191 0192 m_qc->setEnabled(!enabled); 0193 m_ro->setEnabled(!enabled); 0194 m_rw->setEnabled(!enabled); 0195 m_hc->setEnabled(!enabled); 0196 m_konqc->setEnabled(!enabled); 0197 0198 m_btnEnable->setEnabled(!m_btnEnable->isEnabled()); 0199 } 0200 0201 void KComboBoxTest::slotCurrentIndexChanged(int index) 0202 { 0203 qDebug() << qPrintable(sender()->objectName()) << ", index:" << index; 0204 } 0205 0206 void KComboBoxTest::slotCurrentTextChanged(const QString &item) 0207 { 0208 qDebug() << qPrintable(sender()->objectName()) << ", item:" << item; 0209 } 0210 0211 void KComboBoxTest::slotActivated(int index) 0212 { 0213 qDebug() << "Activated Combo:" << qPrintable(sender()->objectName()) << ", index:" << index; 0214 } 0215 0216 void KComboBoxTest::slotTextActivated(const QString &item) 0217 { 0218 qDebug() << "Activated Combo:" << qPrintable(sender()->objectName()) << ", item:" << item; 0219 } 0220 0221 void KComboBoxTest::slotReturnPressed(const QString &item) 0222 { 0223 qDebug() << "Return Pressed:" << qPrintable(sender()->objectName()) << ", value =" << item; 0224 } 0225 0226 void KComboBoxTest::quitApp() 0227 { 0228 qApp->closeAllWindows(); 0229 } 0230 0231 int main(int argc, char **argv) 0232 { 0233 QApplication::setApplicationName(QStringLiteral("kcomboboxtest")); 0234 0235 QApplication a(argc, argv); 0236 0237 KComboBoxTest *t = new KComboBoxTest; 0238 t->show(); 0239 return a.exec(); 0240 } 0241 0242 #include "moc_kcomboboxtest.cpp"