File indexing completed on 2024-10-06 12:22:43

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Omat Holding B.V. <info@omat.nl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kfilterproxysearchline.h"
0008 
0009 #include <QHBoxLayout>
0010 #include <QLineEdit>
0011 #include <QSortFilterProxyModel>
0012 #include <QTimer>
0013 
0014 #if KITEMVIEWS_BUILD_DEPRECATED_SINCE(5, 50)
0015 
0016 /**
0017  * KFilterProxySearchLinePrivate class that helps to provide binary compatibility between releases.
0018  * @internal
0019  */
0020 //@cond PRIVATE
0021 class KFilterProxySearchLinePrivate
0022 {
0023 public:
0024     KFilterProxySearchLinePrivate(KFilterProxySearchLine *parent)
0025         : q(parent)
0026     {
0027         timer = new QTimer(q);
0028         timer->setSingleShot(true);
0029         QObject::connect(timer, SIGNAL(timeout()), q, SLOT(slotSearchLineActivate()));
0030     }
0031     QTimer *timer;
0032     KFilterProxySearchLine *const q;
0033     QSortFilterProxyModel *proxy = nullptr;
0034     QLineEdit *searchLine = nullptr;
0035 
0036     void slotSearchLineChange(const QString &newText);
0037     void slotSearchLineActivate();
0038 };
0039 
0040 void KFilterProxySearchLinePrivate::slotSearchLineChange(const QString &)
0041 {
0042     timer->start(300);
0043 }
0044 
0045 void KFilterProxySearchLinePrivate::slotSearchLineActivate()
0046 {
0047     if (!proxy) {
0048         return;
0049     }
0050 
0051     proxy->setFilterKeyColumn(-1);
0052     proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
0053     proxy->setFilterFixedString(searchLine->text());
0054 }
0055 //@endcond
0056 
0057 KFilterProxySearchLine::KFilterProxySearchLine(QWidget *parent)
0058     : QWidget(parent)
0059     , d(new KFilterProxySearchLinePrivate(this))
0060 {
0061     d->searchLine = new QLineEdit(this);
0062     d->searchLine->setClearButtonEnabled(true);
0063     d->searchLine->setPlaceholderText(tr("Search...", "@info:placeholder"));
0064 
0065     QHBoxLayout *layout = new QHBoxLayout(this);
0066     layout->setContentsMargins(0, 0, 0, 0);
0067     layout->addWidget(d->searchLine);
0068 
0069     connect(d->searchLine, SIGNAL(textChanged(QString)), SLOT(slotSearchLineChange(QString)));
0070 }
0071 
0072 KFilterProxySearchLine::~KFilterProxySearchLine() = default;
0073 
0074 void KFilterProxySearchLine::setText(const QString &text)
0075 {
0076     d->searchLine->setText(text);
0077     d->slotSearchLineActivate();
0078 }
0079 
0080 void KFilterProxySearchLine::setProxy(QSortFilterProxyModel *proxy)
0081 {
0082     d->proxy = proxy;
0083 }
0084 
0085 QLineEdit *KFilterProxySearchLine::lineEdit() const
0086 {
0087     return d->searchLine;
0088 }
0089 
0090 #include "moc_kfilterproxysearchline.cpp"
0091 
0092 #endif