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 ),
|
2024-06-25 07:24:33 +00:00
|
|
|
finished( false ),
|
2023-04-22 05:32:10 +00:00
|
|
|
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
|
|
|
|
{
|
2024-06-25 07:24:33 +00:00
|
|
|
return finished || ( words.size() >= totalSize );
|
2022-10-02 11:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// export headword
|
|
|
|
QString HeadwordListModel::getRow( int row )
|
|
|
|
{
|
|
|
|
if ( fileSortedList.empty() ) {
|
|
|
|
fileSortedList << words;
|
|
|
|
fileSortedList.sort();
|
|
|
|
}
|
|
|
|
return fileSortedList.at( row );
|
|
|
|
}
|
|
|
|
|
2024-06-25 07:24:33 +00:00
|
|
|
void HeadwordListModel::setFilter( const QRegularExpression & reg )
|
2022-10-02 11:53:54 +00:00
|
|
|
{
|
2024-06-25 07:24:33 +00:00
|
|
|
//if the headword is already finished loaded, do nothing。
|
|
|
|
if ( finished ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//back to normal state ,restore the original model;
|
2022-10-02 11:53:54 +00:00
|
|
|
if ( reg.pattern().isEmpty() ) {
|
2024-06-25 07:24:33 +00:00
|
|
|
QMutexLocker _( &lock );
|
|
|
|
//race condition.
|
|
|
|
if ( !filtering ) {
|
|
|
|
return;
|
|
|
|
}
|
2022-10-02 11:53:54 +00:00
|
|
|
filtering = false;
|
2024-06-25 07:24:33 +00:00
|
|
|
|
|
|
|
//reset to previous models
|
|
|
|
beginResetModel();
|
|
|
|
words = QStringList( original_words );
|
|
|
|
endResetModel();
|
|
|
|
|
2022-10-02 11:53:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2024-06-25 07:24:33 +00:00
|
|
|
else {
|
|
|
|
QMutexLocker _( &lock );
|
|
|
|
//the first time to enter filtering mode.
|
|
|
|
if ( !filtering ) {
|
|
|
|
filtering = true;
|
|
|
|
original_words = QStringList( words );
|
|
|
|
}
|
|
|
|
}
|
2022-10-02 11:53:54 +00:00
|
|
|
filterWords.clear();
|
2024-06-25 07:24:33 +00:00
|
|
|
auto sr = _dict->prefixMatch( gd::removeTrailingZero( reg.pattern() ), maxFilterResults );
|
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 );
|
|
|
|
}
|
|
|
|
|
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(); i != queuedRequests.end(); ) {
|
2022-10-02 11:53:54 +00:00
|
|
|
if ( ( *i )->isFinished() ) {
|
|
|
|
if ( !( *i )->getErrorString().isEmpty() ) {
|
|
|
|
qDebug() << "error:" << ( *i )->getErrorString();
|
|
|
|
}
|
2024-06-25 07:24:33 +00:00
|
|
|
else if ( ( *i )->matchesCount() ) {
|
2022-10-02 11:53:54 +00:00
|
|
|
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() ) {
|
2024-06-25 07:24:33 +00:00
|
|
|
if ( filterWords.isEmpty() ) {
|
2022-10-05 07:42:08 +00:00
|
|
|
return;
|
2024-06-25 07:24:33 +00:00
|
|
|
}
|
|
|
|
beginResetModel();
|
|
|
|
words = QStringList( filterWords );
|
|
|
|
endResetModel();
|
|
|
|
emit numberPopulated( words.size() );
|
2022-10-02 11:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2024-06-25 07:24:33 +00:00
|
|
|
if ( parent.isValid() || filtering || finished )
|
2022-10-02 11:53:54 +00:00
|
|
|
return false;
|
|
|
|
return ( words.size() < totalSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
void HeadwordListModel::fetchMore( const QModelIndex & parent )
|
|
|
|
{
|
2024-06-25 07:24:33 +00:00
|
|
|
if ( parent.isValid() || filtering || finished )
|
|
|
|
return;
|
|
|
|
|
|
|
|
//arbitrary number
|
|
|
|
if ( totalSize < HEADWORDS_MAX_LIMIT ) {
|
|
|
|
finished = true;
|
|
|
|
|
|
|
|
beginInsertRows( QModelIndex(), 0, totalSize - 1 );
|
|
|
|
_dict->getHeadwords( words );
|
|
|
|
|
|
|
|
endInsertRows();
|
|
|
|
emit numberPopulated( words.size() );
|
2022-10-02 11:53:54 +00:00
|
|
|
return;
|
2024-06-25 07:24:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 11:53:54 +00:00
|
|
|
|
|
|
|
QSet< QString > headword;
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &lock );
|
2022-10-02 11:53:54 +00:00
|
|
|
|
|
|
|
_dict->findHeadWordsWithLenth( index, &headword, 1000 );
|
|
|
|
if ( headword.isEmpty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-25 07:24:33 +00:00
|
|
|
beginInsertRows( QModelIndex(), words.size(), words.size() + headword.count() - 1 );
|
2024-03-22 00:45:29 +00:00
|
|
|
for ( const auto & word : std::as_const( headword ) ) {
|
2024-06-25 07:24:33 +00:00
|
|
|
words << 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 );
|
|
|
|
}
|
|
|
|
|
2024-06-25 07:24:33 +00:00
|
|
|
void HeadwordListModel::setMaxFilterResults( int _maxFilterResults )
|
|
|
|
{
|
|
|
|
this->maxFilterResults = _maxFilterResults;
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:53:54 +00:00
|
|
|
QSet< QString > HeadwordListModel::getRemainRows( int & nodeIndex )
|
|
|
|
{
|
|
|
|
QSet< QString > headword;
|
2023-05-29 13:56:04 +00:00
|
|
|
QMutexLocker _( &lock );
|
2022-10-02 11:53:54 +00:00
|
|
|
_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();
|
|
|
|
}
|