akregator/src

searchbar.cpp

00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2005 Frank Osterfeld <frank.osterfeld at kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "akregatorconfig.h"
00026 #include "articlefilter.h"
00027 #include "article.h"
00028 #include "searchbar.h"
00029 
00030 #include <kcombobox.h>
00031 #include <kiconloader.h>
00032 #include <klineedit.h>
00033 #include <klocale.h>
00034 #include <kstandarddirs.h>
00035 
00036 #include <qapplication.h>
00037 #include <qhbox.h>
00038 #include <qlabel.h>
00039 #include <qpixmap.h>
00040 #include <qstring.h>
00041 #include <qtimer.h>
00042 #include <qtoolbutton.h>
00043 #include <qtooltip.h>
00044 
00045 using Akregator::Filters::ArticleMatcher;
00046 using Akregator::Filters::Criterion;
00047 
00048 namespace Akregator
00049 {
00050 
00051 class SearchBar::SearchBarPrivate
00052 {
00053 public:
00054     Akregator::Filters::ArticleMatcher textFilter;
00055     Akregator::Filters::ArticleMatcher statusFilter;
00056     QString searchText;
00057     QTimer timer;
00058     KLineEdit* searchLine;
00059     KComboBox* searchCombo;
00060     int delay;
00061 };
00062 
00063 SearchBar::SearchBar(QWidget* parent, const char* name) : QHBox(parent, name), d(new SearchBar::SearchBarPrivate)
00064 {
00065     d->delay = 400;
00066     setMargin(2);
00067     setSpacing(5);
00068     setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed ) );
00069     QToolButton *clearButton = new QToolButton(this);
00070     clearButton->setIconSet( SmallIconSet( QApplication::reverseLayout() ? "clear_left" : "locationbar_erase" ) );
00071 
00072     clearButton->setAutoRaise(true);
00073 
00074     QLabel* searchLabel = new QLabel(this);
00075     searchLabel->setText( i18n("S&earch:") );
00076 
00077     d->searchLine = new KLineEdit(this, "searchline");
00078     connect(d->searchLine, SIGNAL(textChanged(const QString &)),
00079                         this, SLOT(slotSearchStringChanged(const QString &)));
00080 
00081     searchLabel->setBuddy(d->searchLine);
00082 
00083     QLabel* statusLabel = new QLabel(this);
00084     statusLabel->setText( i18n("Status:") );
00085 
00086     d->searchCombo = new KComboBox(this, "searchcombo");
00087     QPixmap iconAll = KGlobal::iconLoader()->loadIcon("exec", KIcon::Small);
00088     QPixmap iconNew(locate("data", "akregator/pics/kmmsgnew.png"));
00089     QPixmap iconUnread(locate("data", "akregator/pics/kmmsgunseen.png"));
00090     QPixmap iconKeep(locate("data", "akregator/pics/kmmsgflag.png"));
00091     
00092     d->searchCombo->insertItem(iconAll, i18n("All Articles"));
00093     d->searchCombo->insertItem(iconUnread, i18n("Unread"));
00094     d->searchCombo->insertItem(iconNew, i18n("New"));
00095     d->searchCombo->insertItem(iconKeep, i18n("Important"));
00096     
00097     QToolTip::add( clearButton, i18n( "Clear filter" ) );
00098     QToolTip::add( d->searchLine, i18n( "Enter space-separated terms to filter article list" ) );
00099     QToolTip::add( d->searchCombo, i18n( "Choose what kind of articles to show in article list" ) );
00100 
00101     connect(clearButton, SIGNAL( clicked() ),
00102                     this, SLOT(slotClearSearch()) );
00103 
00104     connect(d->searchCombo, SIGNAL(activated(int)),
00105                         this, SLOT(slotSearchComboChanged(int)));
00106 
00107     connect(&(d->timer), SIGNAL(timeout()), this, SLOT(slotActivateSearch()));
00108 }
00109 
00110 SearchBar::~SearchBar()
00111 {
00112     delete d;
00113     d = 0;
00114 }
00115 
00116 QString SearchBar::text() const
00117 {
00118     return d->searchText;
00119 }
00120 
00121 int SearchBar::status() const
00122 {
00123     return d->searchCombo->currentItem();
00124 }
00125 
00126 void SearchBar::setDelay(int ms)
00127 {
00128     d->delay = ms;
00129 }
00130 
00131 int SearchBar::delay() const
00132 {
00133     return d->delay;
00134 }
00135                 
00136 void SearchBar::slotClearSearch()
00137 {
00138     if (status() != 0 || !d->searchLine->text().isEmpty())
00139     {
00140         d->searchLine->clear();
00141         d->searchCombo->setCurrentItem(0);
00142         d->timer.stop();
00143         slotActivateSearch();
00144     }
00145 }
00146 
00147 void SearchBar::slotSetStatus(int status)
00148 {
00149      d->searchCombo->setCurrentItem(status);
00150      slotSearchComboChanged(status);
00151 }
00152 
00153 void SearchBar::slotSetText(const QString& text)
00154 {
00155      d->searchLine->setText(text);
00156      slotSearchStringChanged(text);
00157 }
00158         
00159 void SearchBar::slotSearchComboChanged(int /*index*/)
00160 {
00161     if (d->timer.isActive())
00162         d->timer.stop();    
00163         
00164     d->timer.start(200, true);
00165 }
00166 
00167 void SearchBar::slotSearchStringChanged(const QString& search)
00168 {
00169     d->searchText = search;
00170     if (d->timer.isActive())
00171         d->timer.stop();    
00172 
00173     d->timer.start(200, true);
00174 }
00175 
00176 void SearchBar::slotActivateSearch()
00177 {
00178     QValueList<Criterion> textCriteria;
00179     QValueList<Criterion> statusCriteria;
00180 
00181     if (!d->searchText.isEmpty())
00182     {
00183         Criterion subjCrit( Criterion::Title, Criterion::Contains, d->searchText);
00184         textCriteria << subjCrit;
00185         Criterion crit1( Criterion::Description, Criterion::Contains, d->searchText);
00186         textCriteria << crit1;
00187         Criterion crit2( Criterion::Author, Criterion::Contains, d->searchText);
00188         textCriteria << crit2;
00189     }
00190 
00191     if (d->searchCombo->currentItem())
00192     {
00193         switch (d->searchCombo->currentItem())
00194         {
00195             case 1: // Unread
00196             {
00197                 Criterion crit1( Criterion::Status, Criterion::Equals, Article::New);
00198                 Criterion crit2( Criterion::Status, Criterion::Equals, Article::Unread);
00199                 statusCriteria << crit1;
00200                 statusCriteria << crit2;
00201                 break;
00202             }
00203             case 2: // New
00204             {
00205                 Criterion crit( Criterion::Status, Criterion::Equals, Article::New);
00206                 statusCriteria << crit;
00207                 break;
00208             }
00209             case 3: // Keep flag set
00210             {
00211                 Criterion crit( Criterion::KeepFlag, Criterion::Equals, true);
00212                 statusCriteria << crit;
00213                 break;
00214             }
00215             default:
00216                 break;
00217         }
00218     }
00219 
00220     d->textFilter = ArticleMatcher(textCriteria, ArticleMatcher::LogicalOr);
00221     d->statusFilter = ArticleMatcher(statusCriteria, ArticleMatcher::LogicalOr);
00222     Settings::setStatusFilter(d->searchCombo->currentItem());
00223     Settings::setTextFilter(d->searchText);
00224     emit signalSearch(d->textFilter, d->statusFilter);
00225 }
00226 
00227 }
00228 
00229 #include "searchbar.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys