forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordlist.cc
156 lines (121 loc) · 3.55 KB
/
wordlist.cc
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
/* This file is (c) 2013 Tvangeste <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include <QDebug>
#include "wordlist.hh"
WordList::WordList( QWidget * parent ) : QListWidget( parent )
, listItemDelegate( itemDelegate() )
{
wordFinder = 0;
translateLine = 0;
setItemDelegate( &listItemDelegate );
}
void WordList::attachFinder( WordFinder * finder )
{
// qDebug() << "Attaching the word finder..." << finder;
if ( wordFinder == finder )
return;
if ( wordFinder )
{
disconnect( wordFinder, SIGNAL( updated() ),
this, SLOT( prefixMatchUpdated() ) );
disconnect( wordFinder, SIGNAL( finished() ),
this, SLOT( prefixMatchFinished() ) );
}
wordFinder = finder;
connect( wordFinder, SIGNAL( updated() ),
this, SLOT( prefixMatchUpdated() ) );
connect( wordFinder, SIGNAL( finished() ),
this, SLOT( prefixMatchFinished() ) );
}
void WordList::prefixMatchUpdated()
{
updateMatchResults( false );
}
void WordList::prefixMatchFinished()
{
updateMatchResults( true );
}
void WordList::updateMatchResults( bool finished )
{
WordFinder::SearchResults const & results = wordFinder->getResults();
setUpdatesEnabled( false );
for( unsigned x = 0; x < results.size(); ++x )
{
QListWidgetItem * i = item( x );
if ( !i )
{
i = new QListWidgetItem( results[ x ].first, this );
if ( results[ x ].second )
{
QFont f = i->font();
f.setItalic( true );
i->setFont( f );
}
addItem( i );
}
else
{
if ( i->text() != results[ x ].first )
i->setText( results[ x ].first );
QFont f = i->font();
if ( f.italic() != results[ x ].second )
{
f.setItalic( results[ x ].second );
i->setFont( f );
}
}
i->setTextAlignment(Qt::AlignLeft);
}
while ( count() > (int) results.size() )
{
// Chop off any extra items that were there
QListWidgetItem * i = takeItem( count() - 1 );
if ( i )
delete i;
else
break;
}
if ( count() )
{
scrollToItem( item( 0 ), QAbstractItemView::PositionAtTop );
setCurrentItem( 0, QItemSelectionModel::Clear );
}
setUpdatesEnabled( true );
if ( finished )
{
unsetCursor();
refreshTranslateLine();
if ( !wordFinder->getErrorString().isEmpty() )
emit statusBarMessage( tr( "WARNING: %1" ).arg( wordFinder->getErrorString() ),
20000 , QPixmap( ":/icons/error.png" ) );
}
if( !results.empty() && results.front().first.isRightToLeft() )
setLayoutDirection( Qt::RightToLeft );
else
setLayoutDirection( Qt::LeftToRight );
emit contentChanged();
}
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() );
}
}
void WordList::resizeEvent( QResizeEvent * ev )
{
// In some rare cases Qt start send QResizeEvent recursively
// up to full stack depletion (tested on Qt 4.8.5, 4.8.6).
// We use this trick to break such suicidal process.
for( int x = 0; x < resizedSizes.size(); x++ )
if( resizedSizes.at( x ) == ev->size() )
return;
resizedSizes.push_back( ev->size() );
QListWidget::resizeEvent( ev );
resizedSizes.pop_back();
}