File indexing completed on 2024-05-05 04:44:01

0001 /*
0002     SPDX-FileCopyrightText: 2008 Laurent Montel <montel@kde.org>
0003     SPDX-FileCopyrightText: 2008 Benjamin C. Meyer <ben@meyerhome.net>
0004     SPDX-FileCopyrightText: 2008 Urs Wolfer <uwolfer@kde.org>
0005     SPDX-FileCopyrightText: 2009 Dawit Alemayehu <adawit@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 
0009 */
0010 
0011 #include "searchbar.h"
0012 
0013 #include <KLineEdit>
0014 #include <KColorScheme>
0015 #include <KLocalizedString>
0016 
0017 #include <QIcon>
0018 #include <QResizeEvent>
0019 
0020 
0021 SearchBar::SearchBar(QWidget *parent)
0022     :QWidget(parent)
0023 {
0024 
0025     // Get the widget that currently has the focus so we can properly
0026     // restore it when the filter bar is closed.
0027     QWidget* widgetWindow = (parent ? parent->window() : nullptr);
0028     m_focusWidget = (widgetWindow ? widgetWindow->focusWidget() : nullptr);
0029 
0030     // Initialize the user interface...
0031     m_ui.setupUi(this);
0032     m_ui.optionsButton->addAction(m_ui.actionMatchCase);
0033     m_ui.optionsButton->addAction(m_ui.actionHighlightMatch);
0034     m_ui.optionsButton->addAction(m_ui.actionSearchAutomatically);
0035 
0036     setFocusProxy(m_ui.searchComboBox);
0037     
0038     connect(m_ui.nextButton, SIGNAL(clicked()),
0039             this, SLOT(findNext()));
0040     connect(m_ui.previousButton, SIGNAL(clicked()),
0041             this, SLOT(findPrevious()));
0042     connect(m_ui.searchComboBox, SIGNAL(returnPressed()),
0043             this, SLOT(findNext()));
0044     connect(m_ui.searchComboBox, SIGNAL(editTextChanged(QString)),
0045             this, SLOT(textChanged(QString)));
0046 
0047     // Start off hidden by default...
0048     setVisible(false);
0049 }
0050 
0051 SearchBar::~SearchBar()
0052 {
0053     // NOTE: For some reason, if we do not clear the focus from the line edit
0054     // widget before we delete this object, it seems to cause a crash!!
0055     m_ui.searchComboBox->clearFocus();
0056 }
0057 
0058 void SearchBar::clear()
0059 {
0060     m_ui.searchComboBox->clear();
0061 }
0062 
0063 void SearchBar::setVisible (bool visible)
0064 {
0065     if (visible) {
0066         m_ui.searchComboBox->setFocus(Qt::ActiveWindowFocusReason);
0067         m_ui.searchComboBox->lineEdit()->selectAll();
0068     } else {
0069         m_ui.searchComboBox->setPalette(QPalette());
0070         emit searchTextChanged(QString());
0071     }
0072 
0073     QWidget::setVisible(visible);
0074 }
0075 
0076 QString SearchBar::searchText() const
0077 {
0078     return m_ui.searchComboBox->currentText();
0079 }
0080 
0081 bool SearchBar::caseSensitive() const
0082 {
0083     return m_ui.actionMatchCase->isChecked();
0084 }
0085 
0086 bool SearchBar::highlightMatches() const
0087 {
0088     return m_ui.actionHighlightMatch->isChecked();
0089 }
0090 
0091 void SearchBar::setSearchText(const QString& text)
0092 {
0093     show();
0094     m_ui.searchComboBox->setEditText(text);
0095 }
0096 
0097 void SearchBar::setFoundMatch(bool match)
0098 {
0099     //qCDebug(KWEBKITPART_LOG) << match;
0100     if (m_ui.searchComboBox->currentText().isEmpty()) {
0101         m_ui.searchComboBox->setPalette(QPalette());
0102         return;
0103     }
0104 
0105     KColorScheme::BackgroundRole role = (match ? KColorScheme::PositiveBackground : KColorScheme::NegativeBackground);
0106     QPalette newPal(m_ui.searchComboBox->palette());
0107     KColorScheme::adjustBackground(newPal, role);
0108     m_ui.searchComboBox->setPalette(newPal);
0109 }
0110 
0111 void SearchBar::findNext()
0112 {
0113     if (!isVisible())
0114         return;
0115 
0116     const QString text (m_ui.searchComboBox->currentText());
0117     if (m_ui.searchComboBox->findText(text) == -1) {
0118         m_ui.searchComboBox->addItem(text);
0119     }
0120 
0121     emit searchTextChanged(text);
0122 }
0123 
0124 void SearchBar::findPrevious()
0125 {
0126     if (!isVisible())
0127         return;
0128 
0129     const QString text (m_ui.searchComboBox->currentText());
0130     if (m_ui.searchComboBox->findText(text) == -1) {
0131         m_ui.searchComboBox->addItem(text);
0132     }
0133 
0134     emit searchTextChanged(m_ui.searchComboBox->currentText(), true);
0135 }
0136 
0137 void SearchBar::textChanged(const QString &text)
0138 {
0139     if (text.isEmpty()) {
0140         m_ui.searchComboBox->setPalette(QPalette());
0141         m_ui.nextButton->setEnabled(false);
0142         m_ui.previousButton->setEnabled(false);
0143     } else {
0144         m_ui.nextButton->setEnabled(true);
0145         m_ui.previousButton->setEnabled(true);
0146     }
0147 
0148     if (m_ui.actionSearchAutomatically->isChecked()) {
0149         emit searchTextChanged(m_ui.searchComboBox->currentText());
0150     }
0151 }
0152 
0153 bool SearchBar::event(QEvent* e)
0154 {
0155     // Close the bar when Escape is pressed. Note we cannot
0156     // assign Escape as a shortcut key because it would cause
0157     // a conflict with the Stop button.
0158     if (e->type() == QEvent::ShortcutOverride) {
0159         QKeyEvent* kev = static_cast<QKeyEvent*>(e);
0160         if (kev->key() == Qt::Key_Escape) {
0161             e->accept();
0162             close();
0163             if (m_focusWidget) {
0164                 m_focusWidget->setFocus();
0165                 m_focusWidget = nullptr;
0166             }
0167             return true;
0168         }
0169     }
0170     return QWidget::event(e);
0171 }