Diffstat (limited to 'noncore/apps/dagger/searchbar.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/dagger/searchbar.cpp | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/noncore/apps/dagger/searchbar.cpp b/noncore/apps/dagger/searchbar.cpp new file mode 100644 index 0000000..747d696 --- a/dev/null +++ b/noncore/apps/dagger/searchbar.cpp | |||
@@ -0,0 +1,167 @@ | |||
1 | /* | ||
2 | Dagger - A Bible study program utilizing the Sword library. | ||
3 | Copyright (c) 2004 Dan Williams <drw@handhelds.org> | ||
4 | |||
5 | This file is free software; you can redistribute it and/or modify it under | ||
6 | the terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This file is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
11 | PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with this | ||
14 | file; see the file COPYING. If not, write to the Free Software Foundation, Inc., | ||
15 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
16 | */ | ||
17 | |||
18 | #include "searchbar.h" | ||
19 | #include "textwidget.h" | ||
20 | |||
21 | #include <opie2/owait.h> | ||
22 | |||
23 | #include <qpe/qpeapplication.h> | ||
24 | #include <qpe/resource.h> | ||
25 | |||
26 | #include <qaction.h> | ||
27 | #include <qcombobox.h> | ||
28 | #include <qlineedit.h> | ||
29 | |||
30 | #include <listkey.h> | ||
31 | #include <regex.h> | ||
32 | #include <versekey.h> | ||
33 | |||
34 | SearchBar::SearchBar( QMainWindow *parent ) | ||
35 | : QToolBar( QString::null, parent, QMainWindow::Top, true ) | ||
36 | , m_currText( 0x0 ) | ||
37 | { | ||
38 | // Initialize UI | ||
39 | m_searchText = new QLineEdit( this ); | ||
40 | setStretchableWidget( m_searchText ); | ||
41 | connect(m_searchText, SIGNAL(textChanged(const QString &)), | ||
42 | this, SLOT(slotTextChanged(const QString &)) ); | ||
43 | |||
44 | m_actionFind = new QAction( tr( "Find" ), Resource::loadPixmap( "find" ), QString::null, | ||
45 | 0, this, 0 ); | ||
46 | m_actionFind->setEnabled( false ); | ||
47 | m_actionFind->addTo( this ); | ||
48 | connect( m_actionFind, SIGNAL(activated()), this, SLOT(slotFind()) ); | ||
49 | |||
50 | addSeparator(); | ||
51 | |||
52 | m_actionPrev = new QAction( tr( "Previous result" ), Resource::loadPixmap( "back" ), | ||
53 | QString::null, 0, this, 0 ); | ||
54 | m_actionPrev->setEnabled( false ); | ||
55 | m_actionPrev->addTo( this ); | ||
56 | connect( m_actionPrev, SIGNAL(activated()), this, SLOT(slotPrev()) ); | ||
57 | |||
58 | m_resultList = new QComboBox( this ); | ||
59 | m_resultList->setEnabled( false ); | ||
60 | connect( m_resultList, SIGNAL(activated(const QString &)), this, SIGNAL(sigResultClicked(const QString &)) ); | ||
61 | |||
62 | m_actionNext = new QAction( tr( "Next result" ), Resource::loadPixmap( "forward" ), | ||
63 | QString::null, 0, this, 0 ); | ||
64 | m_actionNext->setEnabled( false ); | ||
65 | m_actionNext->addTo( this ); | ||
66 | connect( m_actionNext, SIGNAL(activated()), this, SLOT(slotNext()) ); | ||
67 | |||
68 | if ( parent ) | ||
69 | { | ||
70 | installEventFilter( parent ); | ||
71 | // TODO - install for all controls | ||
72 | m_searchText->installEventFilter( parent ); | ||
73 | } | ||
74 | } | ||
75 | |||
76 | void SearchBar::setCurrModule( TextWidget *currText ) | ||
77 | { | ||
78 | m_actionFind->setEnabled( ( m_searchText->text() != "" ) && currText ); | ||
79 | |||
80 | if ( !m_currText || ( currText->getModuleName() != m_currText->getModuleName() ) ) | ||
81 | { | ||
82 | m_actionPrev->setEnabled( false ); | ||
83 | m_resultList->clear(); | ||
84 | m_resultList->setEnabled( false ); | ||
85 | m_actionNext->setEnabled( false ); | ||
86 | } | ||
87 | |||
88 | m_currText = currText; | ||
89 | } | ||
90 | |||
91 | void SearchBar::slotTextChanged( const QString &newText ) | ||
92 | { | ||
93 | m_actionFind->setEnabled( ( newText != "" ) && m_currText ); | ||
94 | } | ||
95 | |||
96 | void SearchBar::slotFind() | ||
97 | { | ||
98 | m_resultList->clear(); | ||
99 | |||
100 | // Change application title and display Opie wait dialog to indicate search is beginning | ||
101 | QWidget *pWidget = reinterpret_cast<QWidget *>(parent()); | ||
102 | QString caption = pWidget->caption(); | ||
103 | pWidget->setCaption( "Searching..." ); | ||
104 | |||
105 | Opie::Ui::OWait wait( pWidget ); | ||
106 | wait.show(); | ||
107 | qApp->processEvents(); | ||
108 | |||
109 | // Perform search | ||
110 | // TODO - implement search callback function to animate wait cursor | ||
111 | sword::ListKey results = m_currText->getModule()->Search( m_searchText->text().latin1(), REG_ICASE, 0 ); | ||
112 | |||
113 | // Process results | ||
114 | int count = results.Count(); | ||
115 | bool found = count > 0; | ||
116 | if ( found ) | ||
117 | { | ||
118 | // Populate results combo box | ||
119 | sword::VerseKey key; | ||
120 | for ( int i = 0; i < count; i++ ) | ||
121 | { | ||
122 | key.setText( results.GetElement( i )->getText() ); | ||
123 | m_resultList->insertItem( key.getShortText() ); | ||
124 | } | ||
125 | |||
126 | // Goto first result in list | ||
127 | m_resultList->setCurrentItem( 0 ); | ||
128 | emit sigResultClicked( m_resultList->currentText() ); | ||
129 | } | ||
130 | else | ||
131 | { | ||
132 | // Reset application title | ||
133 | pWidget->setCaption( caption ); | ||
134 | } | ||
135 | |||
136 | // UI clean-up | ||
137 | wait.hide(); | ||
138 | |||
139 | m_actionPrev->setEnabled( false ); | ||
140 | m_resultList->setEnabled( found ); | ||
141 | m_actionNext->setEnabled( count > 1 ); | ||
142 | } | ||
143 | |||
144 | void SearchBar::slotPrev() | ||
145 | { | ||
146 | int item = m_resultList->currentItem() - 1; | ||
147 | m_resultList->setCurrentItem( item ); | ||
148 | emit sigResultClicked( m_resultList->currentText() ); | ||
149 | |||
150 | m_actionPrev->setEnabled( item > 0 ); | ||
151 | m_actionNext->setEnabled( item < m_resultList->count() - 1 ); | ||
152 | } | ||
153 | |||
154 | void SearchBar::slotNext() | ||
155 | { | ||
156 | int item = m_resultList->currentItem() + 1; | ||
157 | m_resultList->setCurrentItem( item ); | ||
158 | emit sigResultClicked( m_resultList->currentText() ); | ||
159 | |||
160 | m_actionPrev->setEnabled( true ); | ||
161 | m_actionNext->setEnabled( item < m_resultList->count() - 1 ); | ||
162 | } | ||
163 | |||
164 | void SearchBar::slotCloseBtn() | ||
165 | { | ||
166 | hide(); | ||
167 | } | ||