uisupport: Allow to disable spell checking
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 25 Jul 2018 20:26:59 +0000 (22:26 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 23 Aug 2018 00:14:54 +0000 (02:14 +0200)
Apparently, Sonnet, when used standalone (i.e. not in KTextEdit),
does not honor its own setting to disable it by default.

Explicitly read the setting when initializing the highlighter, and
set its active state accordingly. Provide a context menu option
to enable/disable spell check on-the-fly as well.

Remove the bogus/unused checkbox in the InputWidget settings page;
there is a dedicated settings page for configuring spell checking.

Explicitly depend on Sonnet when building with KDE Frameworks
(it was transitively depended upon anyway), so we can be sure to
offer the Sonnet settings page even when using Frameworks.

CMakeLists.txt
src/qtui/inputwidget.cpp
src/qtui/inputwidget.h
src/qtui/settingspages/inputwidgetsettingspage.cpp
src/qtui/settingspages/inputwidgetsettingspage.ui
src/uisupport/CMakeLists.txt
src/uisupport/multilineedit.cpp
src/uisupport/multilineedit.h

index afdb357..721e339 100644 (file)
@@ -350,7 +350,7 @@ if (USE_QT5)
         if (ECM_FOUND)
             list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
             if (WITH_KDE)
         if (ECM_FOUND)
             list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
             if (WITH_KDE)
-                find_package(KF5 COMPONENTS ConfigWidgets CoreAddons Notifications NotifyConfig TextWidgets WidgetsAddons XmlGui QUIET)
+                find_package(KF5 COMPONENTS ConfigWidgets CoreAddons Notifications NotifyConfig Sonnet TextWidgets WidgetsAddons XmlGui QUIET REQUIRED)
                 set_package_properties(KF5 PROPERTIES TYPE REQUIRED
                     URL "http://www.kde.org"
                     DESCRIPTION "KDE Frameworks"
                 set_package_properties(KF5 PROPERTIES TYPE REQUIRED
                     URL "http://www.kde.org"
                     DESCRIPTION "KDE Frameworks"
index 48d9bc6..98ef2ba 100644 (file)
@@ -118,11 +118,6 @@ InputWidget::InputWidget(QWidget *parent)
 
     UiSettings s("InputWidget");
 
 
     UiSettings s("InputWidget");
 
-#ifdef HAVE_KDE4
-    s.notify("EnableSpellCheck", this, SLOT(setEnableSpellCheck(QVariant)));
-    setEnableSpellCheck(s.value("EnableSpellCheck", false));
-#endif
-
     s.notify("EnableEmacsMode", this, SLOT(setEnableEmacsMode(QVariant)));
     setEnableEmacsMode(s.value("EnableEmacsMode", false));
 
     s.notify("EnableEmacsMode", this, SLOT(setEnableEmacsMode(QVariant)));
     setEnableEmacsMode(s.value("EnableEmacsMode", false));
 
@@ -189,12 +184,6 @@ void InputWidget::setCustomFont(const QVariant &v)
 }
 
 
 }
 
 
-void InputWidget::setEnableSpellCheck(const QVariant &v)
-{
-    ui.inputEdit->setSpellCheckEnabled(v.toBool());
-}
-
-
 void InputWidget::setEnableEmacsMode(const QVariant &v)
 {
     ui.inputEdit->setEmacsMode(v.toBool());
 void InputWidget::setEnableEmacsMode(const QVariant &v)
 {
     ui.inputEdit->setEmacsMode(v.toBool());
index b4aaf0f..11512b2 100644 (file)
@@ -102,7 +102,6 @@ protected slots:
 private slots:
     void setCustomFont(const QVariant &font);
     void setUseCustomFont(const QVariant &);
 private slots:
     void setCustomFont(const QVariant &font);
     void setUseCustomFont(const QVariant &);
-    void setEnableSpellCheck(const QVariant &);
     void setEnableEmacsMode(const QVariant &);
     void setShowNickSelector(const QVariant &);
     void setShowStyleButtons(const QVariant &);
     void setEnableEmacsMode(const QVariant &);
     void setShowNickSelector(const QVariant &);
     void setShowStyleButtons(const QVariant &);
index a8837b3..6ed5647 100644 (file)
@@ -24,10 +24,5 @@ InputWidgetSettingsPage::InputWidgetSettingsPage(QWidget *parent)
     : SettingsPage(tr("Interface"), tr("Input Widget"), parent)
 {
     ui.setupUi(this);
     : SettingsPage(tr("Interface"), tr("Input Widget"), parent)
 {
     ui.setupUi(this);
-
-#ifndef HAVE_KDE4
-    ui.enableSpellCheck->hide();
-#endif
-
     initAutoWidgets();
 }
     initAutoWidgets();
 }
index 14f3666..a956567 100644 (file)
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>507</width>
+    <width>512</width>
     <height>514</height>
    </rect>
   </property>
     <height>514</height>
    </rect>
   </property>
      </item>
     </layout>
    </item>
      </item>
     </layout>
    </item>
-   <item>
-    <widget class="QCheckBox" name="enableSpellCheck">
-     <property name="text">
-      <string>Enable spell check</string>
-     </property>
-     <property name="settingsKey" stdset="0">
-      <string notr="true">EnableSpellCheck</string>
-     </property>
-     <property name="defaultValue" stdset="0">
-      <bool>false</bool>
-     </property>
-    </widget>
-   </item>
    <item>
     <widget class="QCheckBox" name="enablePerBufferHistory">
      <property name="text">
    <item>
     <widget class="QCheckBox" name="enablePerBufferHistory">
      <property name="text">
    <item>
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
    <item>
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
-      <string>Multi-Line Editing</string>
+      <string>&amp;Multi-Line Editing</string>
      </property>
      <property name="checkable">
       <bool>true</bool>
      </property>
      <property name="checkable">
       <bool>true</bool>
            <bool>false</bool>
           </property>
           <property name="settingsKey" stdset="0">
            <bool>false</bool>
           </property>
           <property name="settingsKey" stdset="0">
-            <string notr="true">/TabCompletion/AddSpaceMidSentence</string>
+           <string notr="true">/TabCompletion/AddSpaceMidSentence</string>
           </property>
           <property name="defaultValue" stdset="0">
           </property>
           <property name="defaultValue" stdset="0">
-            <bool>false</bool>
+           <bool>false</bool>
           </property>
          </widget>
         </item>
           </property>
          </widget>
         </item>
  </customwidgets>
  <tabstops>
   <tabstop>customFont</tabstop>
  </customwidgets>
  <tabstops>
   <tabstop>customFont</tabstop>
-  <tabstop>enableSpellCheck</tabstop>
   <tabstop>showNickSelector</tabstop>
   <tabstop>groupBox</tabstop>
   <tabstop>maxNumLines</tabstop>
   <tabstop>showNickSelector</tabstop>
   <tabstop>groupBox</tabstop>
   <tabstop>maxNumLines</tabstop>
index 1a9dcf3..591b993 100644 (file)
@@ -62,7 +62,9 @@ endif()
 
 if (WITH_KF5)
     target_link_libraries(mod_uisupport KF5::CoreAddons KF5::TextWidgets KF5::XmlGui)
 
 if (WITH_KF5)
     target_link_libraries(mod_uisupport KF5::CoreAddons KF5::TextWidgets KF5::XmlGui)
-elseif (KF5Sonnet_FOUND)
+endif()
+
+if (KF5Sonnet_FOUND)
     add_definitions(-DHAVE_SONNET)
     target_link_libraries(mod_uisupport KF5::SonnetUi)
 endif()
     add_definitions(-DHAVE_SONNET)
     target_link_libraries(mod_uisupport KF5::SonnetUi)
 endif()
index 8a7f4bf..133a243 100644 (file)
 #include <QMessageBox>
 #include <QScrollBar>
 
 #include <QMessageBox>
 #include <QScrollBar>
 
-#ifdef HAVE_SONNET
-#  include <Sonnet/SpellCheckDecorator>
-#endif
-
 #include "actioncollection.h"
 #include "bufferview.h"
 #include "graphicalui.h"
 #include "actioncollection.h"
 #include "bufferview.h"
 #include "graphicalui.h"
@@ -54,8 +50,9 @@ MultiLineEdit::MultiLineEdit(QWidget *parent)
     enableFindReplace(false);
 #endif
 
     enableFindReplace(false);
 #endif
 
-#ifdef HAVE_SONNET
-    new Sonnet::SpellCheckDecorator(this);
+#if defined HAVE_SONNET && !defined HAVE_KDE
+    _spellCheckDecorator = new Sonnet::SpellCheckDecorator(this);
+    highlighter()->setActive(highlighter()->checkerEnabledByDefault());
 #endif
 
     setMode(SingleLine);
 #endif
 
     setMode(SingleLine);
@@ -90,15 +87,39 @@ MultiLineEdit::~MultiLineEdit()
 {
 }
 
 {
 }
 
-#if defined HAVE_KF5 || defined HAVE_KDE4
-void MultiLineEdit::createHighlighter()
+
+#if defined HAVE_SONNET && !defined HAVE_KDE
+Sonnet::Highlighter *MultiLineEdit::highlighter() const
+{
+    return _spellCheckDecorator->highlighter();
+}
+
+
+void MultiLineEdit::setSpellCheckEnabled(bool enabled)
+{
+    highlighter()->setActive(enabled);
+    if (enabled) {
+        highlighter()->slotRehighlight();
+    }
+}
+
+void MultiLineEdit::contextMenuEvent(QContextMenuEvent *event)
 {
 {
-    KTextEdit::createHighlighter();
-    if (highlighter())
-        highlighter()->setAutomatic(false);
+    QMenu *menu = createStandardContextMenu();
+    menu->addSeparator();
+
+    auto action = menu->addAction(tr("Auto Spell Check"));
+    action->setCheckable(true);
+    action->setChecked(highlighter()->isActive());
+    connect(action, SIGNAL(toggled(bool)), this, SLOT(setSpellCheckEnabled(bool)));
+
+    menu->exec(event->globalPos());
+    delete menu;
 }
 }
+
 #endif
 
 #endif
 
+
 void MultiLineEdit::setCustomFont(const QFont &font)
 {
     setFont(font);
 void MultiLineEdit::setCustomFont(const QFont &font)
 {
     setFont(font);
@@ -229,16 +250,6 @@ void MultiLineEdit::setEmacsMode(bool enable)
 }
 
 
 }
 
 
-void MultiLineEdit::setSpellCheckEnabled(bool enable)
-{
-#ifdef HAVE_KDE
-    setCheckSpellingEnabled(enable);
-#else
-    Q_UNUSED(enable)
-#endif
-}
-
-
 void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *)
 {
     _pasteProtectionEnabled = enable;
 void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *)
 {
     _pasteProtectionEnabled = enable;
index 7cf4189..bd59bbc 100644 (file)
 #  define MultiLineEditParent QTextEdit
 #endif
 
 #  define MultiLineEditParent QTextEdit
 #endif
 
+#if defined HAVE_SONNET && !defined HAVE_KDE
+#  include <QContextMenuEvent>
+#  include <Sonnet/Highlighter>
+#  include <Sonnet/SpellCheckDecorator>
+#endif
+
 class MultiLineEdit : public MultiLineEditParent
 {
     Q_OBJECT
 class MultiLineEdit : public MultiLineEditParent
 {
     Q_OBJECT
@@ -74,9 +80,6 @@ public:
     inline bool emacsMode() const { return _emacsMode; }
 
     void addCompletionSpace();
     inline bool emacsMode() const { return _emacsMode; }
 
     void addCompletionSpace();
-#if defined HAVE_KF5 || defined HAVE_KDE4
-    void createHighlighter() override;
-#endif
 
 public slots:
     void setMode(Mode mode);
 
 public slots:
     void setMode(Mode mode);
@@ -84,7 +87,6 @@ public slots:
     void setMaxHeight(int numLines);
     void setEmacsMode(bool enable = true);
     void setScrollBarsEnabled(bool enable = true);
     void setMaxHeight(int numLines);
     void setEmacsMode(bool enable = true);
     void setScrollBarsEnabled(bool enable = true);
-    void setSpellCheckEnabled(bool enable = true);
     void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
     void setLineWrapEnabled(bool enable = false);
 
     void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
     void setLineWrapEnabled(bool enable = false);
 
@@ -101,6 +103,10 @@ protected:
     void keyPressEvent(QKeyEvent *event) override;
     void resizeEvent(QResizeEvent *event) override;
 
     void keyPressEvent(QKeyEvent *event) override;
     void resizeEvent(QResizeEvent *event) override;
 
+#if defined HAVE_SONNET && !defined HAVE_KDE
+    void contextMenuEvent(QContextMenuEvent *event) override;
+#endif
+
 private slots:
     void on_returnPressed();
     void on_returnPressed(QString text);
 private slots:
     void on_returnPressed();
     void on_returnPressed(QString text);
@@ -115,6 +121,12 @@ private slots:
     QString convertMircCodesToHtml(const QString &text);
     bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
 
     QString convertMircCodesToHtml(const QString &text);
     bool mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor);
 
+private:
+    void reset();
+    void showHistoryEntry();
+    void updateScrollBars();
+    void updateSizeHint();
+
 private:
     QStringList _history;
     QHash<int, QString> _tempHistory;
 private:
     QStringList _history;
     QHash<int, QString> _tempHistory;
@@ -133,10 +145,16 @@ private:
 
     QMap<QString, QString> _mircColorMap;
 
 
     QMap<QString, QString> _mircColorMap;
 
-    void reset();
-    void showHistoryEntry();
-    void updateScrollBars();
-    void updateSizeHint();
+#if defined HAVE_SONNET && !defined HAVE_KDE
+    // This member function is provided by KTextEdit
+    Sonnet::Highlighter *highlighter() const;
+
+private slots:
+    void setSpellCheckEnabled(bool enabled);
+
+private:
+    Sonnet::SpellCheckDecorator *_spellCheckDecorator{nullptr};
+#endif
 };
 
 
 };