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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/* This file is (c) 2014 Abs62
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "dictheadwords.hh"
#include "gddebug.hh"
#include "mainwindow.hh"
#include <QRegExp>
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
#include <QtCore5Compat>
#endif
#include <QDir>
#include <QFileDialog>
#include <QTimer>
#include <QProgressDialog>
#include <QRegularExpression>
#include "wildcard.hh"
#include "gddebug.hh"
#include <QMessageBox>
#define AUTO_APPLY_LIMIT 150000
DictHeadwords::DictHeadwords( QWidget *parent, Config::Class & cfg_,
Dictionary::Class * dict_ ) :
QDialog(parent)
, cfg( cfg_ )
, dict( dict_ )
, helpAction( this )
{
ui.setupUi( this );
bool fromMainWindow = parent->objectName() == "MainWindow";
if( fromMainWindow )
setAttribute( Qt::WA_DeleteOnClose, false );
setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
if( cfg.headwordsDialog.headwordsDialogGeometry.size() > 0 )
restoreGeometry( cfg.headwordsDialog.headwordsDialogGeometry );
ui.searchModeCombo->addItem( tr( "Text" ), QRegExp::FixedString );
ui.searchModeCombo->addItem( tr( "Wildcards" ), QRegExp::WildcardUnix );
ui.searchModeCombo->addItem( tr( "RegExp" ), QRegExp::RegExp );
ui.searchModeCombo->setCurrentIndex( cfg.headwordsDialog.searchMode );
ui.exportButton->setAutoDefault( false );
ui.OKButton->setAutoDefault( false);
ui.applyButton->setAutoDefault( true );
ui.applyButton->setDefault( true );
ui.matchCase->setChecked( cfg.headwordsDialog.matchCase );
model = new HeadwordListModel( this );
proxy = new QSortFilterProxyModel( this );
proxy->setSourceModel( model );
proxy->setSortCaseSensitivity( Qt::CaseInsensitive );
proxy->setSortLocaleAware( true );
proxy->setDynamicSortFilter( false );
ui.headersListView->setModel( proxy );
ui.headersListView->setEditTriggers( QAbstractItemView::NoEditTriggers );
// very important call, for performance reasons:
ui.headersListView->setUniformItemSizes( true );
delegate = new WordListItemDelegate( ui.headersListView->itemDelegate() );
if( delegate )
ui.headersListView->setItemDelegate( delegate );
ui.autoApply->setChecked( cfg.headwordsDialog.autoApply );
connect( this, &QDialog::finished, this, &DictHeadwords::savePos );
if( !fromMainWindow )
{
ui.helpButton->hide();
connect( this, &DictHeadwords::closeDialog, this, &QDialog::accept );
}
else
{
connect( ui.helpButton, &QAbstractButton::clicked, this, &DictHeadwords::helpRequested );
helpAction.setShortcut( QKeySequence( "F1" ) );
helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
connect( &helpAction, &QAction::triggered, this, &DictHeadwords::helpRequested );
addAction( &helpAction );
}
connect( ui.OKButton, &QAbstractButton::clicked, this, &DictHeadwords::okButtonClicked );
connect( ui.exportButton, &QAbstractButton::clicked, this, &DictHeadwords::exportButtonClicked );
connect( ui.applyButton, &QAbstractButton::clicked, this, &DictHeadwords::filterChanged );
connect( ui.autoApply, &QCheckBox::stateChanged, this, &DictHeadwords::autoApplyStateChanged );
connect( ui.filterLine, &QLineEdit::textChanged, this, &DictHeadwords::filterChangedInternal );
connect( ui.searchModeCombo, &QComboBox::currentIndexChanged, this, &DictHeadwords::filterChangedInternal );
connect( ui.matchCase, &QCheckBox::stateChanged, this, &DictHeadwords::filterChangedInternal );
connect( ui.headersListView, &QAbstractItemView::clicked, this, &DictHeadwords::itemClicked );
connect( proxy, &QAbstractItemModel::dataChanged, this, &DictHeadwords::showHeadwordsNumber );
ui.headersListView->installEventFilter( this );
setup( dict_ );
}
DictHeadwords::~DictHeadwords()
{
if( delegate )
delegate->deleteLater();
}
void DictHeadwords::setup( Dictionary::Class *dict_ )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
dict = dict_;
setWindowTitle( QString::fromUtf8( dict->getName().c_str() ) );
auto size = dict->getWordCount();
model->setDict(dict);
proxy->sort( 0 );
filterChanged();
if( size > AUTO_APPLY_LIMIT )
{
cfg.headwordsDialog.autoApply = ui.autoApply->isChecked();
ui.autoApply->setChecked( false );
ui.autoApply->setEnabled( false );
}
else
{
ui.autoApply->setEnabled( true );
ui.autoApply->setChecked( cfg.headwordsDialog.autoApply );
}
ui.applyButton->setEnabled( !ui.autoApply->isChecked() );
setWindowIcon( dict->getIcon() );
dictId = QString( dict->getId().c_str() );
QApplication::restoreOverrideCursor();
}
void DictHeadwords::savePos()
{
cfg.headwordsDialog.searchMode = ui.searchModeCombo->currentIndex();
cfg.headwordsDialog.matchCase = ui.matchCase->isChecked();
if( model->totalCount() <= AUTO_APPLY_LIMIT )
cfg.headwordsDialog.autoApply = ui.autoApply->isChecked();
cfg.headwordsDialog.headwordsDialogGeometry = saveGeometry();
}
bool DictHeadwords::eventFilter( QObject * obj, QEvent * ev )
{
if( obj == ui.headersListView && ev->type() == QEvent::KeyPress )
{
QKeyEvent * kev = static_cast< QKeyEvent * >( ev );
if( kev->key() == Qt::Key_Return || kev->key() == Qt::Key_Enter )
{
itemClicked( ui.headersListView->currentIndex() );
return true;
}
}
return QDialog::eventFilter( obj, ev );
}
void DictHeadwords::okButtonClicked()
{
savePos();
closeDialog();
}
void DictHeadwords::reject()
{
savePos();
closeDialog();
}
void DictHeadwords::exportButtonClicked()
{
saveHeadersToFile();
}
void DictHeadwords::filterChangedInternal()
{
// emit signal in async manner, to avoid UI slowdown
if( ui.autoApply->isChecked() )
QTimer::singleShot( 100, this, &DictHeadwords::filterChanged );
}
void DictHeadwords::filterChanged()
{
QRegExp::PatternSyntax syntax =
QRegExp::PatternSyntax( ui.searchModeCombo->itemData(
ui.searchModeCombo->currentIndex()).toInt() );
QRegularExpression::PatternOptions options = QRegularExpression::UseUnicodePropertiesOption;
if( !ui.matchCase->isChecked() )
options |= QRegularExpression::CaseInsensitiveOption;
QString pattern;
switch( syntax )
{
case QRegExp::FixedString:
pattern = QRegularExpression::escape( ui.filterLine->text() );
break;
case QRegExp::WildcardUnix:
pattern = wildcardsToRegexp( ui.filterLine->text() );
break;
default:
pattern = ui.filterLine->text();
break;
}
QRegularExpression regExp( pattern, options );
if( !regExp.isValid() )
{
gdWarning( "Invalid regexp pattern: %s\n", pattern.toUtf8().data() );
regExp.setPattern( QString::fromLatin1( "\1" ) );
}
QApplication::setOverrideCursor( Qt::WaitCursor );
model->setFilter(regExp);
proxy->setFilterRegularExpression( regExp );
proxy->sort( 0 );
QApplication::restoreOverrideCursor();
showHeadwordsNumber();
}
void DictHeadwords::itemClicked( const QModelIndex & index )
{
QVariant value = proxy->data( index, Qt::DisplayRole );
if ( value.canConvert< QString >() )
{
QString headword = value.toString();
emit headwordSelected( headword, dictId );
}
}
void DictHeadwords::autoApplyStateChanged( int state )
{
ui.applyButton->setEnabled( state == Qt::Unchecked );
}
void DictHeadwords::showHeadwordsNumber()
{
ui.headersNumber->setText( tr( "Unique headwords total: %1, filtered: %2" )
.arg( QString::number( model->totalCount() ), QString::number( proxy->rowCount() ) ) );
}
void DictHeadwords::saveHeadersToFile()
{
QString exportPath;
if( cfg.headwordsDialog.headwordsExportPath.isEmpty() )
exportPath = QDir::homePath();
else
{
exportPath = QDir::fromNativeSeparators( cfg.headwordsDialog.headwordsExportPath );
if( !QDir( exportPath ).exists() )
exportPath = QDir::homePath();
}
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save headwords to file" ),
exportPath,
tr( "Text files (*.txt);;All files (*.*)" ) );
if( fileName.size() == 0)
return;
QFile file( fileName );
if ( !file.open( QFile::WriteOnly | QIODevice::Text ) )
{
QMessageBox::critical( this, "GoldenDict", tr( "Can not open exported file" ) );
return;
}
cfg.headwordsDialog.headwordsExportPath = QDir::toNativeSeparators(
QFileInfo( fileName ).absoluteDir().absolutePath() );
QSet< QString > allHeadwords;
int headwordsNumber = model->totalCount();
//headwordsNumber*2 , read + write
QProgressDialog progress( tr( "Export headwords..." ), tr( "Cancel" ), 0, headwordsNumber*2, this );
progress.setWindowModality( Qt::WindowModal );
int totalCount=0;
for( int i = 0; i < headwordsNumber && i < model->wordCount(); ++i )
{
if( progress.wasCanceled() )
break;
progress.setValue( totalCount++ );
QVariant value = model->getRow( i );
if( !value.canConvert< QString >() )
continue;
allHeadwords.insert( value.toString() );
}
// continue to write the remaining headword
int nodeIndex = model->getCurrentIndex();
auto headwords = model->getRemainRows( nodeIndex );
while( !headwords.isEmpty() )
{
if( progress.wasCanceled() )
break;
allHeadwords.unite(headwords);
totalCount += headwords.size();
progress.setValue( totalCount );
headwords = model->getRemainRows( nodeIndex );
}
qDebug()<<model->getCurrentIndex();
// Write UTF-8 BOM
QByteArray line;
line.append( 0xEF ).append( 0xBB ).append( 0xBF );
file.write( line );
QList< QString > sortedWords = allHeadwords.values();
sortedWords.sort();
// Write headwords
for( auto const & word : sortedWords )
{
if( progress.wasCanceled() )
break;
progress.setValue( totalCount++ );
line = word.toUtf8();
line.replace( '\n', ' ' );
line.replace( '\r', ' ' );
line += "\n";
if( file.write( line ) != line.size() )
break;
}
file.close();
if( progress.wasCanceled() )
{
QMessageBox::warning( this, "GoldenDict", tr( "Export process is interrupted" ) );
gdWarning( "Headers export error: %s", file.errorString().toUtf8().data() );
}
else
{
//completed.
progress.setValue(headwordsNumber*2);
progress.hide();
QMessageBox::information( this, "GoldenDict", tr( "Export finished" ) );
}
}
void DictHeadwords::helpRequested()
{
MainWindow * mainWindow = qobject_cast< MainWindow * >( parentWidget() );
if( mainWindow )
mainWindow->showGDHelpForID( "Dictionary headwords" );
}
|