2014-02-28 12:36:28 +00:00
|
|
|
/* This file is (c) 2014 Abs62
|
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "dictheadwords.hh"
|
|
|
|
#include "gddebug.hh"
|
2014-06-24 13:55:06 +00:00
|
|
|
#include "mainwindow.hh"
|
2014-02-28 12:36:28 +00:00
|
|
|
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QProgressDialog>
|
|
|
|
|
2019-03-25 15:08:52 +00:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include "wildcard.hh"
|
|
|
|
#include "gddebug.hh"
|
|
|
|
|
2014-02-28 12:36:28 +00:00
|
|
|
#define AUTO_APPLY_LIMIT 150000
|
|
|
|
|
|
|
|
DictHeadwords::DictHeadwords( QWidget *parent, Config::Class & cfg_,
|
2015-03-23 15:58:49 +00:00
|
|
|
Dictionary::Class * dict_ ) :
|
2014-02-28 12:36:28 +00:00
|
|
|
QDialog(parent)
|
|
|
|
, cfg( cfg_ )
|
|
|
|
, dict( dict_ )
|
2014-06-24 13:55:06 +00:00
|
|
|
, helpAction( this )
|
2014-02-28 12:36:28 +00:00
|
|
|
{
|
|
|
|
ui.setupUi( this );
|
|
|
|
|
2015-03-23 15:58:49 +00:00
|
|
|
bool fromMainWindow = parent->objectName() == "MainWindow";
|
|
|
|
|
|
|
|
if( fromMainWindow )
|
|
|
|
setAttribute( Qt::WA_DeleteOnClose, false );
|
|
|
|
|
2014-03-03 13:46:41 +00:00
|
|
|
setWindowFlags( windowFlags() & ~Qt::WindowContextHelpButtonHint );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
2019-03-19 14:36:07 +00:00
|
|
|
if( cfg.headwordsDialog.headwordsDialogGeometry.size() > 0 )
|
|
|
|
restoreGeometry( cfg.headwordsDialog.headwordsDialogGeometry );
|
|
|
|
|
2014-02-28 12:36:28 +00:00
|
|
|
ui.searchModeCombo->addItem( tr( "Text" ), QRegExp::FixedString );
|
2014-03-14 12:38:59 +00:00
|
|
|
ui.searchModeCombo->addItem( tr( "Wildcards" ), QRegExp::WildcardUnix );
|
2014-02-28 12:36:28 +00:00
|
|
|
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 QStringListModel( this );
|
|
|
|
model->setStringList( headers );
|
|
|
|
|
|
|
|
proxy = new QSortFilterProxyModel( this );
|
|
|
|
|
|
|
|
proxy->setSourceModel( model );
|
2014-03-01 13:41:05 +00:00
|
|
|
|
|
|
|
proxy->setSortCaseSensitivity( Qt::CaseInsensitive );
|
|
|
|
proxy->setSortLocaleAware( true );
|
2014-03-18 13:44:41 +00:00
|
|
|
proxy->setDynamicSortFilter( false );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
|
|
|
ui.headersListView->setModel( proxy );
|
2014-03-01 13:11:14 +00:00
|
|
|
ui.headersListView->setEditTriggers( QAbstractItemView::NoEditTriggers );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
|
|
|
// very important call, for performance reasons:
|
|
|
|
ui.headersListView->setUniformItemSizes( true );
|
|
|
|
|
|
|
|
delegate = new WordListItemDelegate( ui.headersListView->itemDelegate() );
|
|
|
|
if( delegate )
|
|
|
|
ui.headersListView->setItemDelegate( delegate );
|
|
|
|
|
2014-03-14 12:38:59 +00:00
|
|
|
ui.autoApply->setChecked( cfg.headwordsDialog.autoApply );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
2014-03-03 13:46:41 +00:00
|
|
|
connect( this, SIGNAL( finished( int ) ), this, SLOT( savePos() ) );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
2015-03-23 15:58:49 +00:00
|
|
|
if( !fromMainWindow )
|
|
|
|
{
|
|
|
|
ui.helpButton->hide();
|
|
|
|
connect( this, SIGNAL( closeDialog() ), this, SLOT( accept() ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
connect( ui.helpButton, SIGNAL( clicked() ),
|
|
|
|
this, SLOT( helpRequested() ) );
|
|
|
|
|
|
|
|
helpAction.setShortcut( QKeySequence( "F1" ) );
|
|
|
|
helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
|
|
|
|
|
|
|
|
connect( &helpAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( helpRequested() ) );
|
|
|
|
|
|
|
|
addAction( &helpAction );
|
|
|
|
}
|
|
|
|
|
2014-02-28 12:36:28 +00:00
|
|
|
connect( ui.OKButton, SIGNAL( clicked( bool ) ), this, SLOT( okButtonClicked() ) );
|
|
|
|
connect( ui.exportButton, SIGNAL( clicked( bool ) ), this, SLOT( exportButtonClicked() ) );
|
|
|
|
connect( ui.applyButton, SIGNAL( clicked( bool ) ), this, SLOT( filterChanged() ) );
|
|
|
|
|
|
|
|
connect( ui.autoApply, SIGNAL( stateChanged( int ) ),
|
|
|
|
this, SLOT( autoApplyStateChanged( int ) ) );
|
|
|
|
|
|
|
|
connect( ui.filterLine, SIGNAL( textChanged( QString ) ),
|
|
|
|
this, SLOT( filterChangedInternal() ) );
|
|
|
|
connect( ui.searchModeCombo, SIGNAL( currentIndexChanged( int ) ),
|
|
|
|
this, SLOT( filterChangedInternal() ) );
|
|
|
|
connect( ui.matchCase, SIGNAL( stateChanged( int ) ),
|
|
|
|
this, SLOT( filterChangedInternal() ) );
|
|
|
|
|
|
|
|
connect( ui.headersListView, SIGNAL( clicked( QModelIndex ) ),
|
|
|
|
this, SLOT( itemClicked( QModelIndex ) ) );
|
|
|
|
|
|
|
|
connect( proxy, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ),
|
|
|
|
this, SLOT( showHeadwordsNumber() ) );
|
|
|
|
|
2014-03-04 17:38:47 +00:00
|
|
|
ui.headersListView->installEventFilter( this );
|
|
|
|
|
2014-03-03 13:46:41 +00:00
|
|
|
setup( dict_ );
|
2014-02-28 12:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DictHeadwords::~DictHeadwords()
|
|
|
|
{
|
|
|
|
if( delegate )
|
2019-03-20 14:49:39 +00:00
|
|
|
delegate->deleteLater();
|
2014-02-28 12:36:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 13:46:41 +00:00
|
|
|
void DictHeadwords::setup( Dictionary::Class *dict_ )
|
|
|
|
{
|
|
|
|
QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
|
|
|
|
dict = dict_;
|
|
|
|
|
|
|
|
setWindowTitle( QString::fromUtf8( dict->getName().c_str() ) );
|
|
|
|
|
|
|
|
headers.clear();
|
|
|
|
model->setStringList( headers );
|
|
|
|
|
|
|
|
dict->getHeadwords( headers );
|
|
|
|
model->setStringList( headers );
|
|
|
|
|
|
|
|
proxy->sort( 0 );
|
|
|
|
filterChanged();
|
|
|
|
|
2014-03-14 12:38:59 +00:00
|
|
|
if( headers.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() );
|
|
|
|
|
2014-11-14 16:45:06 +00:00
|
|
|
setWindowIcon( dict->getIcon() );
|
|
|
|
|
2014-11-17 14:37:19 +00:00
|
|
|
dictId = QString( dict->getId().c_str() );
|
|
|
|
|
2014-03-03 13:46:41 +00:00
|
|
|
QApplication::restoreOverrideCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictHeadwords::savePos()
|
2014-02-28 12:36:28 +00:00
|
|
|
{
|
|
|
|
cfg.headwordsDialog.searchMode = ui.searchModeCombo->currentIndex();
|
|
|
|
cfg.headwordsDialog.matchCase = ui.matchCase->isChecked();
|
|
|
|
|
|
|
|
if( headers.size() <= AUTO_APPLY_LIMIT )
|
|
|
|
cfg.headwordsDialog.autoApply = ui.autoApply->isChecked();
|
|
|
|
|
|
|
|
cfg.headwordsDialog.headwordsDialogGeometry = saveGeometry();
|
|
|
|
}
|
|
|
|
|
2014-03-04 17:38:47 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2014-02-28 12:36:28 +00:00
|
|
|
void DictHeadwords::okButtonClicked()
|
|
|
|
{
|
2014-03-03 13:46:41 +00:00
|
|
|
savePos();
|
|
|
|
closeDialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictHeadwords::reject()
|
|
|
|
{
|
|
|
|
savePos();
|
|
|
|
closeDialog();
|
2014-02-28 12:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictHeadwords::exportButtonClicked()
|
|
|
|
{
|
|
|
|
saveHeadersToFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictHeadwords::filterChangedInternal()
|
|
|
|
{
|
|
|
|
// emit signal in async manner, to avoid UI slowdown
|
|
|
|
if( ui.autoApply->isChecked() )
|
|
|
|
QTimer::singleShot( 100, this, SLOT( filterChanged() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictHeadwords::filterChanged()
|
|
|
|
{
|
|
|
|
QRegExp::PatternSyntax syntax =
|
|
|
|
QRegExp::PatternSyntax( ui.searchModeCombo->itemData(
|
|
|
|
ui.searchModeCombo->currentIndex()).toInt() );
|
|
|
|
|
2019-03-25 15:08:52 +00:00
|
|
|
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 );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
2019-03-25 15:08:52 +00:00
|
|
|
if( !regExp.isValid() )
|
|
|
|
{
|
|
|
|
gdWarning( "Invalid regexp pattern: %s\n", pattern.toUtf8().data() );
|
|
|
|
regExp.setPattern( QString::fromLatin1( "\1" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QApplication::setOverrideCursor( Qt::WaitCursor );
|
|
|
|
|
|
|
|
proxy->setFilterRegularExpression( regExp );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
2019-03-25 15:08:52 +00:00
|
|
|
|
2014-03-18 13:44:41 +00:00
|
|
|
proxy->sort( 0 );
|
2014-02-28 12:36:28 +00:00
|
|
|
|
2014-03-01 18:43:31 +00:00
|
|
|
QApplication::restoreOverrideCursor();
|
2014-02-28 12:36:28 +00:00
|
|
|
|
|
|
|
showHeadwordsNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictHeadwords::itemClicked( const QModelIndex & index )
|
|
|
|
{
|
|
|
|
QVariant value = proxy->data( index, Qt::DisplayRole );
|
|
|
|
if ( value.canConvert< QString >() )
|
2014-03-17 17:23:36 +00:00
|
|
|
{
|
|
|
|
QString headword = value.toString();
|
2014-11-17 14:37:19 +00:00
|
|
|
emit headwordSelected( headword, dictId );
|
2014-03-17 17:23:36 +00:00
|
|
|
}
|
2014-02-28 12:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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( headers.size() ) )
|
|
|
|
.arg( 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;
|
|
|
|
|
|
|
|
cfg.headwordsDialog.headwordsExportPath = QDir::toNativeSeparators(
|
|
|
|
QFileInfo( fileName ).absoluteDir().absolutePath() );
|
|
|
|
QFile file( fileName );
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if ( !file.open( QFile::WriteOnly | QIODevice::Text ) )
|
|
|
|
break;
|
|
|
|
|
|
|
|
int headwordsNumber = proxy->rowCount();
|
|
|
|
|
|
|
|
// Setup progress dialog
|
|
|
|
int n = headwordsNumber;
|
|
|
|
int step = 1;
|
|
|
|
while( n > 1000 )
|
|
|
|
{
|
|
|
|
step *= 10;
|
|
|
|
n /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
QProgressDialog progress( tr( "Export headwords..."), tr( "Cancel" ),
|
|
|
|
0, n, this );
|
|
|
|
progress.setWindowModality( Qt::WindowModal );
|
|
|
|
|
|
|
|
// Write UTF-8 BOM
|
|
|
|
QByteArray line;
|
|
|
|
line.append( 0xEF ).append( 0xBB ).append( 0xBF );
|
|
|
|
if ( file.write( line ) != line.size() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Write headwords
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for( i = 0; i < headwordsNumber; ++i )
|
|
|
|
{
|
|
|
|
if( i % step == 0 )
|
|
|
|
progress.setValue( i / step );
|
|
|
|
|
|
|
|
if( progress.wasCanceled() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
QVariant value = proxy->data( proxy->index( i, 0 ) );
|
|
|
|
if( !value.canConvert< QString >() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
line = value.toString().toUtf8();
|
|
|
|
|
|
|
|
line.replace( '\n', ' ' );
|
|
|
|
line.replace( '\r', ' ' );
|
|
|
|
|
|
|
|
line += "\n";
|
|
|
|
|
|
|
|
if ( file.write( line ) != line.size() )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i < headwordsNumber && !progress.wasCanceled() )
|
|
|
|
break;
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gdWarning( "Headers export error: %s", file.errorString().toUtf8().data() );
|
|
|
|
file.close();
|
|
|
|
}
|
2014-06-24 13:55:06 +00:00
|
|
|
|
|
|
|
void DictHeadwords::helpRequested()
|
|
|
|
{
|
|
|
|
MainWindow * mainWindow = qobject_cast< MainWindow * >( parentWidget() );
|
|
|
|
if( mainWindow )
|
|
|
|
mainWindow->showGDHelpForID( "Dictionary headwords" );
|
|
|
|
}
|