[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH] Support for multiple languages in KDictSpellingHighlighter
From:       Ingo =?iso-8859-15?q?Kl=F6cker?= <kloecker () kde ! org>
Date:       2003-10-19 21:23:19
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


Hi,

the patches do the following.

ksconfig.diff:
KSpellConfig::fillDicts( QComboBox *box ) fills a combo box with the 
available dictionaries. But it doesn't set the currently used 
dictionary as current item of the combo box. This patch fixes this bug. 
It's a bug because there is no other way to set the correct current 
item because the user of KSpellConfig only knows about the human 
readable dictionary names but not about the internal dictionary names. 
Without this fix fillDicts is completely useless.

ksyntaxhighlighter.diff:
Currently KDictSpellingHighlighter (the class that's for example used in 
KMail for as-you-type spell checking) has a static cache for already 
checked words and uses the global KSpellConfig. In order to be able to 
compose two messages in different languages at the same time in KMail 
it's necessary to add support for a custom KSpellConfig to 
KDictSpellingHighlighter. Furthermore the static cache is useless for 
multiple languages. Therefore this patch makes KDictSpellingHighlighter 
use a non-static cache in case a custom KSpellConfig is provided.

With those two patches in kdelibs it was quite easy to add a language 
selection box to KMail's composer (which is a must for multilingual 
users).

Regards,
Ingo

P.S.: If the signature on this message is broken then it's most likely 
mailmans fault.

["ksyntaxhighlighter.diff" (text/x-diff)]

Index: ksyntaxhighlighter.h
===================================================================
RCS file: /home/kde/kdelibs/kdeui/ksyntaxhighlighter.h,v
retrieving revision 1.6
diff -u -3 -p -r1.6 ksyntaxhighlighter.h
--- ksyntaxhighlighter.h	18 Oct 2003 19:22:42 -0000	1.6
+++ ksyntaxhighlighter.h	19 Oct 2003 20:36:59 -0000
@@ -32,6 +32,7 @@
 class QAccel;
 class QTimer;
 class KSpell;
+class KSpellConfig;
 
 class KSyntaxHighlighter : public QSyntaxHighlighter
 {
@@ -95,7 +96,8 @@ public:
 			      const QColor& QuoteColor0 = black,
 			      const QColor& QuoteColor1 = QColor( 0x00, 0x80, 0x00 ),
 			      const QColor& QuoteColor2 = QColor( 0x00, 0x70, 0x00 ),
-			      const QColor& QuoteColor3 = QColor( 0x00, 0x60, 0x00 ) );
+			      const QColor& QuoteColor3 = QColor( 0x00, 0x60, 0x00 ),
+                              KSpellConfig *spellConfig = 0 );
     ~KDictSpellingHighlighter();
 
     virtual bool isMisspelled( const QString &word );
@@ -138,6 +140,7 @@ protected slots:
     void slotDictionaryChanged();
     void slotSpellReady( KSpell *spell );
     void slotAutoDetection();
+    void slotLocalSpellConfigChanged();
 
 private:
     class KDictSpellingHighlighterPrivate;
Index: ksyntaxhighlighter.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/ksyntaxhighlighter.cpp,v
retrieving revision 1.12
diff -u -3 -p -r1.12 ksyntaxhighlighter.cpp
--- ksyntaxhighlighter.cpp	18 Oct 2003 19:22:42 -0000	1.12
+++ ksyntaxhighlighter.cpp	19 Oct 2003 20:36:59 -0000
@@ -67,10 +67,14 @@ class KDictSpellingHighlighter::KDictSpe
 {
 public:
     KDictSpellingHighlighterPrivate() :
+        mDict( 0 ),
 	spell( 0 ),
+        mSpellConfig( 0 ),
+        rehighlightRequest( 0 ),
 	wordCount( 0 ),
 	errorCount( 0 ),
-	autoReady( false ) {}
+	autoReady( false ),
+        globalConfig( true ) {}
 
     ~KDictSpellingHighlighterPrivate() {
 	delete rehighlightRequest;
@@ -84,14 +88,17 @@ public:
 	return statDict;
     }
 
+    QDict<int>* mDict;
     QDict<int> autoDict;
     QDict<int> autoIgnoreDict;
     static QObject *sDictionaryMonitor;
     KSpell *spell;
+    KSpellConfig *mSpellConfig;
     QTimer *rehighlightRequest;
     QString spellKey;
     int wordCount, errorCount;
     bool active, automatic, autoReady;
+    bool globalConfig;
 private:
     static QDict<int>* statDict;
 
@@ -257,12 +264,15 @@ KDictSpellingHighlighter::KDictSpellingH
 						    const QColor& depth0,
 						    const QColor& depth1,
 						    const QColor& depth2,
-						    const QColor& depth3 )
+						    const QColor& depth3,
+                                                    KSpellConfig *spellConfig )
     : KSpellingHighlighter( textEdit, spellColor,
 			    colorQuoting, depth0, depth1, depth2, depth3 )
 {
     d = new KDictSpellingHighlighterPrivate();
 
+    d->mSpellConfig = spellConfig;
+    d->globalConfig = ( spellConfig == 0 );
     d->automatic = autoEnable;
     d->active = spellCheckingActive;
 
@@ -273,10 +283,17 @@ KDictSpellingHighlighter::KDictSpellingH
     connect( d->rehighlightRequest, SIGNAL( timeout() ),
 	     this, SLOT( slotRehighlight() ));
 
-    d->spellKey = spellKey();
+    if ( d->globalConfig ) {
+        d->spellKey = spellKey();
 
-    if ( !d->sDictionaryMonitor )
-	d->sDictionaryMonitor = new QObject();
+        if ( !d->sDictionaryMonitor )
+            d->sDictionaryMonitor = new QObject();
+    }
+    else {
+        d->mDict = new QDict<int>(50021);
+        connect( d->mSpellConfig, SIGNAL( configChanged() ),
+                 this, SLOT( slotLocalSpellConfigChanged() ) );
+    }
 
     slotDictionaryChanged();
     startTimer( 2 * 1000 );
@@ -286,13 +303,17 @@ KDictSpellingHighlighter::~KDictSpelling
 {
     delete d->spell;
     d->spell = 0;
+    delete d->mDict;
+    d->mDict = 0;
     delete d;
 }
 
 void KDictSpellingHighlighter::slotSpellReady( KSpell *spell )
 {
-    connect( d->sDictionaryMonitor, SIGNAL( destroyed()),
-	     this, SLOT( slotDictionaryChanged() ));
+    if ( d->globalConfig ) {
+        connect( d->sDictionaryMonitor, SIGNAL( destroyed()),
+                 this, SLOT( slotDictionaryChanged() ));
+    }
     if ( spell != d->spell )
     {
         delete d->spell;
@@ -318,7 +341,8 @@ bool KDictSpellingHighlighter::isMisspel
 	d->autoIgnoreDict.replace( word, Ignore );
 
     // "dict" is used as a cache to store the results of KSpell
-    if ( !d->sDict()->isEmpty() && (*d->sDict())[word] == NotOkay ) {
+    QDict<int>* dict = ( d->globalConfig ? d->sDict() : d->mDict );
+    if ( !dict->isEmpty() && (*dict)[word] == NotOkay ) {
 	if ( d->autoReady && ( d->autoDict[word] != NotOkay )) {
 	    if ( !d->autoIgnoreDict[word] ) {
 		++d->errorCount;
@@ -329,7 +353,7 @@ bool KDictSpellingHighlighter::isMisspel
 	}
 	return d->active;
     }
-    if ( !d->sDict()->isEmpty() && (*d->sDict())[word] == Okay ) {
+    if ( !dict->isEmpty() && (*dict)[word] == Okay ) {
 	if ( d->autoReady && !d->autoDict[word] ) {
 	    d->autoDict.replace( word, Okay );
 	    if ( !d->autoIgnoreDict[word] )
@@ -339,7 +363,7 @@ bool KDictSpellingHighlighter::isMisspel
     }
 
     // there is no 'spelt correctly' signal so default to Okay
-    d->sDict()->replace( word, Okay );
+    dict->replace( word, Okay );
 
     // yes I tried checkWord, the docs lie and it didn't give useful signals :-(
     if ( d->spell )
@@ -362,7 +386,10 @@ void KDictSpellingHighlighter::slotMissp
 {
     Q_UNUSED( suggestions );
     // kdDebug() << suggestions.join( " " ).latin1() << endl;
-    d->sDict()->replace( originalWord, NotOkay );
+    if ( d->globalConfig )
+        d->sDict()->replace( originalWord, NotOkay );
+    else
+        d->mDict->replace( originalWord, NotOkay );
 
     //Emit this baby so that apps that want to have suggestions in a popup over
     //the misspelled word can catch them.
@@ -418,7 +447,14 @@ void KDictSpellingHighlighter::slotDicti
     d->autoDict.clear();
 
     d->spell = new KSpell( 0, i18n( "Incremental Spellcheck" ), this,
-		SLOT( slotSpellReady( KSpell * ) ));
+		SLOT( slotSpellReady( KSpell * ) ), d->mSpellConfig );
+}
+
+void KDictSpellingHighlighter::slotLocalSpellConfigChanged()
+{
+    // the spell config has been changed, so we have to restart from scratch
+    d->mDict->clear();
+    slotDictionaryChanged();
 }
 
 QString KDictSpellingHighlighter::spellKey()
@@ -477,11 +514,13 @@ bool KDictSpellingHighlighter::eventFilt
 {
 	// ### this is a joke, isn't it? Reparsing KGlobal::config() upon every focus-in-event???
     if (o == textEdit() && (e->type() == QEvent::FocusIn)) {
-	QString skey = spellKey();
-	if ( d->spell && d->spellKey != skey ) {
-	    d->spellKey = skey;
-	    KDictSpellingHighlighter::dictionaryChanged();
-	}
+        if ( d->globalConfig ) {
+            QString skey = spellKey();
+            if ( d->spell && d->spellKey != skey ) {
+                d->spellKey = skey;
+                KDictSpellingHighlighter::dictionaryChanged();
+            }
+        }
     }
 
     if (o == textEdit() && (e->type() == QEvent::KeyPress)) {

["ksconfig.diff" (text/x-diff)]

Index: ksconfig.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdeui/ksconfig.cpp,v
retrieving revision 1.80
diff -u -3 -p -r1.80 ksconfig.cpp
--- ksconfig.cpp	30 Sep 2003 01:37:33 -0000	1.80
+++ ksconfig.cpp	19 Oct 2003 20:55:39 -0000
@@ -671,6 +671,14 @@ KSpellConfig::fillDicts( QComboBox* box 
         }
       }
     }
+    int whichelement = -1;
+    for ( unsigned int i = 0; i < langfnames.count(); ++i ) {
+      if ( langfnames[i] == qsdict )
+        whichelement = i;
+    }
+    if ( whichelement >= 0 ) {
+      box->setCurrentItem( whichelement );
+    }
   }
 }
 

[Attachment #7 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic