6
6
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
9
#include "csearchdialog.h"
10
#include "btsearchoptionsarea.h"
11
#include "btsearchresultarea.h"
10
#include "frontend/searchdialog/csearchdialog.h"
13
#include <QDialogButtonBox>
15
#include <QPushButton>
17
#include <QSizePolicy>
20
#include <QVBoxLayout>
23
#include "backend/config/cbtconfig.h"
13
24
#include "backend/cswordmodulesearch.h"
14
25
#include "backend/keys/cswordkey.h"
15
26
#include "backend/keys/cswordversekey.h"
16
#include "backend/config/cbtconfig.h"
18
27
#include "frontend/cmoduleindexdialog.h"
28
#include "frontend/searchdialog/btsearchoptionsarea.h"
29
#include "frontend/searchdialog/btsearchresultarea.h"
20
30
#include "util/cresmgr.h"
21
#include "util/ctoolclass.h"
22
#include "util/directoryutil.h"
31
#include "util/directory.h"
32
#include "util/dialogutil.h"
27
#include <QMessageBox>
28
#include <QVBoxLayout>
31
#include <QDialogButtonBox>
32
#include <QPushButton>
33
#include <QSizePolicy>
38
37
static CSearchDialog* m_staticDialog = 0;
40
void CSearchDialog::openDialog(const QList<CSwordModuleInfo*> modules, const QString& searchText, QWidget* parentDialog)
42
if (!m_staticDialog) {
43
m_staticDialog = new CSearchDialog(parentDialog);
45
m_staticDialog->reset();
47
if (modules.count()) {
48
m_staticDialog->setModules(modules);
51
m_staticDialog->showModulesSelector();
54
m_staticDialog->setSearchText(searchText);
55
if (m_staticDialog->isHidden()) {
56
m_staticDialog->show();
59
if (modules.count() && !searchText.isEmpty()) {
60
m_staticDialog->startSearch();
62
// moved these to after the startSearch() because
63
// the progress dialog caused them to loose focus.
64
m_staticDialog->raise();
65
m_staticDialog->activateWindow();
68
CSearchDialog* CSearchDialog::getSearchDialog()
70
Q_ASSERT(m_staticDialog);
71
return m_staticDialog;
39
void CSearchDialog::openDialog(const QList<CSwordModuleInfo*> modules, const QString& searchText, QWidget* parentDialog) {
40
if (!m_staticDialog) {
41
m_staticDialog = new CSearchDialog(parentDialog);
43
m_staticDialog->reset();
45
if (modules.count()) {
46
m_staticDialog->setModules(modules);
49
m_staticDialog->showModulesSelector();
52
m_staticDialog->setSearchText(searchText);
53
if (m_staticDialog->isHidden()) {
54
m_staticDialog->show();
57
if (modules.count() && !searchText.isEmpty()) {
58
m_staticDialog->startSearch();
60
// moved these to after the startSearch() because
61
// the progress dialog caused them to loose focus.
62
m_staticDialog->raise();
63
m_staticDialog->activateWindow();
66
void CSearchDialog::closeDialog()
68
if (m_staticDialog != 0)
69
m_staticDialog->closeButtonClicked();
72
CSearchDialog* CSearchDialog::getSearchDialog() {
73
Q_ASSERT(m_staticDialog);
74
return m_staticDialog;
74
77
CSearchDialog::CSearchDialog(QWidget *parent)
75
:QDialog(parent), /*m_searchButton(0),*/ m_closeButton(0),
76
m_searchResultArea(0), m_searchOptionsArea(0)
78
setWindowIcon( util::filesystem::DirectoryUtil::getIcon(CResMgr::searchdialog::icon) );
79
setWindowTitle(tr("Search"));
80
setAttribute(Qt::WA_DeleteOnClose);
81
m_searcher.connectFinished( this, SLOT(searchFinished()));
78
: QDialog(parent), /*m_searchButton(0),*/ m_closeButton(0),
79
m_searchResultArea(0), m_searchOptionsArea(0) {
80
namespace DU = util::directory;
82
setWindowIcon(DU::getIcon(CResMgr::searchdialog::icon));
83
setWindowTitle(tr("Search"));
84
setAttribute(Qt::WA_DeleteOnClose);
85
m_searcher.connectFinished( this, SLOT(searchFinished()));
86
CSearchDialog::~CSearchDialog()
90
CSearchDialog::~CSearchDialog() {
92
95
/** Starts the search with the set modules and the set search text. */
93
void CSearchDialog::startSearch()
95
QString originalSearchText(m_searchOptionsArea->searchText());
96
QString searchText("");
98
if (originalSearchText.isEmpty()) {
102
searchText = prepareSearchText(originalSearchText);
104
// Insert search text into history list of combobox
105
m_searchOptionsArea->addToHistory(originalSearchText);
107
// check that we have the indices we need for searching
108
if (!m_searcher.modulesHaveIndices( modules() ) ) {
109
int result = QMessageBox::question(this, tr("Missing indices"),
110
tr("One or more works need indexing before they can be searched.\n"
111
"This could take a long time. Proceed with indexing?"),
112
QMessageBox::Yes | QMessageBox::Default,
113
QMessageBox::No | QMessageBox::Escape);
114
// In SuSE 10.0 the result is the logical or of the button type, just like it is
115
// inputed into the QMessageBox.
116
if ( (result == (QMessageBox::Yes | QMessageBox::Default)) ||
117
(result == QMessageBox::Yes) || (result == QMessageBox::Default) ) {
118
CModuleIndexDialog* dlg = CModuleIndexDialog::getInstance();
119
dlg->indexUnindexedModules( modules() );
96
void CSearchDialog::startSearch() {
97
QString originalSearchText(m_searchOptionsArea->searchText());
98
QString searchText("");
100
// first check the search string for errors
102
QString TestString(originalSearchText);
103
QRegExp ReservedWords("heading:|footnote:|morph:|strong:");
104
if (TestString.replace(ReservedWords, "").simplified().isEmpty()) {
126
if (m_searchOptionsArea->hasSearchScope()) {
127
m_searcher.setSearchScope( m_searchOptionsArea->searchScope() );
130
m_searcher.resetSearchScope();
133
m_searcher.setModules( modules() );
134
m_searcher.setSearchedText(searchText);
137
//Just to be sure that it can't be clicked again, if the search happens to be a bit slow.
138
m_searchOptionsArea->searchButton()->setEnabled(false);
139
m_searchOptionsArea->m_searchTextCombo->setEnabled(false);
141
m_searcher.startSearch();
143
m_searchOptionsArea->searchButton()->setEnabled(true);
144
m_searchOptionsArea->m_searchTextCombo->setEnabled(true);
145
m_searchOptionsArea->m_searchTextCombo->setFocus();
109
searchText = prepareSearchText(originalSearchText);
111
// Insert search text into history list of combobox
112
m_searchOptionsArea->addToHistory(originalSearchText);
114
// check that we have the indices we need for searching
115
if (!m_searcher.modulesHaveIndices( modules() ) ) {
116
int result = util::showQuestion(this, tr("Missing indices"),
117
tr("One or more works need indexing before they can be searched.\n"
118
"This could take a long time. Proceed with indexing?"),
119
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
120
// In SuSE 10.0 the result is the logical or of the button type, just like it is
121
// inputed into the QMessageBox.
122
if ( (result == (QMessageBox::Yes | QMessageBox::Default)) ||
123
(result == QMessageBox::Yes) || (result == QMessageBox::Default) ) {
124
CModuleIndexDialog* dlg = CModuleIndexDialog::getInstance();
125
dlg->indexUnindexedModules( modules() );
132
if (m_searchOptionsArea->hasSearchScope()) {
133
m_searcher.setSearchScope( m_searchOptionsArea->searchScope() );
136
m_searcher.resetSearchScope();
139
m_searcher.setModules( modules() );
140
m_searcher.setSearchedText(searchText);
143
//Just to be sure that it can't be clicked again, if the search happens to be a bit slow.
144
m_searchOptionsArea->searchButton()->setEnabled(false);
145
m_searchOptionsArea->m_searchTextCombo->setEnabled(false);
147
m_searcher.startSearch();
149
m_searchOptionsArea->searchButton()->setEnabled(true);
150
m_searchOptionsArea->m_searchTextCombo->setEnabled(true);
151
m_searchOptionsArea->m_searchTextCombo->setFocus();
148
QString CSearchDialog::prepareSearchText(const QString& orig)
150
qDebug() << "Original search text:" << orig;
151
static const QRegExp syntaxCharacters("[+\\-()!\"~]");
152
static const QRegExp andWords("\\band\\b", Qt::CaseInsensitive);
153
static const QRegExp orWords("\\bor\\b", Qt::CaseInsensitive);
155
if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::AndType) {
156
qDebug() << "AND type";
157
text = orig.simplified();
158
text.remove(syntaxCharacters);
159
qDebug() << "After syntax characters removed:" << text;
160
text.replace(andWords, "\"and\"");
161
text.replace(orWords, "\"or\"");
162
qDebug() << "After andor repclaced:" << text;
163
text.replace(" ", " AND ");
165
if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::OrType) {
166
text = orig.simplified();
167
text.remove(syntaxCharacters);
168
text.replace(andWords, "\"and\"");
169
text.replace(orWords, "\"or\"");
171
if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::FullType) {
174
qDebug() << "The final search string:" << text;
154
QString CSearchDialog::prepareSearchText(const QString& orig) {
155
qDebug() << "Original search text:" << orig;
156
static const QRegExp syntaxCharacters("[+\\-()!\"~]");
157
static const QRegExp andWords("\\band\\b", Qt::CaseInsensitive);
158
static const QRegExp orWords("\\bor\\b", Qt::CaseInsensitive);
160
if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::AndType) {
161
qDebug() << "AND type";
162
text = orig.simplified();
163
text.remove(syntaxCharacters);
164
qDebug() << "After syntax characters removed:" << text;
165
text.replace(andWords, "\"and\"");
166
text.replace(orWords, "\"or\"");
167
qDebug() << "After andor repclaced:" << text;
168
text.replace(" ", " AND ");
170
if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::OrType) {
171
text = orig.simplified();
172
text.remove(syntaxCharacters);
173
text.replace(andWords, "\"and\"");
174
text.replace(orWords, "\"or\"");
176
if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::FullType) {
179
qDebug() << "The final search string:" << text;
178
183
/** Starts the search with the given module list and given search text. */
179
void CSearchDialog::startSearch( const QList<CSwordModuleInfo*> modules, const QString& searchText)
181
m_searchResultArea->reset();
182
m_searchOptionsArea->reset();
184
setSearchText(searchText);
184
void CSearchDialog::startSearch( const QList<CSwordModuleInfo*> modules, const QString& searchText) {
185
m_searchResultArea->reset();
186
m_searchOptionsArea->reset();
188
setSearchText(searchText);
189
193
/** Returns the list of used modules. */
190
QList<CSwordModuleInfo*> CSearchDialog::modules() const
192
return m_searchOptionsArea->modules();
194
QList<CSwordModuleInfo*> CSearchDialog::modules() const {
195
return m_searchOptionsArea->modules();
195
198
/** Sets the list of modules for the search. */
196
void CSearchDialog::setModules( const QList<CSwordModuleInfo*> modules )
198
m_searchOptionsArea->setModules(modules);
199
void CSearchDialog::setModules( const QList<CSwordModuleInfo*> modules ) {
200
m_searchOptionsArea->setModules(modules);
201
203
/** Returns the search text which is set currently. */
202
QString CSearchDialog::searchText() const
204
return m_searchOptionsArea->searchText();
204
QString CSearchDialog::searchText() const {
205
return m_searchOptionsArea->searchText();
207
sword::ListKey CSearchDialog::searchScope()
209
return m_searchOptionsArea->searchScope();
208
sword::ListKey CSearchDialog::searchScope() {
209
return m_searchOptionsArea->searchScope();
212
212
/** Sets the search text which is used for the search. */
213
void CSearchDialog::setSearchText( const QString searchText )
215
m_searchOptionsArea->setSearchText(searchText);
213
void CSearchDialog::setSearchText( const QString searchText ) {
214
m_searchOptionsArea->setSearchText(searchText);
218
217
/** Initializes this object. */
219
void CSearchDialog::initView()
221
QVBoxLayout* verticalLayout = new QVBoxLayout(this);
222
setLayout(verticalLayout);
224
m_searchOptionsArea = new BtSearchOptionsArea(this);
225
verticalLayout->addWidget(m_searchOptionsArea);
227
m_searchResultArea = new BtSearchResultArea(this);
228
verticalLayout->addWidget(m_searchResultArea);
230
QHBoxLayout* horizontalLayout = new QHBoxLayout();
232
m_analyseButton = new QPushButton(tr("&Analyze results..."), 0);
233
m_analyseButton->setToolTip(tr("Show a graphical analyzis of the search result"));
234
QSpacerItem* spacerItem = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
235
horizontalLayout->addWidget(m_analyseButton);
236
horizontalLayout->addItem(spacerItem);
238
m_closeButton = new QPushButton(this);
239
m_closeButton->setText(tr("&Close"));
240
m_closeButton->setIcon( util::filesystem::DirectoryUtil::getIcon(CResMgr::searchdialog::close_icon));
241
horizontalLayout->addWidget(m_closeButton);
243
verticalLayout->addLayout(horizontalLayout);
245
loadDialogSettings();
218
void CSearchDialog::initView() {
219
namespace DU = util::directory;
221
QVBoxLayout* verticalLayout = new QVBoxLayout(this);
222
setLayout(verticalLayout);
224
m_searchOptionsArea = new BtSearchOptionsArea(this);
225
verticalLayout->addWidget(m_searchOptionsArea);
227
m_searchResultArea = new BtSearchResultArea(this);
228
verticalLayout->addWidget(m_searchResultArea);
230
QHBoxLayout* horizontalLayout = new QHBoxLayout();
232
m_analyseButton = new QPushButton(tr("&Analyze results..."), 0);
233
m_analyseButton->setToolTip(tr("Show a graphical analyzis of the search result"));
234
QSpacerItem* spacerItem = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
235
horizontalLayout->addWidget(m_analyseButton);
236
horizontalLayout->addItem(spacerItem);
238
m_closeButton = new QPushButton(this);
239
m_closeButton->setText(tr("&Close"));
240
m_closeButton->setIcon(DU::getIcon(CResMgr::searchdialog::close_icon));
241
horizontalLayout->addWidget(m_closeButton);
243
verticalLayout->addLayout(horizontalLayout);
245
loadDialogSettings();
248
248
void CSearchDialog::searchFinished() {
249
if ( m_searcher.foundItems() ) {
250
m_searchResultArea->setSearchResult(modules());
253
m_searchResultArea->reset();
255
m_staticDialog->raise();
256
m_staticDialog->activateWindow();
249
if ( m_searcher.foundItems() ) {
250
m_searchResultArea->setSearchResult(modules());
253
m_searchResultArea->reset();
255
m_staticDialog->raise();
256
m_staticDialog->activateWindow();
259
259
void CSearchDialog::showModulesSelector() {
260
m_searchOptionsArea->chooseModules();
260
m_searchOptionsArea->chooseModules();
263
263
/** Initializes the signal slot connections */
264
264
void CSearchDialog::initConnections() {
265
// Search button is clicked
266
bool ok = connect(m_searchOptionsArea->searchButton(), SIGNAL(clicked()),this, SLOT(startSearch()));
268
// Return/Enter is pressed in the search text field
269
ok = connect(m_searchOptionsArea, SIGNAL(sigStartSearch()), this, SLOT(startSearch()) );
265
// Search button is clicked
266
bool ok = connect(m_searchOptionsArea->searchButton(), SIGNAL(clicked()), this, SLOT(startSearch()));
268
// Return/Enter is pressed in the search text field
269
ok = connect(m_searchOptionsArea, SIGNAL(sigStartSearch()), this, SLOT(startSearch()) );
271
271
ok = connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
274
connect(m_analyseButton, SIGNAL(clicked()), m_searchResultArea, SLOT(showAnalysis()));
274
connect(m_analyseButton, SIGNAL(clicked()), m_searchResultArea, SLOT(showAnalysis()));
278
278
/** Resets the parts to the default. */
279
279
void CSearchDialog::reset() {
280
m_searchOptionsArea->reset();
281
m_searchResultArea->reset();
280
m_searchOptionsArea->reset();
281
m_searchResultArea->reset();
284
284
void CSearchDialog::closeButtonClicked() {
285
// With Qt::WA_DeleteOnClose set, the dialog will be deleted now
286
m_staticDialog->close();
289
void CSearchDialog::loadDialogSettings()
291
resize(CBTConfig::get(CBTConfig::searchDialogWidth), CBTConfig::get(CBTConfig::searchDialogHeight));
292
move(CBTConfig::get(CBTConfig::searchDialogX), CBTConfig::get(CBTConfig::searchDialogY));
295
void CSearchDialog::saveDialogSettings()
297
CBTConfig::set(CBTConfig::searchDialogWidth, size().width());
298
CBTConfig::set(CBTConfig::searchDialogHeight, size().height());
299
CBTConfig::set(CBTConfig::searchDialogX, x());
300
CBTConfig::set(CBTConfig::searchDialogY, y());
285
// With Qt::WA_DeleteOnClose set, the dialog will be deleted now
286
m_staticDialog->close();
289
void CSearchDialog::loadDialogSettings() {
290
resize(CBTConfig::get(CBTConfig::searchDialogWidth), CBTConfig::get(CBTConfig::searchDialogHeight));
291
move(CBTConfig::get(CBTConfig::searchDialogX), CBTConfig::get(CBTConfig::searchDialogY));
294
void CSearchDialog::saveDialogSettings() {
295
CBTConfig::set(CBTConfig::searchDialogWidth, size().width());
296
CBTConfig::set(CBTConfig::searchDialogHeight, size().height());
297
CBTConfig::set(CBTConfig::searchDialogX, x());
298
CBTConfig::set(CBTConfig::searchDialogY, y());