Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

searchbar.cpp

Go to the documentation of this file.
00001 /*
00002 Dagger - A Bible study program utilizing the Sword library.
00003 Copyright (c) 2004 Dan Williams <drw@handhelds.org>
00004 
00005 This file is free software; you can redistribute it and/or modify it under
00006 the terms of the GNU General Public License as published by the Free Software
00007 Foundation; either version 2 of the License, or (at your option) any later version.
00008 
00009 This file is distributed in the hope that it will be useful, but WITHOUT ANY
00010 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
00011 PARTICULAR PURPOSE. See the GNU General Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License along with this
00014 file; see the file COPYING. If not, write to the Free Software Foundation, Inc.,
00015 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 */
00017 
00018 #include "searchbar.h"
00019 #include "textwidget.h"
00020 
00021 #include <opie2/oresource.h>
00022 #include <opie2/owait.h>
00023 
00024 #include <qpe/qpeapplication.h>
00025 
00026 #include <qaction.h>
00027 #include <qcombobox.h>
00028 #include <qlineedit.h>
00029 #include <qwhatsthis.h>
00030 
00031 #include <listkey.h>
00032 #include <regex.h>
00033 #include <versekey.h>
00034 
00035 void searchCallback( char /*percent*/, void */*userData*/ )
00036 {
00037     qApp->processEvents();
00038 }
00039 
00040 SearchBar::SearchBar( QMainWindow *parent )
00041     : QToolBar( QString::null, parent, QMainWindow::Top, true )
00042     , m_currText( 0x0 )
00043 {
00044     // Initialize UI
00045     m_searchText = new QLineEdit( this );
00046     setStretchableWidget( m_searchText );
00047     QWhatsThis::add( m_searchText, tr( "Enter text to search for here." ) );
00048     connect(m_searchText, SIGNAL(textChanged(const QString &)),
00049             this, SLOT(slotTextChanged(const QString &)) );
00050 
00051     m_actionFind = new QAction( tr( "Find" ),
00052                                 Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
00053                                 QString::null, 0, this, 0 );
00054     m_actionFind->setEnabled( false );
00055     m_actionFind->addTo( this );
00056     m_actionFind->setWhatsThis( tr( "Tap here search the current module for the text entered to the left." ) );
00057     connect( m_actionFind, SIGNAL(activated()), this, SLOT(slotFind()) );
00058 
00059     addSeparator();
00060 
00061     m_actionPrev = new QAction( tr( "Previous result" ),
00062                                 Opie::Core::OResource::loadPixmap( "back", Opie::Core::OResource::SmallIcon ),
00063                                 QString::null, 0, this, 0 );
00064     m_actionPrev->setEnabled( false );
00065     m_actionPrev->addTo( this );
00066     m_actionPrev->setWhatsThis( tr( "Tap here to view the previous search result." ) );
00067     connect( m_actionPrev, SIGNAL(activated()), this, SLOT(slotPrev()) );
00068 
00069     m_resultList = new QComboBox( this );
00070     m_resultList->setEnabled( false );
00071     QWhatsThis::add( m_resultList, tr( "Select the desired search result here." ) );
00072     connect( m_resultList, SIGNAL(activated(const QString &)), this, SIGNAL(sigResultClicked(const QString &)) );
00073 
00074     m_actionNext = new QAction( tr( "Next result" ),
00075                                 Opie::Core::OResource::loadPixmap( "forward", Opie::Core::OResource::SmallIcon ),
00076                                 QString::null, 0, this, 0 );
00077     m_actionNext->setEnabled( false );
00078     m_actionNext->addTo( this );
00079     m_actionNext->setWhatsThis( tr( "Tap here to view the next search result." ) );
00080     connect( m_actionNext, SIGNAL(activated()), this, SLOT(slotNext()) );
00081 
00082     if ( parent )
00083     {
00084         installEventFilter( parent );
00085         // TODO - install for all controls
00086         m_searchText->installEventFilter( parent );
00087     }
00088 }
00089 
00090 void SearchBar::setCurrModule( TextWidget *currText )
00091 {
00092     m_actionFind->setEnabled( ( m_searchText->text() != "" ) && currText );
00093 
00094     if ( !m_currText || !currText || ( currText->getModuleName() != m_currText->getModuleName() ) )
00095     {
00096         m_actionPrev->setEnabled( false );
00097         m_resultList->clear();
00098         m_resultList->setEnabled( false );
00099         m_actionNext->setEnabled( false );
00100     }
00101 
00102     m_currText = currText;
00103 }
00104 
00105 void SearchBar::slotTextChanged( const QString &newText )
00106 {
00107     m_actionFind->setEnabled( ( newText != "" ) && m_currText );
00108 }
00109 
00110 void SearchBar::slotFind()
00111 {
00112     m_resultList->clear();
00113 
00114     // Change application title and display Opie wait dialog to indicate search is beginning
00115     QWidget *pWidget = reinterpret_cast<QWidget *>(parent());
00116     QString caption = pWidget->caption();
00117     pWidget->setCaption( "Searching..." );
00118 
00119     Opie::Ui::OWait wait( pWidget );
00120     wait.show();
00121     qApp->processEvents();
00122 
00123     // Perform search
00124     // TODO - implement search callback function to animate wait cursor
00125     sword::ListKey results = m_currText->getModule()->Search( m_searchText->text().latin1(), 0, REG_ICASE, 0, 0,
00126                                                               &searchCallback );
00127 
00128     // Process results
00129     int count = results.Count();
00130     bool found = count > 0;
00131     if ( found )
00132     {
00133         // Populate results combo box
00134         sword::VerseKey key;
00135         for ( int i = 0; i < count; i++ )
00136         {
00137             key.setText( results.GetElement( i )->getText() );
00138             m_resultList->insertItem( key.getShortText() );
00139         }
00140 
00141         // Goto first result in list
00142         m_resultList->setCurrentItem( 0 );
00143         emit sigResultClicked( m_resultList->currentText() );
00144     }
00145     else
00146     {
00147         // Reset application title
00148         pWidget->setCaption( caption );
00149     }
00150 
00151     // UI clean-up
00152     wait.hide();
00153 
00154     m_actionPrev->setEnabled( false );
00155     m_resultList->setEnabled( found );
00156     m_actionNext->setEnabled( count > 1 );
00157 }
00158 
00159 void SearchBar::slotPrev()
00160 {
00161     int item = m_resultList->currentItem() - 1;
00162     m_resultList->setCurrentItem( item );
00163     emit sigResultClicked( m_resultList->currentText() );
00164 
00165     m_actionPrev->setEnabled( item > 0 );
00166     m_actionNext->setEnabled( item < m_resultList->count() - 1 );
00167 }
00168 
00169 void SearchBar::slotNext()
00170 {
00171     int item = m_resultList->currentItem() + 1;
00172     m_resultList->setCurrentItem( item );
00173     emit sigResultClicked( m_resultList->currentText() );
00174 
00175     m_actionPrev->setEnabled( true );
00176     m_actionNext->setEnabled( item < m_resultList->count() - 1 );
00177 }
00178 
00179 void SearchBar::slotCloseBtn()
00180 {
00181     hide();
00182 }

Generated on Sat Nov 5 16:16:48 2005 for OPIE by  doxygen 1.4.2