2014-04-16 16:18:28 +00:00
|
|
|
/* This file is (c) 2014 Abs62
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "fulltextsearch.hh"
|
|
|
|
#include "ftshelpers.hh"
|
|
|
|
#include "gddebug.hh"
|
2023-04-22 00:43:50 +00:00
|
|
|
#include "help.hh"
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QMessageBox>
|
2023-04-18 00:41:47 +00:00
|
|
|
#include "globalregex.hh"
|
2014-04-23 14:19:46 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
namespace FTS {
|
|
|
|
|
|
|
|
void Indexing::run()
|
|
|
|
{
|
|
|
|
try {
|
2022-10-11 14:09:15 +00:00
|
|
|
timerThread->start();
|
2023-07-20 14:50:32 +00:00
|
|
|
const int parallel_count = GlobalBroadcaster::instance()->getPreference()->fts.parallelThreads;
|
|
|
|
QSemaphore sem( parallel_count < 1 ? 1 : parallel_count );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2023-07-20 14:50:32 +00:00
|
|
|
QFutureSynchronizer< void > synchronizer;
|
|
|
|
qDebug() << "starting create the fts with thread:" << parallel_count;
|
2023-05-28 16:01:21 +00:00
|
|
|
for ( const auto & dictionary : dictionaries ) {
|
2023-07-20 14:50:32 +00:00
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) {
|
|
|
|
// synchronizer.setCancelOnWait( true );
|
2014-04-16 16:18:28 +00:00
|
|
|
break;
|
2023-07-20 14:50:32 +00:00
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2023-05-28 16:01:21 +00:00
|
|
|
if ( dictionary->canFTS() && !dictionary->haveFTSIndex() ) {
|
2023-07-20 14:50:32 +00:00
|
|
|
sem.acquire();
|
|
|
|
QFuture< void > const f = QtConcurrent::run( [ this, &sem, &dictionary ]() {
|
|
|
|
QSemaphoreReleaser const _( sem );
|
2023-10-13 02:43:52 +00:00
|
|
|
const QString & dictionaryName = QString::fromUtf8( dictionary->getName().c_str() );
|
2023-10-28 05:23:14 +00:00
|
|
|
qDebug() << "[FULLTEXT] checking fts for the dictionary:" << dictionaryName;
|
2023-10-13 02:43:52 +00:00
|
|
|
emit sendNowIndexingName( dictionaryName );
|
2024-08-23 23:45:45 +00:00
|
|
|
dictionary->makeFTSIndex( isCancelled );
|
2023-07-20 14:50:32 +00:00
|
|
|
} );
|
|
|
|
synchronizer.addFuture( f );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-20 14:50:32 +00:00
|
|
|
qDebug() << "waiting for all the fts creation to finish.";
|
|
|
|
synchronizer.waitForFinished();
|
|
|
|
qDebug() << "finished/cancel all the fts creation";
|
2022-10-11 14:09:15 +00:00
|
|
|
timerThread->quit();
|
|
|
|
timerThread->wait();
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & ex ) {
|
2023-05-28 16:01:21 +00:00
|
|
|
gdWarning( "Exception occurred while full-text search: %s", ex.what() );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
2015-11-01 12:22:18 +00:00
|
|
|
emit sendNowIndexingName( QString() );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2023-05-28 16:01:21 +00:00
|
|
|
void Indexing::timeout()
|
|
|
|
{
|
2023-10-13 02:43:52 +00:00
|
|
|
QString indexingDicts;
|
2023-05-28 16:01:21 +00:00
|
|
|
for ( const auto & dictionary : dictionaries ) {
|
|
|
|
if ( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2022-10-06 11:32:45 +00:00
|
|
|
break;
|
2023-10-13 02:43:52 +00:00
|
|
|
//Finished, clear the msg.
|
|
|
|
if ( dictionary->haveFTSIndex() ) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-28 16:01:21 +00:00
|
|
|
auto newProgress = dictionary->getIndexingFtsProgress();
|
|
|
|
if ( newProgress > 0 && newProgress < 100 ) {
|
2023-10-13 02:43:52 +00:00
|
|
|
if ( !indexingDicts.isEmpty() )
|
|
|
|
indexingDicts.append( "," );
|
|
|
|
indexingDicts.append(
|
2023-05-28 16:01:21 +00:00
|
|
|
QString( "%1......%%2" ).arg( QString::fromStdString( dictionary->getName() ) ).arg( newProgress ) );
|
2022-10-06 11:32:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 02:43:52 +00:00
|
|
|
|
|
|
|
if ( !indexingDicts.isEmpty() ) {
|
|
|
|
emit sendNowIndexingName( indexingDicts );
|
|
|
|
}
|
2022-10-06 11:32:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
FtsIndexing::FtsIndexing( std::vector< sptr< Dictionary::Class > > const & dicts ):
|
|
|
|
dictionaries( dicts ),
|
2015-11-01 12:22:18 +00:00
|
|
|
started( false )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void FtsIndexing::doIndexing()
|
|
|
|
{
|
|
|
|
if ( started )
|
|
|
|
stopIndexing();
|
|
|
|
|
|
|
|
if ( !started ) {
|
2021-11-27 07:17:33 +00:00
|
|
|
while ( Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
isCancelled.deref();
|
|
|
|
|
|
|
|
Indexing * idx = new Indexing( isCancelled, dictionaries, indexingExited );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( idx, &Indexing::sendNowIndexingName, this, &FtsIndexing::setNowIndexedName );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
QThreadPool::globalInstance()->start( idx );
|
|
|
|
|
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FtsIndexing::stopIndexing()
|
|
|
|
{
|
|
|
|
if ( started ) {
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( !Utils::AtomicInt::loadAcquire( isCancelled ) )
|
2014-04-16 16:18:28 +00:00
|
|
|
isCancelled.ref();
|
|
|
|
|
|
|
|
indexingExited.acquire();
|
|
|
|
started = false;
|
2015-11-01 12:22:18 +00:00
|
|
|
|
|
|
|
setNowIndexedName( QString() );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-28 16:01:21 +00:00
|
|
|
void FtsIndexing::setNowIndexedName( const QString & name )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
{
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &nameMutex );
|
2014-04-16 16:18:28 +00:00
|
|
|
nowIndexing = name;
|
|
|
|
}
|
|
|
|
emit newIndexingName( name );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FtsIndexing::nowIndexingName()
|
|
|
|
{
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &nameMutex );
|
2014-04-16 16:18:28 +00:00
|
|
|
return nowIndexing;
|
|
|
|
}
|
|
|
|
|
2022-09-08 15:14:31 +00:00
|
|
|
void addSortedHeadwords( QList< FtsHeadword > & base_list, QList< FtsHeadword > const & add_list )
|
|
|
|
{
|
|
|
|
QList< FtsHeadword > list;
|
|
|
|
|
|
|
|
if ( add_list.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( base_list.isEmpty() ) {
|
|
|
|
base_list = add_list;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.reserve( base_list.size() + add_list.size() );
|
|
|
|
|
|
|
|
QList< FtsHeadword >::iterator base_it = base_list.begin();
|
|
|
|
QList< FtsHeadword >::const_iterator add_it = add_list.constBegin();
|
|
|
|
|
|
|
|
while ( base_it != base_list.end() || add_it != add_list.end() ) {
|
|
|
|
if ( base_it == base_list.end() ) {
|
|
|
|
while ( add_it != add_list.end() ) {
|
|
|
|
list.append( *add_it );
|
|
|
|
++add_it;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( add_it == add_list.end() ) {
|
|
|
|
while ( base_it != base_list.end() ) {
|
|
|
|
list.append( *base_it );
|
|
|
|
++base_it;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( *add_it < *base_it ) {
|
|
|
|
list.append( *add_it );
|
|
|
|
++add_it;
|
|
|
|
}
|
|
|
|
else if ( *add_it == *base_it ) {
|
|
|
|
base_it->dictIDs.append( add_it->dictIDs );
|
|
|
|
for ( QStringList::const_iterator itr = add_it->foundHiliteRegExps.constBegin();
|
|
|
|
itr != add_it->foundHiliteRegExps.constEnd();
|
|
|
|
++itr ) {
|
|
|
|
if ( !base_it->foundHiliteRegExps.contains( *itr ) )
|
|
|
|
base_it->foundHiliteRegExps.append( *itr );
|
|
|
|
}
|
|
|
|
++add_it;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
list.append( *base_it );
|
|
|
|
++base_it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
base_list.swap( list );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
FullTextSearchDialog::FullTextSearchDialog( QWidget * parent,
|
|
|
|
Config::Class & cfg_,
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & dictionaries_,
|
|
|
|
std::vector< Instances::Group > const & groups_,
|
2023-05-17 14:29:56 +00:00
|
|
|
FtsIndexing & ftsidx ):
|
2014-04-16 16:18:28 +00:00
|
|
|
QDialog( parent ),
|
|
|
|
cfg( cfg_ ),
|
|
|
|
dictionaries( dictionaries_ ),
|
|
|
|
groups( groups_ ),
|
|
|
|
group( 0 ),
|
2023-05-17 14:29:56 +00:00
|
|
|
ftsIdx( ftsidx ),
|
|
|
|
helpAction( this )
|
2014-04-16 16:18:28 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
|
|
|
|
2014-04-29 13:54:09 +00:00
|
|
|
setAttribute( Qt::WA_DeleteOnClose, false );
|
|
|
|
setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
setWindowTitle( tr( "Full-text search" ) );
|
|
|
|
|
2019-03-19 14:36:07 +00:00
|
|
|
if ( cfg.preferences.fts.dialogGeometry.size() > 0 )
|
|
|
|
restoreGeometry( cfg.preferences.fts.dialogGeometry );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
setNewIndexingName( ftsIdx.nowIndexingName() );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( &ftsIdx, &FtsIndexing::newIndexingName, this, &FullTextSearchDialog::setNewIndexingName );
|
2023-05-28 16:01:21 +00:00
|
|
|
connect( GlobalBroadcaster::instance(),
|
|
|
|
&GlobalBroadcaster::indexingDictionary,
|
|
|
|
this,
|
|
|
|
&FullTextSearchDialog::setNewIndexingName );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2023-07-01 15:39:23 +00:00
|
|
|
ui.searchMode->addItem( tr( "Default" ), WholeWords );
|
2014-04-16 16:18:28 +00:00
|
|
|
ui.searchMode->addItem( tr( "Wildcards" ), Wildcards );
|
2022-10-01 13:57:55 +00:00
|
|
|
|
2024-08-13 08:43:10 +00:00
|
|
|
ui.searchLine->setToolTip( tr( "Support xapian search syntax, such as AND OR +/- etc." ) );
|
2023-05-17 14:29:56 +00:00
|
|
|
|
2014-04-17 14:31:51 +00:00
|
|
|
ui.searchMode->setCurrentIndex( cfg.preferences.fts.searchMode );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-23 13:45:21 +00:00
|
|
|
ui.searchProgressBar->hide();
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
model = new HeadwordsListModel( this, results, activeDicts );
|
|
|
|
ui.headwordsView->setModel( model );
|
|
|
|
|
2014-04-23 13:45:21 +00:00
|
|
|
ui.articlesFoundLabel->setText( tr( "Articles found: " ) + "0" );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( ui.headwordsView, &QAbstractItemView::clicked, this, &FullTextSearchDialog::itemClicked );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this, &QDialog::finished, this, &FullTextSearchDialog::saveData );
|
2014-04-29 13:54:09 +00:00
|
|
|
|
2023-06-11 14:31:47 +00:00
|
|
|
connect( ui.OKButton, &QPushButton::clicked, this, &QDialog::accept );
|
|
|
|
connect( ui.cancelButton, &QPushButton::clicked, this, &QDialog::reject );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-06-24 13:55:06 +00:00
|
|
|
|
|
|
|
helpAction.setShortcut( QKeySequence( "F1" ) );
|
|
|
|
helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
|
|
|
|
|
2023-04-17 02:23:39 +00:00
|
|
|
connect( &helpAction, &QAction::triggered, []() {
|
2023-04-22 00:43:50 +00:00
|
|
|
Help::openHelpWebpage( Help::section::ui_fulltextserch );
|
2023-04-17 02:23:39 +00:00
|
|
|
} );
|
|
|
|
connect( ui.helpButton, &QAbstractButton::clicked, &helpAction, &QAction::trigger );
|
2014-06-24 13:55:06 +00:00
|
|
|
|
|
|
|
addAction( &helpAction );
|
|
|
|
|
2014-04-23 13:47:56 +00:00
|
|
|
ui.headwordsView->installEventFilter( this );
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
delegate = new WordListItemDelegate( ui.headwordsView->itemDelegate() );
|
|
|
|
if ( delegate )
|
|
|
|
ui.headwordsView->setItemDelegate( delegate );
|
2016-04-24 19:04:22 +00:00
|
|
|
|
|
|
|
ui.searchLine->selectAll();
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 08:56:14 +00:00
|
|
|
void FullTextSearchDialog::setSearchText( const QString & text )
|
|
|
|
{
|
|
|
|
ui.searchLine->setText( text );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
FullTextSearchDialog::~FullTextSearchDialog()
|
|
|
|
{
|
|
|
|
if ( delegate )
|
2019-03-20 14:49:39 +00:00
|
|
|
delegate->deleteLater();
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::stopSearch()
|
|
|
|
{
|
|
|
|
if ( !searchReqs.empty() ) {
|
|
|
|
for ( std::list< sptr< Dictionary::DataRequest > >::iterator it = searchReqs.begin(); it != searchReqs.end(); ++it )
|
|
|
|
if ( !( *it )->isFinished() )
|
|
|
|
( *it )->cancel();
|
|
|
|
|
|
|
|
while ( searchReqs.size() )
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::showDictNumbers()
|
|
|
|
{
|
|
|
|
ui.totalDicts->setText( QString::number( activeDicts.size() ) );
|
|
|
|
|
2014-05-08 12:38:00 +00:00
|
|
|
unsigned ready = 0, toIndex = 0;
|
2014-04-16 16:18:28 +00:00
|
|
|
for ( unsigned x = 0; x < activeDicts.size(); x++ ) {
|
2014-05-08 12:38:00 +00:00
|
|
|
if ( activeDicts.at( x )->haveFTSIndex() )
|
|
|
|
ready++;
|
2014-04-16 16:18:28 +00:00
|
|
|
else
|
2014-05-08 12:38:00 +00:00
|
|
|
toIndex++;
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.readyDicts->setText( QString::number( ready ) );
|
|
|
|
ui.toIndexDicts->setText( QString::number( toIndex ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::saveData()
|
|
|
|
{
|
2014-04-17 14:31:51 +00:00
|
|
|
cfg.preferences.fts.searchMode = ui.searchMode->currentIndex();
|
|
|
|
|
|
|
|
cfg.preferences.fts.dialogGeometry = saveGeometry();
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::setNewIndexingName( QString name )
|
|
|
|
{
|
2015-11-01 12:22:18 +00:00
|
|
|
ui.nowIndexingLabel->setText( tr( "Now indexing: " ) + ( name.isEmpty() ? tr( "None" ) : name ) );
|
2014-04-16 16:18:28 +00:00
|
|
|
showDictNumbers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::accept()
|
|
|
|
{
|
2023-06-10 13:02:22 +00:00
|
|
|
const int mode = ui.searchMode->itemData( ui.searchMode->currentIndex() ).toInt();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
2014-04-22 13:47:02 +00:00
|
|
|
model->clear();
|
2022-10-07 01:59:41 +00:00
|
|
|
matchedCount = 0;
|
2014-04-23 13:45:21 +00:00
|
|
|
ui.articlesFoundLabel->setText( tr( "Articles found: " ) + QString::number( results.size() ) );
|
2014-04-22 13:47:02 +00:00
|
|
|
|
2023-06-10 13:02:22 +00:00
|
|
|
if ( ui.searchLine->text().isEmpty() ) {
|
2022-06-18 10:50:05 +00:00
|
|
|
QMessageBox message( QMessageBox::Warning,
|
|
|
|
"GoldenDict",
|
2023-06-10 13:02:22 +00:00
|
|
|
tr( "The querying word can not be empty." ),
|
2022-06-18 10:50:05 +00:00
|
|
|
QMessageBox::Ok,
|
|
|
|
this );
|
|
|
|
message.exec();
|
2014-04-16 16:18:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( activeDicts.empty() ) {
|
|
|
|
QMessageBox message( QMessageBox::Warning,
|
|
|
|
"GoldenDict",
|
|
|
|
tr( "No dictionaries for full-text search" ),
|
|
|
|
QMessageBox::Ok,
|
|
|
|
this );
|
|
|
|
message.exec();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.OKButton->setEnabled( false );
|
2014-04-23 13:45:21 +00:00
|
|
|
ui.searchProgressBar->show();
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
// Make search requests
|
2023-06-10 13:02:22 +00:00
|
|
|
for ( unsigned x = 0; x < activeDicts.size(); ++x ) {
|
|
|
|
if ( !activeDicts[ x ]->haveFTSIndex() ) {
|
2022-06-18 10:50:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-05-17 14:29:56 +00:00
|
|
|
//max results=100
|
|
|
|
sptr< Dictionary::DataRequest > req =
|
2023-05-30 23:42:31 +00:00
|
|
|
activeDicts[ x ]->getSearchResults( ui.searchLine->text(), mode, false, false );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( req.get(),
|
|
|
|
&Dictionary::Request::finished,
|
|
|
|
this,
|
|
|
|
&FullTextSearchDialog::searchReqFinished,
|
|
|
|
Qt::QueuedConnection );
|
|
|
|
|
|
|
|
connect( req.get(),
|
|
|
|
&Dictionary::Request::matchCount,
|
|
|
|
this,
|
|
|
|
&FullTextSearchDialog::matchCount,
|
|
|
|
Qt::QueuedConnection );
|
2022-10-07 01:59:41 +00:00
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
searchReqs.push_back( req );
|
|
|
|
}
|
|
|
|
|
|
|
|
searchReqFinished(); // Handle any ones which have already finished
|
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::searchReqFinished()
|
|
|
|
{
|
2022-09-08 15:14:31 +00:00
|
|
|
QList< FtsHeadword > allHeadwords;
|
2014-04-16 16:18:28 +00:00
|
|
|
while ( searchReqs.size() ) {
|
|
|
|
std::list< sptr< Dictionary::DataRequest > >::iterator it;
|
|
|
|
for ( it = searchReqs.begin(); it != searchReqs.end(); ++it ) {
|
|
|
|
if ( ( *it )->isFinished() ) {
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "one finished.\n" );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
QString errorString = ( *it )->getErrorString();
|
|
|
|
|
|
|
|
if ( ( *it )->dataSize() >= 0 || errorString.size() ) {
|
|
|
|
QList< FtsHeadword > * headwords;
|
|
|
|
if ( (unsigned)( *it )->dataSize() >= sizeof( headwords ) ) {
|
2022-09-08 15:14:31 +00:00
|
|
|
QList< FtsHeadword > hws;
|
2014-04-16 16:18:28 +00:00
|
|
|
try {
|
|
|
|
( *it )->getDataSlice( 0, sizeof( headwords ), &headwords );
|
2022-09-08 15:14:31 +00:00
|
|
|
hws.swap( *headwords );
|
|
|
|
std::sort( hws.begin(), hws.end() );
|
2014-04-16 16:18:28 +00:00
|
|
|
delete headwords;
|
2022-09-08 15:14:31 +00:00
|
|
|
addSortedHeadwords( allHeadwords, hws );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
|
|
|
gdWarning( "getDataSlice error: %s\n", e.what() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( it != searchReqs.end() ) {
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "erasing..\n" );
|
2014-04-16 16:18:28 +00:00
|
|
|
searchReqs.erase( it );
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "erase done..\n" );
|
2014-04-16 16:18:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2022-09-08 15:14:31 +00:00
|
|
|
|
|
|
|
if ( !allHeadwords.isEmpty() ) {
|
2022-10-07 01:59:41 +00:00
|
|
|
model->addResults( QModelIndex(), allHeadwords );
|
|
|
|
if ( results.size() > matchedCount )
|
|
|
|
ui.articlesFoundLabel->setText( tr( "Articles found: " ) + QString::number( results.size() ) );
|
2022-09-08 15:14:31 +00:00
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
if ( searchReqs.empty() ) {
|
2014-04-23 13:45:21 +00:00
|
|
|
ui.searchProgressBar->hide();
|
2014-04-16 16:18:28 +00:00
|
|
|
ui.OKButton->setEnabled( true );
|
|
|
|
QApplication::beep();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 01:59:41 +00:00
|
|
|
void FullTextSearchDialog::matchCount( int _matchCount )
|
|
|
|
{
|
|
|
|
matchedCount += _matchCount;
|
|
|
|
ui.articlesFoundLabel->setText( tr( "Articles found: " ) + QString::number( matchedCount ) );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
void FullTextSearchDialog::reject()
|
|
|
|
{
|
|
|
|
if ( !searchReqs.empty() )
|
|
|
|
stopSearch();
|
|
|
|
else {
|
2014-04-29 13:54:09 +00:00
|
|
|
saveData();
|
2014-04-16 16:18:28 +00:00
|
|
|
emit closeDialog();
|
2014-04-29 13:54:09 +00:00
|
|
|
}
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::itemClicked( const QModelIndex & idx )
|
|
|
|
{
|
|
|
|
if ( idx.isValid() && idx.row() < results.size() ) {
|
|
|
|
QString headword = results[ idx.row() ].headword;
|
2024-06-22 12:01:52 +00:00
|
|
|
QRegularExpression reg;
|
2022-10-01 13:57:55 +00:00
|
|
|
auto searchText = ui.searchLine->text();
|
2022-10-07 02:17:44 +00:00
|
|
|
searchText.replace( RX::Ftx::tokenBoundary, " " );
|
2022-10-06 01:43:16 +00:00
|
|
|
|
2024-06-27 10:00:06 +00:00
|
|
|
if ( !searchText.isEmpty() ) {
|
|
|
|
reg = QRegularExpression( searchText, QRegularExpression::CaseInsensitiveOption );
|
2022-10-01 13:57:55 +00:00
|
|
|
}
|
2023-05-17 14:29:56 +00:00
|
|
|
|
|
|
|
emit showTranslationFor( headword, results[ idx.row() ].dictIDs, reg, false );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FullTextSearchDialog::updateDictionaries()
|
|
|
|
{
|
|
|
|
activeDicts.clear();
|
|
|
|
|
|
|
|
// Find the given group
|
|
|
|
|
|
|
|
Instances::Group const * activeGroup = 0;
|
|
|
|
|
|
|
|
for ( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
if ( groups[ x ].id == group ) {
|
|
|
|
activeGroup = &groups[ x ];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've found a group, use its dictionaries; otherwise, use the global
|
|
|
|
// heap.
|
|
|
|
std::vector< sptr< Dictionary::Class > > const & groupDicts = activeGroup ? activeGroup->dictionaries : dictionaries;
|
|
|
|
|
|
|
|
// Exclude muted dictionaries
|
|
|
|
|
|
|
|
Config::Group const * grp = cfg.getGroup( group );
|
|
|
|
Config::MutedDictionaries const * mutedDicts;
|
|
|
|
|
|
|
|
if ( group == Instances::Group::AllGroupId )
|
|
|
|
mutedDicts = &cfg.mutedDictionaries;
|
|
|
|
else
|
|
|
|
mutedDicts = grp ? &grp->mutedDictionaries : 0;
|
|
|
|
|
|
|
|
if ( mutedDicts && !mutedDicts->isEmpty() ) {
|
|
|
|
activeDicts.reserve( groupDicts.size() );
|
|
|
|
for ( unsigned x = 0; x < groupDicts.size(); ++x )
|
|
|
|
if ( groupDicts[ x ]->canFTS() && !mutedDicts->contains( QString::fromStdString( groupDicts[ x ]->getId() ) ) )
|
|
|
|
activeDicts.push_back( groupDicts[ x ] );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for ( unsigned x = 0; x < groupDicts.size(); ++x )
|
|
|
|
if ( groupDicts[ x ]->canFTS() )
|
|
|
|
activeDicts.push_back( groupDicts[ x ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
showDictNumbers();
|
|
|
|
}
|
|
|
|
|
2014-04-23 13:47:56 +00:00
|
|
|
bool FullTextSearchDialog::eventFilter( QObject * obj, QEvent * ev )
|
|
|
|
{
|
|
|
|
if ( obj == ui.headwordsView && ev->type() == QEvent::KeyPress ) {
|
|
|
|
QKeyEvent * kev = static_cast< QKeyEvent * >( ev );
|
|
|
|
if ( kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Enter ) {
|
|
|
|
itemClicked( ui.headwordsView->currentIndex() );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QDialog::eventFilter( obj, ev );
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
/// HeadwordsListModel
|
|
|
|
|
|
|
|
int HeadwordsListModel::rowCount( QModelIndex const & ) const
|
|
|
|
{
|
|
|
|
return headwords.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant HeadwordsListModel::data( QModelIndex const & index, int role ) const
|
|
|
|
{
|
|
|
|
if ( index.row() < 0 )
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
FtsHeadword const & head = headwords[ index.row() ];
|
|
|
|
|
|
|
|
if ( head.headword.isEmpty() )
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch ( role ) {
|
|
|
|
case Qt::ToolTipRole: {
|
|
|
|
QString tt;
|
|
|
|
for ( int x = 0; x < head.dictIDs.size(); x++ ) {
|
|
|
|
if ( x != 0 )
|
|
|
|
tt += "<br>";
|
|
|
|
|
|
|
|
int n = getDictIndex( head.dictIDs[ x ] );
|
|
|
|
if ( n != -1 )
|
|
|
|
tt += QString::fromUtf8( dictionaries[ n ]->getName().c_str() );
|
|
|
|
}
|
|
|
|
return tt;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return head.headword;
|
|
|
|
|
|
|
|
case Qt::EditRole:
|
|
|
|
return head.headword;
|
|
|
|
|
|
|
|
default:;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeadwordsListModel::addResults( const QModelIndex & parent, QList< FtsHeadword > const & hws )
|
|
|
|
{
|
|
|
|
Q_UNUSED( parent );
|
|
|
|
beginResetModel();
|
|
|
|
|
2022-09-08 15:14:31 +00:00
|
|
|
addSortedHeadwords( headwords, hws );
|
2014-04-16 16:18:28 +00:00
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
emit contentChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HeadwordsListModel::clear()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
|
|
|
|
headwords.clear();
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
emit contentChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int HeadwordsListModel::getDictIndex( QString const & id ) const
|
|
|
|
{
|
|
|
|
std::string dictID( id.toUtf8().data() );
|
|
|
|
for ( unsigned x = 0; x < dictionaries.size(); x++ ) {
|
|
|
|
if ( dictionaries[ x ]->getId().compare( dictID ) == 0 )
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:15:56 +00:00
|
|
|
QString FtsHeadword::trimQuotes( QString const & str ) const
|
|
|
|
{
|
|
|
|
QString trimmed( str );
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
while ( str[ n ] == '\"' || str[ n ] == '\'' )
|
|
|
|
n++;
|
|
|
|
if ( n )
|
|
|
|
trimmed = trimmed.mid( n );
|
|
|
|
|
|
|
|
while ( trimmed.endsWith( '\"' ) || trimmed.endsWith( '\'' ) )
|
|
|
|
trimmed.chop( 1 );
|
|
|
|
|
|
|
|
return trimmed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtsHeadword::operator<( FtsHeadword const & other ) const
|
|
|
|
{
|
|
|
|
QString first = trimQuotes( headword );
|
|
|
|
QString second = trimQuotes( other.headword );
|
|
|
|
|
|
|
|
int result = first.localeAwareCompare( second );
|
|
|
|
if ( result )
|
|
|
|
return result < 0;
|
|
|
|
|
|
|
|
// Headwords without quotes are equal
|
|
|
|
|
|
|
|
if ( first.size() != headword.size() || second.size() != other.headword.size() )
|
|
|
|
return headword.localeAwareCompare( other.headword ) < 0;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:18:28 +00:00
|
|
|
} // namespace FTS
|