2023-04-17 20:12:27 +00:00
|
|
|
#include "headwordsmodel.hh"
|
2022-10-02 11:53:54 +00:00
|
|
|
#include "wstring_qt.hh"
|
|
|
|
|
|
|
|
HeadwordListModel::HeadwordListModel( QObject * parent ) :
|
2023-04-22 05:32:10 +00:00
|
|
|
QAbstractListModel( parent ), filtering( false ), totalSize(0), index( 0 ),ptr( nullptr )
|
2022-10-02 11:53:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int HeadwordListModel::rowCount( const QModelIndex & parent ) const
|
|
|
|
{
|
|
|
|
return parent.isValid() ? 0 : words.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int HeadwordListModel::totalCount() const
|
|
|
|
{
|
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HeadwordListModel::isFinish() const
|
|
|
|
{
|
|
|
|
return words.size() >= totalSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// export headword
|
|
|
|
QString HeadwordListModel::getRow( int row )
|
|
|
|
{
|
|
|
|
if( fileSortedList.empty() )
|
|
|
|
{
|
|
|
|
fileSortedList << words;
|
|
|
|
fileSortedList.sort();
|
|
|
|
}
|
|
|
|
return fileSortedList.at( row );
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeadwordListModel::setFilter( QRegularExpression reg )
|
|
|
|
{
|
|
|
|
if( reg.pattern().isEmpty() )
|
|
|
|
{
|
|
|
|
filtering = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
filtering = true;
|
|
|
|
filterWords.clear();
|
2022-12-31 03:01:45 +00:00
|
|
|
auto sr = _dict->prefixMatch( gd::toWString( reg.pattern() ), 500 );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( sr.get(), &Dictionary::Request::finished, this, &HeadwordListModel::requestFinished, Qt::QueuedConnection );
|
2022-10-02 11:53:54 +00:00
|
|
|
queuedRequests.push_back( sr );
|
|
|
|
}
|
|
|
|
|
2023-04-22 09:04:03 +00:00
|
|
|
void HeadwordListModel::appendWord( const QString & word )
|
|
|
|
{
|
|
|
|
hashedWords.insert( word );
|
|
|
|
words.append( word );
|
|
|
|
}
|
|
|
|
|
2023-04-22 04:03:53 +00:00
|
|
|
void HeadwordListModel::addMatches( QStringList matches)
|
|
|
|
{
|
|
|
|
QStringList filtered;
|
2023-04-22 05:32:10 +00:00
|
|
|
for ( auto const & w : matches ) {
|
2023-04-22 09:04:03 +00:00
|
|
|
if ( !containWord( w ) ) {
|
2023-04-22 04:03:53 +00:00
|
|
|
filtered << w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( filtered.isEmpty() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
beginInsertRows( QModelIndex(), words.size(), words.size() + filtered.count() - 1 );
|
|
|
|
for ( const auto & word : filtered ) {
|
2023-04-22 09:04:03 +00:00
|
|
|
appendWord( word );
|
2023-04-22 04:03:53 +00:00
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:53:54 +00:00
|
|
|
void HeadwordListModel::requestFinished()
|
|
|
|
{
|
|
|
|
// See how many new requests have finished, and if we have any new results
|
2023-04-22 04:03:53 +00:00
|
|
|
for( auto i = queuedRequests.begin();
|
2022-10-02 11:53:54 +00:00
|
|
|
i != queuedRequests.end(); )
|
|
|
|
{
|
|
|
|
if( ( *i )->isFinished() )
|
|
|
|
{
|
|
|
|
if( !( *i )->getErrorString().isEmpty() )
|
|
|
|
{
|
|
|
|
qDebug() << "error:" << ( *i )->getErrorString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ( *i )->matchesCount() )
|
|
|
|
{
|
|
|
|
auto allmatches = ( *i )->getAllMatches();
|
|
|
|
for( auto & match : allmatches )
|
2023-04-16 09:07:07 +00:00
|
|
|
filterWords.append( QString::fromStdU32String( match.word ) );
|
2022-10-02 11:53:54 +00:00
|
|
|
}
|
|
|
|
queuedRequests.erase( i++ );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( queuedRequests.empty() )
|
|
|
|
{
|
|
|
|
QStringList filtered;
|
|
|
|
for( auto & w : filterWords )
|
|
|
|
{
|
2023-04-22 09:04:03 +00:00
|
|
|
if( !containWord( w ) )
|
2022-10-02 11:53:54 +00:00
|
|
|
{
|
|
|
|
filtered << w;
|
|
|
|
}
|
|
|
|
}
|
2022-10-05 07:42:08 +00:00
|
|
|
if( filtered.isEmpty() )
|
|
|
|
return;
|
2022-10-02 11:53:54 +00:00
|
|
|
|
|
|
|
beginInsertRows( QModelIndex(), words.size(), words.size() + filtered.count() - 1 );
|
|
|
|
for( const auto & word : filtered )
|
2023-04-22 09:04:03 +00:00
|
|
|
appendWord( word );
|
2022-10-02 11:53:54 +00:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int HeadwordListModel::wordCount() const
|
|
|
|
{
|
|
|
|
return words.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant HeadwordListModel::data( const QModelIndex & index, int role ) const
|
|
|
|
{
|
|
|
|
if( !index.isValid() )
|
2023-04-22 04:33:11 +00:00
|
|
|
return {};
|
2022-10-02 11:53:54 +00:00
|
|
|
|
|
|
|
if( index.row() >= totalSize || index.row() < 0 || index.row() >= words.size() )
|
2023-04-22 04:33:11 +00:00
|
|
|
return {};
|
2022-10-02 11:53:54 +00:00
|
|
|
|
|
|
|
if( role == Qt::DisplayRole )
|
|
|
|
{
|
|
|
|
return words.at( index.row() );
|
|
|
|
}
|
2023-04-22 04:33:11 +00:00
|
|
|
return {};
|
2022-10-02 11:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HeadwordListModel::canFetchMore( const QModelIndex & parent ) const
|
|
|
|
{
|
|
|
|
if( parent.isValid() || filtering )
|
|
|
|
return false;
|
|
|
|
return ( words.size() < totalSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeadwordListModel::fetchMore( const QModelIndex & parent )
|
|
|
|
{
|
|
|
|
if( parent.isValid() || filtering )
|
|
|
|
return;
|
|
|
|
|
|
|
|
QSet< QString > headword;
|
|
|
|
Mutex::Lock _( lock );
|
|
|
|
|
|
|
|
_dict->findHeadWordsWithLenth( index, &headword, 1000 );
|
|
|
|
if( headword.isEmpty() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QSet< QString > filtered;
|
|
|
|
for( const auto & word : qAsConst( headword ) )
|
|
|
|
{
|
2023-04-22 09:04:03 +00:00
|
|
|
if( !containWord( word ) )
|
2022-10-02 11:53:54 +00:00
|
|
|
filtered.insert( word );
|
|
|
|
}
|
|
|
|
|
|
|
|
beginInsertRows( QModelIndex(), words.size(), words.size() + filtered.count() - 1 );
|
|
|
|
for( const auto & word : filtered )
|
|
|
|
{
|
2023-04-22 09:04:03 +00:00
|
|
|
appendWord( word );
|
2022-10-02 11:53:54 +00:00
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
emit numberPopulated( words.size() );
|
|
|
|
}
|
|
|
|
|
2023-04-22 04:33:11 +00:00
|
|
|
int HeadwordListModel::getCurrentIndex() const
|
2022-10-02 11:53:54 +00:00
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2023-04-22 09:04:03 +00:00
|
|
|
bool HeadwordListModel::containWord( const QString & word )
|
|
|
|
{
|
|
|
|
return hashedWords.contains( word );
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:53:54 +00:00
|
|
|
QSet< QString > HeadwordListModel::getRemainRows( int & nodeIndex )
|
|
|
|
{
|
|
|
|
QSet< QString > headword;
|
|
|
|
Mutex::Lock _( lock );
|
|
|
|
_dict->findHeadWordsWithLenth( nodeIndex, &headword, 10000 );
|
|
|
|
|
|
|
|
QSet< QString > filtered;
|
|
|
|
for( const auto & word : headword )
|
|
|
|
{
|
2023-04-22 09:04:03 +00:00
|
|
|
if( !containWord( word ) )
|
2022-10-02 11:53:54 +00:00
|
|
|
filtered.insert( word );
|
|
|
|
}
|
|
|
|
return filtered;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeadwordListModel::setDict( Dictionary::Class * dict )
|
|
|
|
{
|
|
|
|
_dict = dict;
|
|
|
|
totalSize = _dict->getWordCount();
|
|
|
|
}
|