2013-01-25 15:42:44 +00:00
|
|
|
/* This file is (c) 2013 Tvangeste <i.4m.l33t@yandex.ru>
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
#include "wordlist.hh"
|
|
|
|
|
|
|
|
WordList::WordList( QWidget * parent ) : QListWidget( parent )
|
2013-07-06 15:31:31 +00:00
|
|
|
, listItemDelegate( itemDelegate() )
|
2013-01-25 15:42:44 +00:00
|
|
|
{
|
|
|
|
wordFinder = 0;
|
|
|
|
translateLine = 0;
|
2013-07-06 15:31:31 +00:00
|
|
|
setItemDelegate( &listItemDelegate );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WordList::attachFinder( WordFinder * finder )
|
|
|
|
{
|
|
|
|
// qDebug() << "Attaching the word finder..." << finder;
|
|
|
|
|
|
|
|
if ( wordFinder == finder )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( wordFinder )
|
|
|
|
{
|
2022-12-26 02:08:17 +00:00
|
|
|
disconnect( wordFinder, &WordFinder::updated, this, &WordList::prefixMatchUpdated );
|
|
|
|
disconnect( wordFinder, &WordFinder::finished, this, &WordList::prefixMatchFinished );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wordFinder = finder;
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( wordFinder, &WordFinder::updated, this, &WordList::prefixMatchUpdated );
|
|
|
|
connect( wordFinder, &WordFinder::finished, this, &WordList::prefixMatchFinished );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WordList::prefixMatchUpdated()
|
|
|
|
{
|
|
|
|
updateMatchResults( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
void WordList::prefixMatchFinished()
|
|
|
|
{
|
|
|
|
updateMatchResults( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void WordList::updateMatchResults( bool finished )
|
|
|
|
{
|
|
|
|
WordFinder::SearchResults const & results = wordFinder->getResults();
|
|
|
|
|
|
|
|
setUpdatesEnabled( false );
|
2022-09-20 12:15:26 +00:00
|
|
|
//clear all existed items
|
|
|
|
clear();
|
2013-01-25 15:42:44 +00:00
|
|
|
|
|
|
|
for( unsigned x = 0; x < results.size(); ++x )
|
|
|
|
{
|
2022-09-20 12:15:26 +00:00
|
|
|
QListWidgetItem * i = new QListWidgetItem( results[ x ].first, this );
|
|
|
|
i->setToolTip( results[ x ].first );
|
2013-01-25 15:42:44 +00:00
|
|
|
|
2022-09-20 12:15:26 +00:00
|
|
|
if( results[ x ].second )
|
2013-01-25 15:42:44 +00:00
|
|
|
{
|
|
|
|
QFont f = i->font();
|
2022-09-20 12:15:26 +00:00
|
|
|
f.setItalic( true );
|
|
|
|
i->setFont( f );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
2013-07-06 15:31:31 +00:00
|
|
|
|
2022-09-20 12:15:26 +00:00
|
|
|
i->setTextAlignment( Qt::AlignLeft );
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( count() )
|
|
|
|
{
|
|
|
|
scrollToItem( item( 0 ), QAbstractItemView::PositionAtTop );
|
|
|
|
setCurrentItem( 0, QItemSelectionModel::Clear );
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpdatesEnabled( true );
|
|
|
|
|
|
|
|
if ( finished )
|
|
|
|
{
|
|
|
|
unsetCursor();
|
|
|
|
|
|
|
|
refreshTranslateLine();
|
|
|
|
|
|
|
|
if ( !wordFinder->getErrorString().isEmpty() )
|
2022-01-27 00:23:57 +00:00
|
|
|
emit statusBarMessage(tr("WARNING: %1").arg(wordFinder->getErrorString()),
|
|
|
|
20000, QPixmap(":/icons/error.svg"));
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
2013-01-27 22:12:00 +00:00
|
|
|
|
2013-09-02 15:47:09 +00:00
|
|
|
if( !results.empty() && results.front().first.isRightToLeft() )
|
|
|
|
setLayoutDirection( Qt::RightToLeft );
|
|
|
|
else
|
|
|
|
setLayoutDirection( Qt::LeftToRight );
|
|
|
|
|
2013-01-27 22:12:00 +00:00
|
|
|
emit contentChanged();
|
2013-01-25 15:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WordList::refreshTranslateLine()
|
|
|
|
{
|
|
|
|
if ( !translateLine )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Visually mark the input line to mark if there's no results
|
|
|
|
bool setMark = wordFinder->getResults().empty() && !wordFinder->wasSearchUncertain();
|
|
|
|
|
|
|
|
if ( translateLine->property( "noResults" ).toBool() != setMark )
|
|
|
|
{
|
|
|
|
translateLine->setProperty( "noResults", setMark );
|
|
|
|
translateLine->setStyleSheet( translateLine->styleSheet() );
|
|
|
|
}
|
|
|
|
|
2022-09-21 15:50:14 +00:00
|
|
|
}
|