backgroundthread.cpp

00001 
00021 #include "backgroundthread.h"
00022 
00023 #include "threadevents.h"
00024 #include "broker.h"
00025 #include "filter.h"
00026 #include "dictionary.h"
00027 
00028 #include <kdebug.h>
00029 #include <qapplication.h>
00030 
00031 using namespace KSpell2;
00032 
00033 BackgroundThread::BackgroundThread()
00034     : QThread(), m_broker( 0 ), m_dict( 0 )
00035 {
00036     m_recv   = 0;
00037     m_filter = Filter::defaultFilter();
00038     m_done   = false;
00039 }
00040 
00041 void BackgroundThread::setReceiver( QObject *recv )
00042 {
00043     m_recv = recv;
00044 }
00045 
00046 void BackgroundThread::setBroker( const Broker::Ptr& broker )
00047 {
00048     stop();
00049     m_broker = broker;
00050     delete m_dict;
00051     m_dict   = m_broker->dictionary();
00052     m_filter->restart();
00053 }
00054 
00055 QStringList BackgroundThread::suggest( const QString& word ) const
00056 {
00057     return m_dict->suggest( word );
00058 }
00059 
00060 void BackgroundThread::run()
00061 {
00062     m_mutex.lock();
00063     m_done = false;
00064     for ( Word w = m_filter->nextWord(); !m_done && !w.end;
00065           w = m_filter->nextWord() ) {
00066         if ( !m_dict->check( w.word ) && !m_done ) {
00067             MisspellingEvent *event = new MisspellingEvent( w.word, w.start );
00068             QApplication::postEvent( m_recv, event );
00069         }
00070     }
00071     m_mutex.unlock();
00072     FinishedCheckingEvent *event = new FinishedCheckingEvent();
00073     QApplication::postEvent( m_recv, event );
00074 }
00075 
00076 void BackgroundThread::setText( const QString& buff )
00077 {
00078     stop();
00079     m_mutex.lock();
00080     m_filter->setBuffer( buff );
00081     m_mutex.unlock();
00082     start();
00083 }
00084 
00085 void BackgroundThread::setFilter( Filter *filter )
00086 {
00087     stop();
00088     m_mutex.lock();
00089     Filter *oldFilter = m_filter;
00090     m_filter = filter;
00091     if ( oldFilter ) {
00092         m_filter->setBuffer( oldFilter->buffer() );
00093         oldFilter->setBuffer( QString::null );
00094     }
00095     m_mutex.unlock();
00096     start();
00097 }
00098 
00099 void BackgroundThread::changeLanguage( const QString& lang )
00100 {
00101     stop();
00102     m_mutex.lock();
00103     delete m_dict;
00104     m_dict = m_broker->dictionary( lang );
00105     m_filter->restart();
00106     m_mutex.unlock();
00107     start();
00108 }
00109 
00110 void BackgroundThread::stop()
00111 {
00112     //### maybe terminate() would be better than using m_done
00113     m_done = true;
00114     wait();
00115 }
KDE Home | KDE Accessibility Home | Description of Access Keys