summaryrefslogtreecommitdiff
path: root/src/frontend/searchdialog/csearchdialog.cpp
blob: 6f8214c82446ce3e24fcae2852a7190fc8eab612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/searchdialog/csearchdialog.h"

#include <QDebug>
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QPushButton>
#include <QSettings>
#include <QSizePolicy>
#include <QString>
#include <QRegExp>
#include <QVBoxLayout>
#include <QWidget>

#include "backend/config/cbtconfig.h"
#include "backend/cswordmodulesearch.h"
#include "backend/keys/cswordkey.h"
#include "backend/keys/cswordversekey.h"
#include "frontend/cmoduleindexdialog.h"
#include "frontend/searchdialog/btsearchoptionsarea.h"
#include "frontend/searchdialog/btsearchresultarea.h"
#include "util/cresmgr.h"
#include "util/directory.h"
#include "util/dialogutil.h"


namespace Search {

static CSearchDialog* m_staticDialog = 0;

void CSearchDialog::openDialog(const QList<CSwordModuleInfo*> modules, const QString& searchText, QWidget* parentDialog) {
    if (!m_staticDialog) {
        m_staticDialog = new CSearchDialog(parentDialog);
    };
    m_staticDialog->reset();

    if (modules.count()) {
        m_staticDialog->setModules(modules);
    }
    else {
        m_staticDialog->showModulesSelector();
    }

    m_staticDialog->setSearchText(searchText);
    if (m_staticDialog->isHidden()) {
        m_staticDialog->show();
    }

    if (modules.count() && !searchText.isEmpty()) {
        m_staticDialog->startSearch();
    }
    // moved these to after the startSearch() because
    // the progress dialog caused them to loose focus.
    m_staticDialog->raise();
    m_staticDialog->activateWindow();
}

void CSearchDialog::closeDialog() {
    if (m_staticDialog != 0)
        m_staticDialog->closeButtonClicked();
}

CSearchDialog* CSearchDialog::getSearchDialog() {
    Q_ASSERT(m_staticDialog);
    return m_staticDialog;
}

CSearchDialog::CSearchDialog(QWidget *parent)
        : QDialog(parent), /*m_searchButton(0),*/ m_closeButton(0),
        m_searchResultArea(0), m_searchOptionsArea(0) {
    namespace DU = util::directory;

    setWindowIcon(DU::getIcon(CResMgr::searchdialog::icon));
    setWindowTitle(tr("Search"));
    setAttribute(Qt::WA_DeleteOnClose);
    m_searcher.connectFinished( this, SLOT(searchFinished()));
    initView();
    initConnections();
}

CSearchDialog::~CSearchDialog() {
    saveDialogSettings();
    m_staticDialog = 0;
}

/** Starts the search with the set modules and the set search text. */
void CSearchDialog::startSearch() {
    QString originalSearchText(m_searchOptionsArea->searchText());
    QString searchText("");

    // first check the search string for errors
    {
        QString TestString(originalSearchText);
        QRegExp ReservedWords("heading:|footnote:|morph:|strong:");
        if (TestString.replace(ReservedWords, "").simplified().isEmpty()) {
            return;
        }
    }

    searchText = prepareSearchText(originalSearchText);

    // Insert search text into history list of combobox
    m_searchOptionsArea->addToHistory(originalSearchText);

    // check that we have the indices we need for searching
    if (!m_searcher.modulesHaveIndices( modules() ) )	{
        int result = util::showQuestion(this, tr("Missing indices"),
                                        tr("One or more works need indexing before they can be searched.\n"
                                           "This could take a long time. Proceed with indexing?"),
                                        QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        // In SuSE 10.0 the result is the logical or of the button type, just like it is
        // inputed into the QMessageBox.
        if ( (result == (QMessageBox::Yes | QMessageBox::Default)) ||
                (result == QMessageBox::Yes) || (result == QMessageBox::Default) ) {
            CModuleIndexDialog* dlg = CModuleIndexDialog::getInstance();
            dlg->indexUnindexedModules( modules() );
        }
        else {
            return;
        }
    }

    if (m_searchOptionsArea->hasSearchScope()) {
        m_searcher.setSearchScope( m_searchOptionsArea->searchScope() );
    }
    else {
        m_searcher.resetSearchScope();
    }

    m_searcher.setModules( modules() );
    m_searcher.setSearchedText(searchText);


    //Just to be sure that it can't be clicked again, if the search happens to be a bit slow.
    m_searchOptionsArea->searchButton()->setEnabled(false);
    m_searchOptionsArea->m_searchTextCombo->setEnabled(false);

    m_searcher.startSearch();

    m_searchOptionsArea->searchButton()->setEnabled(true);
    m_searchOptionsArea->m_searchTextCombo->setEnabled(true);
    m_searchOptionsArea->m_searchTextCombo->setFocus();
}

QString CSearchDialog::prepareSearchText(const QString& orig) {
    qDebug() << "Original search text:" << orig;
    static const QRegExp syntaxCharacters("[+\\-()!\"~]");
    static const QRegExp andWords("\\band\\b", Qt::CaseInsensitive);
    static const QRegExp orWords("\\bor\\b", Qt::CaseInsensitive);
    QString text("");
    if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::AndType) {
        qDebug() << "AND type";
        text = orig.simplified();
        text.remove(syntaxCharacters);
        qDebug() << "After syntax characters removed:" << text;
        text.replace(andWords, "\"and\"");
        text.replace(orWords, "\"or\"");
        qDebug() << "After andor repclaced:" << text;
        text.replace(" ", " AND ");
    }
    if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::OrType) {
        text = orig.simplified();
        text.remove(syntaxCharacters);
        text.replace(andWords, "\"and\"");
        text.replace(orWords, "\"or\"");
    }
    if (m_searchOptionsArea->searchType() == BtSearchOptionsArea::FullType) {
        text = orig;
    }
    qDebug() << "The final search string:" << text;
    return text;
}

/** Starts the search with the given module list and given search text. */
void CSearchDialog::startSearch( const QList<CSwordModuleInfo*> modules, const QString& searchText) {
    m_searchResultArea->reset();
    m_searchOptionsArea->reset();
    setModules(modules);
    setSearchText(searchText);

    startSearch();
}

/** Returns the list of used modules. */
QList<CSwordModuleInfo*> CSearchDialog::modules() const {
    return m_searchOptionsArea->modules();
}

/** Sets the list of modules for the search. */
void CSearchDialog::setModules( const QList<CSwordModuleInfo*> modules ) {
    m_searchOptionsArea->setModules(modules);
}

/** Returns the search text which is set currently. */
QString CSearchDialog::searchText() const {
    return m_searchOptionsArea->searchText();
}

sword::ListKey CSearchDialog::searchScope() {
    return m_searchOptionsArea->searchScope();
}

/** Sets the search text which is used for the search. */
void CSearchDialog::setSearchText( const QString searchText ) {
    m_searchOptionsArea->setSearchText(searchText);
}

/** Initializes this object. */
void CSearchDialog::initView() {
    namespace DU = util::directory;

    QVBoxLayout* verticalLayout = new QVBoxLayout(this);
    setLayout(verticalLayout);

    m_searchOptionsArea = new BtSearchOptionsArea(this);
    verticalLayout->addWidget(m_searchOptionsArea);

    m_searchResultArea = new BtSearchResultArea(this);
    verticalLayout->addWidget(m_searchResultArea);

    QHBoxLayout* horizontalLayout = new QHBoxLayout();

    m_analyseButton = new QPushButton(tr("&Analyze results..."), 0);
    m_analyseButton->setToolTip(tr("Show a graphical analyzis of the search result"));
    QSpacerItem* spacerItem = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
    horizontalLayout->addWidget(m_analyseButton);
    horizontalLayout->addItem(spacerItem);

    m_closeButton = new QPushButton(this);
    m_closeButton->setText(tr("&Close"));
    m_closeButton->setIcon(DU::getIcon(CResMgr::searchdialog::close_icon));
    horizontalLayout->addWidget(m_closeButton);

    verticalLayout->addLayout(horizontalLayout);

    loadDialogSettings();
}

void CSearchDialog::searchFinished() {
    if ( m_searcher.foundItems() ) {
        m_searchResultArea->setSearchResult(modules());
    }
    else {
        m_searchResultArea->reset();
    }
    m_staticDialog->raise();
    m_staticDialog->activateWindow();
}

void CSearchDialog::showModulesSelector() {
    m_searchOptionsArea->chooseModules();
}

/** Initializes the signal slot connections */
void CSearchDialog::initConnections() {
    // Search button is clicked
    bool ok = connect(m_searchOptionsArea->searchButton(), SIGNAL(clicked()), this, SLOT(startSearch()));
    Q_ASSERT(ok);
    // Return/Enter is pressed in the search text field
    ok = connect(m_searchOptionsArea, SIGNAL(sigStartSearch()), this, SLOT(startSearch()) );
    Q_ASSERT(ok);
    ok = connect(m_closeButton, SIGNAL(clicked()), this, SLOT(closeButtonClicked()));
    Q_ASSERT(ok);

    connect(m_analyseButton, SIGNAL(clicked()), m_searchResultArea, SLOT(showAnalysis()));

}

/** Resets the parts to the default. */
void CSearchDialog::reset() {
    m_searchOptionsArea->reset();
    m_searchResultArea->reset();
}

void CSearchDialog::closeButtonClicked() {
    // With Qt::WA_DeleteOnClose set, the dialog will be deleted now
    m_staticDialog->close();
}

void CSearchDialog::loadDialogSettings() {
    resize(CBTConfig::get(CBTConfig::searchDialogWidth), CBTConfig::get(CBTConfig::searchDialogHeight));
    move(CBTConfig::get(CBTConfig::searchDialogX), CBTConfig::get(CBTConfig::searchDialogY));
}

void CSearchDialog::saveDialogSettings() {
    CBTConfig::set(CBTConfig::searchDialogWidth, size().width());
    CBTConfig::set(CBTConfig::searchDialogHeight, size().height());
    CBTConfig::set(CBTConfig::searchDialogX, x());
    CBTConfig::set(CBTConfig::searchDialogY, y());
}


} //end of namespace Search