File indexing completed on 2024-12-01 03:41:42

0001 #include "ktreewidgetsearchlinetest.h"
0002 
0003 #include <QApplication>
0004 #include <QBoxLayout>
0005 #include <QDialogButtonBox>
0006 #include <QHeaderView>
0007 #include <QPushButton>
0008 #include <QString>
0009 #include <QTreeWidget>
0010 
0011 #include <ktreewidgetsearchline.h>
0012 #include <ktreewidgetsearchlinewidget.h>
0013 
0014 KTreeWidgetSearchLineTest::KTreeWidgetSearchLineTest()
0015     : QDialog()
0016 {
0017     // to test KWhatsThisManager too:
0018     setWhatsThis(QStringLiteral("This is a test dialog for KTreeWidgetSearchLineTest"));
0019     tw = new QTreeWidget(this);
0020     tw->setColumnCount(4);
0021     tw->setHeaderLabels(QStringList() << QStringLiteral("Item") << QStringLiteral("Price") << QStringLiteral("HIDDEN COLUMN") << QStringLiteral("Source"));
0022     tw->hideColumn(2);
0023 
0024     KTreeWidgetSearchLineWidget *searchWidget = new KTreeWidgetSearchLineWidget(this, tw);
0025     m_searchLine = searchWidget->searchLine();
0026 
0027     QTreeWidgetItem *red = new QTreeWidgetItem(tw, QStringList() << QStringLiteral("Red"));
0028     red->setWhatsThis(0, QStringLiteral("This item is red"));
0029     red->setWhatsThis(1, QStringLiteral("This item is pricy"));
0030     tw->expandItem(red);
0031     QTreeWidgetItem *blue = new QTreeWidgetItem(tw, QStringList() << QStringLiteral("Blue"));
0032     tw->expandItem(blue);
0033     QTreeWidgetItem *green = new QTreeWidgetItem(tw, QStringList() << QStringLiteral("Green"));
0034     tw->expandItem(green);
0035     QTreeWidgetItem *yellow = new QTreeWidgetItem(tw, QStringList() << QStringLiteral("Yellow"));
0036     tw->expandItem(yellow);
0037 
0038     create2ndLevel(red);
0039     create2ndLevel(blue);
0040     create2ndLevel(green);
0041     create2ndLevel(yellow);
0042 
0043     QVBoxLayout *layout = new QVBoxLayout(this);
0044 
0045     QHBoxLayout *hbox = new QHBoxLayout();
0046 
0047     QPushButton *caseSensitive = new QPushButton(QStringLiteral("&Case Sensitive"), this);
0048     hbox->addWidget(caseSensitive);
0049     caseSensitive->setCheckable(true);
0050     connect(caseSensitive, SIGNAL(toggled(bool)), SLOT(switchCaseSensitivity(bool)));
0051 
0052     QPushButton *keepParentsVisible = new QPushButton(QStringLiteral("Keep &Parents Visible"), this);
0053     hbox->addWidget(keepParentsVisible);
0054     keepParentsVisible->setCheckable(true);
0055     keepParentsVisible->setChecked(true);
0056     connect(keepParentsVisible, SIGNAL(toggled(bool)), m_searchLine, SLOT(setKeepParentsVisible(bool)));
0057 
0058     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0059     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0060     connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0061     connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0062 
0063     layout->addWidget(searchWidget);
0064     layout->addWidget(tw);
0065     layout->addLayout(hbox);
0066     layout->addWidget(buttonBox);
0067 
0068     m_searchLine->setFocus();
0069 
0070     resize(350, 600);
0071 }
0072 
0073 void KTreeWidgetSearchLineTest::create3rdLevel(QTreeWidgetItem *item)
0074 {
0075     new QTreeWidgetItem(item, QStringList() << QStringLiteral("Growing") << QStringLiteral("$2.00") << QString("") << QStringLiteral("Farmer"));
0076     new QTreeWidgetItem(item, QStringList() << QStringLiteral("Ripe") << QStringLiteral("$8.00") << QString("") << QStringLiteral("Market"));
0077     new QTreeWidgetItem(item, QStringList() << QStringLiteral("Decaying") << QStringLiteral("$0.50") << QString("") << QStringLiteral("Ground"));
0078     new QTreeWidgetItem(item, QStringList() << QStringLiteral("Pickled") << QStringLiteral("$4.00") << QString("") << QStringLiteral("Shop"));
0079 }
0080 
0081 void KTreeWidgetSearchLineTest::create2ndLevel(QTreeWidgetItem *item)
0082 {
0083     QTreeWidgetItem *beans = new QTreeWidgetItem(item, QStringList() << QStringLiteral("Beans"));
0084     tw->expandItem(beans);
0085     create3rdLevel(beans);
0086 
0087     QTreeWidgetItem *grapes = new QTreeWidgetItem(item, QStringList() << QStringLiteral("Grapes"));
0088     tw->expandItem(grapes);
0089     create3rdLevel(grapes);
0090 
0091     QTreeWidgetItem *plums = new QTreeWidgetItem(item, QStringList() << QStringLiteral("Plums"));
0092     tw->expandItem(plums);
0093     create3rdLevel(plums);
0094 
0095     QTreeWidgetItem *bananas = new QTreeWidgetItem(item, QStringList() << QStringLiteral("Bananas"));
0096     tw->expandItem(bananas);
0097     create3rdLevel(bananas);
0098 }
0099 
0100 void KTreeWidgetSearchLineTest::switchCaseSensitivity(bool cs)
0101 {
0102     m_searchLine->setCaseSensitivity(cs ? Qt::CaseSensitive : Qt::CaseInsensitive);
0103 }
0104 
0105 void KTreeWidgetSearchLineTest::showEvent(QShowEvent *event)
0106 {
0107     QDialog::showEvent(event);
0108 
0109     for (int i = 0; i < tw->header()->count(); ++i) {
0110         if (!tw->header()->isSectionHidden(i)) {
0111             tw->resizeColumnToContents(i);
0112         }
0113     }
0114 }
0115 
0116 int main(int argc, char **argv)
0117 {
0118     QApplication::setApplicationName(QStringLiteral("KTreeWidgetSearchLineTest"));
0119     QApplication app(argc, argv);
0120     KTreeWidgetSearchLineTest dialog;
0121 
0122     dialog.exec();
0123 
0124     return 0;
0125 }
0126 
0127 #include "moc_ktreewidgetsearchlinetest.cpp"