• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kate

katespell.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2008 Mirko Stocker <me@misto.ch>
00003    Copyright (C) 2004-2005 Anders Lund <anders@alweb.dk>
00004    Copyright (C) 2003 Clarence Dang <dang@kde.org>
00005    Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
00006    Copyright (C) 2001-2004 Christoph Cullmann <cullmann@kde.org>
00007    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00008    Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
00009 
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Library General Public
00012    License version 2 as published by the Free Software Foundation.
00013 
00014    This library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Library General Public License for more details.
00018 
00019    You should have received a copy of the GNU Library General Public License
00020    along with this library; see the file COPYING.LIB.  If not, write to
00021    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00022    Boston, MA 02110-1301, USA.
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 //END
00167 
00168 
00169 // kate: space-indent on; indent-width 2; replace-tabs on;

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal