File indexing completed on 2022-11-29 14:16:04

0001 /*
0002     Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com>
0003     Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net>
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Lesser General Public
0007     License as published by the Free Software Foundation; either
0008     version 2.1 of the License, or (at your option) any later version.
0009 
0010     This library is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013     Lesser General Public License for more details.
0014 
0015     You should have received a copy of the GNU Lesser General Public
0016     License along with this library; if not, write to the Free Software
0017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018 */
0019 
0020 #include "filter-bar.h"
0021 
0022 #include <QBoxLayout>
0023 #include <QKeyEvent>
0024 #include <QLabel>
0025 #include <QToolButton>
0026 
0027 #include <KLocalizedString>
0028 #include <KLineEdit>
0029 #include <KIconLoader>
0030 
0031 FilterBar::FilterBar(QWidget* parent) :
0032     QWidget(parent)
0033 {
0034     // Create close button
0035     QToolButton *closeButton = new QToolButton(this);
0036     closeButton->setAutoRaise(true);
0037     closeButton->setIcon(QIcon::fromTheme("dialog-close"));
0038     closeButton->setToolTip(i18nc("@info:tooltip", "Hide Filter Bar"));
0039     connect(closeButton, SIGNAL(clicked()), this, SIGNAL(closeRequest()));
0040 
0041     // Create label
0042     QLabel* filterLabel = new QLabel(i18nc("@label:textbox", "Filter:"), this);
0043 
0044     // Create filter editor
0045     m_filterInput = new KLineEdit(this);
0046     m_filterInput->setLayoutDirection(Qt::LeftToRight);
0047     m_filterInput->setClearButtonEnabled(true);
0048     connect(m_filterInput, SIGNAL(textChanged(QString)),
0049             this, SIGNAL(filterChanged(QString)));
0050     setFocusProxy(m_filterInput);
0051 
0052     // Apply layout
0053     QHBoxLayout* hLayout = new QHBoxLayout(this);
0054     hLayout->setMargin(0);
0055     hLayout->addWidget(closeButton);
0056     hLayout->addWidget(filterLabel);
0057     hLayout->addWidget(m_filterInput);
0058 
0059     filterLabel->setBuddy(m_filterInput);
0060 }
0061 
0062 FilterBar::~FilterBar()
0063 {
0064 }
0065 
0066 void FilterBar::selectAll()
0067 {
0068     m_filterInput->selectAll();
0069 }
0070 
0071 void FilterBar::clear()
0072 {
0073     m_filterInput->clear();
0074 }
0075 
0076 void FilterBar::hide()
0077 {
0078     clear();
0079     QWidget::hide();
0080 }
0081 
0082 void FilterBar::showEvent(QShowEvent* event)
0083 {
0084     if (!event->spontaneous()) {
0085         m_filterInput->setFocus();
0086     }
0087 }
0088 
0089 void FilterBar::keyReleaseEvent(QKeyEvent* event)
0090 {
0091     QWidget::keyReleaseEvent(event);
0092     if (event->key() == Qt::Key_Escape) {
0093         if (m_filterInput->text().isEmpty()) {
0094             emit closeRequest();
0095         } else {
0096             m_filterInput->clear();
0097         }
0098     }
0099 }