Warning, file /education/kalzium/src/searchwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2006 Pino Toscano <toscano.pino@tiscali.it> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "searchwidget.h" 0008 0009 #include <QHBoxLayout> 0010 #include <QTimer> 0011 0012 #include <KLineEdit> 0013 #include <KLocalizedString> 0014 0015 #include "kalziumdataobject.h" 0016 #include "search.h" 0017 0018 SearchWidget::SearchWidget(QWidget *parent) 0019 : QWidget(parent) 0020 , m_searchLine(new KLineEdit(this)) 0021 { 0022 auto mainlay = new QHBoxLayout(this); 0023 mainlay->setContentsMargins(2, 2, 2, 2); 0024 mainlay->setSpacing(5); 0025 0026 m_searchLine->setClearButtonEnabled(true); 0027 m_searchLine->setPlaceholderText(i18n("Search...")); 0028 m_searchLine->setTrapReturnKey(true); 0029 connect(m_searchLine, &QLineEdit::textChanged, this, &SearchWidget::searchTextChanged); 0030 connect(m_searchLine, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed())); 0031 mainlay->addWidget(m_searchLine); 0032 } 0033 0034 SearchWidget::~SearchWidget() 0035 { 0036 delete m_searchLine; 0037 delete m_timer; 0038 } 0039 0040 void SearchWidget::giveFocus() 0041 { 0042 m_searchLine->setFocus(Qt::MouseFocusReason); 0043 m_searchLine->setCursorPosition(m_searchLine->text().length()); 0044 } 0045 0046 void SearchWidget::searchTextChanged(const QString &) 0047 { 0048 if (m_timer) { 0049 m_timer->stop(); 0050 } else { 0051 m_timer = new QTimer(this); 0052 m_timer->setSingleShot(true); 0053 connect(m_timer, &QTimer::timeout, this, &SearchWidget::doSearch); 0054 } 0055 // 1/3 of second should be ok 0056 m_timer->start(333); 0057 } 0058 0059 void SearchWidget::slotReturnPressed() 0060 { 0061 if (m_timer) { 0062 m_timer->stop(); 0063 } 0064 doSearch(); 0065 } 0066 0067 void SearchWidget::doSearch() 0068 { 0069 Search *s = KalziumDataObject::instance()->search(); 0070 if (!s) { 0071 return; 0072 } 0073 0074 const QString txt = m_searchLine->text(); 0075 if (!txt.isEmpty()) { 0076 s->doSearch(txt, Search::SearchAll); 0077 } else { 0078 s->resetSearch(); 0079 } 0080 }