Kate
katespell.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "katespell.h"
00026 #include "katespell.moc"
00027
00028 #include "kateview.h"
00029 #include "katedocument.h"
00030
00031 #include <kaction.h>
00032 #include <kactioncollection.h>
00033 #include <kicon.h>
00034 #include <kstandardaction.h>
00035 #include <sonnet/dialog.h>
00036 #include <sonnet/backgroundchecker.h>
00037
00038 #include <kdebug.h>
00039 #include <kmessagebox.h>
00040
00041 KateSpell::KateSpell( KateView* view )
00042 : QObject( view )
00043 , m_view (view)
00044 , m_sonnetDialog(0)
00045 {
00046 }
00047
00048 KateSpell::~KateSpell()
00049 {
00050 if( m_sonnetDialog )
00051 {
00052 delete m_sonnetDialog;
00053 }
00054 }
00055
00056 void KateSpell::createActions( KActionCollection* ac )
00057 {
00058 ac->addAction( KStandardAction::Spelling, this, SLOT(spellcheck()) );
00059
00060 KAction *a = new KAction( i18n("Spelling (from cursor)..."), this);
00061 ac->addAction("tools_spelling_from_cursor", a );
00062 a->setIcon( KIcon( "tools-check-spelling" ) );
00063 a->setWhatsThis(i18n("Check the document's spelling from the cursor and forward"));
00064 connect( a, SIGNAL( triggered() ), this, SLOT(spellcheckFromCursor()) );
00065
00066 m_spellcheckSelection = new KAction( i18n("Spellcheck Selection..."), this );
00067 ac->addAction("tools_spelling_selection", m_spellcheckSelection);
00068 m_spellcheckSelection->setIcon( KIcon( "tools-check-spelling" ) );
00069 m_spellcheckSelection->setWhatsThis(i18n("Check spelling of the selected text"));
00070 connect( m_spellcheckSelection, SIGNAL( triggered() ), this, SLOT(spellcheckSelection()) );
00071 }
00072
00073 void KateSpell::updateActions ()
00074 {
00075 m_spellcheckSelection->setEnabled (m_view->selection ());
00076 }
00077
00078 void KateSpell::spellcheckFromCursor()
00079 {
00080 spellcheck( m_view->cursorPosition() );
00081 }
00082
00083 void KateSpell::spellcheckSelection()
00084 {
00085 spellcheck( m_view->selectionRange().start(), m_view->selectionRange().end() );
00086 }
00087
00088 void KateSpell::spellcheck()
00089 {
00090 spellcheck( KTextEditor::Cursor( 0, 0 ) );
00091 }
00092
00093 void KateSpell::spellcheck( const KTextEditor::Cursor &from, const KTextEditor::Cursor &to )
00094 {
00095 m_spellStart = from;
00096 m_spellEnd = to;
00097
00098 if ( to.line() == 0 && to.column() == 0 )
00099 {
00100 m_spellEnd = m_view->doc()->documentEnd();
00101 }
00102
00103 m_spellPosCursor = from;
00104 m_spellLastPos = 0;
00105
00106 if ( !m_sonnetDialog )
00107 {
00108 m_sonnetDialog = new Sonnet::Dialog(new Sonnet::BackgroundChecker(this), m_view);
00109
00110 connect(m_sonnetDialog,SIGNAL(done(const QString&)),this,SLOT(spellResult()));
00111
00112 connect(m_sonnetDialog,SIGNAL(replace(const QString&,int,const QString&)),
00113 this,SLOT(corrected(const QString&,int,const QString&)));
00114
00115 connect(m_sonnetDialog,SIGNAL(misspelling(const QString&,int)),
00116 this,SLOT(misspelling(const QString&,int)));
00117 }
00118
00119 m_sonnetDialog->setBuffer(m_view->doc()->text( KTextEditor::Range(m_spellStart, m_spellEnd) ));
00120 m_sonnetDialog->show();
00121 }
00122
00123 KTextEditor::Cursor KateSpell::locatePosition( int pos )
00124 {
00125 uint remains;
00126
00127 while ( m_spellLastPos < (uint)pos )
00128 {
00129 remains = pos - m_spellLastPos;
00130 uint l = m_view->doc()->lineLength( m_spellPosCursor.line() ) - m_spellPosCursor.column();
00131 if ( l > remains )
00132 {
00133 m_spellPosCursor.setColumn( m_spellPosCursor.column() + remains );
00134 m_spellLastPos = pos;
00135 }
00136 else
00137 {
00138 m_spellPosCursor.setLine( m_spellPosCursor.line() + 1 );
00139 m_spellPosCursor.setColumn(0);
00140 m_spellLastPos += l + 1;
00141 }
00142 }
00143
00144 return m_spellPosCursor;
00145 }
00146
00147 void KateSpell::misspelling( const QString& origword, int pos )
00148 {
00149 KTextEditor::Cursor cursor = locatePosition( pos );
00150
00151 m_view->setCursorPositionInternal (cursor, 1);
00152 m_view->setSelection( KTextEditor::Range(cursor, origword.length()) );
00153 }
00154
00155 void KateSpell::corrected( const QString& originalword, int pos, const QString& newword)
00156 {
00157 KTextEditor::Cursor cursor = locatePosition( pos );
00158
00159 m_view->doc()->replaceText( KTextEditor::Range(cursor, originalword.length()), newword );
00160 }
00161
00162 void KateSpell::spellResult()
00163 {
00164 m_view->clearSelection();
00165 }
00166
00167
00168
00169