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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "SpellDialog.h"
#include "Debug.h"
#include "DictionarySelectionButton.h"
#include "FilterSelectionButton.h"
#include "GridLayout.h"
#include "InformationDialog.h"
#include "LineEditor.h"
#include "SpellInterface.h"
#include "TextEditor.h"
#include "TreeView.h"
#include "Util.h"
#include "XmlOptions.h"
#include <QApplication>
#include <QHeaderView>
#include <QLayout>
#include <QPushButton>
#include <QTextCursor>
namespace SpellCheck
{
//_______________________________________________
SpellDialog::SpellDialog( TextEditor* parent, bool readOnly ):
CustomDialog( parent, CloseButton|Separator ),
editor_( parent ),
readOnly_( readOnly )
{
Debug::Throw( "SpellCheck::SpellDialog::SpellDialog.\n" );
// window title
setWindowTitle( QString( readOnly ? tr( "Spell Check (read-only) - %1" ) : tr( "Spell Check - %1" ) ).arg( qApp->applicationName() ) );
setOptionName( "SPELL_DIALOG" );
// horizontal layout for suggestions and buttons
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->setMargin(0);
hLayout->setSpacing(10);
mainLayout().addLayout( hLayout, 1 );
// insert left vertical box
QVBoxLayout *vLayout = new QVBoxLayout;
vLayout->setMargin( 0 );
vLayout->setSpacing(5);
hLayout->addLayout( vLayout );
// grid for text editors
GridLayout *gridLayout = new GridLayout;
gridLayout->setMargin( 0 );
gridLayout->setSpacing(5);
gridLayout->setMaxCount( 2 );
gridLayout->setColumnAlignment( 0, Qt::AlignRight|Qt::AlignVCenter );
vLayout->addLayout( gridLayout, 0 );
// misspelled word line editor
gridLayout->addWidget( new QLabel( tr( "Misspelled word:" ), this ) );
gridLayout->addWidget( sourceLabel_ = new QLabel( this ) );
sourceLabel_->setTextInteractionFlags( Qt::TextSelectableByMouse );
// replacement line editor
gridLayout->addWidget( new QLabel( tr( "Replace with:" ), this ) );
gridLayout->addWidget( replaceEditor_ = new LineEditor( this ) );
if( readOnly ) replaceEditor_->setEnabled( false );
gridLayout->setColumnStretch( 1, 1 );
// suggestions
vLayout->addWidget( list_ = new TreeView( this ) );
list_->setModel( &model_ );
list_->header()->hide();
// grid layout for dictionary and filter
gridLayout = new GridLayout;
gridLayout->setMargin( 0 );
gridLayout->setSpacing(5);
gridLayout->setMaxCount( 3 );
gridLayout->setColumnAlignment( 0, Qt::AlignRight|Qt::AlignVCenter );
vLayout->addLayout( gridLayout, 0 );
// dictionaries combobox
QLabel* label;
gridLayout->addWidget( label = new QLabel( tr( "Dictionary:" ), this ) );
gridLayout->addWidget( dictionariesComboBox_ = new QComboBox( this ) );
label->setBuddy( dictionariesComboBox_ );
_updateDictionaries();
connect( dictionariesComboBox_, SIGNAL(activated(QString)), SLOT(_selectDictionary(QString)) );
// configuration
DictionarySelectionButton* dictionarySelectionButton;
gridLayout->addWidget( dictionarySelectionButton = new DictionarySelectionButton( this ) );
connect( dictionarySelectionButton, SIGNAL(modified()), SLOT(_updateDictionaries()) );
// filter combobox
gridLayout->addWidget( filterLabel_ = new QLabel( tr( "Filter:" ), this ), 1, 0, 1, 1 );
gridLayout->addWidget( filtersComboBox_ = new QComboBox( this ), 1, 1, 1, 1 );
_updateFilters();
filterLabel_->setBuddy( dictionariesComboBox_ );
connect( filtersComboBox_, SIGNAL(activated(QString)), SLOT(_selectFilter(QString)) );
// configuration
FilterSelectionButton* filterSelectionButton;
gridLayout->addWidget( filterSelectionButton = new FilterSelectionButton( this ) );
connect( filterSelectionButton, SIGNAL(modified()), SLOT(_updateFilters()) );
// stretch
gridLayout->setColumnStretch( 1, 1 );
// right vbox
vLayout = new QVBoxLayout;
vLayout->setMargin( 0 );
vLayout->setSpacing(5);
hLayout->addLayout( vLayout );
// add word button
vLayout->addWidget( addWordButton_ = new QPushButton( tr( "<< Add to Dictionary" ), this ) );
addWordButton_->setToolTip( tr( "Add misspelled word to the current dictionnary" ) );
connect( addWordButton_, SIGNAL(clicked()), SLOT(_addWord()) );
// check word button
vLayout->addWidget( checkWordButton_ = new QPushButton( tr( "Suggest" ), this ) );
checkWordButton_->setToolTip( tr( "Suggest correct spelling for the word to use as a replacement" ) );
connect( checkWordButton_, SIGNAL(clicked()), SLOT(_checkWord()) );
// recheck button
{
QPushButton* button;
vLayout->addWidget( button = new QPushButton( tr( "Recheck Page" ), this ) );
connect( button, SIGNAL(clicked()), SLOT(_restart()) );
}
QFrame* frame;
vLayout->addWidget( frame = new QFrame(this) );
frame->setFrameShape( QFrame::HLine );
// replace button
vLayout->addWidget( replaceButton_ = new QPushButton( tr( "Replace" ), this ) );
connect( replaceButton_, SIGNAL(clicked()), SLOT(_replace()) );
if( readOnly ) replaceButton_->setVisible( false );
// replace button
vLayout->addWidget( replaceAllButton_ = new QPushButton( tr( "Replace All" ), this ) );
connect( replaceAllButton_, SIGNAL(clicked()), SLOT(_replaceAll()) );
if( readOnly ) replaceAllButton_->setVisible( false );
vLayout->addWidget( frame = new QFrame(this) );
frame->setFrameShape( QFrame::HLine );
// ignore button
vLayout->addWidget( ignoreButton_ = new QPushButton( tr( "Ignore" ), this ) );
connect( ignoreButton_, SIGNAL(clicked()), SLOT(_ignore()) );
// ignore button
vLayout->addWidget( ignoreAllButton_ = new QPushButton( tr( "Ignore All" ), this ) );
connect( ignoreAllButton_, SIGNAL(clicked()), SLOT(_ignoreAll()) );
vLayout->addStretch( 1 );
// connections
connect( list_->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(_selectSuggestion(QModelIndex)) );
connect( list_->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(_updateButtons()) );
if( !readOnly ) { connect( list_, SIGNAL(activated(QModelIndex)), SLOT(_replace(QModelIndex)) ); }
// set text
// check Editor seletion
int indexBegin( 0 );
int indexEnd( editor_->toPlainText().length() );
if( editor_->textCursor().hasSelection() )
{
indexBegin = editor_->textCursor().anchor();
indexEnd = editor_->textCursor().position();
if( indexBegin > indexEnd ) std::swap( indexBegin, indexEnd );
}
// asign text
if( !interface_.setText( editor_->toPlainText(), indexBegin, indexEnd ) )
{ InformationDialog( this, interface_.error() ).exec(); }
// set TextEditor as ReadOnly
readOnlyEditor_ = editor_->isReadOnly();
editor_->setReadOnly( true );
// connection
connect( &closeButton(), SIGNAL(clicked()), SLOT(_saveWordList()) );
connect( &closeButton(), SIGNAL(clicked()), SLOT(_restoreReadOnly()) );
Debug::Throw( "SpellCheck::SpellDialog::SpellDialog - done.\n" );
}
//__________________________________________
void SpellDialog::showFilter( bool value )
{
Debug::Throw( "SpellCheck::SpellDialog::showFilter.\n" );
if( value ){
filterLabel_->show();
filtersComboBox_->show();
} else {
filterLabel_->hide();
filtersComboBox_->hide();
}
}
//____________________________________________________
bool SpellDialog::setDictionary( const QString& dictionary )
{
Debug::Throw( "SpellCheck::SpellDialog::setDictionary.\n" );
// find matching index
int index( dictionariesComboBox_->findText( dictionary ) );
if( index < 0 )
{
InformationDialog( this, QString( tr( "Invalid dictionary: %1" ) ).arg( dictionary ) ).exec();
return false;
}
// update interface
if( !interface_.setDictionary( dictionary ) )
{
InformationDialog( this, interface_.error() ).exec();
return false;
}
// select index
dictionariesComboBox_->setCurrentIndex( index );
return true;
}
//____________________________________________________
bool SpellDialog::setFilter( const QString& filter )
{
Debug::Throw( "SpellCheck::SpellDialog::setFilter.\n" );
// find matching index
int index( filtersComboBox_->findText( filter ) );
if( index < 0 )
{
InformationDialog( this, QString( tr( "Invalid filter: %1" ) ).arg( filter ) ).exec();
return false;
}
// update interface
if( !interface_.setFilter( filter ) )
{
InformationDialog( this, interface_.error() ).exec();
return false;
}
// select index
filtersComboBox_->setCurrentIndex( index );
return true;
}
//___________________________________________
void SpellDialog::_updateDictionaries()
{
Debug::Throw( "SpellCheck::SpellDialog::_updateDictionaries.\n" );
// store selection
const QString selection( dictionariesComboBox_->currentText() );
// read list of disabled dictionaries
const QStringList disabledDictionaries( QString( XmlOptions::get().raw( "SPELLCHECK_DISABLED_DICTIONARIES" ) ).split( " " ) );
// clear combobox
dictionariesComboBox_->clear();
// get dictionary list and populate combobox
for( const auto& dictionary:interface_.dictionaries() )
{ if( !disabledDictionaries.contains( dictionary ) ) dictionariesComboBox_->addItem( dictionary ); }
// restore default value
dictionariesComboBox_->setCurrentIndex( dictionariesComboBox_->findText( selection ) );
}
//___________________________________________
void SpellDialog::_updateFilters()
{
Debug::Throw( "SpellCheck::SpellDialog::_updateFilters.\n" );
// store selection
const QString selection( filtersComboBox_->currentText() );
// read list of disabled filters
const QStringList disabledFilters( QString( XmlOptions::get().raw( "SPELLCHECK_DISABLED_FILTERS" ) ).split( " " ) );
// clear combobox
filtersComboBox_->clear();
// get dictionary list and populate combobox
for( const auto& filter:interface_.filters() )
{ if( !disabledFilters.contains( filter ) ) filtersComboBox_->addItem( filter ); }
// restore default value
filtersComboBox_->setCurrentIndex( filtersComboBox_->findText( selection ) );
}
//____________________________________________________
void SpellDialog::_selectSuggestion( const QModelIndex& index )
{
Debug::Throw( "SpellCheck::SpellDialog::_selectSuggestion.\n" );
if( index.isValid() ) { replaceEditor_->setText( model_.get( index ) ); }
}
//____________________________________________________
void SpellDialog::_selectDictionary( const QString& dictionary )
{
Debug::Throw( "SpellCheck::SpellDialog::_SelectDictionary.\n" );
// see if changed
if( interface_.dictionary() == dictionary ) return;
// try update interface
if( !interface_.setDictionary( dictionary ) )
{
InformationDialog( this, interface_.error() ).exec();
return;
}
// emit signal
emit dictionaryChanged( interface_.dictionary() );
// restart
_restart();
}
//____________________________________________________
void SpellDialog::_selectFilter( const QString& filter )
{
Debug::Throw( "SpellCheck::SpellDialog::_SelectFilter.\n" );
// see if changed
if( interface_.filter() == filter ) return;
// try update interface
if( !interface_.setFilter( filter ) )
{
InformationDialog( this, interface_.error() ).exec();
return;
}
// emit signal
emit filterChanged( interface_.filter() );
// restart
_restart();
}
//____________________________________________________
void SpellDialog::_restart()
{
Debug::Throw( "SpellCheck::SpellDialog::_restart.\n" );
if( !interface_.reset() )
{
InformationDialog( this, interface_.error() ).exec();
return;
}
nextWord();
}
//____________________________________________________
void SpellDialog::_addWord()
{
Debug::Throw( "SpellCheck::SpellDialog::_addWord.\n" );
if( !interface_.addWord( interface_.word() ) )
{
InformationDialog( this, interface_.error() ).exec();
return;
}
emit ignoreWord( interface_.word() );
emit needUpdate();
// get next mispelled word
nextWord();
}
//____________________________________________________
void SpellDialog::_checkWord()
{
Debug::Throw( "SpellCheck::SpellDialog::_checkWord.\n" );
// retrieve check word
_displayWord( replaceEditor_->text() );
return;
}
//____________________________________________________
void SpellDialog::_ignore()
{
Debug::Throw( "SpellCheck::SpellDialog::_ignore.\n" );
nextWord();
}
//____________________________________________________
void SpellDialog::_ignoreAll()
{
Debug::Throw( "SpellCheck::SpellDialog::_ignoreAll.\n" );
interface_.ignoreWord( sourceLabel_->text() );
_ignore();
}
//____________________________________________________
void SpellDialog::_replace( const QModelIndex& index )
{
Debug::Throw( "SpellCheck::SpellDialog::_replace.\n" );
QModelIndex local( index );
if( !local.isValid() ) local = list_->selectionModel()->currentIndex();
if( !local.isValid() ) return;
const QString word( model_.get( local ) );
if( interface_.replace( word ) ) _replaceSelection( word );
// parse next word
nextWord();
}
//____________________________________________________
void SpellDialog::_replaceAll()
{
Debug::Throw( "SpellCheck::SpellDialog::_replaceAll.\n" );
QString oldWord( sourceLabel_->text() );
QString newWord( replaceEditor_->text() );
replacedWords_.insert( oldWord, newWord );
_replace();
}
//____________________________________________________
void SpellDialog::_updateButtons()
{
Debug::Throw( "SpellCheck::SpellDialog::_updateButtons.\n" );
const bool hasSelection( list_->selectionModel()->hasSelection() );
const bool hasWord( !sourceLabel_->text().isEmpty() );
addWordButton_->setEnabled( hasWord );
checkWordButton_->setEnabled( hasWord );
ignoreButton_->setEnabled( hasWord );
ignoreAllButton_->setEnabled( hasWord );
replaceButton_->setEnabled( hasWord && hasSelection && !readOnly_ );
replaceAllButton_->setEnabled( hasWord && hasSelection && !readOnly_ );
}
//____________________________________________________
void SpellDialog::nextWord()
{
Debug::Throw( "SpellCheck::SpellDialog::nextWord.\n" );
bool accepted( false );
QString word;
while( !accepted )
{
// get next word from interface
if( !interface_.nextWord() )
{
InformationDialog( this, interface_.error() ).exec();
return;
}
// check for completed spelling
word = interface_.word();
if( word.isEmpty() )
{
Debug::Throw() << "SpellCheck::SpellDialog::NextWord - empty word " << endl;
break;
}
Debug::Throw() << "SpellCheck::SpellDialog::NextWord - word: " << word << endl;
// see if word is in ignore list
if( interface_.isWordIgnored( word ) ) continue;
// see if word is in replace all list
if( replacedWords_.find( word ) != replacedWords_.end() )
{
// automatic replacement
QHash< QString, QString >::iterator iter = replacedWords_.find( word );
_updateSelection( interface_.position() + interface_.offset(), word.size() );
_replaceSelection( iter.value() );
interface_.replace( iter.value() );
continue;
}
accepted = true;
}
if( accepted )
{
_updateSelection( interface_.position() + interface_.offset(), word.size() );
_displayWord( word );
} else {
// spelling completed. Clear everything
sourceLabel_->clear();
replaceEditor_->clear();
model_.clear();
_updateButtons();
}
return;
}
//__________________________________________
void SpellDialog::closeEvent( QCloseEvent *e )
{
Debug::Throw( 0, "SpellCheck::SpellDialog::closeEvent.\n" );
_restoreReadOnly();
_saveWordList();
QDialog::closeEvent( e );
}
//__________________________________________
void SpellDialog::_saveWordList()
{ interface_.saveWordList(); }
//__________________________________________
void SpellDialog::_restoreReadOnly()
{ editor_->setReadOnly( readOnlyEditor_ ); }
//_________________________________________________________________
void SpellDialog::_updateSelection( int index, int length )
{
Debug::Throw() << "SpellCheck::SpellDialog::_updateSelection - index=" << index << endl;
QTextCursor cursor( editor_->textCursor() );
cursor.setPosition( index, QTextCursor::MoveAnchor );
cursor.setPosition( index+length, QTextCursor::KeepAnchor );
editor_->setTextCursor( cursor );
}
//_________________________________________________________________
void SpellDialog::_replaceSelection( const QString& word )
{
Debug::Throw() << "SpellCheck::SpellDialog::_ReplaceSelection - word: " << word << endl;
editor_->setReadOnly( false );
editor_->textCursor().insertText( word );
editor_->setReadOnly( true );
}
//_________________________________________________
void SpellDialog::_displayWord( const QString& word )
{
Debug::Throw( "SpellCheck::SpellDialog::_displayWord.\n" );
// set text in line_editor
sourceLabel_->setText( word );
replaceEditor_->setText( word );
// clear list of suggestions
model_.clear();
Model::List suggestions( interface_.suggestions( word ) );
model_.add( suggestions );
// select the first suggestion, if any
if( !suggestions.isEmpty() )
{
QModelIndex index( model_.index( suggestions.front() ) );
list_->selectionModel()->select( index, QItemSelectionModel::Select|QItemSelectionModel::Rows );
list_->selectionModel()->setCurrentIndex( index, QItemSelectionModel::Current );
}
}
//_________________________________________________
QVariant SpellDialog::Model::data( const QModelIndex &index, int role) const
{
// check index
if( !contains( index ) ) return QVariant();
// retrieve associated file info
const QString& word( get(index) );
// return text associated to file and column
if( role == Qt::DisplayRole ) return word;
else return QVariant();
}
//__________________________________________________________________
QVariant SpellDialog::Model::headerData(int section, Qt::Orientation orientation, int role) const
{ return QVariant(); }
}
|