backgroundengine.cpp

00001 
00021 #include "backgroundengine.h"
00022 
00023 #include "defaultdictionary.h"
00024 #include "dictionary.h"
00025 #include "filter.h"
00026 
00027 #include <kdebug.h>
00028 
00029 #include <qtimer.h>
00030 
00031 using namespace KSpell2;
00032 
00033 BackgroundEngine::BackgroundEngine( QObject *parent )
00034     : QObject( parent )
00035 {
00036     m_filter = Filter::defaultFilter();
00037     m_dict = 0;
00038 }
00039 
00040 BackgroundEngine::~BackgroundEngine()
00041 {
00042     delete m_dict; m_dict = 0;
00043 }
00044 
00045 void BackgroundEngine::setBroker( const Broker::Ptr& broker )
00046 {
00047     m_broker = broker;
00048     delete m_dict;
00049     m_defaultDict = m_broker->defaultDictionary();
00050     m_filter->setSettings( m_broker->settings() );
00051 }
00052 
00053 void BackgroundEngine::setText( const QString& text )
00054 {
00055     m_filter->setBuffer( text );
00056 }
00057 
00058 QString BackgroundEngine::text() const
00059 {
00060     return m_filter->buffer();
00061 }
00062 
00063 void BackgroundEngine::changeLanguage( const QString& lang )
00064 {
00065     delete m_dict;
00066     if ( lang.isEmpty() ) {
00067         m_dict = 0;
00068     } else {
00069         m_dict = m_broker->dictionary( lang );
00070     }
00071 }
00072 
00073 QString BackgroundEngine::language() const
00074 {
00075     if ( m_dict )
00076         return m_dict->language();
00077     else
00078         return m_defaultDict->language();
00079 }
00080 
00081 void BackgroundEngine::setFilter( Filter *filter )
00082 {
00083     QString oldText = m_filter->buffer();
00084     m_filter = filter;
00085     m_filter->setBuffer( oldText );
00086 }
00087 
00088 void BackgroundEngine::start()
00089 {
00090     QTimer::singleShot( 0, this, SLOT(checkNext()) );
00091 }
00092 
00093 void BackgroundEngine::stop()
00094 {
00095 }
00096 
00097 void BackgroundEngine::continueChecking()
00098 {
00099     QTimer::singleShot( 0, this, SLOT(checkNext()) );
00100 }
00101 
00102 void BackgroundEngine::checkNext()
00103 {
00104     Word w = m_filter->nextWord();
00105     if ( w.end ) {
00106         emit done();
00107         return;
00108     }
00109 
00110     Dictionary *dict = ( m_dict ) ? m_dict : static_cast<Dictionary*>( m_defaultDict );
00111 
00112     if ( !dict->check( w.word ) ) {
00113         //kdDebug()<<"found misspelling "<< w.word <<endl;
00114         emit misspelling( w.word, w.start );
00115         //wait for the handler. the parent will decide itself when to continue
00116     } else
00117         continueChecking();
00118 }
00119 
00120 bool BackgroundEngine::checkWord( const QString& word )
00121 {
00122     Dictionary *dict = ( m_dict ) ? m_dict : static_cast<Dictionary*>( m_defaultDict );
00123     return dict->check( word );
00124 }
00125 
00126 bool BackgroundEngine::addWord( const QString& word )
00127 {
00128     Dictionary *dict = ( m_dict ) ? m_dict : static_cast<Dictionary*>( m_defaultDict );
00129     return dict->addToPersonal( word );
00130 }
00131 
00132 QStringList BackgroundEngine::suggest( const QString& word )
00133 {
00134     Dictionary *dict = ( m_dict ) ? m_dict : static_cast<Dictionary*>( m_defaultDict );
00135     return dict->suggest( word );
00136 }
00137 
00138 #include "backgroundengine.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys