File indexing completed on 2024-04-28 03:53:11

0001 #include "kcompletionuitest.h"
0002 
0003 #include <QApplication>
0004 #include <QGroupBox>
0005 #include <QLabel>
0006 #include <QLayout>
0007 #include <QListWidget>
0008 #include <QPushButton>
0009 
0010 #include <khistorycombobox.h>
0011 #include <klineedit.h>
0012 
0013 Form1::Form1(QWidget *parent)
0014     : QWidget(parent)
0015 {
0016     setAttribute(Qt::WA_DeleteOnClose);
0017     resize(559, 465);
0018     setWindowTitle(QStringLiteral("Form1"));
0019     Form1Layout = new QVBoxLayout(this);
0020 
0021     GroupBox1 = new QGroupBox(this);
0022     GroupBox1->setLayout(new QVBoxLayout());
0023     GroupBox1->setTitle(QStringLiteral("Completion Test"));
0024     GroupBox1->layout()->setSpacing(0);
0025     GroupBox1->layout()->setContentsMargins(0, 0, 0, 0);
0026     GroupBox1Layout = new QVBoxLayout;
0027     GroupBox1Layout->setAlignment(Qt::AlignTop);
0028     GroupBox1->layout()->addItem(GroupBox1Layout);
0029     GroupBox1Layout->setParent(GroupBox1->layout());
0030 
0031     Layout9 = new QVBoxLayout;
0032     Layout9->setSpacing(6);
0033     Layout9->setContentsMargins(0, 0, 0, 0);
0034 
0035     Layout1 = new QHBoxLayout;
0036     Layout1->setSpacing(6);
0037     Layout1->setContentsMargins(0, 0, 0, 0);
0038 
0039     TextLabel1 = new QLabel(GroupBox1);
0040     TextLabel1->setObjectName(QStringLiteral("TextLabel1"));
0041     TextLabel1->setText(QStringLiteral("Completion"));
0042     Layout1->addWidget(TextLabel1);
0043 
0044     edit = new KLineEdit(GroupBox1);
0045     edit->setObjectName(QStringLiteral("edit"));
0046     Layout1->addWidget(edit);
0047     Layout9->addLayout(Layout1);
0048     edit->completionObject()->setItems(defaultItems());
0049     edit->completionObject()->setIgnoreCase(true);
0050     edit->setFocus();
0051     edit->setToolTip(QStringLiteral("right-click to change completion mode"));
0052 
0053     Layout2 = new QHBoxLayout;
0054     Layout2->setSpacing(6);
0055     Layout2->setContentsMargins(0, 0, 0, 0);
0056 
0057     combo = new KHistoryComboBox(GroupBox1);
0058     combo->setObjectName(QStringLiteral("history combo"));
0059     combo->setCompletionObject(edit->completionObject());
0060     // combo->setMaxCount( 5 );
0061     combo->setHistoryItems(defaultItems(), true);
0062     connect(combo, &QComboBox::textActivated, combo, &KHistoryComboBox::addToHistory);
0063     combo->setToolTip(QStringLiteral("KHistoryComboBox"));
0064     Layout2->addWidget(combo);
0065 
0066     LineEdit1 = new KLineEdit(GroupBox1);
0067     LineEdit1->setObjectName(QStringLiteral("LineEdit1"));
0068     Layout2->addWidget(LineEdit1);
0069 
0070     PushButton1 = new QPushButton(GroupBox1);
0071     PushButton1->setObjectName(QStringLiteral("PushButton1"));
0072     PushButton1->setText(QStringLiteral("Add"));
0073     connect(PushButton1, &QAbstractButton::clicked, this, &Form1::slotAdd);
0074     Layout2->addWidget(PushButton1);
0075     Layout9->addLayout(Layout2);
0076 
0077     Layout3 = new QHBoxLayout;
0078     Layout3->setSpacing(6);
0079     Layout3->setContentsMargins(0, 0, 0, 0);
0080     QSpacerItem *spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
0081     Layout3->addItem(spacer);
0082 
0083     PushButton1_4 = new QPushButton(GroupBox1);
0084     PushButton1_4->setObjectName(QStringLiteral("PushButton1_4"));
0085     PushButton1_4->setText(QStringLiteral("Remove"));
0086     connect(PushButton1_4, &QAbstractButton::clicked, this, &Form1::slotRemove);
0087     Layout3->addWidget(PushButton1_4);
0088     Layout9->addLayout(Layout3);
0089 
0090     Layout8 = new QHBoxLayout;
0091     Layout8->setSpacing(6);
0092     Layout8->setContentsMargins(0, 0, 0, 0);
0093 
0094     ListBox1 = new QListWidget(GroupBox1);
0095     Layout8->addWidget(ListBox1);
0096     connect(ListBox1, &QListWidget::currentRowChanged, this, &Form1::slotHighlighted);
0097     ListBox1->setToolTip(QStringLiteral("Contains the contents of the completion object.\n:x is the weighting, i.e. how often an item has been inserted"));
0098 
0099     Layout7 = new QVBoxLayout;
0100     Layout7->setSpacing(6);
0101     Layout7->setContentsMargins(0, 0, 0, 0);
0102 
0103     PushButton1_3 = new QPushButton(GroupBox1);
0104     PushButton1_3->setObjectName(QStringLiteral("PushButton1_3"));
0105     PushButton1_3->setText(QStringLiteral("Completion items"));
0106     connect(PushButton1_3, &QAbstractButton::clicked, this, &Form1::slotList);
0107     Layout7->addWidget(PushButton1_3);
0108 
0109     PushButton1_2 = new QPushButton(GroupBox1);
0110     PushButton1_2->setObjectName(QStringLiteral("PushButton1_2"));
0111     PushButton1_2->setText(QStringLiteral("Clear"));
0112     connect(PushButton1_2, &QAbstractButton::clicked, edit->completionObject(), &KCompletion::clear);
0113     Layout7->addWidget(PushButton1_2);
0114     Layout8->addLayout(Layout7);
0115     Layout9->addLayout(Layout8);
0116     GroupBox1Layout->addLayout(Layout9);
0117     Form1Layout->addWidget(GroupBox1);
0118 
0119     slotList();
0120 }
0121 
0122 /*
0123  *  Destroys the object and frees any allocated resources
0124  */
0125 Form1::~Form1()
0126 {
0127     // no need to delete child widgets, Qt does it all for us
0128 }
0129 
0130 void Form1::slotAdd()
0131 {
0132     qDebug("** adding: %s", LineEdit1->text().toLatin1().constData());
0133     edit->completionObject()->addItem(LineEdit1->text());
0134 
0135     QStringList matches = edit->completionObject()->allMatches(QStringLiteral("S"));
0136     QStringList::ConstIterator it = matches.constBegin();
0137     for (; it != matches.constEnd(); ++it) {
0138         qDebug("-- %s", (*it).toLatin1().constData());
0139     }
0140 }
0141 
0142 void Form1::slotRemove()
0143 {
0144     edit->completionObject()->removeItem(LineEdit1->text());
0145 }
0146 
0147 void Form1::slotList()
0148 {
0149     ListBox1->clear();
0150     QStringList items = edit->completionObject()->items();
0151     ListBox1->addItems(items);
0152 }
0153 
0154 void Form1::slotHighlighted(int row)
0155 {
0156     if (row == -1) {
0157         return;
0158     }
0159 
0160     QListWidgetItem *i = ListBox1->item(row);
0161     Q_ASSERT(i != nullptr);
0162 
0163     QString text = i->text();
0164 
0165     // remove any "weighting"
0166     int index = text.lastIndexOf(QLatin1Char(':'));
0167     if (index > 0) {
0168         LineEdit1->setText(text.left(index));
0169     } else {
0170         LineEdit1->setText(text);
0171     }
0172 }
0173 
0174 QStringList Form1::defaultItems() const
0175 {
0176     // clang-format off
0177     QStringList items;
0178     items << QStringLiteral("Super") << QStringLiteral("Sushi") << QStringLiteral("Samson") << QStringLiteral("Sucks") << QStringLiteral("Sumo") << QStringLiteral("Schumi");
0179     items << QStringLiteral("Slashdot") << QStringLiteral("sUpEr") << QStringLiteral("SUshi") << QStringLiteral("sUshi") << QStringLiteral("sUShi");
0180     items << QStringLiteral("sushI") << QStringLiteral("SushI");
0181     // clang-format on
0182     return items;
0183 }
0184 
0185 int main(int argc, char **argv)
0186 {
0187     QApplication::setApplicationName(QStringLiteral("kcompletiontest"));
0188 
0189     QApplication app(argc, argv);
0190 
0191     Form1 *form = new Form1();
0192     form->show();
0193 
0194     return app.exec();
0195 }
0196 
0197 #include "moc_kcompletionuitest.cpp"