| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2009-2010 Michel Ludwig <michel.ludwig@kdemail.net> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "spellingmenu.h" |
| 8 | |
| 9 | #include "katedocument.h" |
| 10 | #include "kateglobal.h" |
| 11 | #include "kateview.h" |
| 12 | #include "ontheflycheck.h" |
| 13 | #include "spellcheck/spellcheck.h" |
| 14 | |
| 15 | #include "katepartdebug.h" |
| 16 | |
| 17 | #include <QActionGroup> |
| 18 | #include <QMenu> |
| 19 | |
| 20 | #include <KLocalizedString> |
| 21 | #include <KTextEditor/Range> |
| 22 | |
| 23 | KateSpellingMenu::(KTextEditor::ViewPrivate *view) |
| 24 | : QObject(view) |
| 25 | , m_view(view) |
| 26 | , m_spellingMenuAction(nullptr) |
| 27 | , m_ignoreWordAction(nullptr) |
| 28 | , m_addToDictionaryAction(nullptr) |
| 29 | , m_spellingMenu(nullptr) |
| 30 | , m_currentMisspelledRange(nullptr) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | KateSpellingMenu::() |
| 35 | { |
| 36 | m_currentMisspelledRange = nullptr; // it shouldn't be accessed anymore as it could |
| 37 | } |
| 38 | |
| 39 | bool KateSpellingMenu::() const |
| 40 | { |
| 41 | if (!m_spellingMenuAction) { |
| 42 | return false; |
| 43 | } |
| 44 | return m_spellingMenuAction->isEnabled(); |
| 45 | } |
| 46 | |
| 47 | bool KateSpellingMenu::() const |
| 48 | { |
| 49 | if (!m_spellingMenuAction) { |
| 50 | return false; |
| 51 | } |
| 52 | return m_spellingMenuAction->isVisible(); |
| 53 | } |
| 54 | |
| 55 | void KateSpellingMenu::(bool b) |
| 56 | { |
| 57 | if (m_spellingMenuAction) { |
| 58 | m_spellingMenuAction->setEnabled(b); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void KateSpellingMenu::(bool b) |
| 63 | { |
| 64 | if (m_spellingMenuAction) { |
| 65 | m_spellingMenuAction->setVisible(b); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void KateSpellingMenu::(KActionCollection *ac) |
| 70 | { |
| 71 | m_spellingMenuAction = new KActionMenu(i18n("Spelling" ), this); |
| 72 | ac->addAction(QStringLiteral("spelling_suggestions" ), action: m_spellingMenuAction); |
| 73 | m_spellingMenu = m_spellingMenuAction->menu(); |
| 74 | connect(sender: m_spellingMenu, signal: &QMenu::aboutToShow, context: this, slot: &KateSpellingMenu::populateSuggestionsMenu); |
| 75 | |
| 76 | m_ignoreWordAction = new QAction(i18n("Ignore Word" ), this); |
| 77 | connect(sender: m_ignoreWordAction, signal: &QAction::triggered, context: this, slot: &KateSpellingMenu::ignoreCurrentWord); |
| 78 | |
| 79 | m_addToDictionaryAction = new QAction(i18n("Add to Dictionary" ), this); |
| 80 | connect(sender: m_addToDictionaryAction, signal: &QAction::triggered, context: this, slot: &KateSpellingMenu::addCurrentWordToDictionary); |
| 81 | |
| 82 | m_dictionaryGroup = new QActionGroup(this); |
| 83 | QMapIterator<QString, QString> i(Sonnet::Speller().preferredDictionaries()); |
| 84 | while (i.hasNext()) { |
| 85 | i.next(); |
| 86 | QAction *action = m_dictionaryGroup->addAction(text: i.key()); |
| 87 | action->setData(i.value()); |
| 88 | } |
| 89 | connect(sender: m_dictionaryGroup, signal: &QActionGroup::triggered, slot: [this](QAction *action) { |
| 90 | if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) { |
| 91 | const bool blockmode = m_view->blockSelection(); |
| 92 | m_view->doc()->setDictionary(dict: action->data().toString(), range: m_selectedRange, blockmode); |
| 93 | } |
| 94 | }); |
| 95 | |
| 96 | setVisible(false); |
| 97 | } |
| 98 | |
| 99 | void KateSpellingMenu::(KTextEditor::MovingRange *range) |
| 100 | { |
| 101 | if (m_currentMisspelledRange == range) { |
| 102 | return; |
| 103 | } |
| 104 | m_currentMisspelledRange = range; |
| 105 | } |
| 106 | |
| 107 | void KateSpellingMenu::(KTextEditor::MovingRange *range) |
| 108 | { |
| 109 | if (range != m_currentMisspelledRange) { |
| 110 | // The order of 'exited' and 'entered' signals was wrong |
| 111 | return; |
| 112 | } |
| 113 | m_currentMisspelledRange = nullptr; |
| 114 | } |
| 115 | |
| 116 | void KateSpellingMenu::(KTextEditor::MovingRange *range) |
| 117 | { |
| 118 | if (m_currentMisspelledRange == range) { |
| 119 | m_currentMisspelledRange = nullptr; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void KateSpellingMenu::() |
| 124 | { |
| 125 | // Ugly hack to avoid segfaults. |
| 126 | // cleanUpAfterShown/ViewPrivate::aboutToHideContextMenu is called before |
| 127 | // some action slot is processed. |
| 128 | QTimer::singleShot(interval: 0, slot: [this]() { |
| 129 | if (m_currentMisspelledRangeNeedCleanUp) { |
| 130 | m_currentMisspelledRange = nullptr; |
| 131 | m_currentMisspelledRangeNeedCleanUp = false; |
| 132 | } |
| 133 | |
| 134 | // We need to remove our list or they will accumulated on next show event |
| 135 | for (auto act : m_menuOnTopSuggestionList) { |
| 136 | qobject_cast<QWidget *>(o: act->parent())->removeAction(action: act); |
| 137 | delete act; |
| 138 | } |
| 139 | m_menuOnTopSuggestionList.clear(); |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | void KateSpellingMenu::(QMenu *) |
| 144 | { |
| 145 | Q_ASSERT(contextMenu); |
| 146 | |
| 147 | if (!m_view->doc()->onTheFlySpellChecker()) { |
| 148 | // Nothing todo! |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | m_selectedRange = m_view->selectionRange(); |
| 153 | if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) { |
| 154 | // Selected words need a special handling to work properly |
| 155 | auto imv = m_view->doc()->onTheFlySpellChecker()->installedMovingRanges(range: m_selectedRange); |
| 156 | for (int i = 0; i < imv.size(); ++i) { |
| 157 | if (imv.at(i)->toRange() == m_selectedRange) { |
| 158 | m_currentMisspelledRange = imv.at(i); |
| 159 | m_currentMisspelledRangeNeedCleanUp = true; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (m_currentMisspelledRange != nullptr) { |
| 166 | setVisible(true); |
| 167 | m_selectedRange = m_currentMisspelledRange->toRange(); // Support actions of m_dictionaryGroup |
| 168 | |
| 169 | // avoid that we create xxxxxx pixel long menus |
| 170 | QFontMetrics fontMetrics(contextMenu->fontMetrics()); |
| 171 | const QString &misspelledWord = m_view->doc()->text(range: *m_currentMisspelledRange); |
| 172 | m_spellingMenuAction->setText(i18n("Spelling '%1'" , fontMetrics.elidedText(misspelledWord, Qt::ElideRight, fontMetrics.maxWidth() * 16))); |
| 173 | |
| 174 | // Add suggestions on top of menu |
| 175 | m_currentDictionary = m_view->doc()->dictionaryForMisspelledRange(range: *m_currentMisspelledRange); |
| 176 | m_currentSuggestions = KTextEditor::EditorPrivate::self()->spellCheckManager()->suggestions(word: misspelledWord, dictionary: m_currentDictionary); |
| 177 | int counter = 5; |
| 178 | QFont boldFont; // Emphasize on-top suggestions, so does Falkon |
| 179 | boldFont.setBold(true); |
| 180 | for (QStringList::const_iterator i = m_currentSuggestions.cbegin(); i != m_currentSuggestions.cend() && counter > 0; ++i) { |
| 181 | const QString &suggestion = *i; |
| 182 | QAction *action = new QAction(suggestion, contextMenu); |
| 183 | action->setFont(boldFont); |
| 184 | m_menuOnTopSuggestionList.append(t: action); |
| 185 | connect(sender: action, signal: &QAction::triggered, context: this, slot: [suggestion, this]() { |
| 186 | replaceWordBySuggestion(suggestion); |
| 187 | }); |
| 188 | m_spellingMenu->addAction(action); |
| 189 | --counter; |
| 190 | } |
| 191 | contextMenu->insertActions(before: m_spellingMenuAction, actions: m_menuOnTopSuggestionList); |
| 192 | |
| 193 | } else if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) { |
| 194 | setVisible(true); |
| 195 | m_spellingMenuAction->setText(i18n("Spelling" )); |
| 196 | } else { |
| 197 | setVisible(false); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void KateSpellingMenu::() |
| 202 | { |
| 203 | m_spellingMenu->clear(); |
| 204 | |
| 205 | if (m_currentMisspelledRange) { |
| 206 | m_spellingMenu->addAction(action: m_ignoreWordAction); |
| 207 | m_spellingMenu->addAction(action: m_addToDictionaryAction); |
| 208 | |
| 209 | m_spellingMenu->addSeparator(); |
| 210 | bool dictFound = false; |
| 211 | for (auto action : m_dictionaryGroup->actions()) { |
| 212 | action->setCheckable(true); |
| 213 | if (action->data().toString() == m_currentDictionary) { |
| 214 | dictFound = true; |
| 215 | action->setChecked(true); |
| 216 | } |
| 217 | m_spellingMenu->addAction(action); |
| 218 | } |
| 219 | if (!dictFound && !m_currentDictionary.isEmpty()) { |
| 220 | const QString dictName = Sonnet::Speller().availableDictionaries().key(value: m_currentDictionary); |
| 221 | QAction *action = m_dictionaryGroup->addAction(text: dictName); |
| 222 | action->setData(m_currentDictionary); |
| 223 | action->setCheckable(true); |
| 224 | action->setChecked(true); |
| 225 | m_spellingMenu->addAction(action); |
| 226 | } |
| 227 | |
| 228 | m_spellingMenu->addSeparator(); |
| 229 | int counter = 10; |
| 230 | for (QStringList::const_iterator i = m_currentSuggestions.cbegin(); i != m_currentSuggestions.cend() && counter > 0; ++i) { |
| 231 | const QString &suggestion = *i; |
| 232 | QAction *action = new QAction(suggestion, m_spellingMenu); |
| 233 | connect(sender: action, signal: &QAction::triggered, context: this, slot: [suggestion, this]() { |
| 234 | replaceWordBySuggestion(suggestion); |
| 235 | }); |
| 236 | m_spellingMenu->addAction(action); |
| 237 | --counter; |
| 238 | } |
| 239 | |
| 240 | } else if (m_selectedRange.isValid() && !m_selectedRange.isEmpty()) { |
| 241 | for (auto action : m_dictionaryGroup->actions()) { |
| 242 | action->setCheckable(false); |
| 243 | m_spellingMenu->addAction(action); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void KateSpellingMenu::(const QString &suggestion) |
| 249 | { |
| 250 | if (!m_currentMisspelledRange) { |
| 251 | return; |
| 252 | } |
| 253 | // Ensure we keep some special dictionary setting... |
| 254 | const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(range: *m_currentMisspelledRange); |
| 255 | KTextEditor::Range newRange = m_currentMisspelledRange->toRange(); |
| 256 | newRange.setEnd(KTextEditor::Cursor(newRange.start().line(), newRange.start().column() + suggestion.size())); |
| 257 | |
| 258 | KTextEditor::DocumentPrivate *doc = m_view->doc(); |
| 259 | KTextEditor::EditorPrivate::self()->spellCheckManager()->replaceCharactersEncodedIfNecessary(newWord: suggestion, doc, replacementRange: *m_currentMisspelledRange); |
| 260 | |
| 261 | // ...on the replaced word |
| 262 | m_view->doc()->setDictionary(dict: dictionary, range: newRange); |
| 263 | m_view->clearSelection(); // Ensure cursor move and next right click works properly if there was a selection |
| 264 | } |
| 265 | |
| 266 | void KateSpellingMenu::() |
| 267 | { |
| 268 | if (!m_currentMisspelledRange) { |
| 269 | return; |
| 270 | } |
| 271 | const QString &misspelledWord = m_view->doc()->text(range: *m_currentMisspelledRange); |
| 272 | const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(range: *m_currentMisspelledRange); |
| 273 | KTextEditor::EditorPrivate::self()->spellCheckManager()->addToDictionary(word: misspelledWord, dictionary); |
| 274 | m_view->doc()->clearMisspellingForWord(word: misspelledWord); // WARNING: 'm_currentMisspelledRange' is deleted here! |
| 275 | m_view->clearSelection(); |
| 276 | } |
| 277 | |
| 278 | void KateSpellingMenu::() |
| 279 | { |
| 280 | if (!m_currentMisspelledRange) { |
| 281 | return; |
| 282 | } |
| 283 | const QString &misspelledWord = m_view->doc()->text(range: *m_currentMisspelledRange); |
| 284 | const QString dictionary = m_view->doc()->dictionaryForMisspelledRange(range: *m_currentMisspelledRange); |
| 285 | KTextEditor::EditorPrivate::self()->spellCheckManager()->ignoreWord(word: misspelledWord, dictionary); |
| 286 | m_view->doc()->clearMisspellingForWord(word: misspelledWord); // WARNING: 'm_currentMisspelledRange' is deleted here! |
| 287 | m_view->clearSelection(); |
| 288 | } |
| 289 | |
| 290 | #include "moc_spellingmenu.cpp" |
| 291 | |