Parent: [72fd14] (diff)

Child: [1f17af] (diff)

Download this file

spell_w.cpp    237 lines (209 with data), 6.4 kB

  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
#ifndef lint
static char rcsid[] = "@(#$Id: spell_w.cpp,v 1.8 2006-12-19 12:11:21 dockes Exp $ (C) 2005 J.F.Dockes";
#endif
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "autoconfig.h"
#include <unistd.h>
#include <list>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <qlabel.h>
#include <qlineedit.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qcombobox.h>
#if (QT_VERSION < 0x040000)
#include <qlistview.h>
#else
#include <q3listview.h>
#endif
#include "debuglog.h"
#include "recoll.h"
#include "spell_w.h"
#include "guiutils.h"
#include "rcldb.h"
#ifdef RCL_USE_ASPELL
#include "rclaspell.h"
#endif
void SpellW::init()
{
// Don't change the order, or fix the rest of the code...
/*0*/expTypeCMB->insertItem(tr("Wildcards"));
/*1*/expTypeCMB->insertItem(tr("Regexp"));
/*2*/expTypeCMB->insertItem(tr("Stem expansion"));
#ifdef RCL_USE_ASPELL
bool noaspell = false;
rclconfig->getConfParam("noaspell", &noaspell);
if (!noaspell)
/*3*/expTypeCMB->insertItem(tr("Spelling/Phonetic"));
#endif
int typ = prefs.termMatchType;
if (typ < 0 || typ > expTypeCMB->count())
typ = 0;
expTypeCMB->setCurrentItem(typ);
// Stemming language combobox
stemLangCMB->clear();
list<string> langs;
if (!getStemLangs(langs)) {
QMessageBox::warning(0, "Recoll",
tr("error retrieving stemming languages"));
}
for (list<string>::const_iterator it = langs.begin();
it != langs.end(); it++) {
stemLangCMB->
insertItem(QString::fromAscii(it->c_str(), it->length()));
}
stemLangCMB->setEnabled(false);
// signals and slots connections
connect(baseWordLE, SIGNAL(textChanged(const QString&)),
this, SLOT(wordChanged(const QString&)));
connect(baseWordLE, SIGNAL(returnPressed()), this, SLOT(doExpand()));
connect(expandPB, SIGNAL(clicked()), this, SLOT(doExpand()));
connect(dismissPB, SIGNAL(clicked()), this, SLOT(close()));
connect(suggsLV,
#if (QT_VERSION < 0x040000)
SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)),
#else
SIGNAL(doubleClicked(Q3ListViewItem *, const QPoint &, int)),
#endif
this, SLOT(textDoubleClicked()));
connect(expTypeCMB, SIGNAL(activated(int)),
this, SLOT(modeSet(int)));
suggsLV->setColumnWidth(0, 200);
suggsLV->setColumnWidth(1, 100);
// No initial sorting: user can choose to establish one
suggsLV->setSorting(100, false);
}
// Subclass qlistviewitem for numeric sorting on column 1
class MyListViewItem : public QListViewItem
{
public:
MyListViewItem(QListView *listView, const QString& s1, const QString& s2)
: QListViewItem(listView, s1, s2)
{ }
int compare(QListViewItem * i, int col, bool ascending) const {
if (col == 0)
return i->text(0).compare(text(0));
if (col == 1)
return i->text(1).toInt() - text(1).toInt();
// ??
return 0;
}
};
/* Expand term according to current mode */
void SpellW::doExpand()
{
suggsLV->clear();
if (baseWordLE->text().isEmpty())
return;
string reason;
if (!maybeOpenDb(reason)) {
LOGDEB(("SpellW::doExpand: db error: %s\n", reason.c_str()));
return;
}
string expr = string((const char *)baseWordLE->text().utf8());
list<string> suggs;
prefs.termMatchType = expTypeCMB->currentItem();
Rcl::Db::MatchType mt = Rcl::Db::ET_WILD;
switch(expTypeCMB->currentItem()) {
case 0: mt = Rcl::Db::ET_WILD; break;
case 1:mt = Rcl::Db::ET_REGEXP; break;
case 2:mt = Rcl::Db::ET_STEM; break;
}
list<Rcl::TermMatchEntry> entries;
switch (expTypeCMB->currentItem()) {
case 0:
case 1:
case 2: {
if (!rcldb->termMatch(mt, prefs.queryStemLang.ascii(), expr, entries,
200)) {
LOGERR(("SpellW::doExpand:rcldb::termMatch failed\n"));
return;
}
}
break;
#ifdef RCL_USE_ASPELL
case 3: {
LOGDEB(("SpellW::doExpand: aspelling\n"));
if (!aspell) {
QMessageBox::warning(0, "Recoll",
tr("Aspell init failed. "
"Aspell not installed?"));
LOGDEB(("SpellW::doExpand: aspell init error\n"));
return;
}
list<string> suggs;
if (!aspell->suggest(*rcldb, expr, suggs, reason)) {
QMessageBox::warning(0, "Recoll",
tr("Aspell expansion error. "));
LOGERR(("SpellW::doExpand:suggest failed: %s\n", reason.c_str()));
}
for (list<string>::const_iterator it = suggs.begin();
it != suggs.end(); it++)
entries.push_back(Rcl::TermMatchEntry(*it));
}
#endif
}
if (entries.empty()) {
new MyListViewItem(suggsLV, tr("No expansion found"), "");
} else {
// Seems that need to use a reverse iterator to get same order in
// listview and input list ??
for (list<Rcl::TermMatchEntry>::reverse_iterator it = entries.rbegin();
it != entries.rend(); it++) {
LOGDEB(("SpellW::expand: %6d [%s]\n", it->wcf, it->term.c_str()));
char num[20];
if (it->wcf)
sprintf(num, "%d", it->wcf);
else
num[0] = 0;
new MyListViewItem(suggsLV,
QString::fromUtf8(it->term.c_str()),
QString::fromAscii(num));
}
}
}
void SpellW::wordChanged(const QString &text)
{
if (text.isEmpty()) {
expandPB->setEnabled(false);
suggsLV->clear();
} else {
expandPB->setEnabled(true);
}
}
void SpellW::textDoubleClicked()
{
QListViewItemIterator it(suggsLV);
while (it.current()) {
QListViewItem *item = it.current();
if (!item->isSelected()) {
++it;
continue;
}
emit(wordSelect((const char *)item->text(0)));
++it;
}
}
void SpellW::modeSet(int mode)
{
if (mode == 2)
stemLangCMB->setEnabled(true);
else
stemLangCMB->setEnabled(false);
}