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

List:       kde-core-devel
Subject:    [PATCH] Sonnet::Highlighter API additions
From:       Thomas McGuire <thomas.mcguire () gmx ! net>
Date:       2008-05-02 14:18:19
Message-ID: 200805021618.25454.thomas.mcguire () gmx ! net
[Download RAW message or body]

[Attachment #2 (multipart/mixed)]


Hi,

attached is a patch that makes some changes to the Sonnet highlighter.

The reason why this patch is needed is that the current method of suggesting 
replacements for a misspelled word is horribly inefficient: When a misspelled 
word is detected, the highlighter emits newSuggestions(QString,QStringList), 
which has the wrong word and a list of suggestions as parameters. The client 
(KMail in this case) then has to store a map of wrong words and their 
replacement suggestions. With long texts, this map can get quite big, 
especially because words are marked wrong when writing them, so the signal is 
emitted for word-fragments as well (for example, "ex", "exa", etc for 
"example").

This patch adds 2 new functions, isWordMisspelled() and suggestionsForWord(), 
which eliminate the need for that map.
The old newSuggestions() signal is deprecated.

While I was at it, I added ignoreWord() and newSuggestions(), so that a "Add 
to Dictionary" and "Ignore" actions are finally possible within context-menus.

I also removed some tabs.

Is it OK to commit this?

Regards,
Thomas

["highlighter.diff" (text/x-patch)]

Index: highlighter.cpp
===================================================================
--- highlighter.cpp	(revision 802827)
+++ highlighter.cpp	(working copy)
@@ -409,4 +409,24 @@
   }
   }*/
 
+void Highlighter::addWordToDictionary(const QString &word)
+{
+    d->dict->addToPersonal(word);
 }
+
+void Highlighter::ignoreWord(const QString &word)
+{
+    d->dict->addToSession(word);
+}
+
+QStringList Highlighter::suggestionsForWord(const QString &word)
+{
+    return d->dict->suggest(word);
+}
+
+bool Highlighter::isWordMisspelled(const QString &word)
+{
+    return d->dict->isMisspelled(word);
+}
+
+}
Index: highlighter.h
===================================================================
--- highlighter.h	(revision 802827)
+++ highlighter.h	(working copy)
@@ -34,75 +34,115 @@
     /// The Sonnet Highlighter
     class KDEUI_EXPORT Highlighter : public QSyntaxHighlighter
     {
-	Q_OBJECT
+        Q_OBJECT
     public:
         explicit Highlighter(QTextEdit *textEdit,
                              const QString &configFile = QString(),
                              const QColor &col=QColor());
         ~Highlighter();
 
-	bool spellCheckerFound() const;
+        bool spellCheckerFound() const;
 
         QString currentLanguage() const;
         void setCurrentLanguage(const QString &lang);
 
-	static QStringList personalWords();
+        static QStringList personalWords();
 
-	/**
-	 * @short Enable/Disable spell checking.
-	 *
-	 * If @p active is true then spell checking is enabled; otherwise it
-	 * is disabled. Note that you have to disable automatic (de)activation
-	 * with @ref setAutomatic() before you change the state of spell
-	 * checking if you want to persistently enable/disable spell
-	 * checking.
-	 *
-	 * @param active if true, then spell checking is enabled
-	 *
-	 * @see isActive(), setAutomatic()
-	 */
-	void setActive(bool active);
+        /**
+         * @short Enable/Disable spell checking.
+         *
+         * If @p active is true then spell checking is enabled; otherwise it
+         * is disabled. Note that you have to disable automatic (de)activation
+         * with @ref setAutomatic() before you change the state of spell
+         * checking if you want to persistently enable/disable spell
+         * checking.
+         *
+         * @param active if true, then spell checking is enabled
+         *
+         * @see isActive(), setAutomatic()
+         */
+        void setActive(bool active);
 
-	/**
-	 * Returns the state of spell checking.
-	 *
-	 * @return true if spell checking is active
-	 *
-	 * @see setActive()
-	 */
-	bool isActive() const;
+        /**
+         * Returns the state of spell checking.
+         *
+         * @return true if spell checking is active
+         *
+         * @see setActive()
+         */
+        bool isActive() const;
 
-	bool automatic() const;
+        bool automatic() const;
 
-	void setAutomatic(bool automatic);
+        void setAutomatic(bool automatic);
 
+        /**
+         * Adds the given word permanently to the dictionary. It will never
+         * be marked as misspelled again, even after restarting the application.
+         *
+         * @param word the word which will be added to the dictionary
+         * @since 4.2
+         */
+        void addWordToDictionary(const QString &word);
 
+        /**
+         * Ignores the given word. This word will not be marked misspelled for
+         * this session. It will again be marked as misspelled when creating
+         * new highlighters.
+         *
+         * @param word the word which will be ignored
+         * @since 4.2
+         */
+        void ignoreWord(const QString &word);
+
+        /**
+         * Returns a list of suggested replacements for the given misspelled word.
+         * If the word is not misspelled, the list will be empty.
+         *
+         * @param word the misspelled word
+         * @return a list of suggested replacements for the word
+         * @since 4.1
+         */
+        QStringList suggestionsForWord(const QString &word);
+
+        /**
+         * Checks if a given word is marked as misspelled by the highlighter.
+         *
+         * @param word the word to be checked
+         * @return true if the given word is misspelled.
+         * @since 4.1
+         */
+        bool isWordMisspelled(const QString &word);
+
     Q_SIGNALS:
-	/**
-	 * Emitted when as-you-type spell checking is enabled or disabled.
-	 *
-	 * @param description is a i18n description of the new state,
-	 *        with an optional reason
-	 */
-	void activeChanged(const QString &description);
 
-	/**
-	 *
-	 * @param originalWord missspelled word
-	 *
-	 * @param suggestions list of word which can replace missspelled word
-	 */
-	void newSuggestions(const QString &originalWord, const QStringList &suggestions);
+        /**
+         * Emitted when as-you-type spell checking is enabled or disabled.
+         *
+         * @param description is a i18n description of the new state,
+         *        with an optional reason
+         */
+        void activeChanged(const QString &description);
 
+        /**
+         *
+         * @param originalWord missspelled word
+         *
+         * @param suggestions list of word which can replace missspelled word
+         *
+         * @deprecated use isWordMisspelled() and suggestionsForWord() instead.
+         */
+        QT_MOC_COMPAT void newSuggestions(const QString &originalWord, const QStringList &suggestions);
+
     protected:
-	virtual void highlightBlock(const QString &text);
 
+        virtual void highlightBlock(const QString &text);
         virtual void setMisspelled(int start, int count);
         virtual void unsetMisspelled(int start,  int count);
 
-	bool eventFilter(QObject *o, QEvent *e);
-	bool intraWordEditing() const;
-	void setIntraWordEditing(bool editing);
+        bool eventFilter(QObject *o, QEvent *e);
+        bool intraWordEditing() const;
+        void setIntraWordEditing(bool editing);
 
     public Q_SLOTS:
         void slotAutoDetection();

["signature.asc" (application/pgp-signature)]

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

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